Skip to content

Commit 9a1bb3e

Browse files
committed
[MLIR][LLVM] Fix #llvm.constant_range crashing in storage uniquer
This PR adds the bitwidth parameter to the constant range to allow for comparing of two instances of constant range. This fixes a crash in storage uniquer when two ranges with different bitwidths hashed to the same value and then the comparison triggered an assert in APInt because of the different bitwidths.
1 parent 2fe123a commit 9a1bb3e

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ def LLVM_TBAATagArrayAttr
10951095
//===----------------------------------------------------------------------===//
10961096
def LLVM_ConstantRangeAttr : LLVM_Attr<"ConstantRange", "constant_range"> {
10971097
let parameters = (ins
1098+
"uint32_t":$width,
10981099
"::llvm::APInt":$lower,
10991100
"::llvm::APInt":$upper
11001101
);
@@ -1110,13 +1111,16 @@ def LLVM_ConstantRangeAttr : LLVM_Attr<"ConstantRange", "constant_range"> {
11101111

11111112
Syntax:
11121113
```
1113-
`<` `i`(width($lower)) $lower `,` $upper `>`
1114+
`<` `i`(width) $lower `,` $upper `>`
11141115
```
11151116
}];
11161117

11171118
let builders = [
11181119
AttrBuilder<(ins "uint32_t":$bitWidth, "int64_t":$lower, "int64_t":$upper), [{
1119-
return $_get($_ctxt, ::llvm::APInt(bitWidth, lower), ::llvm::APInt(bitWidth, upper));
1120+
return $_get($_ctxt, bitWidth, ::llvm::APInt(bitWidth, lower), ::llvm::APInt(bitWidth, upper));
1121+
}]>,
1122+
AttrBuilder<(ins "::llvm::APInt":$lower, "::llvm::APInt":$upper), [{
1123+
return $_get($_ctxt, lower.getBitWidth(), lower, upper);
11201124
}]>
11211125
];
11221126

mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,18 @@ Attribute ConstantRangeAttr::parse(AsmParser &parser, Type odsType) {
278278
}
279279

280280
void ConstantRangeAttr::print(AsmPrinter &printer) const {
281-
printer << "<i" << getLower().getBitWidth() << ", " << getLower() << ", "
282-
<< getUpper() << ">";
281+
printer << "<i" << getWidth() << ", " << getLower() << ", " << getUpper()
282+
<< ">";
283283
}
284284

285285
LogicalResult
286286
ConstantRangeAttr::verify(llvm::function_ref<InFlightDiagnostic()> emitError,
287-
APInt lower, APInt upper) {
287+
uint32_t width, llvm::APInt lower,
288+
llvm::APInt upper) {
289+
if (width != lower.getBitWidth())
290+
return emitError()
291+
<< "expected type and value to have matching bitwidths but got "
292+
<< width << " vs. " << lower.getBitWidth();
288293
if (lower.getBitWidth() != upper.getBitWidth())
289294
return emitError()
290295
<< "expected lower and upper to have matching bitwidths but got "
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)