Skip to content

Commit 6542fe7

Browse files
authored
[Backport to 17] [SPIR-V 1.2] SPIRVReader: Add AlignmentId support (#2869) (#2888)
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 6932dc0 commit 6542fe7

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
@@ -287,6 +287,29 @@ Value *SPIRVToLLVM::mapFunction(SPIRVFunction *BF, Function *F) {
287287
return F;
288288
}
289289

290+
std::optional<uint64_t> SPIRVToLLVM::transIdAsConstant(SPIRVId Id) {
291+
auto *V = BM->get<SPIRVValue>(Id);
292+
const auto *ConstValue =
293+
dyn_cast<ConstantInt>(transValue(V, nullptr, nullptr));
294+
if (!ConstValue)
295+
return {};
296+
return ConstValue->getZExtValue();
297+
}
298+
299+
std::optional<uint64_t> SPIRVToLLVM::getAlignment(SPIRVValue *V) {
300+
SPIRVWord AlignmentBytes = 0;
301+
if (V->hasAlignment(&AlignmentBytes)) {
302+
return AlignmentBytes;
303+
}
304+
305+
// If there was no Alignment decoration, look for AlignmentId instead.
306+
SPIRVId AlignId;
307+
if (V->hasDecorateId(DecorationAlignmentId, 0, &AlignId)) {
308+
return transIdAsConstant(AlignId);
309+
}
310+
return {};
311+
}
312+
290313
Type *SPIRVToLLVM::transFPType(SPIRVType *T) {
291314
switch (T->getFloatBitWidth()) {
292315
case 16:
@@ -3086,9 +3109,9 @@ void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) {
30863109
SPIRVWord MaxOffset = 0;
30873110
if (BA->hasDecorate(DecorationMaxByteOffset, 0, &MaxOffset))
30883111
Builder.addDereferenceableAttr(MaxOffset);
3089-
SPIRVWord AlignmentBytes = 0;
3090-
if (BA->hasAlignment(&AlignmentBytes))
3091-
Builder.addAlignmentAttr(AlignmentBytes);
3112+
if (auto Alignment = getAlignment(BA)) {
3113+
Builder.addAlignmentAttr(*Alignment);
3114+
}
30923115
I->addAttrs(Builder);
30933116
}
30943117
BF->foreachReturnValueAttr([&](SPIRVFuncParamAttrKind Kind) {
@@ -4803,16 +4826,14 @@ bool SPIRVToLLVM::transFPGAFunctionMetadata(SPIRVFunction *BF, Function *F) {
48034826
}
48044827

48054828
bool SPIRVToLLVM::transAlign(SPIRVValue *BV, Value *V) {
4806-
if (auto AL = dyn_cast<AllocaInst>(V)) {
4807-
SPIRVWord Align = 0;
4808-
if (BV->hasAlignment(&Align))
4809-
AL->setAlignment(llvm::Align(Align));
4829+
if (auto *AL = dyn_cast<AllocaInst>(V)) {
4830+
if (auto Align = getAlignment(BV))
4831+
AL->setAlignment(llvm::Align(*Align));
48104832
return true;
48114833
}
4812-
if (auto GV = dyn_cast<GlobalVariable>(V)) {
4813-
SPIRVWord Align = 0;
4814-
if (BV->hasAlignment(&Align))
4815-
GV->setAlignment(MaybeAlign(Align));
4834+
if (auto *GV = dyn_cast<GlobalVariable>(V)) {
4835+
if (auto Align = getAlignment(BV))
4836+
GV->setAlignment(MaybeAlign(*Align));
48164837
return true;
48174838
}
48184839
return true;

lib/SPIRV/SPIRVReader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ class SPIRVToLLVM : private BuiltinCallHelper {
214214

215215
bool isDirectlyTranslatedToOCL(Op OpCode) const;
216216
MDString *transOCLKernelArgTypeName(SPIRVFunctionParameter *);
217+
218+
// Attempt to translate Id as a (specialization) constant.
219+
std::optional<uint64_t> transIdAsConstant(SPIRVId Id);
220+
221+
// Return the value of an Alignment or AlignmentId decoration for V.
222+
std::optional<uint64_t> getAlignment(SPIRVValue *V);
223+
217224
Value *mapFunction(SPIRVFunction *BF, Function *F);
218225
Value *getTranslatedValue(SPIRVValue *BV);
219226
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(ptr addrspace(1) align 32 %p)
31+
%p = OpFunctionParameter %ptr
32+
%entry = OpLabel
33+
OpReturn
34+
OpFunctionEnd

0 commit comments

Comments
 (0)