Skip to content

Commit 4c9ce32

Browse files
committed
Apply comments
1 parent 09fb54a commit 4c9ce32

File tree

4 files changed

+52
-45
lines changed

4 files changed

+52
-45
lines changed

llvm/test/tools/sycl-post-link/composite-spec-constant-default-value.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
; RUN: sycl-post-link -spec-const=default --ir-output-only %s -S -o - \
22
; RUN: | FileCheck %s --implicit-check-not __sycl_getCompositeSpecConstantValue
33
;
4+
; This test checks that composite specialization constants can be correctly
5+
; initialized by sycl-post-link tool for AOT use-case (default initialization
6+
; should be used according to the type of constant)
7+
;
48
; TODO: consider adding a test case with vector type: the pass itself already
59
; supports this, but at the moment, sycl::vec type is not a POD type, which
610
; means we can't have it within a spec constant, i.e. we can't generate LLVM IR

llvm/test/tools/sycl-post-link/composite-spec-constant.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ attributes #4 = { convergent }
8383
!0 = !{i32 1, !"wchar_size", i32 4}
8484
!1 = !{i32 1, i32 2}
8585
!2 = !{i32 4, i32 100000}
86-
!3 = !{!"clang version 12.0.0 (/data/github.com/intel/llvm/clang 56ee5b054b5a1f2f703722fc414fcb05af18b40a)"}
86+
!3 = !{!"clang version 12.0.0 "}
8787
!4 = !{i32 -1, i32 -1, i32 -1, i32 -1}
8888
!5 = !{i64 0, i64 16, !6, i64 16, i64 8, !6}
8989
!6 = !{!7, !7, i64 0}

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

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ StringRef getStringLiteralArg(const CallInst *CI, unsigned ArgNo,
109109
return Res;
110110
}
111111

112-
// TODO support spec constant types other than integer or
113-
// floating-point.
114112
Value *getDefaultCPPValue(Type *T) {
115113
if (T->isIntegerTy())
116114
return Constant::getIntegerValue(T, APInt(T->getScalarSizeInBits(), 0));
@@ -167,16 +165,13 @@ std::string manglePrimitiveType(const Type *T) {
167165
// llvm-spirv doesn't care about the mangling and the only intent here is to
168166
// make sure that we won't encounter redefinition error when we proceed two
169167
// spec constants with different types.
170-
if (T->isStructTy()) {
168+
if (T->isStructTy())
171169
return T->getStructName().str();
172-
}
173-
if (T->isArrayTy()) {
170+
if (T->isArrayTy())
174171
return "A" + manglePrimitiveType(T->getArrayElementType());
175-
}
176-
if (auto *VecTy = dyn_cast<FixedVectorType>(T)) {
172+
if (auto *VecTy = dyn_cast<FixedVectorType>(T))
177173
return "Dv" + std::to_string(VecTy->getNumElements()) + "_" +
178174
manglePrimitiveType(VecTy->getElementType());
179-
}
180175
llvm_unreachable("unsupported spec const type");
181176
return "";
182177
}
@@ -196,10 +191,9 @@ void setSpecConstSymIDMetadata(Instruction *I, StringRef SymID,
196191
LLVMContext &Ctx = I->getContext();
197192
SmallVector<Metadata *, 4> MDOperands;
198193
MDOperands.push_back(MDString::get(Ctx, SymID));
199-
for (unsigned ID : IntIDs) {
194+
for (unsigned ID : IntIDs)
200195
MDOperands.push_back(
201196
ConstantAsMetadata::get(ConstantInt::get(Ctx, APInt(32, ID))));
202-
}
203197
MDNode *Entry = MDNode::get(Ctx, MDOperands);
204198
I->setMetadata(SPEC_CONST_SYM_ID_MD_STRING, Entry);
205199
}
@@ -218,7 +212,7 @@ getScalarSpecConstMetadata(const Instruction *I) {
218212

219213
void collectCompositeElementsInfoRecursive(
220214
const Type *Ty, unsigned &Index, unsigned &Offset,
221-
std::vector<CompositeSpecConstDescriptor> &Result) {
215+
std::vector<CompositeSpecConstElementDescriptor> &Result) {
222216
if (auto *ArrTy = dyn_cast<ArrayType>(Ty)) {
223217
for (size_t I = 0; I < ArrTy->getNumElements(); ++I) {
224218
// TODO: this is a spot for potential optimization: for arrays we could
@@ -233,14 +227,14 @@ void collectCompositeElementsInfoRecursive(
233227
}
234228
} else if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
235229
for (size_t I = 0; I < VecTy->getNumElements(); ++I) {
236-
// TODO: this is a spot for potential optimization: for arrays we could
230+
// TODO: this is a spot for potential optimization: for vectors we could
237231
// just make a single recursive call here and use it to populate Result
238232
// in a loop.
239233
collectCompositeElementsInfoRecursive(VecTy->getElementType(), Index,
240234
Offset, Result);
241235
}
242236
} else { // Assume that we encountered some scalar element
243-
CompositeSpecConstDescriptor Desc;
237+
CompositeSpecConstElementDescriptor Desc;
244238
Desc.ID = 0; // To be filled later
245239
Desc.Offset = Offset;
246240
Desc.Size = Ty->getPrimitiveSizeInBits() / 8;
@@ -249,14 +243,16 @@ void collectCompositeElementsInfoRecursive(
249243
}
250244
}
251245

252-
std::pair<StringRef, std::vector<CompositeSpecConstDescriptor>>
246+
std::pair<StringRef, std::vector<CompositeSpecConstElementDescriptor>>
253247
getCompositeSpecConstMetadata(const Instruction *I) {
254248
const MDNode *N = I->getMetadata(SPEC_CONST_SYM_ID_MD_STRING);
255249
if (!N)
256-
return std::make_pair("", std::vector<CompositeSpecConstDescriptor>{});
250+
return std::make_pair("",
251+
std::vector<CompositeSpecConstElementDescriptor>{});
257252
const auto *MDSym = cast<MDString>(N->getOperand(0));
258253

259-
std::vector<CompositeSpecConstDescriptor> Result(N->getNumOperands() - 1);
254+
std::vector<CompositeSpecConstElementDescriptor> Result(N->getNumOperands() -
255+
1);
260256
unsigned Index = 0, Offset = 0;
261257
collectCompositeElementsInfoRecursive(I->getType(), Index, Offset, Result);
262258

@@ -269,6 +265,21 @@ getCompositeSpecConstMetadata(const Instruction *I) {
269265
return std::make_pair(MDSym->getString(), Result);
270266
}
271267

268+
Instruction *emitCall(Type *RetTy, StringRef BaseFunctionName,
269+
ArrayRef<Value *> Args, Instruction *InsertBefore) {
270+
SmallVector<Type *, 8> ArgTys(Args.size());
271+
for (unsigned I = 0; I < Args.size(); ++I) {
272+
ArgTys[I] = Args[I]->getType();
273+
}
274+
auto *FT = FunctionType::get(RetTy, ArgTys, false /*isVarArg*/);
275+
std::string FunctionName = mangleFuncItanium(BaseFunctionName, FT);
276+
Module *M = InsertBefore->getFunction()->getParent();
277+
FunctionCallee FC = M->getOrInsertFunction(FunctionName, FT);
278+
assert(FC.getCallee() && "SPIRV intrinsic creation failed");
279+
auto *Call = CallInst::Create(FT, FC.getCallee(), Args, "", InsertBefore);
280+
return Call;
281+
}
282+
272283
Instruction *emitSpecConstant(unsigned NumericID, Type *Ty,
273284
Instruction *InsertBefore) {
274285
Function *F = InsertBefore->getFunction();
@@ -279,37 +290,17 @@ Instruction *emitSpecConstant(unsigned NumericID, Type *Ty,
279290
Value *Def = getDefaultCPPValue(Ty);
280291
// ... Now replace the call with SPIRV intrinsic version.
281292
Value *Args[] = {ID, Def};
282-
constexpr size_t NArgs = sizeof(Args) / sizeof(Args[0]);
283-
Type *ArgTys[NArgs] = {nullptr};
284-
for (unsigned int I = 0; I < NArgs; ++I)
285-
ArgTys[I] = Args[I]->getType();
286-
FunctionType *FT = FunctionType::get(Ty, ArgTys, false /*isVarArg*/);
287-
Module *M = F->getParent();
288-
std::string SPIRVName = mangleFuncItanium(SPIRV_GET_SPEC_CONST_VAL, FT);
289-
FunctionCallee FC = M->getOrInsertFunction(SPIRVName, FT);
290-
assert(FC.getCallee() && "SPIRV intrinsic creation failed");
291-
CallInst *SpecConstant =
292-
CallInst::Create(FT, FC.getCallee(), Args, "", InsertBefore);
293-
return SpecConstant;
293+
return emitCall(Ty, SPIRV_GET_SPEC_CONST_VAL, Args, InsertBefore);
294294
}
295295

296296
Instruction *emitSpecConstantComposite(Type *Ty,
297297
ArrayRef<Instruction *> Elements,
298298
Instruction *InsertBefore) {
299-
SmallVector<Type *, 8> ArgTys(Elements.size());
300299
SmallVector<Value *, 8> Args(Elements.size());
301300
for (unsigned I = 0; I < Elements.size(); ++I) {
302-
ArgTys[I] = Elements[I]->getType();
303301
Args[I] = cast<Value>(Elements[I]);
304302
}
305-
auto *FT = FunctionType::get(Ty, ArgTys, false /*isVarArg*/);
306-
Module *M = InsertBefore->getFunction()->getParent();
307-
std::string SPIRVName = mangleFuncItanium(SPIRV_GET_SPEC_CONST_COMPOSITE, FT);
308-
FunctionCallee FC = M->getOrInsertFunction(SPIRVName, FT);
309-
assert(FC.getCallee() && "SPIRV intrinsic creation failed");
310-
CallInst *SpecConstant =
311-
CallInst::Create(FT, FC.getCallee(), Args, "", InsertBefore);
312-
return SpecConstant;
303+
return emitCall(Ty, SPIRV_GET_SPEC_CONST_COMPOSITE, Args, InsertBefore);
313304
}
314305

315306
Instruction *
@@ -416,13 +407,14 @@ PreservedAnalyses SpecConstantsPass::run(Module &M,
416407
NextID += GeneratedIDs.size();
417408
}
418409

419-
if (IsComposite)
410+
if (IsComposite) {
420411
// __sycl_getCompositeSpecConstant returns through argument, so, the
421412
// only thing we need to do here is to store into a memory pointed by
422413
// that argument
423414
new StoreInst(SPIRVCall, CI->getArgOperand(0), CI);
424-
else
415+
} else {
425416
CI->replaceAllUsesWith(SPIRVCall);
417+
}
426418

427419
// Mark the instruction with <symbolic_id, int_ids...> list for later
428420
// recollection by collectSpecConstantMetadata method.
@@ -442,13 +434,14 @@ PreservedAnalyses SpecConstantsPass::run(Module &M,
442434
// 2a. Spec constant must be resolved at compile time - just replace
443435
// the intrinsic with default C++ value for the spec constant type.
444436
Value *Default = getDefaultCPPValue(SCTy);
445-
if (IsComposite)
437+
if (IsComposite) {
446438
// __sycl_getCompositeSpecConstant returns through argument, so, the
447439
// only thing we need to do here is to store into a memory pointed by
448440
// that argument
449441
new StoreInst(Default, CI->getArgOperand(0), CI);
450-
else
442+
} else {
451443
CI->replaceAllUsesWith(Default);
444+
}
452445
}
453446

454447
for (auto *I : DelInsts) {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@
2525
using namespace llvm;
2626

2727
using ScalarSpecIDMapTy = std::map<StringRef, unsigned>;
28-
struct CompositeSpecConstDescriptor {
28+
// Represents an element of a composite speciailization constant - at SYCL RT
29+
// level composite specialization constants are being represented as a single
30+
// byte-array, while at SPIR-V level they are represented by a number of scalar
31+
// specialization constants.
32+
struct CompositeSpecConstElementDescriptor {
33+
// Encodes ID of a scalar specialization constants which is a leaf of some
34+
// composite specialization constant.
2935
unsigned ID;
36+
// Encodes offset from the beginning of composite, where scalar resides, i.e.
37+
// location of the scalar value within a byte-array containing the whole
38+
// composite specialization constant.
3039
unsigned Offset;
40+
// Encodes size of scalar specialization constant.
3141
unsigned Size;
3242
};
3343
using CompositeSpecIDMapTy =
34-
std::map<StringRef, std::vector<CompositeSpecConstDescriptor>>;
44+
std::map<StringRef, std::vector<CompositeSpecConstElementDescriptor>>;
3545

3646
class SpecConstantsPass : public PassInfoMixin<SpecConstantsPass> {
3747
public:

0 commit comments

Comments
 (0)