Skip to content

Commit 556ffe7

Browse files
authored
[Backport to 15] [SPIR-V 1.2] SPIRVReader: Add AlignmentId support (#2869) (#2886)
If there is no `OpDecorate .. Alignment` in the input, see if there is an `OpDecorateId .. AlignmentId` and take the alignment from the referenced constant instead. Once `AlignmentId` has been translated to LLVM IR, it is indistinguishable from an (non-ID) `Alignment` decoration. (cherry picked from commit da1731b)
1 parent 426abe8 commit 556ffe7

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,29 @@ Value *SPIRVToLLVM::mapFunction(SPIRVFunction *BF, Function *F) {
284284
return F;
285285
}
286286

287+
llvm::Optional<uint64_t> SPIRVToLLVM::transIdAsConstant(SPIRVId Id) {
288+
auto *V = BM->get<SPIRVValue>(Id);
289+
const auto *ConstValue =
290+
dyn_cast<ConstantInt>(transValue(V, nullptr, nullptr));
291+
if (!ConstValue)
292+
return {};
293+
return ConstValue->getZExtValue();
294+
}
295+
296+
llvm::Optional<uint64_t> SPIRVToLLVM::getAlignment(SPIRVValue *V) {
297+
SPIRVWord AlignmentBytes = 0;
298+
if (V->hasAlignment(&AlignmentBytes)) {
299+
return AlignmentBytes;
300+
}
301+
302+
// If there was no Alignment decoration, look for AlignmentId instead.
303+
SPIRVId AlignId;
304+
if (V->hasDecorateId(DecorationAlignmentId, 0, &AlignId)) {
305+
return transIdAsConstant(AlignId);
306+
}
307+
return {};
308+
}
309+
287310
Type *SPIRVToLLVM::transFPType(SPIRVType *T) {
288311
switch (T->getFloatBitWidth()) {
289312
case 16:
@@ -3114,9 +3137,9 @@ void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) {
31143137
SPIRVWord MaxOffset = 0;
31153138
if (BA->hasDecorate(DecorationMaxByteOffset, 0, &MaxOffset))
31163139
Builder.addDereferenceableAttr(MaxOffset);
3117-
SPIRVWord AlignmentBytes = 0;
3118-
if (BA->hasAlignment(&AlignmentBytes))
3119-
Builder.addAlignmentAttr(AlignmentBytes);
3140+
if (auto Alignment = getAlignment(BA)) {
3141+
Builder.addAlignmentAttr(*Alignment);
3142+
}
31203143
I->addAttrs(Builder);
31213144
}
31223145
BF->foreachReturnValueAttr([&](SPIRVFuncParamAttrKind Kind) {
@@ -4757,16 +4780,14 @@ bool SPIRVToLLVM::transFPGAFunctionMetadata(SPIRVFunction *BF, Function *F) {
47574780
}
47584781

47594782
bool SPIRVToLLVM::transAlign(SPIRVValue *BV, Value *V) {
4760-
if (auto AL = dyn_cast<AllocaInst>(V)) {
4761-
SPIRVWord Align = 0;
4762-
if (BV->hasAlignment(&Align))
4763-
AL->setAlignment(llvm::Align(Align));
4783+
if (auto *AL = dyn_cast<AllocaInst>(V)) {
4784+
if (auto Align = getAlignment(BV))
4785+
AL->setAlignment(llvm::Align(*Align));
47644786
return true;
47654787
}
4766-
if (auto GV = dyn_cast<GlobalVariable>(V)) {
4767-
SPIRVWord Align = 0;
4768-
if (BV->hasAlignment(&Align))
4769-
GV->setAlignment(MaybeAlign(Align));
4788+
if (auto *GV = dyn_cast<GlobalVariable>(V)) {
4789+
if (auto Align = getAlignment(BV))
4790+
GV->setAlignment(MaybeAlign(*Align));
47704791
return true;
47714792
}
47724793
return true;

lib/SPIRV/SPIRVReader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ class SPIRVToLLVM {
199199

200200
bool isDirectlyTranslatedToOCL(Op OpCode) const;
201201
MDString *transOCLKernelArgTypeName(SPIRVFunctionParameter *);
202+
203+
// Attempt to translate Id as a (specialization) constant.
204+
llvm::Optional<uint64_t> transIdAsConstant(SPIRVId Id);
205+
206+
// Return the value of an Alignment or AlignmentId decoration for V.
207+
llvm::Optional<uint64_t> getAlignment(SPIRVValue *V);
208+
202209
Value *mapFunction(SPIRVFunction *BF, Function *F);
203210
Value *getTranslatedValue(SPIRVValue *BV);
204211
IntrinsicInst *getLifetimeStartIntrinsic(Instruction *I);

test/AlignmentId.spvasm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; REQUIRES: spirv-as
2+
3+
; RUN: spirv-as %s --target-env spv1.2 -o %t.spv
4+
; RUN: spirv-val %t.spv
5+
; RUN: llvm-spirv -r -o %t.rev.bc %t.spv
6+
; RUN: llvm-dis %t.rev.bc -o - | FileCheck %s
7+
8+
OpCapability Addresses
9+
OpCapability Linkage
10+
OpCapability Kernel
11+
OpMemoryModel Physical64 OpenCL
12+
OpEntryPoint Kernel %fn "testAlignmentId"
13+
OpName %p "p"
14+
OpDecorate %x LinkageAttributes "x" Export
15+
OpDecorateId %x AlignmentId %al
16+
OpDecorateId %p AlignmentId %al_spec
17+
%void = OpTypeVoid
18+
%uint = OpTypeInt 32 0
19+
%ptr = OpTypePointer CrossWorkgroup %uint
20+
%fnTy = OpTypeFunction %void %ptr
21+
%al = OpConstant %uint 16
22+
%al_spec = OpSpecConstantOp %uint IAdd %al %al
23+
%uint_42 = OpConstant %uint 42
24+
; Verify alignment of variable.
25+
; CHECK: @x = addrspace(1) global i32 42, align 16
26+
%x = OpVariable %ptr CrossWorkgroup %uint_42
27+
28+
%fn = OpFunction %void None %fnTy
29+
; Verify alignment of function parameter.
30+
; CHECK: define spir_kernel void @testAlignmentId(i32 addrspace(1)* align 32 %p)
31+
%p = OpFunctionParameter %ptr
32+
%entry = OpLabel
33+
OpReturn
34+
OpFunctionEnd

0 commit comments

Comments
 (0)