From be50c598ff5211c3dc36469aa7734d49b3645c6d Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Sat, 10 Oct 2026 20:27:34 +0200 Subject: [PATCH] Emit printf formats reached through pointer casts as literals 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 --- lib/instns_ext.cpp | 2 +- lib/translator.h | 3 ++- lib/utils.cpp | 35 ++++++++++++++++++++++---- tests/cases/printf_fmt_cast.spt | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 tests/cases/printf_fmt_cast.spt diff --git a/lib/instns_ext.cpp b/lib/instns_ext.cpp index f87f2ba..c4f2c03 100644 --- a/lib/instns_ext.cpp +++ b/lib/instns_ext.cpp @@ -298,7 +298,7 @@ bool translator_impl::translate_extended_instruction(const Instruction &inst, auto format = operand(4); c::expr_ref format_arg; - // Check if we have cached string data for this variable + // NVIDIA requires the format to be a string literal. auto string_literal = string_literal_for(format); if (string_literal) { format_arg = c::literal(*string_literal); diff --git a/lib/translator.h b/lib/translator.h index 5c494be..b56da18 100644 --- a/lib/translator.h +++ b/lib/translator.h @@ -374,7 +374,8 @@ struct translator_impl { std::optional get_string_literal(const spvtools::opt::Instruction &inst) const; - std::optional string_literal_for(uint32_t var_id) const; + // The string literal `ptr_id` points to the start of, if any. + std::optional string_literal_for(uint32_t ptr_id) const; c::expr_ref null_constant(uint32_t tyid) const; c::expr_ref diff --git a/lib/utils.cpp b/lib/utils.cpp index de5017d..af60881 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -503,10 +503,35 @@ std::optional translator_impl::get_string_literal( } std::optional -translator_impl::string_literal_for(uint32_t var_id) const { - auto it = m_constant_string_literals.find(var_id); - if (it != m_constant_string_literals.end()) { - return it->second; +translator_impl::string_literal_for(uint32_t ptr_id) const { + // Front-ends often cast the string variable before using it (e.g. to a + // `uchar*` for printf), so look through casts and zero-offset access chains. + auto defuse = m_ir->get_def_use_mgr(); + auto cstmgr = m_ir->get_constant_mgr(); + while (true) { + auto it = m_constant_string_literals.find(ptr_id); + if (it != m_constant_string_literals.end()) { + return it->second; + } + + auto def = defuse->GetDef(ptr_id); + switch (def->opcode()) { + case spv::Op::OpBitcast: + break; + case spv::Op::OpAccessChain: + case spv::Op::OpInBoundsAccessChain: + case spv::Op::OpPtrAccessChain: + case spv::Op::OpInBoundsPtrAccessChain: + for (uint32_t i = 1; i < def->NumInOperands(); i++) { + auto cst = cstmgr->FindDeclaredConstant(def->GetSingleWordInOperand(i)); + if (cst == nullptr || !cst->IsZero()) { + return std::nullopt; + } + } + break; + default: + return std::nullopt; + } + ptr_id = def->GetSingleWordInOperand(0); } - return std::nullopt; } diff --git a/tests/cases/printf_fmt_cast.spt b/tests/cases/printf_fmt_cast.spt new file mode 100644 index 0000000..2eba741 --- /dev/null +++ b/tests/cases/printf_fmt_cast.spt @@ -0,0 +1,44 @@ +; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL2.0 -x cl - -o /dev/null +; RUN: %spirv2clc --asm %s | %FileCheck %s +; SPIR-V +; Version: 1.2 +; Generator: Khronos SPIR-V Tools Assembler; 0 +; Bound: 26 +; Schema: 0 +; The format string reaches printf through a pointer cast (LLVM's SPIR-V back-end +; casts it to `uchar*`) or a zero-index access chain (typed-pointer llvm-spirv). +; It must still be emitted as a string literal, which NVIDIA requires. + OpCapability Kernel + OpCapability Addresses + OpCapability Int8 + OpCapability Int64 + OpCapability Linkage + %1 = OpExtInstImport "OpenCL.std" + OpMemoryModel Physical64 OpenCL + %2 = OpTypeVoid + %3 = OpTypeInt 32 0 + %4 = OpTypeInt 8 0 + %5 = OpTypeInt 64 0 + %6 = OpConstant %3 4 + %7 = OpTypeArray %4 %6 + %8 = OpTypePointer UniformConstant %7 + %9 = OpTypePointer UniformConstant %4 + %10 = OpTypeFunction %2 + %11 = OpConstant %4 37 + %12 = OpConstant %4 100 + %13 = OpConstant %4 10 + %14 = OpConstant %4 0 + %15 = OpConstantComposite %7 %11 %12 %13 %14 + %16 = OpVariable %8 UniformConstant %15 + %17 = OpConstant %5 0 + %18 = OpConstant %3 42 + %20 = OpFunction %2 None %10 + %21 = OpLabel + %22 = OpBitcast %9 %16 + %23 = OpExtInst %3 %1 printf %22 %18 +; CHECK: printf("%d\n", (uint)42); + %24 = OpInBoundsPtrAccessChain %9 %16 %17 %17 + %25 = OpExtInst %3 %1 printf %24 %18 +; CHECK: printf("%d\n", (uint)42); + OpReturn + OpFunctionEnd