Skip to content

Commit 13efb6a

Browse files
authored
[SPIR-V] Allow function parameters to be decorated with inline spir-v (#8333)
Fixes #8103
1 parent 31bbeb2 commit 13efb6a

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,8 @@ SpirvInstruction *DeclResultIdMapper::getDeclEvalInfo(const ValueDecl *decl,
10561056

10571057
SpirvFunctionParameter *
10581058
DeclResultIdMapper::createFnParam(const ParmVarDecl *param,
1059-
uint32_t dbgArgNumber) {
1059+
uint32_t dbgArgNumber,
1060+
bool decorateIntrinsicAttrs) {
10601061
const auto type = getTypeOrFnRetType(param);
10611062
const auto loc = param->getLocation();
10621063
const auto range = param->getSourceRange();
@@ -1073,6 +1074,9 @@ DeclResultIdMapper::createFnParam(const ParmVarDecl *param,
10731074
if (isTextureBuffer(type))
10741075
fnParamInstr->setLayoutRule(spirvOptions.tBufferLayoutRule);
10751076

1077+
if (decorateIntrinsicAttrs && param->hasAttrs())
1078+
decorateWithIntrinsicAttrs(param, fnParamInstr);
1079+
10761080
assert(astDecls[param].instr == nullptr);
10771081
registerVariableForDecl(param, fnParamInstr);
10781082

@@ -5028,18 +5032,17 @@ bool DeclResultIdMapper::tryToCreateConstantVar(const ValueDecl *decl) {
50285032
}
50295033

50305034
void DeclResultIdMapper::decorateWithIntrinsicAttrs(
5031-
const NamedDecl *decl, SpirvVariable *varInst,
5035+
const NamedDecl *decl, SpirvInstruction *targetInst,
50325036
llvm::function_ref<void(VKDecorateExtAttr *)> extraFunctionForDecoAttr) {
50335037
if (!decl->hasAttrs())
50345038
return;
50355039

5036-
// TODO: Handle member field in a struct and function parameter.
50375040
for (auto &attr : decl->getAttrs()) {
50385041
if (auto decoAttr = dyn_cast<VKDecorateExtAttr>(attr)) {
50395042
spvBuilder.decorateWithLiterals(
5040-
varInst, decoAttr->getDecorate(),
5043+
targetInst, decoAttr->getDecorate(),
50415044
{decoAttr->literals_begin(), decoAttr->literals_end()},
5042-
varInst->getSourceLocation());
5045+
targetInst->getSourceLocation());
50435046
extraFunctionForDecoAttr(decoAttr);
50445047
continue;
50455048
}
@@ -5048,15 +5051,15 @@ void DeclResultIdMapper::decorateWithIntrinsicAttrs(
50485051
for (Expr *arg : decoAttr->arguments()) {
50495052
args.push_back(theEmitter.doExpr(arg));
50505053
}
5051-
spvBuilder.decorateWithIds(varInst, decoAttr->getDecorate(), args,
5052-
varInst->getSourceLocation());
5054+
spvBuilder.decorateWithIds(targetInst, decoAttr->getDecorate(), args,
5055+
targetInst->getSourceLocation());
50535056
continue;
50545057
}
50555058
if (auto decoAttr = dyn_cast<VKDecorateStringExtAttr>(attr)) {
50565059
llvm::SmallVector<llvm::StringRef, 2> args(decoAttr->arguments_begin(),
50575060
decoAttr->arguments_end());
5058-
spvBuilder.decorateWithStrings(varInst, decoAttr->getDecorate(), args,
5059-
varInst->getSourceLocation());
5061+
spvBuilder.decorateWithStrings(targetInst, decoAttr->getDecorate(), args,
5062+
targetInst->getSourceLocation());
50605063
continue;
50615064
}
50625065
}

tools/clang/lib/SPIRV/DeclResultIdMapper.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ class DeclResultIdMapper {
271271
/// parameter must have "1", not "0", which is what Clang generates for
272272
/// LLVM debug metadata.
273273
SpirvFunctionParameter *createFnParam(const ParmVarDecl *param,
274-
uint32_t dbgArgNumber = 0);
274+
uint32_t dbgArgNumber = 0,
275+
bool decorateIntrinsicAttrs = true);
275276

276277
/// \brief Creates the counter variable associated with the given param.
277278
/// This is meant to be used for forward-declared functions and this objects
@@ -577,7 +578,7 @@ class DeclResultIdMapper {
577578
/// Decorate with spirv intrinsic attributes with lamda function variable
578579
/// check
579580
void decorateWithIntrinsicAttrs(
580-
const NamedDecl *decl, SpirvVariable *varInst,
581+
const NamedDecl *decl, SpirvInstruction *targetInst,
581582
llvm::function_ref<void(VKDecorateExtAttr *)> extraFunctionForDecoAttr =
582583
[](VKDecorateExtAttr *) {});
583584

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,8 +1704,8 @@ void SpirvEmitter::doFunctionDecl(const FunctionDecl *decl) {
17041704
for (uint32_t i = 0; i < decl->getNumParams(); ++i) {
17051705
const ParmVarDecl *paramDecl = decl->getParamDecl(i);
17061706
QualType paramType = paramDecl->getType();
1707-
auto *param =
1708-
declIdMapper.createFnParam(paramDecl, i + 1 + isNonStaticMemberFn);
1707+
auto *param = declIdMapper.createFnParam(
1708+
paramDecl, i + 1 + isNonStaticMemberFn, !isEntry);
17091709
if (isEntry) {
17101710
handleNodePayloadArrayType(paramDecl, param);
17111711
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %dxc -T ps_6_0 -E main -fcgl -Vd -spirv -fcgl %s -spirv | FileCheck %s
2+
3+
void SimpleAdd([[vk::ext_decorate(/* spv::DecorationAliased */ 20)]] inout float a,
4+
[[vk::ext_decorate(/* spv::DecorationAliased */ 20)]] inout float b) {
5+
a += 1.0;
6+
b += 2.0;
7+
}
8+
9+
// CHECK: OpDecorate %x Aliased
10+
// CHECK: OpDecorate %a Aliased
11+
// CHECK: OpDecorate %b Aliased
12+
// CHECK: %SimpleAdd = OpFunction %void None {{%[a-zA-Z0-9_]+}}
13+
// CHECK-NEXT: %a = OpFunctionParameter %_ptr_Function_float
14+
// CHECK-NEXT: %b = OpFunctionParameter %_ptr_Function_float
15+
16+
float4 main() : SV_Target {
17+
[[vk::ext_decorate(/*spv::DecorationAliased*/20)]] float x = 43.0;
18+
SimpleAdd(x, x);
19+
return float4(x, x, x, x);
20+
}

0 commit comments

Comments
 (0)