Skip to content

[MLIR][LLVM] Fix #llvm.constant_range crashing in storage uniquer #135772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,8 @@ def LLVM_TBAATagArrayAttr
// ConstantRangeAttr
//===----------------------------------------------------------------------===//
def LLVM_ConstantRangeAttr : LLVM_Attr<"ConstantRange", "constant_range"> {
let parameters = (ins
"::llvm::APInt":$lower,
"::llvm::APInt":$upper
let parameters = (ins APIntParameter<"">:$lower,
APIntParameter<"">:$upper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let parameters = (ins APIntParameter<"">:$lower,
APIntParameter<"">:$upper
let parameters = (ins
APIntParameter<"">:$lower,
APIntParameter<"">:$upper

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found only:

We discussed with @gysit in side channel that it might be worthwhile to add a check to tablegen to warn on APInt use and suggest APIntParameter instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One conceptual alternative to that I suppose, would be to detect APInt and use APIntParameter equivalent logic instead. Either way, would be nice to remove a footgun.

);
let summary = "A range of two integers, corresponding to LLVM's ConstantRange";
let description = [{
Expand All @@ -1110,7 +1109,7 @@ def LLVM_ConstantRangeAttr : LLVM_Attr<"ConstantRange", "constant_range"> {

Syntax:
```
`<` `i`(width($lower)) $lower `,` $upper `>`
`<` `i`(wdith($lower)), $lower `,` $upper `>`
```
}];

Expand Down
6 changes: 6 additions & 0 deletions mlir/include/mlir/IR/AttrTypeBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ class StringRefParameter<string desc = "", string value = ""> :
let defaultValue = value;
}

// For APInts, which require comparison over different bitwidths
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// For APInts, which require comparison over different bitwidths
// For APInts, which require comparison supporting different bitwidths. The default
// APInt comparison operator asserts when the bitwidths differ, so a custom
// implementation is necessary.

nit: Let's maybe expand a bit why this is necessary.

class APIntParameter<string desc> :
AttrOrTypeParameter<"::llvm::APInt", desc> {
let comparator = "$_lhs.getBitWidth() == $_rhs.getBitWidth() && $_lhs == $_rhs";
}

// For APFloats, which require comparison.
class APFloatParameter<string desc> :
AttrOrTypeParameter<"::llvm::APFloat", desc> {
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Dialect/LLVMIR/range-attr.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: mlir-opt %s -o - | FileCheck %s

// CHECK: #llvm.constant_range<i32, 0, 12>
llvm.func external @foo1(!llvm.ptr, i64) -> (i32 {llvm.range = #llvm.constant_range<i32, 0, 12>})
// CHECK: #llvm.constant_range<i8, 1, 10>
llvm.func external @foo2(!llvm.ptr, i64) -> (i8 {llvm.range = #llvm.constant_range<i8, 1, 10>})
// CHECK: #llvm.constant_range<i64, 0, 2147483648>
llvm.func external @foo3(!llvm.ptr, i64) -> (i64 {llvm.range = #llvm.constant_range<i64, 0, 2147483648>})
// CHECK: #llvm.constant_range<i32, 1, -2147483648>
llvm.func external @foo4(!llvm.ptr, i64) -> (i32 {llvm.range = #llvm.constant_range<i32, 1, -2147483648>})
Loading