Skip to content

Commit 93a896f

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 5d78f1f commit 93a896f

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
@@ -4828,12 +4828,20 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
48284828
return ConstantAsMetadata::get(
48294829
ConstantInt::get(Type::getInt1Ty(*Context), 1));
48304830
});
4831-
// Generate metadata for spirv.ParameterDecorations
4832-
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
4833-
[=](SPIRVFunctionParameter *Arg) {
4834-
return transDecorationsToMetadataList(
4835-
Context, Arg->getDecorations());
4836-
});
4831+
// Generate metadata for spirv.ParameterDecorations.
4832+
// In the entry-point wrapper pattern two SPIR-V functions map to the same
4833+
// LLVM function; only one carries decorated parameters. Skip the
4834+
// undecorated function so it cannot overwrite the decorated one.
4835+
bool HasDecorations = false;
4836+
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) {
4837+
HasDecorations |= Arg->getNumDecorations() > 0;
4838+
});
4839+
if (HasDecorations || !F->hasMetadata(SPIRV_MD_PARAMETER_DECORATIONS))
4840+
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
4841+
[=](SPIRVFunctionParameter *Arg) {
4842+
return transDecorationsToMetadataList(
4843+
Context, Arg->getDecorations());
4844+
});
48374845
return true;
48384846
}
48394847

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)