Skip to content

Commit 22eb614

Browse files
authored
[Backport to 19] [SPIR-V 1.2] SPIRVReader: Add AlignmentId support (#2869) (#2890)
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 309e9c5 commit 22eb614

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

292+
std::optional<uint64_t> SPIRVToLLVM::transIdAsConstant(SPIRVId Id) {
293+
auto *V = BM->get<SPIRVValue>(Id);
294+
const auto *ConstValue =
295+
dyn_cast<ConstantInt>(transValue(V, nullptr, nullptr));
296+
if (!ConstValue)
297+
return {};
298+
return ConstValue->getZExtValue();
299+
}
300+
301+
std::optional<uint64_t> SPIRVToLLVM::getAlignment(SPIRVValue *V) {
302+
SPIRVWord AlignmentBytes = 0;
303+
if (V->hasAlignment(&AlignmentBytes)) {
304+
return AlignmentBytes;
305+
}
306+
307+
// If there was no Alignment decoration, look for AlignmentId instead.
308+
SPIRVId AlignId;
309+
if (V->hasDecorateId(DecorationAlignmentId, 0, &AlignId)) {
310+
return transIdAsConstant(AlignId);
311+
}
312+
return {};
313+
}
314+
292315
Type *SPIRVToLLVM::transFPType(SPIRVType *T) {
293316
switch (T->getFloatBitWidth()) {
294317
case 16:
@@ -3104,9 +3127,9 @@ void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) {
31043127
SPIRVWord MaxOffset = 0;
31053128
if (BA->hasDecorate(DecorationMaxByteOffset, 0, &MaxOffset))
31063129
Builder.addDereferenceableAttr(MaxOffset);
3107-
SPIRVWord AlignmentBytes = 0;
3108-
if (BA->hasAlignment(&AlignmentBytes))
3109-
Builder.addAlignmentAttr(AlignmentBytes);
3130+
if (auto Alignment = getAlignment(BA)) {
3131+
Builder.addAlignmentAttr(*Alignment);
3132+
}
31103133
I->addAttrs(Builder);
31113134
}
31123135
BF->foreachReturnValueAttr([&](SPIRVFuncParamAttrKind Kind) {
@@ -4865,15 +4888,13 @@ bool SPIRVToLLVM::transFPGAFunctionMetadata(SPIRVFunction *BF, Function *F) {
48654888

48664889
bool SPIRVToLLVM::transAlign(SPIRVValue *BV, Value *V) {
48674890
if (auto *AL = dyn_cast<AllocaInst>(V)) {
4868-
SPIRVWord Align = 0;
4869-
if (BV->hasAlignment(&Align))
4870-
AL->setAlignment(llvm::Align(Align));
4891+
if (auto Align = getAlignment(BV))
4892+
AL->setAlignment(llvm::Align(*Align));
48714893
return true;
48724894
}
48734895
if (auto *GV = dyn_cast<GlobalVariable>(V)) {
4874-
SPIRVWord Align = 0;
4875-
if (BV->hasAlignment(&Align))
4876-
GV->setAlignment(MaybeAlign(Align));
4896+
if (auto Align = getAlignment(BV))
4897+
GV->setAlignment(MaybeAlign(*Align));
48774898
return true;
48784899
}
48794900
return true;

lib/SPIRV/SPIRVReader.h

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

216216
bool isDirectlyTranslatedToOCL(Op OpCode) const;
217217
MDString *transOCLKernelArgTypeName(SPIRVFunctionParameter *);
218+
219+
// Attempt to translate Id as a (specialization) constant.
220+
std::optional<uint64_t> transIdAsConstant(SPIRVId Id);
221+
222+
// Return the value of an Alignment or AlignmentId decoration for V.
223+
std::optional<uint64_t> getAlignment(SPIRVValue *V);
224+
218225
Value *mapFunction(SPIRVFunction *BF, Function *F);
219226
Value *getTranslatedValue(SPIRVValue *BV);
220227
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)