-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
spirv: OpenCL printf support #24321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
spirv: OpenCL printf support #24321
Changes from all commits
164ef95
d1b74cd
5830ecc
6dd9d83
bac5e1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const std = @import("std.zig"); | ||
const builtin = @import("builtin"); | ||
|
||
pub const position_in = @extern(*addrspace(.input) @Vector(4, f32), .{ .name = "position" }); | ||
pub const position_out = @extern(*addrspace(.output) @Vector(4, f32), .{ .name = "position" }); | ||
|
@@ -126,3 +127,35 @@ pub fn executionMode(comptime entry_point: anytype, comptime mode: ExecutionMode | |
}, | ||
} | ||
} | ||
|
||
/// Writes formatted output to an implementation-defined stream. | ||
/// Returns 0 on success, –1 on failure. | ||
pub fn printf(comptime fmt: [*:0]const u8, args: anytype) u32 { | ||
if (builtin.zig_backend == .stage2_spirv and builtin.target.os.tag == .opencl) { | ||
comptime var expr: []const u8 = | ||
\\%std = OpExtInstImport "OpenCL.std" | ||
\\%u32 = OpTypeInt 32 0 | ||
\\%ret = OpExtInst %u32 %std 184 %fmt | ||
; | ||
inline for (0..args.len) |i| { | ||
expr = expr ++ std.fmt.comptimePrint(" %arg{d}", .{i}); | ||
} | ||
const result = switch (args.len) { | ||
// zig fmt: off | ||
0 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt)), | ||
1 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0])), | ||
2 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1])), | ||
3 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1]), [arg2] "" (args[2])), | ||
4 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1]), [arg2] "" (args[2]), [arg3] "" (args[3])), | ||
5 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1]), [arg2] "" (args[2]), [arg3] "" (args[3]), [arg4] "" (args[4])), | ||
6 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1]), [arg2] "" (args[2]), [arg3] "" (args[3]), [arg4] "" (args[4]), [arg5] "" (args[5])), | ||
7 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1]), [arg2] "" (args[2]), [arg3] "" (args[3]), [arg4] "" (args[4]), [arg5] "" (args[5]), [arg6] "" (args[6])), | ||
8 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1]), [arg2] "" (args[2]), [arg3] "" (args[3]), [arg4] "" (args[4]), [arg5] "" (args[5]), [arg6] "" (args[6]), [arg7] "" (args[7])), | ||
9 => asm volatile (expr : [ret] "" (-> u32), : [fmt] "c" (fmt), [arg0] "" (args[0]), [arg1] "" (args[1]), [arg2] "" (args[2]), [arg3] "" (args[3]), [arg4] "" (args[4]), [arg5] "" (args[5]), [arg6] "" (args[6]), [arg7] "" (args[7]), [arg8] "" (args[8])), | ||
// zig fmt: on | ||
else => @compileError("too many arguments"), | ||
}; | ||
Comment on lines
+143
to
+157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #compiler > spirv printf discussing on how to replace this switch expression with comptime generated asm expression to allow as many arguments as possible There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some edge cases like this one with constant mixed with a variable do not compile, i.e.: var a: i32 = 10;
_ = &a;
_ = printf("%d %d\n", .{ 10, a }); |
||
return result; | ||
} | ||
@compileError("unsupported Zig backend or target OS"); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.