Skip to content

Commit 30e6487

Browse files
MrSidimsjsji
authored andcommitted
Don't wrap kernels that are not being called in the module (#2119)
* Don't wrap kernels that are not being called in the module This patch is a result of a reflection about previously merged PR KhronosGroup/SPIRV-LLVM-Translator#1149 "add an entry point wrapper around functions (llvm pass)" and is enspired by various reported translator, clang (OpenCL) and Intel GPU drivers issues (see KhronosGroup/SPIRV-LLVM-Translator#2029 for reference). While SPIR-V spec states: === *OpName* --//--. This has nosemantic impact and can safely be removed from a module. === yet having EntryPoint function and a function that shares the name via OpName might be confusing by both (old) drivers and programmers, who read the SPIR-V file. This patch prevents generation of the wrapper function when it's not necessary to generate it aka if a kernel function is not called by other kernel. We can do better in other cases as well, for example I have experiments of renaming a wrapped function adding a previx, so it could be distinguished from the actual kernel/entry point, but for now it doesn't pass validation for E2E OpenCL tests. Signed-off-by: Sidorov, Dmitry <[email protected]> * prevent a copy Signed-off-by: Sidorov, Dmitry <[email protected]> This patch is a result of a reflection about previously merged PR #1149 "add an entry point wrapper around functions (llvm pass)" and is enspired by various reported translator, clang (OpenCL) and Intel GPU drivers issues (see While SPIR-V spec states: OpName --//--. This has nosemantic impact and can safely be removed from a module. yet having EntryPoint function and a function that shares the name via OpName might be confusing by both not-up-to-date drivers and programmers, who read the SPIR-V file. This patch prevents generation of the wrapper function when it's not necessary to generate it aka if a kernel function is not called by other kernel. Signed-off-by: Sidorov, Dmitry <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@46285e4
1 parent 02359a6 commit 30e6487

22 files changed

+63
-61
lines changed

llvm-spirv/lib/SPIRV/SPIRVRegularizeLLVM.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ bool SPIRVRegularizeLLVMBase::regularize() {
427427
simplifyBuiltinVarAccesses(&GV);
428428
}
429429

430+
// Kernels called by other kernels
431+
std::vector<Function *> CalledKernels;
430432
for (auto I = M->begin(), E = M->end(); I != E;) {
431433
Function *F = &(*I++);
432434
if (F->isDeclaration() && F->use_empty()) {
@@ -440,7 +442,9 @@ bool SPIRVRegularizeLLVMBase::regularize() {
440442
if (auto *Call = dyn_cast<CallInst>(&II)) {
441443
Call->setTailCall(false);
442444
Function *CF = Call->getCalledFunction();
443-
if (CF && CF->isIntrinsic()) {
445+
if (CF && CF->getCallingConv() == CallingConv::SPIR_KERNEL) {
446+
CalledKernels.push_back(CF);
447+
} else if (CF && CF->isIntrinsic()) {
444448
removeFnAttr(Call, Attribute::NoUnwind);
445449
auto *II = cast<IntrinsicInst>(Call);
446450
if (II->getIntrinsicID() == Intrinsic::memset ||

llvm-spirv/test/extensions/INTEL/SPV_INTEL_function_pointers/alias.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ target triple = "spir64-unknown-unknown"
1010
; when used since they can't be translated directly.
1111

1212
; CHECK-SPIRV-DAG: Name [[#FOO:]] "foo"
13-
; CHECK-SPIRV-DAG: Name [[#BAR:]] "bar"
13+
; CHECK-SPIRV-DAG: EntryPoint [[#]] [[#BAR:]] "bar"
1414
; CHECK-SPIRV-DAG: Name [[#Y:]] "y"
1515
; CHECK-SPIRV-DAG: Name [[#FOOPTR:]] "foo.alias"
1616
; CHECK-SPIRV-DAG: Decorate [[#FOO]] LinkageAttributes "foo" Export
17-
; CHECK-SPIRV-DAG: Decorate [[#BAR]] LinkageAttributes "bar" Export
1817
; CHECK-SPIRV-DAG: TypeInt [[#I32:]] 32 0
1918
; CHECK-SPIRV-DAG: TypeInt [[#I64:]] 64 0
2019
; CHECK-SPIRV-DAG: TypeFunction [[#FOO_TYPE:]] [[#I32]] [[#I32]]

llvm-spirv/test/extensions/INTEL/SPV_INTEL_function_pointers/fp-from-host.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
; CHECK-SPIRV: Capability FunctionPointersINTEL
1818
; CHECK-SPIRV: Extension "SPV_INTEL_function_pointers"
1919
;
20-
; CHECK-SPIRV: EntryPoint {{[0-9]+}} [[KERNEL_ID:[0-9]+]] "test"
20+
; CHECK-SPIRV: EntryPoint [[#]] [[KERNEL_ID:[0-9]+]] "test"
2121
; CHECK-SPIRV: TypeInt [[INT32_TYPE_ID:[0-9]+]] 32
2222
; CHECK-SPIRV: TypePointer [[INT_PTR:[0-9]+]] 5 [[INT32_TYPE_ID]]
2323
; CHECK-SPIRV: TypeFunction [[FOO_TYPE_ID:[0-9]+]] [[INT32_TYPE_ID]] [[INT32_TYPE_ID]]

llvm-spirv/test/extensions/INTEL/SPV_INTEL_function_pointers/function-pointer-as-function-arg.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
; CHECK-SPIRV: Capability FunctionPointersINTEL
3434
; CHECK-SPIRV: Extension "SPV_INTEL_function_pointers"
3535
;
36-
; CHECK-SPIRV: EntryPoint 6 [[KERNEL_ID:[0-9]+]] "test"
36+
; CHECK-SPIRV: EntryPoint [[#]] [[KERNEL_ID:[0-9]+]] "test"
3737
; CHECK-SPIRV: TypeInt [[TYPE_INT32_ID:[0-9]+]] 32
3838
; CHECK-SPIRV: TypeFunction [[FOO_TYPE_ID:[0-9]+]] [[TYPE_INT32_ID]] [[TYPE_INT32_ID]]
3939
; CHECK-SPIRV: TypePointer [[FOO_PTR_TYPE_ID:[0-9]+]] {{[0-9]+}} [[FOO_TYPE_ID]]

llvm-spirv/test/extensions/INTEL/SPV_INTEL_function_pointers/function-pointer.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
;
2020
; CHECK-SPIRV: Capability FunctionPointersINTEL
2121
; CHECK-SPIRV: Extension "SPV_INTEL_function_pointers"
22-
; CHECK-SPIRV: EntryPoint 6 [[KERNEL_ID:[0-9]+]] "test"
22+
; CHECK-SPIRV: EntryPoint [[#]] [[KERNEL_ID:[0-9]+]] "test"
2323
; CHECK-SPIRV: TypeInt [[TYPE_INT_ID:[0-9]+]]
2424
; CHECK-SPIRV: TypeFunction [[FOO_TYPE_ID:[0-9]+]] [[TYPE_INT_ID]] [[TYPE_INT_ID]]
2525
; CHECK-SPIRV: TypePointer [[FOO_PTR_ID:[0-9]+]] {{[0-9]+}} [[FOO_TYPE_ID]]

llvm-spirv/test/extensions/INTEL/SPV_INTEL_function_pointers/non-uniform-function-pointer.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
; CHECK-SPIRV: Capability FunctionPointersINTEL
3030
; CHECK-SPIRV: Extension "SPV_INTEL_function_pointers"
3131
;
32-
; CHECK-SPIRV: EntryPoint 6 [[KERNEL_ID:[0-9]+]] "test"
32+
; CHECK-SPIRV: EntryPoint [[#]] [[KERNEL_ID:[0-9]+]] "test"
3333
; CHECK-SPIRV: TypeInt [[TYPE_INT32_ID:[0-9+]]] 32
3434
; CHECK-SPIRV: TypeFunction [[FOO_TYPE_ID:[0-9]+]] [[TYPE_INT32_ID]] [[TYPE_INT32_ID]]
3535
; CHECK-SPIRV: TypePointer [[FOO_PTR_TYPE_ID:[0-9]+]] {{[0-9]+}} [[FOO_TYPE_ID]]

llvm-spirv/test/extensions/INTEL/SPV_INTEL_function_pointers/select.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
; RUN: llvm-dis %t.r.bc -o %t.r.ll
77
; RUN: FileCheck < %t.r.ll %s --check-prefix=CHECK-LLVM
88

9-
; CHECK-SPIRV: EntryPoint 6 [[#KERNEL_ID:]] "_ZTS6kernel"
9+
; CHECK-SPIRV-DAG: EntryPoint [[#]] [[#KERNEL_ID:]] "_ZTS6kernel"
1010
; CHECK-SPIRV-DAG: Name [[#BAR:]] "_Z3barii"
1111
; CHECK-SPIRV-DAG: Name [[#BAZ:]] "_Z3bazii"
1212
; CHECK-SPIRV: TypeInt [[#INT32:]] 32

llvm-spirv/test/extensions/INTEL/SPV_INTEL_unstructured_loop_controls/FPGAUnstructuredLoopAttr.ll

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
66
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
77

8-
; CHECK-SPIRV: 2 Capability UnstructuredLoopControlsINTEL
9-
; CHECK-SPIRV: 2 Capability FPGALoopControlsINTEL
10-
; CHECK-SPIRV: 9 Extension "SPV_INTEL_fpga_loop_controls"
11-
; CHECK-SPIRV: 11 Extension "SPV_INTEL_unstructured_loop_controls"
12-
; CHECK-SPIRV: 4 EntryPoint 6 [[FOO:[0-9]+]] "foo"
13-
; CHECK-SPIRV: 4 EntryPoint 6 [[BOO:[0-9]+]] "boo"
14-
; CHECK-SPIRV: 4 Name [[ENTRY_1:[0-9]+]] "entry"
15-
; CHECK-SPIRV: 5 Name [[FOR:[0-9]+]] "for.cond"
16-
; CHECK-SPIRV: 4 Name [[ENTRY_2:[0-9]+]] "entry"
17-
; CHECK-SPIRV: 5 Name [[WHILE:[0-9]+]] "while.body"
8+
; CHECK-SPIRV: Capability UnstructuredLoopControlsINTEL
9+
; CHECK-SPIRV: Capability FPGALoopControlsINTEL
10+
; CHECK-SPIRV: Extension "SPV_INTEL_fpga_loop_controls"
11+
; CHECK-SPIRV: Extension "SPV_INTEL_unstructured_loop_controls"
12+
; CHECK-SPIRV: EntryPoint [[#]] [[FOO:[0-9]+]] "foo"
13+
; CHECK-SPIRV: EntryPoint [[#]] [[BOO:[0-9]+]] "boo"
14+
; CHECK-SPIRV: Name [[ENTRY_1:[0-9]+]] "entry"
15+
; CHECK-SPIRV: Name [[FOR:[0-9]+]] "for.cond"
16+
; CHECK-SPIRV: Name [[ENTRY_2:[0-9]+]] "entry"
17+
; CHECK-SPIRV: Name [[WHILE:[0-9]+]] "while.body"
1818

19-
; CHECK-SPIRV: 5 Function 2 [[FOO]] {{[0-9]+}} {{[0-9]+}}
20-
; CHECK-SPIRV: 2 Label [[ENTRY_1]]
21-
; CHECK-SPIRV: 2 Branch [[FOR]]
22-
; CHECK-SPIRV: 2 Label [[FOR]]
19+
; CHECK-SPIRV: Function [[#]] [[FOO]] {{[0-9]+}} {{[0-9]+}}
20+
; CHECK-SPIRV: Label [[ENTRY_1]]
21+
; CHECK-SPIRV: Branch [[FOR]]
22+
; CHECK-SPIRV: Label [[FOR]]
2323
; Per SPIR-V spec extension INTEL/SPV_INTEL_fpga_loop_controls,
2424
; LoopControlMaxConcurrencyINTELMask = 0x20000 (131072)
25-
; CHECK-SPIRV: 3 LoopControlINTEL 131072 2
26-
; CHECK-SPIRV-NEXT: 2 Branch [[FOR]]
25+
; CHECK-SPIRV: LoopControlINTEL 131072 2
26+
; CHECK-SPIRV-NEXT: Branch [[FOR]]
2727

28-
; CHECK-SPIRV: 5 Function 2 [[BOO]] {{[0-9]+}} {{[0-9]+}}
29-
; CHECK-SPIRV: 2 Label [[ENTRY_2]]
30-
; CHECK-SPIRV: 2 Branch [[WHILE]]
31-
; CHECK-SPIRV: 2 Label [[WHILE]]
28+
; CHECK-SPIRV: Function [[#]] [[BOO]] {{[0-9]+}} {{[0-9]+}}
29+
; CHECK-SPIRV: Label [[ENTRY_2]]
30+
; CHECK-SPIRV: Branch [[WHILE]]
31+
; CHECK-SPIRV: Label [[WHILE]]
3232
; Per SPIR-V spec extension INTEL/SPV_INTEL_fpga_loop_controls,
3333
; LoopControlInitiationIntervalINTELMask = 0x10000 (65536)
34-
; CHECK-SPIRV: 3 LoopControlINTEL 65536 2
35-
; CHECK-SPIRV-NEXT: 2 Branch [[WHILE]]
34+
; CHECK-SPIRV: LoopControlINTEL 65536 2
35+
; CHECK-SPIRV-NEXT: Branch [[WHILE]]
3636

3737
; ModuleID = 'infinite.cl'
3838
source_filename = "infinite.cl"

llvm-spirv/test/mem2reg.cl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// RUN: %clang_cc1 -O0 -S -triple spir-unknown-unknown -cl-std=CL2.0 -x cl -disable-O0-optnone %s -emit-llvm-bc -o %t.bc
22
// RUN: llvm-spirv -s %t.bc
3-
// RUN: llvm-dis < %t.bc | FileCheck %s --check-prefixes=CHECK-WO
3+
// RUN: llvm-dis < %t.bc | FileCheck %s --check-prefixes=CHECK,CHECK-WO
44
// RUN: llvm-spirv -s -spirv-mem2reg %t.bc -o %t.opt.bc
55
// RUN: llvm-dis < %t.opt.bc | FileCheck %s --check-prefixes=CHECK-W
66
// CHECK-W-LABEL: spir_kernel void @foo
77
// CHECK-W-NOT: alloca
8-
// CHECK-WO-LABEL: spir_kernel void @foo
98
// CHECK-WO: alloca
109
__kernel void foo(__global int *a) {
1110
*a = *a + 1;

llvm-spirv/test/transcoding/KernelArgTypeInOpString2.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
4242
target triple = "spir"
4343

44-
; CHECK-SPIRV-WORKAROUND: String 17 "kernel_arg_type.foo.cl::tt::vec<float, 4>*,"
45-
; CHECK-SPIRV-WORKAROUND-NEGATIVE-NOT: String 17 "kernel_arg_type.foo.cl::tt::vec<float, 4>*,"
44+
; CHECK-SPIRV-WORKAROUND: String [[#]] "kernel_arg_type.foo.cl::tt::vec<float, 4>*,"
45+
; CHECK-SPIRV-WORKAROUND-NEGATIVE-NOT: String [[#]] "kernel_arg_type.foo.cl::tt::vec<float, 4>*,"
4646

4747
; CHECK-LLVM-WORKAROUND: !kernel_arg_type [[TYPE:![0-9]+]]
4848
; CHECK-LLVM-WORKAROUND: [[TYPE]] = !{!"cl::tt::vec<float, 4>*"}

0 commit comments

Comments
 (0)