Skip to content

Commit 3217b81

Browse files
committed
Fix a bug in ProgramScopeConstantResolution
Revert a bug in ProgramScopeConstantResolution.cpp introduced by erroneous refactor in commit 4f7756c We need to reach `continue` statement when bufferOffset->second is equal to -1. (cherry picked from commit 469ae7d)
1 parent 39aad29 commit 3217b81

File tree

4 files changed

+113
-76
lines changed

4 files changed

+113
-76
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/ProgramScopeConstants/ProgramScopeConstantAnalysis.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ bool ProgramScopeConstantAnalysis::runOnModule(Module& M)
101101
}
102102
// Include the variable in 'inlineProgramScopeOffsets', otherwise
103103
// code gen will not emit relocation. In case of S_UNDEF/extenal
104-
// variable offset is not expected to be used. Using -1 as a
105-
// placeholder so the unexpected usage will fail visibly when
106-
// trying to emit variable with 0xffffffff offset
104+
// variable offset is not expected to be used.
105+
// Using -1 to communicate to ProgramScopeConstantResolution that
106+
// this offset shouldn't be resolved.
107107
inlineProgramScopeOffsets[globalVar] = -1;
108108
continue;
109109
}

IGC/Compiler/Optimizer/OpenCLPasses/ProgramScopeConstants/ProgramScopeConstantResolution.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*========================== begin_copyright_notice ============================
22
3-
Copyright (C) 2017-2021 Intel Corporation
3+
Copyright (C) 2017-2023 Intel Corporation
44
55
SPDX-License-Identifier: MIT
66
@@ -132,8 +132,10 @@ bool ProgramScopeConstantResolution::runOnModule(Module& M)
132132

133133
// Get the offset of this constant from the base.
134134
int64_t offset = bufferOffset->second;
135-
ConstantInt* pOffset = ConstantInt::get(Type::getInt64Ty(C), offset);
135+
if (offset == -1)
136+
continue;
136137

138+
ConstantInt* pOffset = ConstantInt::get(Type::getInt64Ty(C), offset);
137139
const ImplicitArg::ArgType argType =
138140
AS == ADDRESS_SPACE_GLOBAL ? ImplicitArg::GLOBAL_BASE : ImplicitArg::CONSTANT_BASE;
139141

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;=========================== begin_copyright_notice ============================
22
;
3-
; Copyright (C) 2022 Intel Corporation
3+
; Copyright (C) 2022-2023 Intel Corporation
44
;
55
; SPDX-License-Identifier: MIT
66
;
@@ -26,7 +26,7 @@
2626
@llvm.used = appending global [3 x i8*] [i8* addrspacecast (i8 addrspace(2)* bitcast ([2 x i32] addrspace(2)* @a to i8 addrspace(2)*) to i8*), i8* addrspacecast (i8 addrspace(1)* bitcast (i32 addrspace(1)* @c to i8 addrspace(1)*) to i8*), i8* addrspacecast (i8 addrspace(1)* bitcast (i32 addrspace(2)* addrspace(1)* @d to i8 addrspace(1)*) to i8*)], section "llvm.metadata"
2727

2828
; Function Attrs: convergent noinline nounwind optnone
29-
define spir_kernel void @test_program(i32 addrspace(1)* %dst, <8 x i32> %r0, <8 x i32> %payloadHeader, i8 addrspace(2)* %constBase, i8 addrspace(1)* %globalBase, i8* %privateBase, i32 %bufferOffset) #0 {
29+
define spir_kernel void @test_program(i32 addrspace(1)* %dst, <8 x i32> %r0, <8 x i32> %payloadHeader, i8 addrspace(2)* %constBase, i8 addrspace(1)* %globalBase, i8* %privateBase, i32 %bufferOffset) {
3030
; CHECK-LABEL: @test_program(
3131
; CHECK: entry:
3232
; CHECK: [[OFFC:%.*]] = getelementptr i8, i8 addrspace(1)* %globalBase, i64 8
@@ -59,74 +59,26 @@ entry:
5959
ret void
6060
}
6161

62-
; Function Attrs: nounwind readnone speculatable
63-
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
62+
!IGCMetadata = !{!0}
63+
!igc.functions = !{!20}
6464

65-
; Function Attrs: nounwind readnone speculatable
66-
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
65+
!0 = !{!"ModuleMD", !1}
66+
!1 = !{!"inlineProgramScopeOffsets", !2, !3, !4, !5, !6, !7}
67+
!2 = !{!"inlineProgramScopeOffsetsMap[0]", [2 x i32] addrspace(2)* @a}
68+
!3 = !{!"inlineProgramScopeOffsetsValue[0]", i64 0}
69+
!4 = !{!"inlineProgramScopeOffsetsMap[1]", i32 addrspace(1)* @c}
70+
!5 = !{!"inlineProgramScopeOffsetsValue[1]", i64 8}
71+
!6 = !{!"inlineProgramScopeOffsetsMap[2]", i32 addrspace(2)* addrspace(1)* @d}
72+
!7 = !{!"inlineProgramScopeOffsetsValue[2]", i64 0}
6773

68-
attributes #0 = { convergent noinline nounwind optnone }
69-
attributes #1 = { nounwind readnone speculatable }
70-
71-
!IGCMetadata = !{!3}
72-
!igc.functions = !{!51}
73-
74-
!3 = !{!"ModuleMD", !4, !6, !44}
75-
!4 = !{!"compOpt", !5}
76-
!5 = !{!"OptDisable", i1 true}
77-
!6 = !{!"FuncMD", !7, !8}
78-
!7 = !{!"FuncMDMap[0]", void (i32 addrspace(1)*, <8 x i32>, <8 x i32>, i8 addrspace(2)*, i8 addrspace(1)*, i8*, i32)* @test_program}
79-
!8 = !{!"FuncMDValue[0]", !9, !10, !14, !32, !34, !36, !38, !40, !42}
80-
!9 = !{!"localOffsets"}
81-
!10 = !{!"workGroupWalkOrder", !11, !12, !13}
82-
!11 = !{!"dim0", i32 0}
83-
!12 = !{!"dim1", i32 0}
84-
!13 = !{!"dim2", i32 0}
85-
!14 = !{!"resAllocMD", !15, !16}
86-
!15 = !{!"samplersNumType", i32 0}
87-
!16 = !{!"argAllocMDList", !17, !21, !24, !25, !27, !29, !31}
88-
!17 = !{!"argAllocMDListVec[0]", !18, !19, !20}
89-
!18 = !{!"type", i32 1}
90-
!19 = !{!"extensionType", i32 -1}
91-
!20 = !{!"indexType", i32 0}
92-
!21 = !{!"argAllocMDListVec[1]", !22, !19, !23}
93-
!22 = !{!"type", i32 0}
94-
!23 = !{!"indexType", i32 -1}
95-
!24 = !{!"argAllocMDListVec[2]", !22, !19, !23}
96-
!25 = !{!"argAllocMDListVec[3]", !18, !19, !26}
97-
!26 = !{!"indexType", i32 1}
98-
!27 = !{!"argAllocMDListVec[4]", !18, !19, !28}
99-
!28 = !{!"indexType", i32 2}
100-
!29 = !{!"argAllocMDListVec[5]", !18, !19, !30}
101-
!30 = !{!"indexType", i32 3}
102-
!31 = !{!"argAllocMDListVec[6]", !22, !19, !23}
103-
!32 = !{!"m_OpenCLArgAddressSpaces", !33}
104-
!33 = !{!"m_OpenCLArgAddressSpacesVec[0]", i32 1}
105-
!34 = !{!"m_OpenCLArgAccessQualifiers", !35}
106-
!35 = !{!"m_OpenCLArgAccessQualifiersVec[0]", !"none"}
107-
!36 = !{!"m_OpenCLArgTypes", !37}
108-
!37 = !{!"m_OpenCLArgTypesVec[0]", !"int*"}
109-
!38 = !{!"m_OpenCLArgBaseTypes", !39}
110-
!39 = !{!"m_OpenCLArgBaseTypesVec[0]", !"int*"}
111-
!40 = !{!"m_OpenCLArgTypeQualifiers", !41}
112-
!41 = !{!"m_OpenCLArgTypeQualifiersVec[0]", !""}
113-
!42 = !{!"m_OpenCLArgNames", !43}
114-
!43 = !{!"m_OpenCLArgNamesVec[0]", !"dst"}
115-
!44 = !{!"inlineProgramScopeOffsets", !45, !46, !47, !48, !49, !50}
116-
!45 = !{!"inlineProgramScopeOffsetsMap[0]", [2 x i32] addrspace(2)* @a}
117-
!46 = !{!"inlineProgramScopeOffsetsValue[0]", i32 0}
118-
!47 = !{!"inlineProgramScopeOffsetsMap[1]", i32 addrspace(1)* @c}
119-
!48 = !{!"inlineProgramScopeOffsetsValue[1]", i32 8}
120-
!49 = !{!"inlineProgramScopeOffsetsMap[2]", i32 addrspace(2)* addrspace(1)* @d}
121-
!50 = !{!"inlineProgramScopeOffsetsValue[2]", i32 0}
122-
!51 = !{void (i32 addrspace(1)*, <8 x i32>, <8 x i32>, i8 addrspace(2)*, i8 addrspace(1)*, i8*, i32)* @test_program, !52}
123-
!52 = !{!53, !54}
124-
!53 = !{!"function_type", i32 0}
125-
!54 = !{!"implicit_arg_desc", !55, !56, !57, !58, !59, !60}
126-
!55 = !{i32 0}
127-
!56 = !{i32 1}
128-
!57 = !{i32 10}
129-
!58 = !{i32 11}
130-
!59 = !{i32 12}
131-
!60 = !{i32 14, !61}
132-
!61 = !{!"explicit_arg_num", i32 0}
74+
!20 = !{void (i32 addrspace(1)*, <8 x i32>, <8 x i32>, i8 addrspace(2)*, i8 addrspace(1)*, i8*, i32)* @test_program, !21}
75+
!21 = !{!22, !23}
76+
!22 = !{!"function_type", i32 0}
77+
!23 = !{!"implicit_arg_desc", !24, !25, !26, !27, !28, !29}
78+
!24 = !{i32 0}
79+
!25 = !{i32 1}
80+
!26 = !{i32 10}
81+
!27 = !{i32 11}
82+
!28 = !{i32 12}
83+
!29 = !{i32 14, !30}
84+
!30 = !{!"explicit_arg_num", i32 0}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2023 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
; REQUIRES: regkeys
9+
;
10+
; RUN: igc_opt -regkey EnableZEBinary=0 -enable-debugify --igc-programscope-constant-resolve -S < %s 2>&1 | FileCheck %s
11+
; ------------------------------------------------
12+
; ProgramScopeConstantResolution
13+
; ------------------------------------------------
14+
15+
; CHECK: CheckModuleDebugify: PASS
16+
17+
; This test is based on basic.ll but checks that we skip constant resolution when
18+
; inlineProgramScopeOffsetsValue[...] is set to -1.
19+
; There is at least one case in ProgramScopeConstantAnalysis pass where we need to include
20+
; a variable in 'inlineProgramScopeOffsets' for the purposes of codegen emitting a relocation
21+
; at a later point. We do not want to use the offset value itself so we put -1 there.
22+
;
23+
; Here is an example taken from a shader dump:
24+
; !373 = !{!"inlineProgramScopeOffsetsMap[0]", %struct.kmp_program_data addrspace(1)* @__omp_spirv_program_data}
25+
; !374 = !{!"inlineProgramScopeOffsetsValue[0]", i64 -1}
26+
27+
@a = internal addrspace(2) constant [2 x i32] [i32 0, i32 1], align 4
28+
@d = internal addrspace(1) global i32 addrspace(2)* getelementptr inbounds ([2 x i32], [2 x i32] addrspace(2)* @a, i32 0, i32 0), align 8
29+
@c = internal addrspace(1) global i32 0, align 4
30+
@b = common addrspace(1) global i32 0, align 4
31+
@llvm.used = appending global [3 x i8*] [i8* addrspacecast (i8 addrspace(2)* bitcast ([2 x i32] addrspace(2)* @a to i8 addrspace(2)*) to i8*), i8* addrspacecast (i8 addrspace(1)* bitcast (i32 addrspace(1)* @c to i8 addrspace(1)*) to i8*), i8* addrspacecast (i8 addrspace(1)* bitcast (i32 addrspace(2)* addrspace(1)* @d to i8 addrspace(1)*) to i8*)], section "llvm.metadata"
32+
33+
; Function Attrs: convergent noinline nounwind optnone
34+
define spir_kernel void @test_program(i32 addrspace(1)* %dst, <8 x i32> %r0, <8 x i32> %payloadHeader, i8 addrspace(2)* %constBase, i8 addrspace(1)* %globalBase, i8* %privateBase, i32 %bufferOffset) {
35+
; CHECK-LABEL: @test_program(
36+
; CHECK: entry:
37+
; CHECK: [[DST_ADDR:%.*]] = alloca i32 addrspace(1)*, align 8
38+
; CHECK: [[AA:%.*]] = alloca i32, align 4
39+
; CHECK: store i32 addrspace(1)* [[DST:%.*]], i32 addrspace(1)** [[DST_ADDR]], align 8
40+
; CHECK: [[TMP0:%.*]] = getelementptr inbounds [2 x i32], [2 x i32] addrspace(2)* @a, i64 0, i64 1
41+
; CHECK: [[TMP1:%.*]] = load i32, i32 addrspace(2)* [[TMP0]], align 4
42+
; CHECK: store i32 [[TMP1]], i32* [[AA]], align 4
43+
; CHECK: [[TMP2:%.*]] = load i32 addrspace(2)*, i32 addrspace(2)* addrspace(1)* @d, align 8
44+
; CHECK: [[TMP3:%.*]] = load i32, i32 addrspace(2)* [[TMP2]], align 4
45+
; CHECK: store i32 [[TMP3]], i32 addrspace(1)* @c, align 4
46+
; CHECK: ret void
47+
;
48+
entry:
49+
%dst.addr = alloca i32 addrspace(1)*, align 8
50+
%aa = alloca i32, align 4
51+
store i32 addrspace(1)* %dst, i32 addrspace(1)** %dst.addr, align 8
52+
%0 = getelementptr inbounds [2 x i32], [2 x i32] addrspace(2)* @a, i64 0, i64 1
53+
%1 = load i32, i32 addrspace(2)* %0, align 4
54+
store i32 %1, i32* %aa, align 4
55+
%2 = load i32 addrspace(2)*, i32 addrspace(2)* addrspace(1)* @d, align 8
56+
%3 = load i32, i32 addrspace(2)* %2, align 4
57+
store i32 %3, i32 addrspace(1)* @c, align 4
58+
ret void
59+
}
60+
61+
!IGCMetadata = !{!0}
62+
!igc.functions = !{!20}
63+
64+
!0 = !{!"ModuleMD", !1}
65+
!1 = !{!"inlineProgramScopeOffsets", !2, !3, !4, !5, !6, !7}
66+
!2 = !{!"inlineProgramScopeOffsetsMap[0]", [2 x i32] addrspace(2)* @a}
67+
!3 = !{!"inlineProgramScopeOffsetsValue[0]", i64 -1}
68+
!4 = !{!"inlineProgramScopeOffsetsMap[1]", i32 addrspace(1)* @c}
69+
!5 = !{!"inlineProgramScopeOffsetsValue[1]", i64 -1}
70+
!6 = !{!"inlineProgramScopeOffsetsMap[2]", i32 addrspace(2)* addrspace(1)* @d}
71+
!7 = !{!"inlineProgramScopeOffsetsValue[2]", i64 -1}
72+
73+
!20 = !{void (i32 addrspace(1)*, <8 x i32>, <8 x i32>, i8 addrspace(2)*, i8 addrspace(1)*, i8*, i32)* @test_program, !21}
74+
!21 = !{!22, !23}
75+
!22 = !{!"function_type", i32 0}
76+
!23 = !{!"implicit_arg_desc", !24, !25, !26, !27, !28, !29}
77+
!24 = !{i32 0}
78+
!25 = !{i32 1}
79+
!26 = !{i32 10}
80+
!27 = !{i32 11}
81+
!28 = !{i32 12}
82+
!29 = !{i32 14, !30}
83+
!30 = !{!"explicit_arg_num", i32 0}

0 commit comments

Comments
 (0)