Skip to content

Commit 163a805

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 11477a5 commit 163a805

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

lib/SPIRV/SPIRVReader.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4593,8 +4593,13 @@ void SPIRVToLLVM::transFunctionDecorationsToMetadata(SPIRVFunction *BF,
45934593
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) {
45944594
TotalParameterDecorations += Arg->getNumDecorations();
45954595
});
4596-
if (TotalParameterDecorations == 0)
4597-
return;
4596+
// In the entry-point wrapper pattern two SPIR-V functions map to the same
4597+
// LLVM function; only one carries decorated parameters. Skip the
4598+
// undecorated function so it cannot overwrite the decorated one.
4599+
if (TotalParameterDecorations == 0) {
4600+
if (F->hasMetadata(SPIRV_MD_PARAMETER_DECORATIONS))
4601+
return;
4602+
}
45984603

45994604
// Generate metadata for spirv.ParameterDecorations
46004605
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
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

test/extensions/INTEL/SPV_INTEL_subgroup_buffer_prefetch/subgroup_buffer_prefetch_with_cache_controls.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
1111
target triple = "spir64-unknown-unknown"
1212

13-
@.str.1 = private unnamed_addr addrspace(1) constant [7 x i8] c"file.h\00", section "llvm.metadata"
13+
@.str.1 = private unnamed_addr constant [7 x i8] c"file.h\00", section "llvm.metadata"
1414
; {6442:"0,1"} = {CacheControlLoadINTEL_Token:"CacheLevel,CacheControl"} = L1 Cached
15-
@.str.cc = private unnamed_addr addrspace(1) constant [13 x i8] c"{6442:\220,1\22}\00", section "llvm.metadata"
15+
@.str.cc = private unnamed_addr constant [13 x i8] c"{6442:\220,1\22}\00", section "llvm.metadata"
1616

1717
; CHECK-SPIRV-DAG: Capability SubgroupBufferPrefetchINTEL
1818
; CHECK-SPIRV-DAG: Capability CacheControlsINTEL
@@ -35,7 +35,7 @@ target triple = "spir64-unknown-unknown"
3535

3636
define spir_kernel void @test(i8 addrspace(1)* %ptr, i32 %num_bytes) {
3737
entry:
38-
%annotated = call i8 addrspace(1)* @llvm.ptr.annotation.p1i8(i8 addrspace(1)* %ptr, i8* getelementptr inbounds ([13 x i8], [13 x i8]* addrspacecast ([13 x i8] addrspace(1)* @.str.cc to [13 x i8]*), i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* addrspacecast ([7 x i8] addrspace(1)* @.str.1 to [7 x i8]*), i64 0, i64 0), i32 0, i8* null)
38+
%annotated = call i8 addrspace(1)* @llvm.ptr.annotation.p1i8(i8 addrspace(1)* %ptr, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.cc, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0), i32 0, i8* null)
3939
call spir_func void @_Z34__spirv_SubgroupBlockPrefetchINTELPU3AS1Khj(i8 addrspace(1)* %annotated, i32 %num_bytes)
4040
ret void
4141
}

0 commit comments

Comments
 (0)