Skip to content

Commit b9ce185

Browse files
authored
[MLIR][LLVM] Fix #llvm.constant_range crashing in storage uniquer (#135772)
Add APIntParameter with custom implementation for comparison and use it in llvm.constant_range attribute. This is necessary because the default equality operator of APInt asserts when the bit widths of the compared APInts differ. The comparison is used by StorageUniquer when hashes of two ranges with different bit widths collide.
1 parent 1f96aea commit b9ce185

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,8 @@ def LLVM_TBAATagArrayAttr
10951095
//===----------------------------------------------------------------------===//
10961096
def LLVM_ConstantRangeAttr : LLVM_Attr<"ConstantRange", "constant_range"> {
10971097
let parameters = (ins
1098-
"::llvm::APInt":$lower,
1099-
"::llvm::APInt":$upper
1098+
APIntParameter<"">:$lower,
1099+
APIntParameter<"">:$upper
11001100
);
11011101
let summary = "A range of two integers, corresponding to LLVM's ConstantRange";
11021102
let description = [{

mlir/include/mlir/IR/AttrTypeBase.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ class StringRefParameter<string desc = "", string value = ""> :
383383
let defaultValue = value;
384384
}
385385

386+
// For APInts, which require comparison supporting different bitwidths. The
387+
// default APInt comparison operator asserts when the bitwidths differ, so
388+
// a custom implementation is necessary.
389+
class APIntParameter<string desc> :
390+
AttrOrTypeParameter<"::llvm::APInt", desc> {
391+
let comparator = "$_lhs.getBitWidth() == $_rhs.getBitWidth() && $_lhs == $_rhs";
392+
}
393+
386394
// For APFloats, which require comparison.
387395
class APFloatParameter<string desc> :
388396
AttrOrTypeParameter<"::llvm::APFloat", desc> {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: mlir-opt %s -o - | FileCheck %s
2+
3+
// CHECK: #llvm.constant_range<i32, 0, 12>
4+
llvm.func external @foo1(!llvm.ptr, i64) -> (i32 {llvm.range = #llvm.constant_range<i32, 0, 12>})
5+
// CHECK: #llvm.constant_range<i8, 1, 10>
6+
llvm.func external @foo2(!llvm.ptr, i64) -> (i8 {llvm.range = #llvm.constant_range<i8, 1, 10>})
7+
// CHECK: #llvm.constant_range<i64, 0, 2147483648>
8+
llvm.func external @foo3(!llvm.ptr, i64) -> (i64 {llvm.range = #llvm.constant_range<i64, 0, 2147483648>})
9+
// CHECK: #llvm.constant_range<i32, 1, -2147483648>
10+
llvm.func external @foo4(!llvm.ptr, i64) -> (i32 {llvm.range = #llvm.constant_range<i32, 1, -2147483648>})

0 commit comments

Comments
 (0)