[llvm] 90b8a48 - [SPIRV] Support additional comparison predicates for i1 types (#174585)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 8 03:53:49 PST 2026


Author: Manuel Carrasco
Date: 2026-01-08T11:53:45Z
New Revision: 90b8a481e4827398cba2a2047939bbbb70e8f17e

URL: https://github.com/llvm/llvm-project/commit/90b8a481e4827398cba2a2047939bbbb70e8f17e
DIFF: https://github.com/llvm/llvm-project/commit/90b8a481e4827398cba2a2047939bbbb70e8f17e.diff

LOG: [SPIRV] Support additional comparison predicates for i1 types (#174585)

Previously, the SPIRV BE only handled equality and inequality
comparisons (ICMP_EQ, ICMP_NE) for i1 types using logical operations
(OpLogicalEqual, OpLogicalNotEqual). Other comparison predicates
(signed/unsigned less than, greater than, etc.) triggered an unreachable
assertion.

This patch extends the support for the missing predicates. The BE
considers i1 values as booleans and in SPIR-V only logical operations
can work on them. This patch lowers the missing predicates into
supported logical operations.

The lowering has been validated with instcombine to avoid introducing an
unsound transformation (using the new test case).

Added: 
    llvm/test/CodeGen/SPIRV/icmp-i1.ll

Modified: 
    llvm/lib/Target/SPIRV/SPIRVRegularizer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/SPIRV/SPIRVRegularizer.cpp b/llvm/lib/Target/SPIRV/SPIRVRegularizer.cpp
index 653c9ad53e888..c864556783a2a 100644
--- a/llvm/lib/Target/SPIRV/SPIRVRegularizer.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVRegularizer.cpp
@@ -13,6 +13,7 @@
 
 #include "SPIRV.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/PassManager.h"
@@ -37,6 +38,7 @@ struct SPIRVRegularizer : public FunctionPass {
 
 private:
   void runLowerConstExpr(Function &F);
+  void runLowerI1Comparisons(Function &F);
 };
 } // namespace
 
@@ -151,7 +153,64 @@ void SPIRVRegularizer::runLowerConstExpr(Function &F) {
   }
 }
 
+// Lower i1 comparisons with certain predicates to logical operations.
+// The backend treats i1 as boolean values, and SPIR-V only allows logical
+// operations for boolean values. This function lowers i1 comparisons with
+// certain predicates to logical operations to generate valid SPIR-V.
+void SPIRVRegularizer::runLowerI1Comparisons(Function &F) {
+  for (auto &I : make_early_inc_range(instructions(F))) {
+    auto *Cmp = dyn_cast<ICmpInst>(&I);
+    if (!Cmp)
+      continue;
+
+    bool IsI1 = Cmp->getOperand(0)->getType()->isIntegerTy(1);
+    if (!IsI1)
+      continue;
+
+    auto Pred = Cmp->getPredicate();
+    bool IsTargetPred =
+        Pred >= ICmpInst::ICMP_UGT && Pred <= ICmpInst::ICMP_SLE;
+    if (!IsTargetPred)
+      continue;
+
+    Value *P = Cmp->getOperand(0);
+    Value *Q = Cmp->getOperand(1);
+
+    IRBuilder<> Builder(Cmp);
+    Value *Result = nullptr;
+    switch (Pred) {
+    case ICmpInst::ICMP_UGT:
+    case ICmpInst::ICMP_SLT:
+      // Result = p & !q
+      Result = Builder.CreateAnd(P, Builder.CreateNot(Q));
+      break;
+    case ICmpInst::ICMP_ULT:
+    case ICmpInst::ICMP_SGT:
+      // Result = q & !p
+      Result = Builder.CreateAnd(Q, Builder.CreateNot(P));
+      break;
+    case ICmpInst::ICMP_ULE:
+    case ICmpInst::ICMP_SGE:
+      // Result = q | !p
+      Result = Builder.CreateOr(Q, Builder.CreateNot(P));
+      break;
+    case ICmpInst::ICMP_UGE:
+    case ICmpInst::ICMP_SLE:
+      // Result = p | !q
+      Result = Builder.CreateOr(P, Builder.CreateNot(Q));
+      break;
+    default:
+      llvm_unreachable("Unexpected predicate");
+    }
+
+    Result->takeName(Cmp);
+    Cmp->replaceAllUsesWith(Result);
+    Cmp->eraseFromParent();
+  }
+}
+
 bool SPIRVRegularizer::runOnFunction(Function &F) {
+  runLowerI1Comparisons(F);
   runLowerConstExpr(F);
   return true;
 }

diff  --git a/llvm/test/CodeGen/SPIRV/icmp-i1.ll b/llvm/test/CodeGen/SPIRV/icmp-i1.ll
new file mode 100644
index 0000000000000..576bafebd6124
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/icmp-i1.ll
@@ -0,0 +1,130 @@
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown < %s | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown -filetype=obj < %s  | spirv-val %}
+
+define spir_func i1 @ugt(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function ugt
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp ugt i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @ugt_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function ugt_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp samesign ugt i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @uge(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function uge
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp uge i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @uge_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function uge_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp samesign uge i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @ult(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function ult
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp ult i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @ult_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function ult_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp samesign ult i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @ule(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function ule
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp ule i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @ule_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function ule_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp samesign ule i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @sgt(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function sgt
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp sgt i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @sgt_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function sgt_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp samesign sgt i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @sge(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function sge
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp sge i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @sge_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function sge_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp samesign sge i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @slt(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function slt
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp slt i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @slt_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function slt_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalAnd
+  %r = icmp samesign slt i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @sle(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function sle
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp sle i1 %p, %q
+  ret i1 %r
+}
+
+define spir_func i1 @sle_same_sign(i1 %p, i1 %q) addrspace(4) {
+  ; CHECK: Begin function sle_same_sign
+  ; CHECK: OpLogicalNotEqual
+  ; CHECK: OpLogicalOr
+  %r = icmp samesign sle i1 %p, %q
+  ret i1 %r
+}


        


More information about the llvm-commits mailing list