Skip to content

Declare built-in variables whose pointer escapes a direct load - #18

Merged
maleadt merged 1 commit into
mainfrom
tb/builtin-pointer-use
Oct 9, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/builtin-pointer-use

Conversation

@maleadt

@maleadt maleadt commented Oct 9, 2026

Copy link
Copy Markdown
Member

Built-in variables are never declared in the generated C: a load from one is bound to the matching work-item query (get_local_id(i), ...). Any other use of the variable referred to an undeclared name. The LLVM SPIR-V back end produces such uses when IR loads one component of a vector built-in through a GEP: it emits a bitcast and a byte-offset access chain.

Each function that uses a built-in's pointer that way now gets a private copy, initialized from the queries:

ulong3 lid_storage = (ulong3)(get_local_id(0), get_local_id(1), get_local_id(2));
ma6 * lid = &lid_storage;

The scan also follows uses through module-scope OpSpecConstantOps, since those are bound to expressions that name the variable. Built-ins that are only loaded directly keep the query binding, so existing output doesn't change.

Tested with new lit cases, the module from the issue, and a kernel reading LocalInvocationId.y through a byte offset on Intel CPU, PoCL and NVIDIA.

Fixes #3

Built-in variables were never declared: a load from one is bound to the
corresponding work-item query instead. Any other use of the variable,
such as the bitcast and byte offset the LLVM SPIR-V back end emits to
load a single component of a vector built-in, referred to an undeclared
identifier and the program failed to build.

Give each function that uses a built-in's pointer that way a private
copy, initialized from the queries. Built-ins that are only loaded
directly keep the query binding.

Fixes #3.
@maleadt
maleadt merged commit 2456ffa into main Oct 9, 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.

Built-in variables used through a pointer (bitcast/access chain) are never declared

1 participant