Skip to content

Commit 7f3e57f

Browse files
authored
[Backport to 18] [SPIR-V 1.2] SPIRVReader: Add AlignmentId support (#2869) (#2889)
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 ff99bd5 commit 7f3e57f

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 30 additions & 9 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:
@@ -3159,9 +3182,9 @@ void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) {
31593182
SPIRVWord MaxOffset = 0;
31603183
if (BA->hasDecorate(DecorationMaxByteOffset, 0, &MaxOffset))
31613184
Builder.addDereferenceableAttr(MaxOffset);
3162-
SPIRVWord AlignmentBytes = 0;
3163-
if (BA->hasAlignment(&AlignmentBytes))
3164-
Builder.addAlignmentAttr(AlignmentBytes);
3185+
if (auto Alignment = getAlignment(BA)) {
3186+
Builder.addAlignmentAttr(*Alignment);
3187+
}
31653188
I->addAttrs(Builder);
31663189
}
31673190
BF->foreachReturnValueAttr([&](SPIRVFuncParamAttrKind Kind) {
@@ -4894,15 +4917,13 @@ bool SPIRVToLLVM::transFPGAFunctionMetadata(SPIRVFunction *BF, Function *F) {
48944917

48954918
bool SPIRVToLLVM::transAlign(SPIRVValue *BV, Value *V) {
48964919
if (auto *AL = dyn_cast<AllocaInst>(V)) {
4897-
SPIRVWord Align = 0;
4898-
if (BV->hasAlignment(&Align))
4899-
AL->setAlignment(llvm::Align(Align));
4920+
if (auto Align = getAlignment(BV))
4921+
AL->setAlignment(llvm::Align(*Align));
49004922
return true;
49014923
}
49024924
if (auto *GV = dyn_cast<GlobalVariable>(V)) {
4903-
SPIRVWord Align = 0;
4904-
if (BV->hasAlignment(&Align))
4905-
GV->setAlignment(MaybeAlign(Align));
4925+
if (auto Align = getAlignment(BV))
4926+
GV->setAlignment(MaybeAlign(*Align));
49064927
return true;
49074928
}
49084929
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)