Skip to content

Commit e91c9da

Browse files
vmaksimodbudanov-cmplr
authored andcommitted
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. Original commit: KhronosGroup/SPIRV-LLVM-Translator@2b4ce42
1 parent f5f1d49 commit e91c9da

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
16951695
}
16961696
}
16971697
}
1698-
BVarInit = transValue(Init, nullptr);
1698+
// As global variables define a pointer to their "content" type, we should
1699+
// translate here only pointer without declaration even if it is a
1700+
// function pointer.
1701+
BVarInit = transValue(Init, nullptr, true, FuncTransMode::Pointer);
16991702
}
17001703

17011704
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)