Skip to content

Commit 0c054bd

Browse files
spirv: enhance assembler to handle placeholders and result ID resolution
1 parent 6dd9d83 commit 0c054bd

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/std/gpu.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ pub fn printf(comptime fmt: [*:0]const u8, args: anytype) u32 {
135135
comptime var expr: []const u8 =
136136
\\%std = OpExtInstImport "OpenCL.std"
137137
\\%u32 = OpTypeInt 32 0
138-
\\%ret = OpExtInst %u32 %std 184 %fmt
138+
\\%ret = OpExtInst %u32 %std 184 $fmt
139139
;
140140
inline for (0..args.len) |i| {
141-
expr = expr ++ std.fmt.comptimePrint(" %arg{d}", .{i});
141+
expr = expr ++ std.fmt.comptimePrint(" $arg{d}", .{i});
142142
}
143143
const result = switch (args.len) {
144144
// zig fmt: off

src/codegen/spirv/Assembler.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,40 @@ fn parseValueEnum(self: *Assembler, kind: spec.OperandKind) !void {
779779

780780
fn parseRefId(self: *Assembler) !void {
781781
const tok = self.currentToken();
782+
783+
// Handle placeholders that need to be converted to result IDs
784+
if (self.eatToken(.placeholder)) {
785+
const name = self.tokenText(tok)[1..];
786+
const value = self.value_map.get(name) orelse {
787+
return self.fail(tok.start, "invalid placeholder '${s}'", .{name});
788+
};
789+
switch (value) {
790+
.string => |str| {
791+
// Create a global string constant and use its result ID directly
792+
const string_id = try self.spv.constStringGlobal(str);
793+
// Create a new entry in the value map with the resolved ID
794+
const entry = try self.value_map.getOrPut(self.gpa, name);
795+
entry.value_ptr.* = .{ .value = string_id };
796+
const index: AsmValue.Ref = @intCast(entry.index);
797+
try self.inst.operands.append(self.gpa, .{ .ref_id = index });
798+
return;
799+
},
800+
.constant => {
801+
// TODO: Create a constant instruction for the integer
802+
return self.fail(tok.start, "integer constants as result IDs not yet supported", .{});
803+
},
804+
.value => {
805+
// This is an argument value - use its result ID directly
806+
const entry = try self.value_map.getOrPut(self.gpa, name);
807+
// The value should already be resolved, so just use the index
808+
const index: AsmValue.Ref = @intCast(entry.index);
809+
try self.inst.operands.append(self.gpa, .{ .ref_id = index });
810+
return;
811+
},
812+
else => return self.fail(tok.start, "value '{s}' cannot be used as result-id", .{name}),
813+
}
814+
}
815+
782816
try self.expectToken(.result_id);
783817

784818
const name = self.tokenText(tok)[1..];

0 commit comments

Comments
 (0)