Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/instns_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ struct translator_impl {

std::optional<std::string>
get_string_literal(const spvtools::opt::Instruction &inst) const;
std::optional<std::string> string_literal_for(uint32_t var_id) const;
// The string literal `ptr_id` points to the start of, if any.
std::optional<std::string> string_literal_for(uint32_t ptr_id) const;

c::expr_ref null_constant(uint32_t tyid) const;
c::expr_ref
Expand Down
35 changes: 30 additions & 5 deletions lib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,35 @@ std::optional<std::string> translator_impl::get_string_literal(
}

std::optional<std::string>
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;
}
44 changes: 44 additions & 0 deletions tests/cases/printf_fmt_cast.spt
Original file line number Diff line number Diff line change
@@ -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
Loading