Skip to content

Commit 411742c

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 dbae4d0 commit 411742c

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
@@ -4940,12 +4940,20 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
49404940
return ConstantAsMetadata::get(
49414941
ConstantInt::get(Type::getInt1Ty(*Context), 1));
49424942
});
4943-
// Generate metadata for spirv.ParameterDecorations
4944-
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
4945-
[=](SPIRVFunctionParameter *Arg) {
4946-
return transDecorationsToMetadataList(
4947-
Context, Arg->getDecorations());
4948-
});
4943+
// Generate metadata for spirv.ParameterDecorations.
4944+
// In the entry-point wrapper pattern two SPIR-V functions map to the same
4945+
// LLVM function; only one carries decorated parameters. Skip the
4946+
// undecorated function so it cannot overwrite the decorated one.
4947+
bool HasDecorations = false;
4948+
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) {
4949+
HasDecorations |= Arg->getNumDecorations() > 0;
4950+
});
4951+
if (HasDecorations || !F->hasMetadata(SPIRV_MD_PARAMETER_DECORATIONS))
4952+
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
4953+
[=](SPIRVFunctionParameter *Arg) {
4954+
return transDecorationsToMetadataList(
4955+
Context, Arg->getDecorations());
4956+
});
49494957
return true;
49504958
}
49514959

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)