Skip to content

Commit 2b4ce42

Browse files
authored
Translate function pointer from global variable as pointer, not as declaration (#1608)
This patch helps to avoid invalid SPV generation. When global variable contains a pointer to a function, translator tries to translate it as declaration. Then it translates this function the second time when going through the function list. This leads to double translation of the same function and to the usage of the same IDs in SPIR-V file.
1 parent b412ae5 commit 2b4ce42

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
16831683
}
16841684
}
16851685
}
1686-
BVarInit = transValue(Init, nullptr);
1686+
// As global variables define a pointer to their "content" type, we should
1687+
// translate here only pointer without declaration even if it is a
1688+
// function pointer.
1689+
BVarInit = transValue(Init, nullptr, true, FuncTransMode::Pointer);
16871690
}
16881691

16891692
SPIRVStorageClassKind StorageClass;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: llvm-as < %s | llvm-spirv -spirv-ext=+SPV_INTEL_function_pointers -o %t.spv
2+
; RUN: llvm-spirv %t.spv -spirv-ext=+SPV_INTEL_function_pointers -to-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3+
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-LLVM
4+
5+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
6+
target triple = "spir64"
7+
8+
9+
; CHECK-SPIRV: Capability FunctionPointersINTEL
10+
; CHECK-SPIRV: Extension "SPV_INTEL_function_pointers"
11+
; CHECK-SPIRV: TypeFunction [[#FOO_TY:]] [[#]] [[#]]
12+
; CHECK-SPIRV: TypePointer [[#FOO_TY_PTR:]] [[#]] [[#FOO_TY]]
13+
; CHECK-SPIRV: ConstantFunctionPointerINTEL [[#FOO_TY_PTR]] [[#FOO_PTR:]] [[#FOO:]]
14+
; CHECK-SPIRV: Function [[#]] [[#]] [[#]] [[#FOO_TY]]
15+
16+
; CHECK-LLVM: @two = internal addrspace(1) global i32 (i32, i32)* @_Z4barrii
17+
; CHECK-LLVM: define spir_func i32 @_Z4barrii(i32 %[[#]], i32 %[[#]])
18+
19+
@two = internal addrspace(1) global i32 (i32, i32)* @_Z4barrii, align 8
20+
21+
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn writeonly
22+
define protected spir_func noundef i32 @_Z4barrii(i32 %0, i32 %1) {
23+
entry:
24+
ret i32 1
25+
}

0 commit comments

Comments
 (0)