Skip to content

Commit fba3637

Browse files
spirv: printf demo
1 parent edf785d commit fba3637

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

lib/std/debug.zig

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,32 @@ pub fn unlockStdErr() void {
207207
/// Print to stderr, unbuffered, and silently returning on failure. Intended
208208
/// for use in "printf debugging." Use `std.log` functions for proper logging.
209209
pub fn print(comptime fmt: []const u8, args: anytype) void {
210-
lockStdErr();
211-
defer unlockStdErr();
212-
const stderr = io.getStdErr().writer();
213-
nosuspend stderr.print(fmt, args) catch return;
210+
if (builtin.zig_backend == .stage2_spirv) {
211+
const ret = asm volatile (
212+
\\ %set = OpExtInstImport "OpenCL.std"
213+
\\ %u8 = OpTypeInt 8 0
214+
\\ %u32 = OpTypeInt 32 0
215+
\\ %u64 = OpTypeInt 64 0
216+
\\ %u64_4 = OpConstant %u64 4
217+
\\%_arr_u8_4 = OpTypeArray %u8 %u64_4
218+
\\%_ptr_UniformConstant__arr_u8_4 = OpTypePointer UniformConstant %_arr_u8_4
219+
\\ %char_H = OpConstant %u8 72
220+
\\ %char_i = OpConstant %u8 105
221+
\\ %char__n = OpConstant %u8 10
222+
\\ %char_0 = OpConstant %u8 0
223+
\\%const_str = OpConstantComposite %_arr_u8_4 %char_H %char_i %char__n %char_0
224+
\\ %fmt = OpVariable %_ptr_UniformConstant__arr_u8_4 UniformConstant %const_str
225+
\\
226+
\\ %ret = OpExtInst %u32 %set 184 %fmt
227+
: [ret] "" (-> u32),
228+
);
229+
_ = ret;
230+
} else {
231+
lockStdErr();
232+
defer unlockStdErr();
233+
const stderr = io.getStdErr().writer();
234+
nosuspend stderr.print(fmt, args) catch return;
235+
}
214236
}
215237

216238
pub fn getStderrMutex() *std.Thread.Mutex {

src/codegen/spirv/Assembler.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,18 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
366366
const child_type = try self.resolveRefId(operands[1].ref_id);
367367
break :blk try self.spv.vectorType(operands[2].literal32, child_type);
368368
},
369-
.OpTypeArray => {
369+
.OpTypeArray => blk: {
370370
// TODO: The length of an OpTypeArray is determined by a constant (which may be a spec constant),
371371
// and so some consideration must be taken when entering this in the type system.
372-
return self.todo("process OpTypeArray", .{});
372+
const element_type = try self.resolveRefId(operands[1].ref_id);
373+
const length = try self.resolveRefId(operands[2].ref_id);
374+
const result_id = self.spv.allocId();
375+
try section.emit(self.spv.gpa, .OpTypeArray, .{
376+
.id_result = result_id,
377+
.element_type = element_type,
378+
.length = length,
379+
});
380+
break :blk result_id;
373381
},
374382
.OpTypeRuntimeArray => blk: {
375383
const element_type = try self.resolveRefId(operands[1].ref_id);

0 commit comments

Comments
 (0)