Skip to content

Commit bff9650

Browse files
committed
Fix parameter decoration metadata overwrite in entry-point wrapper
In the entry-point wrapper pattern two SPIR-V functions map to the same LLVM function; only one carries decorated parameters. transOCLMetadata unconditionally set spirv.ParameterDecorations for both, so the undecorated function could overwrite the decorated one's metadata. Skip the write when the current function has no decorations and metadata is already present. AI-assisted: Claude Sonnet 4.6 (commercial SaaS)
1 parent 9c807c1 commit bff9650

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

lib/SPIRV/SPIRVReader.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4953,12 +4953,20 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
49534953
return ConstantAsMetadata::get(
49544954
ConstantInt::get(Type::getInt1Ty(*Context), 1));
49554955
});
4956-
// Generate metadata for spirv.ParameterDecorations
4957-
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
4958-
[=](SPIRVFunctionParameter *Arg) {
4959-
return transDecorationsToMetadataList(
4960-
Context, Arg->getDecorations());
4961-
});
4956+
// Generate metadata for spirv.ParameterDecorations.
4957+
// In the entry-point wrapper pattern two SPIR-V functions map to the same
4958+
// LLVM function; only one carries decorated parameters. Skip the
4959+
// undecorated function so it cannot overwrite the decorated one.
4960+
bool HasDecorations = false;
4961+
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) {
4962+
HasDecorations |= Arg->getNumDecorations() > 0;
4963+
});
4964+
if (HasDecorations || !F->hasMetadata(SPIRV_MD_PARAMETER_DECORATIONS))
4965+
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
4966+
[=](SPIRVFunctionParameter *Arg) {
4967+
return transDecorationsToMetadataList(
4968+
Context, Arg->getDecorations());
4969+
});
49624970
return true;
49634971
}
49644972

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; Verify that CacheControlLoadINTEL decoration on a FunctionParameter
2+
; survives reverse translation of the entry-point wrapper pattern.
3+
4+
; RUN: llvm-spirv -spirv-text --to-binary %s -o %t.spv
5+
; RUN: spirv-val %t.spv
6+
; RUN: llvm-spirv -spirv-text -r --spirv-target-env=SPV-IR %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-LLVM
7+
8+
; CHECK-LLVM: spirv.ParameterDecorations ![[#ParamDecs:]]
9+
; CHECK-LLVM: ![[#ParamDecs]] = !{![[#FirstParam:]]}
10+
; CHECK-LLVM: ![[#FirstParam]] = !{![[#DecoNode:]]}
11+
; CHECK-LLVM: ![[#DecoNode]] = !{i32 6442, i32 0, i32 1}
12+
13+
119734787 66560 393230 14 0
14+
2 Capability Addresses
15+
2 Capability Linkage
16+
2 Capability Kernel
17+
2 Capability CacheControlsINTEL
18+
8 Extension "SPV_INTEL_cache_controls"
19+
5 ExtInstImport 1 "OpenCL.std"
20+
3 MemoryModel 2 2
21+
5 EntryPoint 6 9 "test"
22+
3 Source 4 100000
23+
4 TypeInt 2 32 0
24+
2 TypeVoid 3
25+
4 TypePointer 5 5 2
26+
4 TypeFunction 6 3 5
27+
6 Decorate 4 LinkageAttributes "test" Export
28+
5 Decorate 7 CacheControlLoadINTEL 0 1
29+
30+
5 Function 3 4 0 6
31+
3 FunctionParameter 5 7
32+
2 Label 8
33+
4 Load 2 13 7
34+
1 Return
35+
1 FunctionEnd
36+
37+
5 Function 3 9 0 6
38+
3 FunctionParameter 5 10
39+
2 Label 11
40+
5 FunctionCall 3 12 4 10
41+
1 Return
42+
1 FunctionEnd

0 commit comments

Comments
 (0)