From 524f6d28e27ae9c5ed32410e548006e2651ab814 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Tue, 5 Oct 2021 19:13:45 +0300 Subject: [PATCH] [sycl-post-link] Several fixes for 'default' spec constants (#4649) Fixed collection of default values of `half` scalar spec constants in `SpecConstantsPass` as well as handling of vectors and arrays there. --- .../spec-constants/SYCL-2020.ll | 23 ++++++++++++++---- llvm/tools/sycl-post-link/SpecConstants.cpp | 24 ++++++++++++------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll b/llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll index 88975f11777b8..362a682206c1e 100644 --- a/llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll +++ b/llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll @@ -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 @@ -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| diff --git a/llvm/tools/sycl-post-link/SpecConstants.cpp b/llvm/tools/sycl-post-link/SpecConstants.cpp index cb3275fe7749e..9a51af75f1481 100644 --- a/llvm/tools/sycl-post-link/SpecConstants.cpp +++ b/llvm/tools/sycl-post-link/SpecConstants.cpp @@ -297,7 +297,15 @@ void collectCompositeElementsDefaultValuesRecursive( const Module &M, Constant *C, unsigned &Offset, std::vector &DefaultValues) { Type *Ty = C->getType(); - if (auto *ArrTy = dyn_cast(Ty)) { + if (auto *DataSeqC = dyn_cast(C)) { + // This code is generic for both vectors and arrays of scalars + for (size_t I = 0; I < DataSeqC->getNumElements(); ++I) { + Constant *El = cast(DataSeqC->getElementAsConstant(I)); + collectCompositeElementsDefaultValuesRecursive(M, El, Offset, + DefaultValues); + } + } else if (auto *ArrTy = dyn_cast(Ty)) { + // This branch handles arrays of composite types (structs, arrays, etc.) for (size_t I = 0; I < ArrTy->getNumElements(); ++I) { Constant *El = cast(C->getOperand(I)); collectCompositeElementsDefaultValuesRecursive(M, El, Offset, @@ -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(Ty)) { - for (size_t I = 0; I < VecTy->getNumElements(); ++I) { - Constant *El = cast(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); @@ -341,7 +343,13 @@ void collectCompositeElementsDefaultValuesRecursive( } else if (auto FPConst = dyn_cast(C)) { auto Val = FPConst->getValue(); - if (NumBytes == 4) { + if (NumBytes == 2) { + auto IVal = Val.bitcastToAPInt(); + assert(IVal.getBitWidth() == 16); + auto Storage = static_cast(IVal.getZExtValue()); + std::copy_n(reinterpret_cast(&Storage), NumBytes, + std::back_inserter(DefaultValues)); + } else if (NumBytes == 4) { float v = Val.convertToFloat(); std::copy_n(reinterpret_cast(&v), NumBytes, std::back_inserter(DefaultValues));