Skip to content

Emit printf formats reached through pointer casts as literals - #24

Merged
maleadt merged 1 commit into
mainfrom
tb/printf-fmt-cast
Oct 10, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/printf-fmt-cast

Conversation

@maleadt

@maleadt maleadt commented Oct 10, 2026

Copy link
Copy Markdown
Member

Since llvm/llvm-project#229720 the LLVM SPIR-V back-end casts the printf format string to uchar* (OpBitcast of the UniformConstant variable), and we then passed a pointer instead of a string literal, which NVIDIA's compiler rejects ("The first argument for printf must be a string literal!"). llvm-spirv already had the same problem: in a module with several format strings it bitcasts the later ones to the type of the first.

string_literal_for now looks through OpBitcast and access chains whose indices are all zero before looking up the cached literal. Tested with a new lit case and by building and running a two-printf kernel (compiled by clang + llvm-spirv) on NVIDIA, which failed to build before and prints correctly now.

Fixes #23

The LLVM SPIR-V back-end casts the format string variable to `uchar*`
before passing it to printf (since llvm/llvm-project#229720), and
llvm-spirv bitcasts it to another array type when a module has several
formats. We only recognized the variable itself, so we emitted a pointer
instead of a string literal, which NVIDIA rejects. Look through bitcasts
and zero-offset access chains to find the string.

Fixes #23
@maleadt
maleadt merged commit 1eea7ba into main Oct 10, 2026
5 checks passed
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.

printf format string isn't emitted as a literal when it is bitcast first

1 participant