[Mlir-commits] [mlir] [mlir][SPIR-V] Fix swapped select operands in index.floordivs lowering (PR #214770)

Arseniy Obolenskiy llvmlistbot at llvm.org
Fri Aug 7 08:11:41 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/214770

Fix ConvertIndexFloorDivSPattern to select `negRes` when `cmp` is true

Previously operands were reversed, producing the wrong sign for the floordiv result whenever the negative result branch should've been taken

>From 73c0601632ebebebc629d726f102d6e4ab072f4f Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 7 Aug 2026 17:06:47 +0200
Subject: [PATCH] [mlir][SPIR-V] Fix swapped select operands in index.floordivs
 lowering

Fix ConvertIndexFloorDivSPattern to select `negRes` when `cmp` is true

Previously operands were reversed, producing the wrong sign for the floordiv result whenever the negative result branch should've been taken
---
 mlir/lib/Conversion/IndexToSPIRV/IndexToSPIRV.cpp     | 2 +-
 mlir/test/Conversion/IndexToSPIRV/index-to-spirv.mlir | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Conversion/IndexToSPIRV/IndexToSPIRV.cpp b/mlir/lib/Conversion/IndexToSPIRV/IndexToSPIRV.cpp
index 9718361d4c4ba..92526da965157 100644
--- a/mlir/lib/Conversion/IndexToSPIRV/IndexToSPIRV.cpp
+++ b/mlir/lib/Conversion/IndexToSPIRV/IndexToSPIRV.cpp
@@ -228,7 +228,7 @@ struct ConvertIndexFloorDivSPattern final : OpConversionPattern<FloorDivSOp> {
     Value nNonZero = spirv::INotEqualOp::create(rewriter, loc, n, zero);
 
     Value cmp = spirv::LogicalAndOp::create(rewriter, loc, diffSign, nNonZero);
-    rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, cmp, posRes, negRes);
+    rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, cmp, negRes, posRes);
     return success();
   }
 };
diff --git a/mlir/test/Conversion/IndexToSPIRV/index-to-spirv.mlir b/mlir/test/Conversion/IndexToSPIRV/index-to-spirv.mlir
index 650c030d80f55..2d00adff2e8ba 100644
--- a/mlir/test/Conversion/IndexToSPIRV/index-to-spirv.mlir
+++ b/mlir/test/Conversion/IndexToSPIRV/index-to-spirv.mlir
@@ -141,7 +141,7 @@ func.func @floordivs(%n: index, %m: index) -> index {
   // CHECK: %[[N_NON_ZERO:.*]] = spirv.INotEqual %[[N]], %[[ZERO]]
 
   // CHECK: %[[CMP:.*]] = spirv.LogicalAnd %[[DIFF_SIGN]], %[[N_NON_ZERO]]
-  // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[POS_RES]], %[[NEG_RES]]
+  // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[NEG_RES]], %[[POS_RES]]
   %result = index.floordivs %n, %m
 
   // %[[RESULTI:.*] = builtin.unrealized_conversion_cast %[[RESULT]]



More information about the Mlir-commits mailing list