[SYCL][HIP] Implement buffered printf for the AMD backend#22676
Open
zjin-lcf wants to merge 1 commit into
Open
[SYCL][HIP] Implement buffered printf for the AMD backend#22676zjin-lcf wants to merge 1 commit into
zjin-lcf wants to merge 1 commit into
Conversation
SYCL device code on AMDGPU emitted calls to `printf` that were never lowered to a working runtime scheme, so experimental::printf produced no output (or crashed) on HIP. Enable the buffered printf scheme, which is the one the ROCm HIP runtime allocates and parses automatically (via the hidden_printf_buffer implicit kernarg and the code object's printf metadata); the hostcall scheme is intentionally avoided as it depends on ROCm's hostcall service and PCIe atomics that the SYCL/UR HIP runtime does not set up. The lowering must happen after inlining: in SYCL, printf calls are wrapped in the experimental::printf helper, so the format string only becomes a resolvable constant (needed to identify %s arguments and emit the correct buffer layout) once the wrapper is inlined. Doing this in clang CodeGen mis-encodes %s and corrupts the host heap during parsing. Changes: - AMDGPUPrintfRuntimeBinding: when the module requests the buffered scheme, lower printf via the shared llvm::emitAMDGPUPrintfCall emitter (same layout as clang's HIP -mprintf-kind=buffered) instead of the legacy OpenCL layout. This pass runs late, after inlining. - CodeGenModule: set the amdgpu_printf_kind="buffered" module flag for SYCL AMDGPU device code. - libclc: provide __printf_alloc for the libspirv AMDGPU target and mark it used so it survives internalization/DCE until the backend pass introduces the reference. - sycl-post-link: treat printf as a builtin to avoid a spurious "Undefined function printf" warning. - Re-enable the Printf/* and DeviceLib/built-ins/printf e2e tests on target-amd (issue 22300). Co-authored-by: Cursor <cursoragent@cursor.com>
wenju-he
reviewed
Jul 20, 2026
|
|
||
| IRBuilder<> Builder(Head); | ||
| Builder.SetCurrentDebugLocation(CI->getDebugLoc()); | ||
| Value *Result = emitAMDGPUPrintfCall(Builder, Args, /*IsBuffered=*/true); |
Contributor
There was a problem hiding this comment.
should the call be emitted by frontend? See https://github.com/ROCm/llvm-project/blob/54b9268abd943edc4d359428554d32bf22a94d68/clang/lib/CodeGen/CGGPUBuiltin.cpp#L192
Contributor
Author
There was a problem hiding this comment.
Please feel free to modify the codes. Thank you for your review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
printffor the SYCL HIP (AMD) backend, replacing the unsupported hostcall scheme.AMDGPUPrintfRuntimeBindingviaemitAMDGPUPrintfCall, selected by anamdgpu_printf_kind="buffered"module flag set for SYCL AMDGPU device code (lowering after inlining so format strings are constant).__printf_allocin libspirv (__attribute__((used))) so it survives DCE until the backend pass needs it; suppress the spurioussycl-post-link"Undefined function printf" warning.Printf/*andDeviceLib/built-ins/printfe2e tests on HIP.Test plan
Printf/{char,double,float,int,mixed-address-space,percent-symbol}pass on gfx942DeviceLib/built-ins/printfpasses on gfx942Relates to #22300