From 73420c84f262e8ce8259ef9a1e1bd2019f815383 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 9 Oct 2026 09:26:18 +0200 Subject: [PATCH] Declare built-in variables whose pointer escapes a direct load 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. --- lib/instns.cpp | 28 ++++--- lib/translator.cpp | 36 +++++++++ lib/translator.h | 13 ++-- tests/cases/builtin_pointer_spec_constant.spt | 31 ++++++++ tests/cases/builtin_pointer_use.spt | 74 +++++++++++++++++++ 5 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 tests/cases/builtin_pointer_spec_constant.spt create mode 100644 tests/cases/builtin_pointer_use.spt diff --git a/lib/instns.cpp b/lib/instns.cpp index 2d66352..71f45d1 100644 --- a/lib/instns.cpp +++ b/lib/instns.cpp @@ -349,11 +349,7 @@ bool translator_impl::translate_instruction(const Instruction &inst, // declared. auto builtin = m_builtin_variables.at(ptr); m_builtin_values[result] = builtin; - if (type_for(rtype)->kind() == Type::Kind::kVector) { - m_bindings[result] = builtin_vector(result); - } else { - m_bindings[result] = builtin_scalar(builtin); - } + m_bindings[result] = builtin_value(builtin, rtype); assign_result = false; } else { val = dereference(ptr, memory_access_operands(inst, 3)); @@ -692,7 +688,8 @@ bool translator_impl::translate_instruction(const Instruction &inst, if (m_builtin_values.count(comp)) { // Built-in values are vectors of scalars, so there is a single index. val = builtin_vector_extract( - comp, c::literal(std::to_string(inst.GetSingleWordOperand(3)))); + m_builtin_values.at(comp), + c::literal(std::to_string(inst.GetSingleWordOperand(3)))); break; } c::expr_ref member; @@ -739,7 +736,7 @@ bool translator_impl::translate_instruction(const Instruction &inst, auto vec = inst.GetSingleWordOperand(2); auto idx = inst.GetSingleWordOperand(3); if (m_builtin_values.count(vec)) { - val = builtin_vector_extract(vec, value(idx)); + val = builtin_vector_extract(m_builtin_values.at(vec), value(idx)); } else { val = c::index(c::cast(src_vector_element_type(type_id_for(vec)) + "*", c::address_of(materialize(vec, fb))), @@ -1376,10 +1373,10 @@ c::expr_ref translator_impl::builtin_scalar(SpvBuiltIn builtin) const { } } -c::expr_ref translator_impl::builtin_vector_extract(uint32_t id, +c::expr_ref translator_impl::builtin_vector_extract(SpvBuiltIn builtin, c::expr_ref idx) const { const char *query; - switch (m_builtin_values.at(id)) { + switch (builtin) { case SpvBuiltInGlobalInvocationId: query = "get_global_id"; break; @@ -1407,16 +1404,17 @@ c::expr_ref translator_impl::builtin_vector_extract(uint32_t id, return c::call(query, {std::move(idx)}); } -c::expr_ref translator_impl::builtin_vector(uint32_t id) const { - auto type = type_for_val(id); - auto vec = type ? type->AsVector() : nullptr; +c::expr_ref translator_impl::builtin_value(SpvBuiltIn builtin, + uint32_t tyid) const { + auto vec = type_for(tyid)->AsVector(); if (!vec) { - return c::literal(note_unsupported("non-vector built-in value")); + return builtin_scalar(builtin); } // (typeN)(query(0), query(1), ..., query(N-1)) std::vector comps; for (uint32_t i = 0; i < vec->element_count(); i++) { - comps.push_back(builtin_vector_extract(id, c::literal(std::to_string(i)))); + comps.push_back( + builtin_vector_extract(builtin, c::literal(std::to_string(i)))); } - return c::vector_literal(src_type(type_id_for(id)), std::move(comps)); + return c::vector_literal(src_type(tyid), std::move(comps)); } diff --git a/lib/translator.cpp b/lib/translator.cpp index 5367c97..b020ecd 100644 --- a/lib/translator.cpp +++ b/lib/translator.cpp @@ -24,6 +24,7 @@ #include #include +#include #include using namespace spvtools; @@ -788,6 +789,41 @@ bool translator_impl::translate_function(Function &func) { } } + // Built-in variables aren't declared: loads from them are bound to queries + // (see OpLoad). A function that uses a built-in's pointer some other way, + // e.g. bitcast and offset to load one component, gets a private copy. That + // includes uses through module-scope OpSpecConstantOps, which are bound to + // expressions naming their operands. + std::set used_builtins; + std::function find_builtins = [&](uint32_t id) { + if (m_builtin_variables.count(id)) { + used_builtins.insert(id); + return; + } + auto def = m_ir->get_def_use_mgr()->GetDef(id); + if (def && def->opcode() == spv::Op::OpSpecConstantOp) { + def->ForEachInId([&](const uint32_t *op) { find_builtins(*op); }); + } + }; + for (auto &bb : func) { + for (auto &inst : bb) { + if (inst.opcode() == spv::Op::OpLoad && + m_builtin_variables.count(inst.GetSingleWordOperand(2))) { + continue; + } + inst.ForEachInId([&](const uint32_t *id) { find_builtins(*id); }); + } + } + for (auto var : used_builtins) { + auto tyid = type_id_for(type_for_val(var)->AsPointer()->pointee_type()); + auto &storagename = derived_name(var, "_storage"); + fb.declare_upfront( + src_var_decl(tyid, storagename) + " = " + + c::print(builtin_value(m_builtin_variables.at(var), tyid))); + fb.declare_upfront(src_var_decl(var) + " = " + + c::print(c::address_of(c::name(storagename)))); + } + // ByVal parameters are passed by value; the body expects a pointer to it. func.ForEachParam([this, &fb](const Instruction *inst) { auto result = inst->result_id(); diff --git a/lib/translator.h b/lib/translator.h index 1588901..c05cd0c 100644 --- a/lib/translator.h +++ b/lib/translator.h @@ -349,15 +349,14 @@ struct translator_impl { c::expr_ref from_component(uint32_t composite_tyid, uint32_t elem_tyid, c::expr_ref component) const; - // The query of component `idx` of builtin vector value `id`. - c::expr_ref builtin_vector_extract(uint32_t id, c::expr_ref idx) const; + // The query of component `idx` of vector built-in `builtin`. + c::expr_ref builtin_vector_extract(SpvBuiltIn builtin, c::expr_ref idx) const; - // A whole vector built-in load (e.g. WorkgroupId) as a vector literal of its - // per-dimension queries, used when the load is consumed as a whole value - // (OpPhi incoming, store, ...) rather than component-extracted. - c::expr_ref builtin_vector(uint32_t id) const; + // The whole value of built-in `builtin` of type `tyid`. A vector built-in + // (e.g. WorkgroupId) is a vector literal of its per-dimension queries. + c::expr_ref builtin_value(SpvBuiltIn builtin, uint32_t tyid) const; - // The value of a load from scalar built-in variable `builtin`. + // The value of scalar built-in `builtin`. c::expr_ref builtin_scalar(SpvBuiltIn builtin) const; std::optional diff --git a/tests/cases/builtin_pointer_spec_constant.spt b/tests/cases/builtin_pointer_spec_constant.spt new file mode 100644 index 0000000..90e7250 --- /dev/null +++ b/tests/cases/builtin_pointer_spec_constant.spt @@ -0,0 +1,31 @@ +; A built-in used through a module-scope OpSpecConstantOp pointer is declared +; in the function using that constant, since the constant is bound to an +; expression naming the built-in. +; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null +; RUN: %spirv2clc --asm %s | %FileCheck %s + OpCapability Kernel + OpCapability Addresses + OpCapability Int64 + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %k "k" %lid + OpName %lid "lid" + OpDecorate %lid BuiltIn LocalInvocationId + %void = OpTypeVoid + %ulong = OpTypeInt 64 0 + %v3ulong = OpTypeVector %ulong 3 + %p_in_v3 = OpTypePointer Input %v3ulong + %p_g_v3 = OpTypePointer CrossWorkgroup %v3ulong + %ulong_0 = OpConstant %ulong 0 + %lid = OpVariable %p_in_v3 Input + %p = OpSpecConstantOp %p_in_v3 PtrAccessChain %lid %ulong_0 + %fnty = OpTypeFunction %void %p_g_v3 + %k = OpFunction %void None %fnty + %out = OpFunctionParameter %p_g_v3 + %entry = OpLabel + %v = OpLoad %v3ulong %p + OpStore %out %v + OpReturn + OpFunctionEnd +; CHECK: ulong3 lid_storage = (ulong3)(get_local_id(0), get_local_id(1), get_local_id(2)); +; CHECK: * lid = &lid_storage; +; CHECK: lid + (ulong)0 diff --git a/tests/cases/builtin_pointer_use.spt b/tests/cases/builtin_pointer_use.spt new file mode 100644 index 0000000..0a425a2 --- /dev/null +++ b/tests/cases/builtin_pointer_use.spt @@ -0,0 +1,74 @@ +; Built-in variables whose pointer is used other than by a direct load get a +; private copy in the function using them. The LLVM SPIR-V back end emits a +; bitcast and byte offset to load one component of a vector built-in; a pointer +; to a scalar built-in can be passed to a function. Built-ins only loaded +; directly are not declared. The copy is a plain value: decorations of the +; built-in (here Restrict) don't apply to it. +; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null +; RUN: %spirv2clc --asm %s | %FileCheck %s --implicit-check-not=gid_storage + OpCapability Kernel + OpCapability Addresses + OpCapability Int64 + OpCapability Int8 + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %k "k" %lid %wd %gid + OpName %lid "lid" + OpName %wd "wd" + OpName %gid "gid" + OpName %load "load" + OpDecorate %lid BuiltIn LocalInvocationId + OpDecorate %lid Restrict + OpDecorate %wd BuiltIn WorkDim + OpDecorate %gid BuiltIn GlobalInvocationId + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %ulong = OpTypeInt 64 0 + %uchar = OpTypeInt 8 0 + %v3ulong = OpTypeVector %ulong 3 + %p_in_v3 = OpTypePointer Input %v3ulong + %p_in_ulong = OpTypePointer Input %ulong + %p_in_uchar = OpTypePointer Input %uchar + %p_in_uint = OpTypePointer Input %uint + %p_g_ulong = OpTypePointer CrossWorkgroup %ulong + %ulong_1 = OpConstant %ulong 1 + %ulong_2 = OpConstant %ulong 2 + %ulong_8 = OpConstant %ulong 8 + %lid = OpVariable %p_in_v3 Input + %wd = OpVariable %p_in_uint Input + %gid = OpVariable %p_in_v3 Input + %fty_ld = OpTypeFunction %uint %p_in_uint + %fnty = OpTypeFunction %void %p_g_ulong + %load = OpFunction %uint None %fty_ld + %p = OpFunctionParameter %p_in_uint + %lbb = OpLabel + %pv = OpLoad %uint %p + OpReturnValue %pv + OpFunctionEnd + %k = OpFunction %void None %fnty + %out = OpFunctionParameter %p_g_ulong + %entry = OpLabel + %b = OpBitcast %p_in_uchar %lid + %c = OpInBoundsPtrAccessChain %p_in_uchar %b %ulong_8 + %d = OpBitcast %p_in_ulong %c + %v = OpLoad %ulong %d Aligned 8 + OpStore %out %v Aligned 8 + %w = OpFunctionCall %uint %load %wd + %wl = OpUConvert %ulong %w + %o1 = OpInBoundsPtrAccessChain %p_g_ulong %out %ulong_1 + OpStore %o1 %wl Aligned 8 + %g = OpLoad %v3ulong %gid + %gx = OpCompositeExtract %ulong %g 0 + %o2 = OpInBoundsPtrAccessChain %p_g_ulong %out %ulong_2 + OpStore %o2 %gx Aligned 8 + OpReturn + OpFunctionEnd +; CHECK-LABEL: load( +; CHECK-NOT: _storage +; CHECK-LABEL: void kernel k( +; CHECK-DAG: ulong3 lid_storage = (ulong3)(get_local_id(0), get_local_id(1), get_local_id(2)); +; CHECK-DAG: * lid = &lid_storage; +; CHECK-DAG: uint wd_storage = get_work_dim(); +; CHECK-DAG: * wd = &wd_storage; +; CHECK: (ma{{[0-9]+}} *)lid +; CHECK: load(wd) +; CHECK: get_global_id(0)