Skip to content

Commit 8cdb4f9

Browse files
committed
[mlir][Index] Add index.mins and index.minu
Signed and unsigned minimum operations were missing from the Index dialect and are needed to test integer range inference. Reviewed By: Mogball Differential Revision: https://reviews.llvm.org/D141299
1 parent 6496824 commit 8cdb4f9

File tree

6 files changed

+126
-13
lines changed

6 files changed

+126
-13
lines changed

mlir/include/mlir/Dialect/Index/IR/IndexOps.td

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,45 @@ def Index_MaxUOp : IndexBinaryOp<"maxu"> {
281281
}];
282282
}
283283

284+
//===----------------------------------------------------------------------===//
285+
// MinSOp
286+
//===----------------------------------------------------------------------===//
287+
288+
def Index_MinSOp : IndexBinaryOp<"mins"> {
289+
let summary = "index signed minimum";
290+
let description = [{
291+
The `index.mins` operation takes two index values and computes their signed
292+
minimum value. Treats the leading bit as the sign, i.e. `min(-2, 6) = -2`.
293+
294+
Example:
295+
296+
```mlir
297+
// c = min(a, b)
298+
%c = index.mins %a, %b
299+
```
300+
}];
301+
}
302+
303+
//===----------------------------------------------------------------------===//
304+
// MinUOp
305+
//===----------------------------------------------------------------------===//
306+
307+
def Index_MinUOp : IndexBinaryOp<"minu"> {
308+
let summary = "index unsigned minimum";
309+
let description = [{
310+
The `index.minu` operation takes two index values and computes their
311+
unsigned minimum value. Treats the leading bit as the most significant, i.e.
312+
`min(15, 6) = 6` or `min(-2, 6) = 6`.
313+
314+
Example:
315+
316+
```mlir
317+
// c = min(a, b)
318+
%c = index.minu %a, %b
319+
```
320+
}];
321+
}
322+
284323
//===----------------------------------------------------------------------===//
285324
// ShlOp
286325
//===----------------------------------------------------------------------===//

mlir/lib/Conversion/IndexToLLVM/IndexToLLVM.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ using ConvertIndexMaxS =
268268
mlir::OneToOneConvertToLLVMPattern<MaxSOp, LLVM::SMaxOp>;
269269
using ConvertIndexMaxU =
270270
mlir::OneToOneConvertToLLVMPattern<MaxUOp, LLVM::UMaxOp>;
271+
using ConvertIndexMinS =
272+
mlir::OneToOneConvertToLLVMPattern<MinSOp, LLVM::SMinOp>;
273+
using ConvertIndexMinU =
274+
mlir::OneToOneConvertToLLVMPattern<MinUOp, LLVM::UMinOp>;
271275
using ConvertIndexShl = mlir::OneToOneConvertToLLVMPattern<ShlOp, LLVM::ShlOp>;
272276
using ConvertIndexShrS =
273277
mlir::OneToOneConvertToLLVMPattern<ShrSOp, LLVM::AShrOp>;
@@ -298,6 +302,8 @@ void index::populateIndexToLLVMConversionPatterns(
298302
ConvertIndexRemU,
299303
ConvertIndexMaxS,
300304
ConvertIndexMaxU,
305+
ConvertIndexMinS,
306+
ConvertIndexMinU,
301307
ConvertIndexShl,
302308
ConvertIndexShrS,
303309
ConvertIndexShrU,

mlir/lib/Dialect/Index/IR/IndexOps.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ OpFoldResult MaxUOp::fold(ArrayRef<Attribute> operands) {
286286
});
287287
}
288288

289+
//===----------------------------------------------------------------------===//
290+
// MinSOp
291+
//===----------------------------------------------------------------------===//
292+
293+
OpFoldResult MinSOp::fold(ArrayRef<Attribute> operands) {
294+
return foldBinaryOpChecked(operands, [](const APInt &lhs, const APInt &rhs) {
295+
return lhs.slt(rhs) ? lhs : rhs;
296+
});
297+
}
298+
299+
//===----------------------------------------------------------------------===//
300+
// MinUOp
301+
//===----------------------------------------------------------------------===//
302+
303+
OpFoldResult MinUOp::fold(ArrayRef<Attribute> operands) {
304+
return foldBinaryOpChecked(operands, [](const APInt &lhs, const APInt &rhs) {
305+
return lhs.ult(rhs) ? lhs : rhs;
306+
});
307+
}
308+
289309
//===----------------------------------------------------------------------===//
290310
// ShlOp
291311
//===----------------------------------------------------------------------===//

mlir/test/Conversion/IndexToLLVM/index-to-llvm.mlir

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,24 @@ func.func @trivial_ops(%a: index, %b: index) {
2222
%7 = index.maxs %a, %b
2323
// CHECK: llvm.intr.umax
2424
%8 = index.maxu %a, %b
25+
// CHECK: llvm.intr.smin
26+
%9 = index.mins %a, %b
27+
// CHECK: llvm.intr.umin
28+
%10 = index.minu %a, %b
2529
// CHECK: llvm.shl
26-
%9 = index.shl %a, %b
30+
%11 = index.shl %a, %b
2731
// CHECK: llvm.ashr
28-
%10 = index.shrs %a, %b
32+
%12 = index.shrs %a, %b
2933
// CHECK: llvm.lshr
30-
%11 = index.shru %a, %b
34+
%13 = index.shru %a, %b
3135
// CHECK: llvm.add
32-
%12 = index.add %a, %b
36+
%14 = index.add %a, %b
3337
// CHECK: llvm.or
34-
%13 = index.or %a, %b
38+
%15 = index.or %a, %b
3539
// CHECK: llvm.xor
36-
%14 = index.xor %a, %b
40+
%16 = index.xor %a, %b
3741
// CHECK: llvm.mlir.constant(true
38-
%15 = index.bool.constant true
42+
%17 = index.bool.constant true
3943
return
4044
}
4145

mlir/test/Dialect/Index/index-canonicalize.mlir

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,46 @@ func.func @maxu() -> index {
279279
return %0 : index
280280
}
281281

282+
// CHECK-LABEL: @mins
283+
func.func @mins() -> index {
284+
%lhs = index.constant -4
285+
%rhs = index.constant 2
286+
// CHECK: %[[A:.*]] = index.constant -4
287+
%0 = index.mins %lhs, %rhs
288+
// CHECK: return %[[A]]
289+
return %0 : index
290+
}
291+
292+
// CHECK-LABEL: @mins_nofold
293+
func.func @mins_nofold() -> index {
294+
%lhs = index.constant 1
295+
%rhs = index.constant 0x100000000
296+
// 32-bit result differs from 64-bit.
297+
// CHECK: index.mins
298+
%0 = index.mins %lhs, %rhs
299+
return %0 : index
300+
}
301+
302+
// CHECK-LABEL: @mins_nofold_2
303+
func.func @mins_nofold_2() -> index {
304+
%lhs = index.constant 0x7fffffff
305+
%rhs = index.constant 0x80000000
306+
// 32-bit result differs from 64-bit.
307+
// CHECK: index.mins
308+
%0 = index.mins %lhs, %rhs
309+
return %0 : index
310+
}
311+
312+
// CHECK-LABEL: @minu
313+
func.func @minu() -> index {
314+
%lhs = index.constant -1
315+
%rhs = index.constant 1
316+
// CHECK: %[[A:.*]] = index.constant 1
317+
%0 = index.minu %lhs, %rhs
318+
// CHECK: return %[[A]]
319+
return %0 : index
320+
}
321+
282322
// CHECK-LABEL: @shl
283323
func.func @shl() -> index {
284324
%lhs = index.constant 128

mlir/test/Dialect/Index/index-ops.mlir

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ func.func @binary_ops(%a: index, %b: index) {
2727
%10 = index.maxs %a, %b
2828
// CHECK-NEXT: index.maxu %[[A]], %[[B]]
2929
%11 = index.maxu %a, %b
30+
// CHECK-NEXT: index.mins %[[A]], %[[B]]
31+
%12 = index.mins %a, %b
32+
// CHECK-NEXT: index.minu %[[A]], %[[B]]
33+
%13 = index.minu %a, %b
3034
// CHECK-NEXT: index.shl %[[A]], %[[B]]
31-
%12 = index.shl %a, %b
35+
%14 = index.shl %a, %b
3236
// CHECK-NEXT: index.shrs %[[A]], %[[B]]
33-
%13 = index.shrs %a, %b
37+
%15 = index.shrs %a, %b
3438
// CHECK-NEXT: index.shru %[[A]], %[[B]]
35-
%14 = index.shru %a, %b
39+
%16 = index.shru %a, %b
3640
// CHECK-NEXT: index.and %[[A]], %[[B]]
37-
%15 = index.and %a, %b
41+
%17 = index.and %a, %b
3842
// CHECK-NEXT: index.or %[[A]], %[[B]]
39-
%16 = index.or %a, %b
43+
%18 = index.or %a, %b
4044
// CHECK-NEXT: index.xor %[[A]], %[[B]]
41-
%17 = index.xor %a, %b
45+
%19 = index.xor %a, %b
4246
return
4347
}
4448

0 commit comments

Comments
 (0)