[Mlir-commits] [mlir] [mlir][arith] Mark `arith.remsi` and `arith.remui` as conditionally speculatable (PR #188263)
Matthias Springer
llvmlistbot at llvm.org
Tue Mar 24 10:49:02 PDT 2026
https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/188263
>From c191ad0c87fa913c5b22a0f1c1cfd56288e10019 Mon Sep 17 00:00:00 2001
From: Matthias Springer <me at m-sp.org>
Date: Tue, 24 Mar 2026 14:55:30 +0000
Subject: [PATCH] [mlir][arith] Mark `arith.remsi` and `arith.remui` as
conditionally speculatable
---
mlir/include/mlir/Dialect/Arith/IR/ArithOps.td | 18 ++++++++++++++++--
mlir/lib/Dialect/Arith/IR/ArithOps.cpp | 13 +++++++++++++
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
index 4b830c05bf585..50a09761f110d 100644
--- a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
+++ b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
@@ -678,7 +678,8 @@ def Arith_FloorDivSIOp : Arith_TotalIntBinaryOp<"floordivsi"> {
// RemUIOp
//===----------------------------------------------------------------------===//
-def Arith_RemUIOp : Arith_TotalIntBinaryOp<"remui"> {
+def Arith_RemUIOp : Arith_IntBinaryOp<"remui",
+ [ConditionallySpeculatable]> {
let summary = "unsigned integer division remainder operation";
let description = [{
Unsigned integer division remainder. Treats the leading bit as the most
@@ -701,6 +702,12 @@ def Arith_RemUIOp : Arith_TotalIntBinaryOp<"remui"> {
%x = arith.remui %y, %z : tensor<4x?xi8>
```
}];
+
+ let extraClassDeclaration = [{
+ /// Interface method for ConditionallySpeculatable.
+ Speculation::Speculatability getSpeculatability();
+ }];
+
let hasFolder = 1;
}
@@ -708,7 +715,8 @@ def Arith_RemUIOp : Arith_TotalIntBinaryOp<"remui"> {
// RemSIOp
//===----------------------------------------------------------------------===//
-def Arith_RemSIOp : Arith_TotalIntBinaryOp<"remsi"> {
+def Arith_RemSIOp : Arith_IntBinaryOp<"remsi",
+ [ConditionallySpeculatable]> {
let summary = "signed integer division remainder operation";
let description = [{
Signed integer division remainder. Treats the leading bit as sign, i.e. `6 %
@@ -731,6 +739,12 @@ def Arith_RemSIOp : Arith_TotalIntBinaryOp<"remsi"> {
%x = arith.remsi %y, %z : tensor<4x?xi8>
```
}];
+
+ let extraClassDeclaration = [{
+ /// Interface method for ConditionallySpeculatable.
+ Speculation::Speculatability getSpeculatability();
+ }];
+
let hasFolder = 1;
}
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 155edc5070a9d..105bdb9238c1b 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -931,6 +931,10 @@ OpFoldResult arith::RemUIOp::fold(FoldAdaptor adaptor) {
return div0 ? Attribute() : result;
}
+Speculation::Speculatability arith::RemUIOp::getSpeculatability() {
+ return getDivUISpeculatability(getRhs());
+}
+
//===----------------------------------------------------------------------===//
// RemSIOp
//===----------------------------------------------------------------------===//
@@ -954,6 +958,15 @@ OpFoldResult arith::RemSIOp::fold(FoldAdaptor adaptor) {
return div0 ? Attribute() : result;
}
+Speculation::Speculatability arith::RemSIOp::getSpeculatability() {
+ // X % 0 => UB
+ // X % -1 is well-defined (always 0), unlike X / -1 which can overflow.
+ if (matchPattern(getRhs(), m_IntRangeWithoutZeroS()))
+ return Speculation::Speculatable;
+
+ return Speculation::NotSpeculatable;
+}
+
//===----------------------------------------------------------------------===//
// AndIOp
//===----------------------------------------------------------------------===//
More information about the Mlir-commits
mailing list