[llvm] [SPIRV] Prevent creation of jump tables from switch (PR #82287)

Vyacheslav Levytskyy via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 20 01:48:13 PST 2024


https://github.com/VyacheslavLevytskyy updated https://github.com/llvm/llvm-project/pull/82287

>From e052583b192ea779b9f2c00969841647c19e2a4d Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Mon, 19 Feb 2024 15:07:46 -0800
Subject: [PATCH 1/3] prevent creation of jump tables from switch

---
 llvm/lib/Target/SPIRV/SPIRVISelLowering.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
index f317b262071954..9ad18acd163dd7 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
@@ -31,6 +31,12 @@ class SPIRVTargetLowering : public TargetLowering {
     return true;
   }
 
+  /// Set the lower limit for number of blocks in a jump table in such a way to
+  /// prevent creation of jump tables from switch
+  unsigned getMinimumJumpTableEntries() const override {
+    return 0xffffffff;
+  }
+
   // This is to prevent sexts of non-i64 vector indices which are generated
   // within general IRTranslator hence type generation for it is omitted.
   MVT getVectorIdxTy(const DataLayout &DL) const override {

>From d0ce6472efb2386e105048ccf3ff23840a7e396d Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Mon, 19 Feb 2024 15:08:26 -0800
Subject: [PATCH 2/3] prevent creation of jump tables from switch

---
 llvm/lib/Target/SPIRV/SPIRVISelLowering.h | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
index 9ad18acd163dd7..526c24b20c3e1c 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
@@ -31,11 +31,9 @@ class SPIRVTargetLowering : public TargetLowering {
     return true;
   }
 
-  /// Set the lower limit for number of blocks in a jump table in such a way to
-  /// prevent creation of jump tables from switch
-  unsigned getMinimumJumpTableEntries() const override {
-    return 0xffffffff;
-  }
+  // Set the lower limit for number of blocks in a jump table in such a way to
+  // prevent creation of jump tables from switch
+  unsigned getMinimumJumpTableEntries() const override { return 0xffffffff; }
 
   // This is to prevent sexts of non-i64 vector indices which are generated
   // within general IRTranslator hence type generation for it is omitted.

>From f255110fdb1ff1adf8f521acc1d57d9b9a8caa15 Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Tue, 20 Feb 2024 01:48:01 -0800
Subject: [PATCH 3/3] change the callback; add test

---
 llvm/lib/Target/SPIRV/SPIRVISelLowering.h     |  5 ++--
 .../CodeGen/SPIRV/switch-no-jump-table.ll     | 30 +++++++++++++++++++
 2 files changed, 32 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/switch-no-jump-table.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
index 526c24b20c3e1c..d34f802e9d889f 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
@@ -31,9 +31,8 @@ class SPIRVTargetLowering : public TargetLowering {
     return true;
   }
 
-  // Set the lower limit for number of blocks in a jump table in such a way to
-  // prevent creation of jump tables from switch
-  unsigned getMinimumJumpTableEntries() const override { return 0xffffffff; }
+  // prevent creation of jump tables
+  bool areJTsAllowed(const Function *) const override { return false; }
 
   // This is to prevent sexts of non-i64 vector indices which are generated
   // within general IRTranslator hence type generation for it is omitted.
diff --git a/llvm/test/CodeGen/SPIRV/switch-no-jump-table.ll b/llvm/test/CodeGen/SPIRV/switch-no-jump-table.ll
new file mode 100644
index 00000000000000..c9c0f17f0b91ef
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/switch-no-jump-table.ll
@@ -0,0 +1,30 @@
+; The test is to check that jump tables are not generated from switch
+
+; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK: OpSwitch %[[#]] %[[Label:]]
+; CHECK-4: OpBranch %[[Label]]
+
+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
+}



More information about the llvm-commits mailing list