Skip to content

Commit ddd366b

Browse files
spirv: enhance printf debugging with temporary mock formatting
1 parent fba3637 commit ddd366b

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

lib/std/debug.zig

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,25 +208,37 @@ pub fn unlockStdErr() void {
208208
/// for use in "printf debugging." Use `std.log` functions for proper logging.
209209
pub fn print(comptime fmt: []const u8, args: anytype) void {
210210
if (builtin.zig_backend == .stage2_spirv) {
211-
const ret = asm volatile (
212-
\\ %set = OpExtInstImport "OpenCL.std"
211+
const size = 20;
212+
var fmt_fmt: [size]u8 = [1]u8{undefined} ** size;
213+
214+
// TODO: All formatting is done by std.
215+
// std.fmt.bufPrint(&fmt_fmt, fmt, args) catch unreachable;
216+
// Temporarely mock the fmt_fmt buffer to test printing 's\n'.
217+
fmt_fmt[0] = 115;
218+
fmt_fmt[1] = 115;
219+
fmt_fmt[2] = 115;
220+
fmt_fmt[3] = 115;
221+
fmt_fmt[4] = 10;
222+
fmt_fmt[5] = 0;
223+
224+
// Imports std and calls OpenCL.std.printf("%s\0", fmt_fmt)
225+
_ = asm volatile (
226+
\\ %std = OpExtInstImport "OpenCL.std"
213227
\\ %u8 = OpTypeInt 8 0
214228
\\ %u32 = OpTypeInt 32 0
215229
\\ %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
230+
\\ %u64_3 = OpConstant %u64 3
231+
\\%_arr_u8_3 = OpTypeArray %u8 %u64_3
232+
\\%_ptr_UniformConstant__arr_u8_3 = OpTypePointer UniformConstant %_arr_u8_3
233+
\\ %c1 = OpConstant %u8 37
234+
\\ %c2 = OpConstant %u8 115
235+
\\ %c3 = OpConstant %u8 0
236+
\\%const_str = OpConstantComposite %_arr_u8_3 %c1 %c2 %c3
237+
\\ %fmt = OpVariable %_ptr_UniformConstant__arr_u8_3 UniformConstant %const_str
238+
\\ %ret = OpExtInst %u32 %std 184 %fmt %fmt_fmt
227239
: [ret] "" (-> u32),
240+
: [fmt_fmt] "" (&fmt_fmt),
228241
);
229-
_ = ret;
230242
} else {
231243
lockStdErr();
232244
defer unlockStdErr();

0 commit comments

Comments
 (0)