Skip to content

Commit 92a15dd

Browse files
authored
[mlir][LLVM] Plumb range attributes on parameters and results through (#117801)
We've had the ability to define LLVM's `range` attribute through #llvm.constant_range for some time, and have used this for some GPU intrinsics. This commit allows using `llvm.range` as a parameter or result attribute on function declarations and definitions.
1 parent 1bc9de2 commit 92a15dd

File tree

6 files changed

+32
-3
lines changed

6 files changed

+32
-3
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def LLVM_Dialect : Dialect {
5252
static StringRef getNoFreeAttrName() { return "llvm.nofree"; }
5353
static StringRef getNonNullAttrName() { return "llvm.nonnull"; }
5454
static StringRef getPreallocatedAttrName() { return "llvm.preallocated"; }
55+
static StringRef getRangeAttrName() { return "llvm.range"; }
5556
static StringRef getReadonlyAttrName() { return "llvm.readonly"; }
5657
static StringRef getReturnedAttrName() { return "llvm.returned"; }
5758
static StringRef getSExtAttrName() { return "llvm.signext"; }

mlir/lib/Target/LLVMIR/AttrKindDetail.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ getAttrKindToNameMapping() {
4545
{llvm::Attribute::AttrKind::NonNull, LLVMDialect::getNonNullAttrName()},
4646
{llvm::Attribute::AttrKind::Preallocated,
4747
LLVMDialect::getPreallocatedAttrName()},
48+
{llvm::Attribute::AttrKind::Range, LLVMDialect::getRangeAttrName()},
4849
{llvm::Attribute::AttrKind::ReadOnly, LLVMDialect::getReadonlyAttrName()},
4950
{llvm::Attribute::AttrKind::ReadNone, LLVMDialect::getReadnoneAttrName()},
5051
{llvm::Attribute::AttrKind::Returned, LLVMDialect::getReturnedAttrName()},

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,11 @@ ModuleImport::convertParameterAttribute(llvm::AttributeSet llvmParamAttrs,
20222022
mlirAttr = builder.getI64IntegerAttr(llvmAttr.getValueAsInt());
20232023
else if (llvmAttr.isEnumAttribute())
20242024
mlirAttr = builder.getUnitAttr();
2025-
else
2025+
else if (llvmAttr.isConstantRangeAttribute()) {
2026+
const llvm::ConstantRange &value = llvmAttr.getValueAsConstantRange();
2027+
mlirAttr = builder.getAttr<LLVM::ConstantRangeAttr>(value.getLower(),
2028+
value.getUpper());
2029+
} else
20262030
llvm_unreachable("unexpected parameter attribute kind");
20272031
paramAttrs.push_back(builder.getNamedAttr(mlirName, mlirAttr));
20282032
}

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,12 @@ ModuleTranslation::convertParameterAttrs(LLVMFuncOp func, int argIdx,
15811581
.Case<IntegerAttr>([&](auto intAttr) {
15821582
attrBuilder.addRawIntAttr(llvmKind, intAttr.getInt());
15831583
})
1584-
.Case<UnitAttr>([&](auto) { attrBuilder.addAttribute(llvmKind); });
1584+
.Case<UnitAttr>([&](auto) { attrBuilder.addAttribute(llvmKind); })
1585+
.Case<LLVM::ConstantRangeAttr>([&](auto rangeAttr) {
1586+
attrBuilder.addConstantRangeAttr(
1587+
llvmKind, llvm::ConstantRange(rangeAttr.getLower(),
1588+
rangeAttr.getUpper()));
1589+
});
15851590
} else if (namedAttr.getNameDialect()) {
15861591
if (failed(iface.convertParameterAttr(func, argIdx, namedAttr, *this)))
15871592
return failure();

mlir/test/Target/LLVMIR/Import/function-attributes.ll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ attributes #0 = { readnone }
4545
; CHECK-SAME: !llvm.ptr {llvm.returned}
4646
; CHECK-SAME: !llvm.ptr {llvm.alignstack = 32 : i64}
4747
; CHECK-SAME: !llvm.ptr {llvm.writeonly}
48+
; CHECK-SAME: i64 {llvm.range = #llvm.constant_range<i64, 0, 4097>}
4849
define ptr @func_arg_attrs(
4950
ptr byval(i64) %arg0,
5051
ptr byref(i64) %arg1,
@@ -63,7 +64,8 @@ define ptr @func_arg_attrs(
6364
ptr preallocated(double) %arg16,
6465
ptr returned %arg17,
6566
ptr alignstack(32) %arg18,
66-
ptr writeonly %arg19) {
67+
ptr writeonly %arg19,
68+
i64 range(i64 0, 4097) %arg20) {
6769
ret ptr %arg17
6870
}
6971

@@ -141,6 +143,12 @@ declare inreg ptr @func_res_attr_inreg()
141143

142144
; // -----
143145

146+
; CHECK-LABEL: @func_res_attr_range
147+
; CHECK-SAME: (i64 {llvm.range = #llvm.constant_range<i64, 0, 4097>})
148+
declare range(i64 0, 4097) i64 @func_res_attr_range()
149+
150+
; // -----
151+
144152
; CHECK-LABEL: @entry_count
145153
; CHECK-SAME: attributes {function_entry_count = 4242 : i64}
146154
define void @entry_count() !prof !1 {

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,6 +2328,16 @@ llvm.func @vararg_function(%arg0: i32, ...) -> i32 {
23282328

23292329
// -----
23302330

2331+
// CHECK: declare void @range_arg_function(i64 range(i64 0, 4097))
2332+
llvm.func @range_arg_function(%arg0: i64 {llvm.range = #llvm.constant_range<i64, 0, 4097>})
2333+
2334+
// -----
2335+
2336+
// CHECK: declare range(i64 0, 4097) i64 @range_res_function()
2337+
llvm.func @range_res_function() -> (i64 {llvm.range = #llvm.constant_range<i64, 0, 4097>})
2338+
2339+
// -----
2340+
23312341
// CHECK: declare void @readonly_function([[PTR:.+]] readonly)
23322342
llvm.func @readonly_function(%arg0: !llvm.ptr {llvm.readonly})
23332343

0 commit comments

Comments
 (0)