Skip to content

Commit 0db0f98

Browse files
MrSidimsvmaksimo
andauthored
Fix SPV_INTEL_runtime_aligned implementation part 1 (#1796)
It was implemented via new decoration, which is not correct. Instead it should be Function Parameter Attribute decoration. In this commit starts fixing this in step-by-step manner. Signed-off-by: Sidorov, Dmitry <[email protected]> Co-authored-by: Maksimova, Viktoria <[email protected]>
1 parent 681f027 commit 0db0f98

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,19 @@ static void addRuntimeAlignedMetadata(
201201
LLVMContext *Context, SPIRVFunction *BF, llvm::Function *Fn,
202202
std::function<Metadata *(SPIRVFunctionParameter *)> ForeachFnArg) {
203203
std::vector<Metadata *> ValueVec;
204-
bool DecorationFound = false;
204+
bool RuntimeAlignedFound = false;
205+
[[maybe_unused]] llvm::Metadata *DefaultNode =
206+
ConstantAsMetadata::get(ConstantInt::get(Type::getInt1Ty(*Context), 0));
205207
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) {
206-
if (Arg->getType()->isTypePointer() &&
208+
if (Arg->hasAttr(FunctionParameterAttributeRuntimeAlignedINTEL) ||
207209
Arg->hasDecorate(internal::DecorationRuntimeAlignedINTEL)) {
208-
DecorationFound = true;
210+
RuntimeAlignedFound = true;
209211
ValueVec.push_back(ForeachFnArg(Arg));
210212
} else {
211-
llvm::Metadata *DefaultNode = ConstantAsMetadata::get(
212-
ConstantInt::get(Type::getInt1Ty(*Context), 0));
213213
ValueVec.push_back(DefaultNode);
214214
}
215215
});
216-
if (DecorationFound)
216+
if (RuntimeAlignedFound)
217217
Fn->setMetadata("kernel_arg_runtime_aligned",
218218
MDNode::get(*Context, ValueVec));
219219
}
@@ -2984,6 +2984,10 @@ void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) {
29842984
setName(&(*I), BA);
29852985
AttributeMask IllegalAttrs = AttributeFuncs::typeIncompatible(I->getType());
29862986
BA->foreachAttr([&](SPIRVFuncParamAttrKind Kind) {
2987+
// Skip this function parameter attribute as it will translated among
2988+
// OpenCL metadata
2989+
if (Kind == FunctionParameterAttributeRuntimeAlignedINTEL)
2990+
return;
29872991
Attribute::AttrKind LLVMKind = SPIRSPIRVFuncParamAttrMap::rmap(Kind);
29882992
if (IllegalAttrs.contains(LLVMKind))
29892993
return;
@@ -4466,13 +4470,8 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
44664470
});
44674471
// Generate metadata for kernel_arg_runtime_aligned
44684472
addRuntimeAlignedMetadata(Context, BF, F, [=](SPIRVFunctionParameter *Arg) {
4469-
auto Literals =
4470-
Arg->getDecorationLiterals(internal::DecorationRuntimeAlignedINTEL);
4471-
assert(Literals.size() == 1 &&
4472-
"RuntimeAlignedINTEL decoration shall have 1 ID literal");
4473-
44744473
return ConstantAsMetadata::get(
4475-
ConstantInt::get(Type::getInt1Ty(*Context), Literals[0]));
4474+
ConstantInt::get(Type::getInt1Ty(*Context), 1));
44764475
});
44774476
// Generate metadata for spirv.ParameterDecorations
44784477
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -951,12 +951,19 @@ SPIRVFunction *LLVMToSPIRVBase::transFunctionDecl(Function *F) {
951951
// Order of integer numbers in MD node follows the order of function
952952
// parameters on which we shall attach the appropriate decoration. Add
953953
// decoration only if MD value is 1.
954-
int LocID = 0;
954+
int IsRuntimeAligned = 0;
955955
if (!isa<MDString>(RuntimeAligned->getOperand(ArgNo)) &&
956956
!isa<MDNode>(RuntimeAligned->getOperand(ArgNo)))
957-
LocID = getMDOperandAsInt(RuntimeAligned, ArgNo);
958-
if (LocID == 1)
959-
BA->addDecorate(internal::DecorationRuntimeAlignedINTEL, LocID);
957+
IsRuntimeAligned = getMDOperandAsInt(RuntimeAligned, ArgNo);
958+
if (IsRuntimeAligned == 1) {
959+
// TODO: to replace non-conformant to the spec decoration generation
960+
// with:
961+
// BM->addExtension(ExtensionID::SPV_INTEL_runtime_aligned);
962+
// BM->addCapability(CapabilityRuntimeAlignedAttributeINTEL);
963+
// BA->addAttr(FunctionParameterAttributeRuntimeAlignedINTEL);
964+
BA->addDecorate(internal::DecorationRuntimeAlignedINTEL,
965+
IsRuntimeAligned);
966+
}
960967
}
961968
}
962969
if (Attrs.hasRetAttr(Attribute::ZExt))

lib/SPIRV/libSPIRV/SPIRVIsValidEnum.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ inline bool isValid(spv::FunctionParameterAttribute V) {
171171
case FunctionParameterAttributeNoCapture:
172172
case FunctionParameterAttributeNoWrite:
173173
case FunctionParameterAttributeNoReadWrite:
174+
case FunctionParameterAttributeRuntimeAlignedINTEL:
174175
return true;
175176
default:
176177
return false;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; RUN: llvm-spirv -spirv-text -r %s -o %t.bc
2+
; RUN: llvm-dis < %t.bc | FileCheck %s --check-prefix=CHECK-LLVM
3+
4+
; CHECK-LLVM: define spir_kernel void @test{{.*}} !kernel_arg_runtime_aligned ![[RTALIGN_MD:[0-9]+]] {{.*}}
5+
; CHECK-LLVM: ![[RTALIGN_MD]] = !{i1 true, i1 false, i1 true, i1 false, i1 false}
6+
7+
119734787 65536 393230 22 0
8+
2 Capability Addresses
9+
2 Capability Linkage
10+
2 Capability Kernel
11+
2 Capability Int8
12+
2 Capability RuntimeAlignedAttributeINTEL
13+
8 Extension "SPV_INTEL_runtime_aligned"
14+
5 ExtInstImport 1 "OpenCL.std"
15+
3 MemoryModel 2 2
16+
5 EntryPoint 6 14 "test"
17+
3 Source 0 0
18+
4 Name 7 "test"
19+
3 Name 8 "a"
20+
3 Name 9 "b"
21+
3 Name 10 "c"
22+
3 Name 11 "d"
23+
3 Name 12 "e"
24+
4 Name 13 "entry"
25+
3 Name 15 "a"
26+
3 Name 16 "b"
27+
3 Name 17 "c"
28+
3 Name 18 "d"
29+
3 Name 19 "e"
30+
31+
6 Decorate 7 LinkageAttributes "test" Export
32+
4 Decorate 8 FuncParamAttr 5940
33+
4 Decorate 10 FuncParamAttr 5940
34+
4 Decorate 15 FuncParamAttr 5940
35+
4 Decorate 17 FuncParamAttr 5940
36+
4 TypeInt 3 8 0
37+
4 TypeInt 5 32 0
38+
2 TypeVoid 2
39+
4 TypePointer 4 5 3
40+
8 TypeFunction 6 2 4 4 4 5 5
41+
42+
43+
44+
5 Function 2 7 0 6
45+
3 FunctionParameter 4 8
46+
3 FunctionParameter 4 9
47+
3 FunctionParameter 4 10
48+
3 FunctionParameter 5 11
49+
3 FunctionParameter 5 12
50+
51+
2 Label 13
52+
1 Return
53+
54+
1 FunctionEnd
55+
56+
5 Function 2 14 0 6
57+
3 FunctionParameter 4 15
58+
3 FunctionParameter 4 16
59+
3 FunctionParameter 4 17
60+
3 FunctionParameter 5 18
61+
3 FunctionParameter 5 19
62+
63+
2 Label 20
64+
9 FunctionCall 2 21 7 15 16 17 18 19
65+
1 Return
66+
67+
1 FunctionEnd
68+

0 commit comments

Comments
 (0)