Skip to content

Commit 3023528

Browse files
authored
[sycl-post-link] Fix struct type handling for 'default' spec constants (#4797)
This patch adds handling of struct types that were zero initialized and have no real struct operands - re-create operands to proceed with default values collection.
1 parent 1cf024a commit 3023528

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
%class.specialization_id.6 = type { %struct.MArrayConst3 }
2828
%struct.MArrayConst4 = type { [2 x [2 x [3 x i32]]] }
2929
%class.specialization_id.7 = type { %struct.MArrayConst4 }
30+
%class.specialization_id.8 = type { %struct.ComposConst3 }
31+
%struct.ComposConst3 = type { i32, i32, %struct.myConst}
3032

3133
@id_half = dso_local global %class.specialization_id { half 0xH4000 }, align 8
3234
@id_int = dso_local global %class.specialization_id.0 { i32 42 }, align 4
@@ -37,6 +39,7 @@
3739
@id_marray2 = dso_local global %class.specialization_id.5 { %struct.MArrayConst2 { [3 x i32] [i32 1, i32 2, i32 3] } }, align 8
3840
@id_marray3 = dso_local global %class.specialization_id.6 { %struct.MArrayConst3 { [3 x i64] [i64 1, i64 2, i64 3] } }, align 8
3941
@id_marray4 = dso_local global %class.specialization_id.7 { %struct.MArrayConst4 { [2 x [2 x [3 x i32]]] [[2 x [3 x i32]] [[3 x i32] [i32 1, i32 2, i32 3], [3 x i32] [i32 1, i32 2, i32 3]], [2 x [3 x i32]] [[3 x i32] [i32 1, i32 2, i32 3], [3 x i32] [i32 1, i32 2, i32 3]]] } }, align 8
42+
@id_compos3 = dso_local global %class.specialization_id.8 zeroinitializer, align 4
4043

4144
; check that the following globals are preserved: even though they are won't be
4245
; used in the module anymore, they could still be referenced by debug info
@@ -51,6 +54,7 @@
5154
@__builtin_unique_stable_name._Z27get_specialization_constantIL_Z6id_intE17specialization_idIiEiET1_v = private unnamed_addr constant [34 x i8] c"_ZTS14name_generatorIL_Z6id_intEE\00", align 1
5255
@__builtin_unique_stable_name._Z27get_specialization_constantIL_Z9id_composE17specialization_idI11ComposConstES1_ET1_v = private unnamed_addr constant [37 x i8] c"_ZTS14name_generatorIL_Z9id_composEE\00", align 1
5356
@__builtin_unique_stable_name._Z27get_specialization_constantIL_Z10id_compos2E17specialization_idI12ComposConst2ES1_ET1_v = private unnamed_addr constant [39 x i8] c"_ZTS14name_generatorIL_Z10id_compos2EE\00", align 1
57+
@__builtin_unique_stable_name._Z27get_specialization_constantIL_Z10id_compos3E17specialization_idI12ComposConst3ES1_ET1_v = private unnamed_addr constant [39 x i8] c"_ZTS14name_generatorIL_Z10id_compos3EE\00", align 1
5458
@__builtin_unique_stable_name._Z27get_specialization_constantIL_Z10id_vectorE17specialization_idI11VectorConstES1_ET1_v = private unnamed_addr constant [38 x i8] c"_ZTS14name_generatorIL_Z10id_vectorEE\00", align 1
5559
@__builtin_unique_stable_name._Z27get_specialization_constantIL_Z10id_marrayE17specialization_idI11MArrayConstES1_ET1_v = private unnamed_addr constant [38 x i8] c"_ZTS14name_generatorIL_Z10id_marrayEE\00", align 1
5660
@__builtin_unique_stable_name.id_marray2 = private unnamed_addr constant [39 x i8] c"_ZTS14name_generatorIL_Z10id_marray2EE\00", align 1
@@ -141,29 +145,46 @@ entry:
141145
ret void
142146
}
143147

148+
define void @test_zeroinit() {
149+
%tmp = alloca %struct.ComposConst3, align 4
150+
%1 = bitcast %struct.ComposConst3* %tmp to i8*
151+
; CHECK-DEF: %[[GEP3:[0-9a-z]+]] = getelementptr i8, i8* null, i32 54
152+
; CHECK-DEF: %[[BITCAST3:[0-9a-z]+]] = bitcast i8* %[[GEP3]] to %struct.ComposConst3*
153+
; CHECK-DEF: %[[C3:[0-9a-z]+]] = load %struct.ComposConst3, %struct.ComposConst3* %[[BITCAST3]], align 4
154+
;
155+
; CHECK-RT: %[[#SE5:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID10:]], i32 0)
156+
; CHECK-RT: %[[#SE6:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID11:]], i32 0)
157+
; CHECK-RT: %[[#SE7:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID12:]], i32 0)
158+
; CHECK-RT: %[[#SE8:]] = call float @_Z20__spirv_SpecConstantif(i32 [[#SCID13:]], float 0.000000e+00)
159+
; CHECK-RT: %[[#SE9:]] = call %struct.myConst @_Z29__spirv_SpecConstantCompositeif_Rstruct.myConst(i32 %[[#SE7]], float %[[#SE8]])
160+
; CHECK-RT: call %struct.ComposConst3 @_Z29__spirv_SpecConstantCompositeiistruct.myConst_Rstruct.ComposConst3(i32 %[[#SE5]], i32 %[[#SE6]], %struct.myConst %[[#SE9]])
161+
call void @_Z40__sycl_getComposite2020SpecConstantValueI12ComposConst3ET_PKcPvS4_(%struct.ComposConst3* sret(%struct.ComposConst3) align 4 %tmp, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @__builtin_unique_stable_name._Z27get_specialization_constantIL_Z10id_compos3E17specialization_idI12ComposConst3ES1_ET1_v, i64 0, i64 0), i8* bitcast (%class.specialization_id.8* @id_compos3 to i8*), i8* null)
162+
ret void
163+
}
164+
144165
define void @test3() {
145166
%tmp = alloca %struct.VectorConst, align 8
146167
%tmp1 = alloca %struct.MArrayConst, align 8
147168
%tmp2 = alloca %struct.MArrayConst2, align 8
148169
%tmp3 = alloca %struct.MArrayConst3, align 8
149170
%tmp4 = alloca %struct.MArrayConst4, align 8
150171
%1 = bitcast %struct.VectorConst* %tmp to i8*
151-
; CHECK-DEF: %[[GEP1:[0-9a-z]+]] = getelementptr i8, i8* null, i32 54
172+
; CHECK-DEF: %[[GEP1:[0-9a-z]+]] = getelementptr i8, i8* null, i32 70
152173
; CHECK-DEF: %[[BITCAST1:[0-9a-z]+]] = bitcast i8* %[[GEP1]] to %struct.VectorConst*
153174
; CHECK-DEF: %[[C1:[0-9a-z]+]] = load %struct.VectorConst, %struct.VectorConst* %[[BITCAST1]], align 8
154175
;
155-
; CHECK-RT: %[[#SE1:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID10:]], i32 1)
156-
; CHECK-RT: %[[#SE2:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID11:]], i32 2)
176+
; CHECK-RT: %[[#SE1:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID14:]], i32 1)
177+
; CHECK-RT: %[[#SE2:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID15:]], i32 2)
157178
; CHECK-RT: %[[#CE1:]] = call <2 x i32> @_Z29__spirv_SpecConstantCompositeii_RDv2_i(i32 %[[#SE1]], i32 %[[#SE2]])
158179
; CHECK-RT: call %struct.VectorConst @_Z29__spirv_SpecConstantCompositeDv2_i_Rstruct.VectorConst(<2 x i32> %[[#CE1]])
159180
call void @_Z40__sycl_getComposite2020SpecConstantValueI11VectorConstET_PKcPvS4_(%struct.VectorConst* nonnull sret(%struct.VectorConst) align 8 %tmp, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @__builtin_unique_stable_name._Z27get_specialization_constantIL_Z10id_vectorE17specialization_idI11VectorConstES1_ET1_v, i64 0, i64 0), i8* bitcast (%class.specialization_id.3* @id_vector to i8*), i8* null)
160181
%2 = bitcast %struct.MArrayConst* %tmp1 to i8*
161-
; CHECK-DEF: %[[GEP2:[0-9a-z]+]] = getelementptr i8, i8* null, i32 62
182+
; CHECK-DEF: %[[GEP2:[0-9a-z]+]] = getelementptr i8, i8* null, i32 78
162183
; CHECK-DEF: %[[BITCAST2:[0-9a-z]+]] = bitcast i8* %[[GEP2]] to %struct.MArrayConst*
163184
; CHECK-DEF: %[[C2:[0-9a-z]+]] = load %struct.MArrayConst, %struct.MArrayConst* %[[BITCAST2]], align 4
164185
;
165-
; CHECK-RT: %[[#SE1:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID12:]], i32 1)
166-
; CHECK-RT: %[[#SE2:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID13:]], i32 2)
186+
; CHECK-RT: %[[#SE1:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID16:]], i32 1)
187+
; CHECK-RT: %[[#SE2:]] = call i32 @_Z20__spirv_SpecConstantii(i32 [[#SCID17:]], i32 2)
167188
; CHECK-RT: %[[#CE1:]] = call [2 x i32] @_Z29__spirv_SpecConstantCompositeii_RA2_i(i32 %[[#SE1]], i32 %[[#SE2]])
168189
; CHECK-RT: call %struct.MArrayConst @_Z29__spirv_SpecConstantCompositeA2_i_Rstruct.MArrayConst([2 x i32] %[[#CE1]])
169190
call void @_Z40__sycl_getComposite2020SpecConstantValueI11MArrayConstET_PKcPvS4_(%struct.MArrayConst* nonnull sret(%struct.MArrayConst) align 8 %tmp1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @__builtin_unique_stable_name._Z27get_specialization_constantIL_Z10id_marrayE17specialization_idI11MArrayConstES1_ET1_v, i64 0, i64 0), i8* bitcast (%class.specialization_id.4* @id_marray to i8*), i8* null)
@@ -194,6 +215,8 @@ declare dso_local void @_Z40__sycl_getComposite2020SpecConstantValueI11ComposCon
194215

195216
declare dso_local void @_Z40__sycl_getComposite2020SpecConstantValueI12ComposConst2ET_PKcPvS4_(%struct.ComposConst2* sret(%struct.ComposConst2) align 8, i8*, i8*, i8*) local_unnamed_addr #2
196217

218+
declare dso_local void @_Z40__sycl_getComposite2020SpecConstantValueI12ComposConst3ET_PKcPvS4_(%struct.ComposConst3* sret(%struct.ComposConst3) align 8, i8*, i8*, i8*) local_unnamed_addr #2
219+
197220
declare dso_local void @_Z40__sycl_getComposite2020SpecConstantValueI11VectorConstET_PKcPvS4_(%struct.VectorConst* sret(%struct.VectorConst) align 8, i8*, i8*, i8*) local_unnamed_addr #2
198221

199222
declare dso_local void @_Z40__sycl_getComposite2020SpecConstantValueI11MArrayConstET_PKcPvS4_(%struct.MArrayConst* sret(%struct.MArrayConst) align 8, i8*, i8*, i8*) local_unnamed_addr #2
@@ -209,9 +232,9 @@ attributes #1 = { argmemonly nofree nosync nounwind willreturn }
209232
attributes #2 = { "denormal-fp-math"="preserve-sign,preserve-sign" "denormal-fp-math-f32"="ieee,ieee" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="true" "use-soft-float"="false" }
210233
attributes #3 = { nounwind }
211234

212-
; CHECK: !sycl.specialization-constants = !{![[#ID0:]], ![[#ID1:]], ![[#ID2:]], ![[#ID3:]], ![[#ID4:]], ![[#ID5:]]
235+
; CHECK: !sycl.specialization-constants = !{![[#ID0:]], ![[#ID1:]], ![[#ID2:]], ![[#ID3:]], ![[#ID_COMPOS3:]], ![[#ID4:]], ![[#ID5:]]
213236
;
214-
; CHECK-DEF: !sycl.specialization-constants-default-values = !{![[#ID4:]], ![[#ID5:]], ![[#ID6:]], ![[#ID7:]], ![[#ID8:]], ![[#ID9:]]
237+
; CHECK-DEF: !sycl.specialization-constants-default-values = !{![[#ID4:]], ![[#ID5:]], ![[#ID6:]], ![[#ID7:]], ![[#ID_COMPOS3_DEFAULT:]], ![[#ID8:]], ![[#ID9:]]
215238
; CHECK-RT-NOT: !sycl.specialization-constants-default-values
216239
;
217240
; CHECK: ![[#ID0]] = !{!"_ZTS14name_generatorIL_Z9id_halfEE", i32 0, i32 0, i32 2}
@@ -221,6 +244,7 @@ attributes #3 = { nounwind }
221244
;
222245
; CHECK-DEF: ![[#ID2]] = !{!"_ZTS14name_generatorIL_Z9id_composEE", i32 2, i32 0, i32 24}
223246
; CHECK-DEF: ![[#ID3]] = !{!"_ZTS14name_generatorIL_Z10id_compos2EE", i32 3, i32 0, i32 24
247+
; CHECK-DEF: ![[#ID_COMPOS3]] = !{!"_ZTS14name_generatorIL_Z10id_compos3EE", i32 4, i32 0, i32 16
224248
;
225249
; CHECK-RT: ![[#ID2]] = !{!"_ZTS14name_generatorIL_Z9id_composEE", i32 [[#SCID2]], i32 0, i32 4,
226250
; CHECK-RT-SAME: i32 [[#SCID3]], i32 8, i32 8,
@@ -235,14 +259,15 @@ attributes #3 = { nounwind }
235259
; CHECK-DEF: ![[#ID5]] = !{i32 42}
236260
; CHECK-DEF: ![[#ID6]] = !{%struct.ComposConst { i32 1, double 2.000000e+00, %struct.myConst { i32 13, float 0x4020666660000000 } }}
237261
; CHECK-DEF: ![[#ID7]] = !{%struct.ComposConst2 { i8 1, %struct.myConst { i32 52, float 0x40479999A0000000 }, double 2.000000e+00 }}
262+
; CHECK-DEF: ![[#ID_COMPOS3_DEFAULT]] = !{%struct.ComposConst3 zeroinitializer}
238263
;
239264
; CHECK-DEF: ![[#ID8]] = !{%struct.VectorConst { <2 x i32> <i32 1, i32 2> }}
240265
; CHECK-DEF: ![[#ID9]] = !{%struct.MArrayConst { [2 x i32] [i32 1, i32 2] }}
241266
;
242-
; CHECK-RT: ![[#ID4]] = !{!"_ZTS14name_generatorIL_Z10id_vectorEE", i32 [[#SCID10]], i32 0, i32 4,
243-
; CHECK-RT-SAME: i32 [[#SCID11]], i32 4, i32 4}
244-
; CHECK-RT: ![[#ID5]] = !{!"_ZTS14name_generatorIL_Z10id_marrayEE", i32 [[#SCID12]], i32 0, i32 4,
245-
; CHECK-RT-SAME: i32 [[#SCID13]], i32 4, i32 4}
267+
; CHECK-RT: ![[#ID4]] = !{!"_ZTS14name_generatorIL_Z10id_vectorEE", i32 [[#SCID14]], i32 0, i32 4,
268+
; CHECK-RT-SAME: i32 [[#SCID15]], i32 4, i32 4}
269+
; CHECK-RT: ![[#ID5]] = !{!"_ZTS14name_generatorIL_Z10id_marrayEE", i32 [[#SCID16]], i32 0, i32 4,
270+
; CHECK-RT-SAME: i32 [[#SCID17]], i32 4, i32 4}
246271

247272
; CHECK-PROPS: [SYCL/specialization constants]
248273
; CHECK-PROPS: _ZTS14name_generatorIL_Z9id_halfEE=2|

llvm/tools/sycl-post-link/SpecConstants.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ void collectCompositeElementsDefaultValuesRecursive(
314314
} else if (auto *StructTy = dyn_cast<StructType>(Ty)) {
315315
const StructLayout *SL = M.getDataLayout().getStructLayout(StructTy);
316316
for (size_t I = 0, E = StructTy->getNumElements(); I < E; ++I) {
317-
Constant *El = cast<Constant>(C->getOperand(I));
317+
Constant *El = nullptr;
318+
if (C->isZeroValue())
319+
El = Constant::getNullValue(StructTy->getElementType(I));
320+
else
321+
El = cast<Constant>(C->getOperand(I));
318322
// When handling elements of a structure, we do not use manually
319323
// calculated offsets (which are sum of sizes of all previously
320324
// encountered elements), but instead rely on data provided for us by

0 commit comments

Comments
 (0)