From 58e2cd8ed23432b9022f8fae720ce73f7ad2b3df Mon Sep 17 00:00:00 2001 From: tn Date: Wed, 12 Nov 2025 14:44:29 +0100 Subject: [PATCH 1/2] [MLIR Attr] Allow LocationAttr to be used as an operation attribute --- mlir/include/mlir/IR/CommonAttrConstraints.td | 7 ++++++- mlir/test/IR/locations.mlir | 7 +++++++ mlir/test/lib/Dialect/Test/TestOps.td | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mlir/include/mlir/IR/CommonAttrConstraints.td b/mlir/include/mlir/IR/CommonAttrConstraints.td index b7e168a3e6f86..8ac1a2ea21422 100644 --- a/mlir/include/mlir/IR/CommonAttrConstraints.td +++ b/mlir/include/mlir/IR/CommonAttrConstraints.td @@ -188,7 +188,12 @@ class AnyAttrOf allowedAttrs, string summary = "", } def LocationAttr : Attr($_self)">, - "location attribute">; + "location attribute"> { + let storageType = [{ ::mlir::LocationAttr }]; + let returnType = [{ ::mlir::Location }]; + let convertFromStorage = "::mlir::Location($_self)"; + let constBuilderCall = "(::mlir::LocationAttr)$0"; +} def BoolAttr : Attr($_self)">, "bool attribute"> { let storageType = [{ ::mlir::BoolAttr }]; diff --git a/mlir/test/IR/locations.mlir b/mlir/test/IR/locations.mlir index b725307b420b7..20d9889308302 100644 --- a/mlir/test/IR/locations.mlir +++ b/mlir/test/IR/locations.mlir @@ -105,3 +105,10 @@ func.func @dialect_location() { test.attr_with_loc("dialectLoc" loc(#test.custom_location<"foo.mlir"*32>)) return } + +// CHECK-LABEL: @location_attr +// CHECK: test.op_with_loc_attr loc("cheetos":10:20) +func.func @location_attr() { + test.op_with_loc_attr loc("cheetos":10:20) + return +} diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td index cae0083f728e0..275025978a784 100644 --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -1192,6 +1192,12 @@ def TestLocationDstNoResOp : TEST_Op<"loc_dst_no_res"> { let results = (outs); } +def TestLocationAttrOp : TEST_Op<"op_with_loc_attr"> { + let arguments = (ins LocationAttr:$loc_attr); + let results = (outs ); + let assemblyFormat = "$loc_attr attr-dict"; +} + //===----------------------------------------------------------------------===// // Test Patterns //===----------------------------------------------------------------------===// From 8d5b88fc5aaa60e6175e155b1d698c727c6be9b8 Mon Sep 17 00:00:00 2001 From: tn Date: Thu, 13 Nov 2025 10:43:03 +0100 Subject: [PATCH 2/2] Add discardable loc attr and check for builtin source loc --- mlir/test/IR/locations.mlir | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/test/IR/locations.mlir b/mlir/test/IR/locations.mlir index 20d9889308302..2e2374629f0f9 100644 --- a/mlir/test/IR/locations.mlir +++ b/mlir/test/IR/locations.mlir @@ -107,8 +107,8 @@ func.func @dialect_location() { } // CHECK-LABEL: @location_attr -// CHECK: test.op_with_loc_attr loc("cheetos":10:20) +// CHECK: test.op_with_loc_attr loc("loc1":10:20) {foo.discardable_loc_attr = loc("loc2":20:30)} loc({{.*}}locations.mlir":[[# @LINE+2]]:3) func.func @location_attr() { - test.op_with_loc_attr loc("cheetos":10:20) + test.op_with_loc_attr loc("loc1":10:20) {foo.discardable_loc_attr = loc("loc2":20:30)} return }