Skip to content

Commit be2f875

Browse files
authored
[MLIR Attr] Allow LocationAttr to be used as an operation attribute (#167690)
Enables locations to be used as operation attributes. In contrast to the implicit source location every operation carries (`Operation::getLoc()`)—which may be fused or modified during transformations—a `LocationAttr` used as an operation attribute has explicit semantics defined by the operation itself. For example, in our Zig-like language frontend (where types are first-class values), we use a location attribute on struct type operations to store the declaration location, which is part of the type's semantic identity. Using an explicit attribute instead of `Operation::getLoc()` ensures this semantic information is preserved during transformations.
1 parent edd8b29 commit be2f875

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

mlir/include/mlir/IR/CommonAttrConstraints.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ class AnyAttrOf<list<Attr> allowedAttrs, string summary = "",
188188
}
189189

190190
def LocationAttr : Attr<CPred<"::llvm::isa<::mlir::LocationAttr>($_self)">,
191-
"location attribute">;
191+
"location attribute"> {
192+
let storageType = [{ ::mlir::LocationAttr }];
193+
let returnType = [{ ::mlir::Location }];
194+
let convertFromStorage = "::mlir::Location($_self)";
195+
let constBuilderCall = "(::mlir::LocationAttr)$0";
196+
}
192197

193198
def BoolAttr : Attr<CPred<"::llvm::isa<::mlir::BoolAttr>($_self)">, "bool attribute"> {
194199
let storageType = [{ ::mlir::BoolAttr }];

mlir/test/IR/locations.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,10 @@ func.func @dialect_location() {
105105
test.attr_with_loc("dialectLoc" loc(#test.custom_location<"foo.mlir"*32>))
106106
return
107107
}
108+
109+
// CHECK-LABEL: @location_attr
110+
// CHECK: test.op_with_loc_attr loc("loc1":10:20) {foo.discardable_loc_attr = loc("loc2":20:30)} loc({{.*}}locations.mlir":[[# @LINE+2]]:3)
111+
func.func @location_attr() {
112+
test.op_with_loc_attr loc("loc1":10:20) {foo.discardable_loc_attr = loc("loc2":20:30)}
113+
return
114+
}

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,12 @@ def TestLocationDstNoResOp : TEST_Op<"loc_dst_no_res"> {
11921192
let results = (outs);
11931193
}
11941194

1195+
def TestLocationAttrOp : TEST_Op<"op_with_loc_attr"> {
1196+
let arguments = (ins LocationAttr:$loc_attr);
1197+
let results = (outs );
1198+
let assemblyFormat = "$loc_attr attr-dict";
1199+
}
1200+
11951201
//===----------------------------------------------------------------------===//
11961202
// Test Patterns
11971203
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)