Skip to content

[SYCL][HIP] Implement buffered printf for the AMD backend#22676

Open
zjin-lcf wants to merge 1 commit into
intel:syclfrom
zjin-lcf:sycl-hip-buffered-printf-22300
Open

[SYCL][HIP] Implement buffered printf for the AMD backend#22676
zjin-lcf wants to merge 1 commit into
intel:syclfrom
zjin-lcf:sycl-hip-buffered-printf-22300

Conversation

@zjin-lcf

Copy link
Copy Markdown
Contributor

Summary

  • Implement a working buffered printf for the SYCL HIP (AMD) backend, replacing the unsupported hostcall scheme.
  • Lower late in AMDGPUPrintfRuntimeBinding via emitAMDGPUPrintfCall, selected by an amdgpu_printf_kind="buffered" module flag set for SYCL AMDGPU device code (lowering after inlining so format strings are constant).
  • Keep __printf_alloc in libspirv (__attribute__((used))) so it survives DCE until the backend pass needs it; suppress the spurious sycl-post-link "Undefined function printf" warning.
  • Re-enable the Printf/* and DeviceLib/built-ins/printf e2e tests on HIP.

Test plan

  • Printf/{char,double,float,int,mixed-address-space,percent-symbol} pass on gfx942
  • DeviceLib/built-ins/printf passes on gfx942

Relates to #22300

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>
@zjin-lcf
zjin-lcf requested review from a team, Maetveis and wenju-he as code owners July 19, 2026 21:50

IRBuilder<> Builder(Head);
Builder.SetCurrentDebugLocation(CI->getDebugLoc());
Value *Result = emitAMDGPUPrintfCall(Builder, Args, /*IsBuffered=*/true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please feel free to modify the codes. Thank you for your review.

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.

2 participants