Skip to content

Commit

Permalink
[sycl-post-link] Several fixes for 'default' spec constants (#4649)
Browse files Browse the repository at this point in the history
Fixed collection of default values of `half` scalar spec constants in
`SpecConstantsPass` as well as handling of vectors and arrays there.
  • Loading branch information
AlexeySachkov authored Oct 5, 2021
1 parent 60ce91f commit 524f6d2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
23 changes: 19 additions & 4 deletions llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
; RUN: sycl-post-link --ir-output-only -spec-const=default %s -S -o - | \
; RUN: FileCheck %s -check-prefixes=CHECK,CHECK-DEF
; RUN: sycl-post-link --ir-output-only -spec-const=rt %s -S -o - | \
; RUN: FileCheck %s -check-prefixes=CHECK,CHECK-RT
; RUN: sycl-post-link -spec-const=default %s -S -o %t.table
; RUN: FileCheck %s -check-prefixes=CHECK,CHECK-DEF < %t_0.ll
; RUN: FileCheck %s --check-prefixes=CHECK-PROPS,CHECK-PROPS-DEF < %t_0.prop
; RUN: sycl-post-link -spec-const=rt %s -S -o %t.table
; RUN: FileCheck %s -check-prefixes=CHECK,CHECK-RT < %t_0.ll
; RUN: FileCheck %s --check-prefixes=CHECK-PROPS < %t_0.prop

; This test checks that the post link tool is able to correctly transform
; SYCL 2020 specialization constant intrinsics for different types in a device
Expand Down Expand Up @@ -241,3 +243,16 @@ attributes #3 = { nounwind }
; CHECK-RT-SAME: i32 [[#SCID11]], i32 4, i32 4}
; CHECK-RT: ![[#ID5]] = !{!"_ZTS14name_generatorIL_Z10id_marrayEE", i32 [[#SCID12]], i32 0, i32 4,
; CHECK-RT-SAME: i32 [[#SCID13]], i32 4, i32 4}

; CHECK-PROPS: [SYCL/specialization constants]
; CHECK-PROPS: _ZTS14name_generatorIL_Z9id_halfEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z6id_intEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z9id_composEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_compos2EE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_vectorEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marrayEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray2EE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray3EE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray4EE=2|
; CHECK-PROPS-DEF: [SYCL/specialization constants default values]
; CHECK-PROPS-DEF: all=2|
24 changes: 16 additions & 8 deletions llvm/tools/sycl-post-link/SpecConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,15 @@ void collectCompositeElementsDefaultValuesRecursive(
const Module &M, Constant *C, unsigned &Offset,
std::vector<char> &DefaultValues) {
Type *Ty = C->getType();
if (auto *ArrTy = dyn_cast<ArrayType>(Ty)) {
if (auto *DataSeqC = dyn_cast<ConstantDataSequential>(C)) {
// This code is generic for both vectors and arrays of scalars
for (size_t I = 0; I < DataSeqC->getNumElements(); ++I) {
Constant *El = cast<Constant>(DataSeqC->getElementAsConstant(I));
collectCompositeElementsDefaultValuesRecursive(M, El, Offset,
DefaultValues);
}
} else if (auto *ArrTy = dyn_cast<ArrayType>(Ty)) {
// This branch handles arrays of composite types (structs, arrays, etc.)
for (size_t I = 0; I < ArrTy->getNumElements(); ++I) {
Constant *El = cast<Constant>(C->getOperand(I));
collectCompositeElementsDefaultValuesRecursive(M, El, Offset,
Expand All @@ -324,12 +332,6 @@ void collectCompositeElementsDefaultValuesRecursive(
// Update "global" offset according to the total size of a handled struct
// type.
Offset += SL->getSizeInBytes();
} else if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
for (size_t I = 0; I < VecTy->getNumElements(); ++I) {
Constant *El = cast<Constant>(C->getOperand(I));
collectCompositeElementsDefaultValuesRecursive(M, El, Offset,
DefaultValues);
}
} else { // Assume that we encountered some scalar element
int NumBytes = Ty->getScalarSizeInBits() / CHAR_BIT +
(Ty->getScalarSizeInBits() % 8 != 0);
Expand All @@ -341,7 +343,13 @@ void collectCompositeElementsDefaultValuesRecursive(
} else if (auto FPConst = dyn_cast<ConstantFP>(C)) {
auto Val = FPConst->getValue();

if (NumBytes == 4) {
if (NumBytes == 2) {
auto IVal = Val.bitcastToAPInt();
assert(IVal.getBitWidth() == 16);
auto Storage = static_cast<uint16_t>(IVal.getZExtValue());
std::copy_n(reinterpret_cast<char *>(&Storage), NumBytes,
std::back_inserter(DefaultValues));
} else if (NumBytes == 4) {
float v = Val.convertToFloat();
std::copy_n(reinterpret_cast<char *>(&v), NumBytes,
std::back_inserter(DefaultValues));
Expand Down

0 comments on commit 524f6d2

Please sign in to comment.