Skip to content

Commit 4f35166

Browse files
smilczekigcbot
authored andcommitted
OpenCLPrintfResolution account for struct type initializers
Initializer for an argument in a printfcall can be a struct type instead of array type. In such case, argIsString() returns true as long as initializer is zero, even though the arg is not string. This commit adds a check if arg is array type.
1 parent 003212a commit 4f35166

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/OpenCLPrintf/OpenCLPrintfResolution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ bool OpenCLPrintfResolution::argIsString(Value* arg)
323323
return false;
324324
}
325325
Constant* initializer = formatString->getInitializer();
326-
if (initializer->isZeroValue())
326+
if (initializer->isZeroValue() && initializer->getType()->isArrayTy())
327327
{
328328
// Is zeroinitializer; can't be casted to ConstantDataArray.
329329
// This caused zeroinitializers to be treated as non-strings,
@@ -397,7 +397,7 @@ void OpenCLPrintfResolution::removeExcessArgs()
397397
unsigned int numFormatSpecifiers = 0;
398398

399399
Constant* initializer = GV->getInitializer();
400-
if (initializer->isZeroValue())
400+
if (initializer->isZeroValue() && initializer->getType()->isArrayTy())
401401
// The string literal is empty, can't be casted to ConstantDataArray
402402
// and there is no way it contains any format specifiers.
403403
numFormatSpecifiers = 0;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2024 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
; RUN: igc_opt -igc-opencl-printf-resolution -S < %s | FileCheck %s
9+
; ------------------------------------------------
10+
; OpenCLPrintfResolution
11+
; ------------------------------------------------
12+
13+
%struct.A.base = type <{ i8 addrspace(4)*, i32 }>
14+
%struct.B = type { %struct.A.base, [4 x i8] }
15+
16+
@.str = internal unnamed_addr addrspace(2) constant [19 x i8] c"constructed A: %p\0A\00", align 1, !spirv.Decorations !0
17+
@.priv.__global = internal addrspace(1) global %struct.B zeroinitializer
18+
19+
; Function Attrs: nounwind
20+
define spir_kernel void @struct_printf_call(i32 addrspace(1)* %out, <8 x i32> %r0, <8 x i32> %payloadHeader, i8 addrspace(2)* %constBase, i8* %privateBase, i8 addrspace(1)* %printfBuffer) #0 {
21+
entry:
22+
%str = getelementptr inbounds [19 x i8], [19 x i8] addrspace(2)* @.str, i64 0, i64 0
23+
%bc = bitcast %struct.B addrspace(1)* @.priv.__global to i8 addrspace(1)*
24+
%asc = addrspacecast i8 addrspace(1)* %bc to i8 addrspace(4)*
25+
; CHECK: [[TMP1:%[A-z0-9]*]] = bitcast %struct.B addrspace(1)* @.priv.__global to i8 addrspace(1)*
26+
; CHECK: [[TMP2:%[A-z0-9]*]] = addrspacecast i8 addrspace(1)* [[TMP1]] to i8 addrspace(4)*
27+
; CHECK: [[TMP3:%[A-z0-9]*]] = ptrtoint i8 addrspace(4)* [[TMP2]] to i64
28+
; CHECK-NOT: [[TMP4:%[A-z0-9]*]] = ptrtoint %struct.B addrspace(1)* @.priv.__global to i64
29+
; CHECK: store i64 [[TMP3]]
30+
%call = call spir_func i32 (i8 addrspace(2)*, ...) @printf(i8 addrspace(2)* %str, i8 addrspace(4)* %asc) #0
31+
store i32 %call, i32 addrspace(1)* %out, align 4
32+
ret void
33+
}
34+
35+
; Function Attrs: nounwind
36+
declare spir_func i32 @printf(i8 addrspace(2)*, ...) #0
37+
38+
attributes #0 = { nounwind }
39+
40+
!igc.functions = !{!0}
41+
!IGCMetadata = !{!9}
42+
43+
!0 = !{void (i32 addrspace(1)*, <8 x i32>, <8 x i32>, i8 addrspace(2)*, i8*, i8 addrspace(1)*)* @struct_printf_call, !1}
44+
!1 = !{!2, !3}
45+
!2 = !{!"function_type", i32 0}
46+
!3 = !{!"implicit_arg_desc", !4, !5, !6, !7, !8}
47+
!4 = !{i32 0}
48+
!5 = !{i32 1}
49+
!6 = !{i32 10}
50+
!7 = !{i32 12}
51+
!8 = !{i32 13}
52+
!9 = !{!"ModuleMD", !10}
53+
!10 = !{!"FuncMD", !11, !12}
54+
!11 = distinct !{!"FuncMDMap[0]", void (i32 addrspace(1)*, <8 x i32>, <8 x i32>, i8 addrspace(2)*, i8*, i8 addrspace(1)*)* @struct_printf_call}
55+
!12 = !{!"FuncMDValue[0]", !13, !14, !15}
56+
!13 = !{!"localOffsets"}
57+
!14 = !{!"funcArgs"}
58+
!15 = !{!"functionType", !"KernelFunction"}

0 commit comments

Comments
 (0)