Skip to content

Commit c9f72b2

Browse files
authored
[MLIR][LLVM] Fix #llvm.constant_range parsing (#123009)
When `APInt` parses negative numbers, it may extend the bit width. This patch ensures the bit width matches with the attribute. Fixes #122996.
1 parent 7c72941 commit c9f72b2

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,9 @@ Attribute ConstantRangeAttr::parse(AsmParser &parser, Type odsType) {
270270
if (parser.parseInteger(lower) || parser.parseComma() ||
271271
parser.parseInteger(upper) || parser.parseGreater())
272272
return Attribute{};
273-
// For some reason, 0 is always parsed as 64-bits, fix that if needed.
274-
if (lower.isZero())
275-
lower = lower.sextOrTrunc(bitWidth);
276-
if (upper.isZero())
277-
upper = upper.sextOrTrunc(bitWidth);
273+
// Non-positive numbers may use more bits than `bitWidth`
274+
lower = lower.sextOrTrunc(bitWidth);
275+
upper = upper.sextOrTrunc(bitWidth);
278276
return parser.getChecked<ConstantRangeAttr>(loc, parser.getContext(), lower,
279277
upper);
280278
}

mlir/test/Dialect/LLVMIR/func.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,9 @@ llvm.func @intel_reqd_sub_group_size_hint() attributes {llvm.intel_reqd_sub_grou
479479
// CHECK-SAME: llvm.workgroup_attribution = #llvm.mlir.workgroup_attribution<512 : i64, i32>
480480
// CHECK-SAME: llvm.workgroup_attribution = #llvm.mlir.workgroup_attribution<128 : i64, !llvm.struct<(i32, i64, f32)>
481481
llvm.func @workgroup_attribution(%arg0: !llvm.ptr {llvm.workgroup_attribution = #llvm.mlir.workgroup_attribution<512 : i64, i32>}, %arg1: !llvm.ptr {llvm.workgroup_attribution = #llvm.mlir.workgroup_attribution<128 : i64, !llvm.struct<(i32, i64, f32)>>})
482+
483+
// -----
484+
485+
// CHECK: @constant_range_negative
486+
// CHECK-SAME: llvm.range = #llvm.constant_range<i32, 0, -2147483648>
487+
llvm.func @constant_range_negative() -> (i32 {llvm.range = #llvm.constant_range<i32, 0, -2147483648>})

0 commit comments

Comments
 (0)