Skip to content

Commit 6cca23a

Browse files
[SPIRV] Prevent creation of jump tables from switch (#82287)
This PR is to prevent creation of jump tables from switch. The reason is that SPIR-V doesn't know how to lower jump tables, and a sequence of commands that IRTranslator generates for switch via jump tables breaks SPIR-V Backend code generation with complains to G_BRJT. The next example is the shortest code to break SPIR-V Backend code generation in this way: ``` 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" target triple = "spir64-unknown-unknown" define spir_func void @foo(i32 noundef %val) { entry: switch i32 %val, label %sw.epilog [ i32 0, label %sw.bb i32 1, label %sw.bb2 i32 2, label %sw.bb3 i32 3, label %sw.bb4 ] sw.bb: br label %sw.epilog sw.bb2: br label %sw.epilog sw.bb3: br label %sw.epilog sw.bb4: br label %sw.epilog sw.epilog: ret void } ``` To resolve the issue we set a high lower limit for number of blocks in a jump table via getMinimumJumpTableEntries() and prevent undesirable (or rather unsupported at the moment) path of code generation.
1 parent fddf23c commit 6cca23a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVISelLowering.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class SPIRVTargetLowering : public TargetLowering {
3131
return true;
3232
}
3333

34+
// prevent creation of jump tables
35+
bool areJTsAllowed(const Function *) const override { return false; }
36+
3437
// This is to prevent sexts of non-i64 vector indices which are generated
3538
// within general IRTranslator hence type generation for it is omitted.
3639
MVT getVectorIdxTy(const DataLayout &DL) const override {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; The test is to check that jump tables are not generated from switch
2+
3+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
4+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
5+
6+
; CHECK: OpSwitch %[[#]] %[[Label:]]
7+
; CHECK-4: OpBranch %[[Label]]
8+
9+
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"
10+
target triple = "spir64-unknown-unknown"
11+
12+
define spir_func void @foo(i32 noundef %val) {
13+
entry:
14+
switch i32 %val, label %sw.epilog [
15+
i32 0, label %sw.bb
16+
i32 1, label %sw.bb2
17+
i32 2, label %sw.bb3
18+
i32 3, label %sw.bb4
19+
]
20+
sw.bb:
21+
br label %sw.epilog
22+
sw.bb2:
23+
br label %sw.epilog
24+
sw.bb3:
25+
br label %sw.epilog
26+
sw.bb4:
27+
br label %sw.epilog
28+
sw.epilog:
29+
ret void
30+
}

0 commit comments

Comments
 (0)