From bdc5c4ebcbfa4c28e6661cf88cf4a192367a4714 Mon Sep 17 00:00:00 2001 From: David Neto Date: Fri, 19 Jul 2024 12:04:03 -0400 Subject: [PATCH] Support printf string args with nul (#1384) * Support printf string args with nul Add a test Fixed: #1371 * Update test/printf/printf_string_64bit_ptr.ll The metadata strings have NUL in them now too --- lib/PrintfPass.cpp | 4 +-- test/Printf/printf_string_64bit_ptr.ll | 4 +-- test/Printf/printf_string_with_null.ll | 43 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/Printf/printf_string_with_null.ll diff --git a/lib/PrintfPass.cpp b/lib/PrintfPass.cpp index b927dcca0..0164d13f9 100644 --- a/lib/PrintfPass.cpp +++ b/lib/PrintfPass.cpp @@ -48,10 +48,10 @@ std::string clspv::PrintfPass::GetStringLiteral(Value *Val) { if (GlobalVal) { auto *Initializer = GlobalVal->getInitializer(); if (auto *InitArray = dyn_cast(Initializer)) { - if (InitArray->isCString()) { + if (InitArray->isString()) { if (ConstExprEquivInstr) ConstExprEquivInstr->deleteValue(); - return InitArray->getAsCString().str(); + return InitArray->getAsString().str(); } } } diff --git a/test/Printf/printf_string_64bit_ptr.ll b/test/Printf/printf_string_64bit_ptr.ll index efc798b25..617d3e1b3 100644 --- a/test/Printf/printf_string_64bit_ptr.ll +++ b/test/Printf/printf_string_64bit_ptr.ll @@ -2,8 +2,8 @@ ; RUN: FileCheck %s < %t.ll ; CHECK-DAG: !clspv.printf_metadata = !{[[format:![0-9]*]], [[literal:![0-9]*]]} -; CHECK-DAG: [[format]] = !{i32 0, !"%4s", [[size:![0-9]*]]} -; CHECK-DAG: [[literal]] = !{i32 1, !"foo", {{.*}}} +; CHECK-DAG: [[format]] = !{i32 0, !"%4s\00", [[size:![0-9]*]]} +; CHECK-DAG: [[literal]] = !{i32 1, !"foo\00", {{.*}}} ; CHECK-DAG: [[size]] = !{i32 4} target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024" diff --git a/test/Printf/printf_string_with_null.ll b/test/Printf/printf_string_with_null.ll new file mode 100644 index 000000000..e03421828 --- /dev/null +++ b/test/Printf/printf_string_with_null.ll @@ -0,0 +1,43 @@ +; RUN: clspv-opt %s -o %t.ll --passes=printf-pass +; RUN: FileCheck %s < %t.ll + +; CHECK: !clspv.printf_metadata = !{![[FMT:[0-9]+]], ![[LITERAL:[0-9]+]]} +; CHECK: ![[FMT]] = !{i32 0, !"%s\00", +; CHECK: ![[LITERAL]] = !{i32 1, !"foo\00foo\00", + +target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1" +target triple = "spir-unknown-unknown" + +@.str = private unnamed_addr addrspace(2) constant [3 x i8] c"%s\00", align 1 +@.str.1 = private unnamed_addr addrspace(2) constant [8 x i8] c"foo\00foo\00", align 1 +@__spirv_WorkgroupSize = addrspace(8) global <3 x i32> zeroinitializer + +; Function Attrs: convergent norecurse nounwind +define dso_local spir_kernel void @m() #0 !kernel_arg_addr_space !6 !kernel_arg_access_qual !6 !kernel_arg_type !6 !kernel_arg_base_type !6 !kernel_arg_type_qual !6 !kernel_arg_name !6 !clspv.pod_args_impl !7 { +entry: + %call = call spir_func i32 (ptr addrspace(2), ...) @printf(ptr addrspace(2) @.str, ptr addrspace(2) @.str.1) #2 + ret void +} + +; Function Attrs: convergent nounwind +declare !kernel_arg_name !8 spir_func i32 @printf(ptr addrspace(2), ...) #1 + +attributes #0 = { convergent norecurse nounwind "no-builtins" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "stackrealign" "uniform-work-group-size"="true" } +attributes #1 = { convergent nounwind "no-builtins" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "stackrealign" } +attributes #2 = { convergent nobuiltin nounwind "no-builtins" } + +!llvm.module.flags = !{!0, !1, !2} +!opencl.ocl.version = !{!3} +!opencl.spir.version = !{!3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3, !3} +!llvm.ident = !{!4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4, !4} +!_Z28clspv.entry_point_attributes = !{!5} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 7, !"direct-access-external-data", i32 0} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 1, i32 2} +!4 = !{!"clang version 19.0.0git (https://github.com/llvm/llvm-project 402eca265f7162e26b8b74d18297fd76c9f100de)"} +!5 = !{!"m", !"kernel"} +!6 = !{} +!7 = !{i32 2} +!8 = !{!""}