@@ -279,10 +279,7 @@ void collectCompositeElementsInfoRecursive(
279
279
SpecConstantDescriptor Desc;
280
280
Desc.ID = 0 ; // To be filled later
281
281
Desc.Offset = Offset;
282
- // We need to add an additional byte if the type size is not evenly
283
- // divisible by eight, which might be the case for i1, i.e. booleans
284
- Desc.Size = Ty->getPrimitiveSizeInBits () / 8 +
285
- (Ty->getPrimitiveSizeInBits () % 8 != 0 );
282
+ Desc.Size = M.getDataLayout ().getTypeStoreSize (Ty);
286
283
Result[Index++] = Desc;
287
284
Offset += Desc.Size ;
288
285
}
@@ -337,8 +334,7 @@ void collectCompositeElementsDefaultValuesRecursive(
337
334
// type.
338
335
Offset += SL->getSizeInBytes ();
339
336
} else { // Assume that we encountered some scalar element
340
- int NumBytes = Ty->getScalarSizeInBits () / CHAR_BIT +
341
- (Ty->getScalarSizeInBits () % 8 != 0 );
337
+ int NumBytes = M.getDataLayout ().getTypeStoreSize (Ty);
342
338
343
339
if (auto IntConst = dyn_cast<ConstantInt>(C)) {
344
340
auto Val = IntConst->getValue ().getZExtValue ();
@@ -403,12 +399,7 @@ MDNode *generateSpecConstantMetadata(const Module &M, StringRef SymbolicID,
403
399
MDOps.push_back (ConstantAsMetadata::get (
404
400
Constant::getIntegerValue (Int32Ty, APInt (32 , 0 ))));
405
401
406
- unsigned Size = 0 ;
407
- if (auto *StructTy = dyn_cast<StructType>(SCTy)) {
408
- const auto *SL = M.getDataLayout ().getStructLayout (StructTy);
409
- Size = SL->getSizeInBytes ();
410
- } else
411
- Size = SCTy->getScalarSizeInBits () / CHAR_BIT;
402
+ unsigned Size = M.getDataLayout ().getTypeStoreSize (SCTy);
412
403
413
404
MDOps.push_back (ConstantAsMetadata::get (
414
405
Constant::getIntegerValue (Int32Ty, APInt (32 , Size))));
@@ -614,9 +605,7 @@ PreservedAnalyses SpecConstantsPass::run(Module &M,
614
605
// to the intrinsic - this should always be possible, as only string
615
606
// literals are passed to it in the SYCL RT source code, and application
616
607
// code can't use this intrinsic directly.
617
- bool IsComposite =
618
- F.getName ().startswith (SYCL_GET_COMPOSITE_SPEC_CONST_VAL) ||
619
- F.getName ().startswith (SYCL_GET_COMPOSITE_2020_SPEC_CONST_VAL);
608
+
620
609
// SYCL 2020 specialization constants provide more functionality so they
621
610
// use separate intrinsic with additional arguments.
622
611
bool Is2020Intrinsic =
@@ -707,20 +696,7 @@ PreservedAnalyses SpecConstantsPass::run(Module &M,
707
696
bool IsNewSpecConstant = Ins.second ;
708
697
unsigned CurrentOffset = Ins.first ->second ;
709
698
if (IsNewSpecConstant) {
710
- unsigned Size = 0 ;
711
- if (IsComposite) {
712
- // When handling elements of a structure, we do not use manually
713
- // calculated offsets (which are sum of sizes of all previously
714
- // encountered elements), but instead rely on data provided for us
715
- // by DataLayout, because the structure can be unpacked, i.e.
716
- // padded in order to ensure particular alignment of its elements.
717
- // We rely on the fact that the StructLayout of spec constant RT
718
- // values is the same for the host and the device.
719
- const StructLayout *SL =
720
- M.getDataLayout ().getStructLayout (cast<StructType>(SCTy));
721
- Size = SL->getSizeInBytes ();
722
- } else
723
- Size = SCTy->getScalarSizeInBits () / CHAR_BIT;
699
+ unsigned Size = M.getDataLayout ().getTypeStoreSize (SCTy);
724
700
725
701
SCMetadata[SymID] = generateSpecConstantMetadata (
726
702
M, SymID, SCTy, NextID, /* is native spec constant */ false );
@@ -735,10 +711,19 @@ PreservedAnalyses SpecConstantsPass::run(Module &M,
735
711
Int8Ty, RTBuffer,
736
712
{ConstantInt::get (Int32Ty, CurrentOffset, false )}, " gep" , CI);
737
713
738
- BitCastInst *BitCast = new BitCastInst (
739
- GEP, PointerType::get (SCTy, GEP->getAddressSpace ()), " bc" , CI);
740
-
741
- Replacement = new LoadInst (SCTy, BitCast, " load" , CI);
714
+ Instruction *BitCast = nullptr ;
715
+ if (SCTy->isIntegerTy (1 )) // No bitcast to i1 before load
716
+ BitCast = GEP;
717
+ else
718
+ BitCast = new BitCastInst (
719
+ GEP, PointerType::get (SCTy, GEP->getAddressSpace ()), " bc" , CI);
720
+
721
+ // When we encounter i1 spec constant, we still load the whole byte
722
+ Replacement = new LoadInst (SCTy->isIntegerTy (1 ) ? Int8Ty : SCTy,
723
+ BitCast, " load" , CI);
724
+ if (SCTy->isIntegerTy (1 )) // trunc back to i1 if necessary
725
+ Replacement = CastInst::CreateIntegerCast (
726
+ Replacement, SCTy, /* IsSigned */ false , " tobool" , CI);
742
727
743
728
if (IsNewSpecConstant && DefaultValue)
744
729
DefaultsMetadata.push_back (
0 commit comments