Skip to content

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ivanstepanovftw
Copy link
Contributor

@ivanstepanovftw ivanstepanovftw commented Jul 2, 2025

I want to have std.debug.printf() implemented for GPGPU, because it lowers entry point for newcomers, and this feature allows to catch bugs faster (like I had with signed_integer - 0).

To test this PR you need to clone zig-spirv-test-executor in root dir and run:

./build/stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib
./stage4/bin/zig build --build-file ./zig-spirv-test-executor/build.zig
export RUSTICL_ENABLE=llvmpipe
./stage4/bin/zig test ./test/behavior/asm.zig \
    --test-runner ./zig-spirv-test-executor/src/test_runner.zig \
    -target spirv64-opencl-none \
    -mcpu generic+int64+float64+float16 \
    -fno-llvm \
    --test-cmd ./zig-spirv-test-executor/zig-out/bin/zig-spirv-test-executor \
    --test-cmd --platform \
    --test-cmd rusticl \
    --test-cmd-bin

@ivanstepanovftw
Copy link
Contributor Author

ivanstepanovftw commented Jul 6, 2025

Currently, PoCL works, rusticl does not (nor llvmpipe, nor radeonsi), probably because of extra OpPtrCastToGeneric (if so, then it is a Mesa bug).


Currently, std.fmt.bufPrint is commented because of cannot call function pointers and TODO (SPIR-V): switch with ranges errors.

Details

lib/std/io/Writer.zig:12:5: error: cannot call function pointers
pub fn write(self: Self, bytes: []const u8) anyerror!usize {
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/std/unicode.zig:26:5: error: TODO (SPIR-V): switch with ranges
pub fn utf8ByteSequenceLength(first_byte: u8) !u3 {
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/std/fmt.zig:1783:5: error: TODO (SPIR-V): switch with ranges
pub fn digitToChar(digit: u8, case: Case) u8 {
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/std/unicode.zig:1617:5: error: TODO (SPIR-V): switch with ranges
pub fn isSurrogateCodepoint(c: u21) bool {
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I am not sure about the design (Zig formatting and printf(%s, after_format)). I fear this approach might not be worth the effort.
Since it is just printf-debugging function, which is not implemented, this may be worth to have it like so, with Zig formatting and buffer.
(... if it will work given error "cannot call function pointers", with writergate as a possible solution)
@alichraghi, what do you think about this design, is this acceptable (at least for the first time), or it should look like comptime mapping Zig's fmt to expected C-like printf?

@alichraghi
Copy link
Contributor

I suggest you to not integrate it with std.debug.print and instead have std.gpu.printf which doesn't try to integrate with any zig formatting and is just a simple call to OpenCL's printf.

@ivanstepanovftw ivanstepanovftw changed the title spirv: printf demo spirv: OpenCL printf support Jul 7, 2025
@ivanstepanovftw
Copy link
Contributor Author

ivanstepanovftw commented Jul 7, 2025

Rebased.

@ivanstepanovftw ivanstepanovftw marked this pull request as ready for review July 8, 2025 02:38
Comment on lines +143 to +157
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"),
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

@ivanstepanovftw ivanstepanovftw Jul 8, 2025

Choose a reason for hiding this comment

The 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 });

Copy link
Contributor

@alichraghi alichraghi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few nitpicks that i can fix myself but otherwise LGTM. Hopefully we can switch to an std.format version later

@ivanstepanovftw
Copy link
Contributor Author

ivanstepanovftw commented Jul 15, 2025

Tell me when you want to merge this. Expect no further changes from me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants