Skip to content

Commit 892605b

Browse files
committed
[mlir][Asm] Add support for using an alias for trailing operation locations
Locations often get very long and clutter up operations when printed inline with them. This revision adds support for using aliases with trailing operation locations, and makes printing with aliases the default behavior. Aliases in the trailing location take the form `loc(<alias>)`, such as `loc(#loc0)`. As with all aliases, using `mlir-print-local-scope` can be used to disable them and get the inline behavior. Differential Revision: https://reviews.llvm.org/D90652
1 parent ebcc022 commit 892605b

16 files changed

+91
-61
lines changed

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ class DummyAliasOperationPrinter : private OpAsmPrinter {
289289

290290
/// Print the given operation.
291291
void print(Operation *op) {
292-
// TODO: Consider the operation location for an alias.
292+
// Visit the operation location.
293+
if (printerFlags.shouldPrintDebugInfo())
294+
initializer.visit(op->getLoc());
293295

294296
// If requested, always print the generic form.
295297
if (!printerFlags.shouldPrintGenericOpForm()) {
@@ -1089,7 +1091,10 @@ class ModulePrinter {
10891091
AttrTypeElision typeElision = AttrTypeElision::Never);
10901092

10911093
void printType(Type type);
1092-
void printLocation(LocationAttr loc);
1094+
1095+
/// Print the given location to the stream. If `allowAlias` is true, this
1096+
/// allows for the internal location to use an attribute alias.
1097+
void printLocation(LocationAttr loc, bool allowAlias = false);
10931098

10941099
void printAffineMap(AffineMap map);
10951100
void
@@ -1152,7 +1157,7 @@ void ModulePrinter::printTrailingLocation(Location loc) {
11521157
return;
11531158

11541159
os << " ";
1155-
printLocation(loc);
1160+
printLocation(loc, /*allowAlias=*/true);
11561161
}
11571162

11581163
void ModulePrinter::printLocationInternal(LocationAttr loc, bool pretty) {
@@ -1269,14 +1274,14 @@ static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
12691274
os << str;
12701275
}
12711276

1272-
void ModulePrinter::printLocation(LocationAttr loc) {
1273-
if (printerFlags.shouldPrintDebugInfoPrettyForm()) {
1274-
printLocationInternal(loc, /*pretty=*/true);
1275-
} else {
1276-
os << "loc(";
1277+
void ModulePrinter::printLocation(LocationAttr loc, bool allowAlias) {
1278+
if (printerFlags.shouldPrintDebugInfoPrettyForm())
1279+
return printLocationInternal(loc, /*pretty=*/true);
1280+
1281+
os << "loc(";
1282+
if (!allowAlias || !state || failed(state->getAliasState().getAlias(loc, os)))
12771283
printLocationInternal(loc);
1278-
os << ')';
1279-
}
1284+
os << ')';
12801285
}
12811286

12821287
/// Returns true if the given dialect symbol data is simple enough to print in

mlir/lib/IR/MLIRContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ struct BuiltinOpAsmDialectInterface : public OpAsmDialectInterface {
9999
os << "set";
100100
return success();
101101
}
102+
if (attr.isa<LocationAttr>()) {
103+
os << "loc";
104+
return success();
105+
}
102106
return failure();
103107
}
104108
};

mlir/lib/Parser/AttributeParser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,14 @@ Attribute Parser::parseAttribute(Type type) {
121121

122122
// Parse a location attribute.
123123
case Token::kw_loc: {
124-
LocationAttr attr;
125-
return failed(parseLocation(attr)) ? Attribute() : attr;
124+
consumeToken(Token::kw_loc);
125+
126+
LocationAttr locAttr;
127+
if (parseToken(Token::l_paren, "expected '(' in inline location") ||
128+
parseLocationInstance(locAttr) ||
129+
parseToken(Token::r_paren, "expected ')' in inline location"))
130+
return Attribute();
131+
return locAttr;
126132
}
127133

128134
// Parse an opaque elements attribute.

mlir/lib/Parser/LocationParser.cpp

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@
1111
using namespace mlir;
1212
using namespace mlir::detail;
1313

14-
/// Parse a location.
15-
///
16-
/// location ::= `loc` inline-location
17-
/// inline-location ::= '(' location-inst ')'
18-
///
19-
ParseResult Parser::parseLocation(LocationAttr &loc) {
20-
// Check for 'loc' identifier.
21-
if (parseToken(Token::kw_loc, "expected 'loc' keyword"))
22-
return emitError();
23-
24-
// Parse the inline-location.
25-
if (parseToken(Token::l_paren, "expected '(' in inline location") ||
26-
parseLocationInstance(loc) ||
27-
parseToken(Token::r_paren, "expected ')' in inline location"))
28-
return failure();
29-
return success();
30-
}
31-
3214
/// Specific location instances.
3315
///
3416
/// location-inst ::= filelinecol-location |
@@ -195,3 +177,35 @@ ParseResult Parser::parseLocationInstance(LocationAttr &loc) {
195177

196178
return emitError("expected location instance");
197179
}
180+
181+
ParseResult Parser::parseOptionalTrailingLocation(Location &loc) {
182+
// If there is a 'loc' we parse a trailing location.
183+
if (!consumeIf(Token::kw_loc))
184+
return success();
185+
if (parseToken(Token::l_paren, "expected '(' in location"))
186+
return failure();
187+
Token tok = getToken();
188+
189+
// Check to see if we are parsing a location alias.
190+
LocationAttr directLoc;
191+
if (tok.is(Token::hash_identifier)) {
192+
// TODO: This should be reworked a bit to allow for resolving operation
193+
// locations to aliases after the operation has already been parsed(i.e.
194+
// allow post parse location fixups).
195+
Attribute attr = parseExtendedAttr(Type());
196+
if (!attr)
197+
return failure();
198+
if (!(directLoc = attr.dyn_cast<LocationAttr>()))
199+
return emitError(tok.getLoc()) << "expected location, but found " << attr;
200+
201+
// Otherwise, we parse the location directly.
202+
} else if (parseLocationInstance(directLoc)) {
203+
return failure();
204+
}
205+
206+
if (parseToken(Token::r_paren, "expected ')' in location"))
207+
return failure();
208+
209+
loc = directLoc;
210+
return success();
211+
}

mlir/lib/Parser/Parser.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ class Parser {
231231
// Location Parsing
232232
//===--------------------------------------------------------------------===//
233233

234-
/// Parse an inline location.
235-
ParseResult parseLocation(LocationAttr &loc);
236-
237234
/// Parse a raw location instance.
238235
ParseResult parseLocationInstance(LocationAttr &loc);
239236

@@ -248,20 +245,9 @@ class Parser {
248245

249246
/// Parse an optional trailing location.
250247
///
251-
/// trailing-location ::= (`loc` `(` location `)`)?
248+
/// trailing-location ::= (`loc` (`(` location `)` | attribute-alias))?
252249
///
253-
ParseResult parseOptionalTrailingLocation(Location &loc) {
254-
// If there is a 'loc' we parse a trailing location.
255-
if (!getToken().is(Token::kw_loc))
256-
return success();
257-
258-
// Parse the location.
259-
LocationAttr directLoc;
260-
if (parseLocation(directLoc))
261-
return failure();
262-
loc = directLoc;
263-
return success();
264-
}
250+
ParseResult parseOptionalTrailingLocation(Location &loc);
265251

266252
//===--------------------------------------------------------------------===//
267253
// Affine Parsing

mlir/test/Dialect/SPIRV/Serialization/debug.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-translate -test-spirv-roundtrip-debug -mlir-print-debuginfo %s | FileCheck %s
1+
// RUN: mlir-translate -test-spirv-roundtrip-debug -mlir-print-debuginfo -mlir-print-local-scope %s | FileCheck %s
22

33
spv.module Logical GLSL450 requires #spv.vce<v1.0, [Shader], []> {
44
// CHECK: loc({{".*debug.mlir"}}:5:3)

mlir/test/IR/invalid-locations.mlir

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
func @location_missing_l_paren() {
66
^bb:
7-
return loc) // expected-error {{expected '(' in inline location}}
7+
return loc) // expected-error {{expected '(' in location}}
88
}
99

1010
// -----
1111

1212
func @location_missing_r_paren() {
1313
^bb:
14-
return loc(unknown // expected-error@+1 {{expected ')' in inline location}}
14+
return loc(unknown // expected-error@+1 {{expected ')' in location}}
1515
}
1616

1717
// -----
@@ -98,3 +98,11 @@ func @location_fused_missing_r_square() {
9898
^bb:
9999
return loc(fused[unknown) // expected-error {{expected ']' in fused location}}
100100
}
101+
102+
// -----
103+
104+
func @location_invalid_alias() {
105+
// expected-error@+1 {{expected location, but found #foo.loc}}
106+
return loc(#foo.loc)
107+
}
108+

mlir/test/IR/locations.mlir

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: mlir-opt -allow-unregistered-dialect %s -mlir-print-debuginfo | FileCheck %s
1+
// RUN: mlir-opt -allow-unregistered-dialect %s -mlir-print-debuginfo -mlir-print-local-scope | FileCheck %s
2+
// RUN: mlir-opt -allow-unregistered-dialect %s -mlir-print-debuginfo | FileCheck %s --check-prefix=CHECK-ALIAS
23
// This test verifies that debug locations are round-trippable.
34

45
#set0 = affine_set<(d0) : (1 == 0)>
@@ -22,3 +23,12 @@ func @inline_notation() -> i32 {
2223
// CHECK: return %0 : i32 loc(unknown)
2324
return %1 : i32 loc(unknown)
2425
}
26+
27+
// CHECK-LABEL: func @loc_attr(i1 {foo.loc_attr = loc(callsite("foo" at "mysource.cc":10:8))})
28+
func @loc_attr(i1 {foo.loc_attr = loc(callsite("foo" at "mysource.cc":10:8))})
29+
30+
// CHECK-ALIAS: #[[LOC:.*]] = loc("out_of_line_location")
31+
#loc = loc("out_of_line_location")
32+
33+
// CHECK-ALIAS: "foo.op"() : () -> () loc(#[[LOC]])
34+
"foo.op"() : () -> () loc(#loc)

mlir/test/IR/module-op.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt -allow-unregistered-dialect %s -split-input-file -mlir-print-debuginfo | FileCheck %s
1+
// RUN: mlir-opt -allow-unregistered-dialect %s -split-input-file -mlir-print-debuginfo -mlir-print-local-scope | FileCheck %s
22

33
// CHECK: module {
44
module {

mlir/test/IR/opaque_locations.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt -allow-unregistered-dialect %s -test-opaque-loc -mlir-print-debuginfo | FileCheck %s
1+
// RUN: mlir-opt -allow-unregistered-dialect %s -test-opaque-loc -mlir-print-debuginfo -mlir-print-local-scope | FileCheck %s
22
// This test verifies that debug opaque locations can be printed.
33

44
#set0 = affine_set<(d0) : (1 == 0)>

mlir/test/IR/parser.mlir

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,9 +1002,6 @@ func @scoped_names() {
10021002
return
10031003
}
10041004

1005-
// CHECK-LABEL: func @loc_attr(i1 {foo.loc_attr = loc(callsite("foo" at "mysource.cc":10:8))})
1006-
func @loc_attr(i1 {foo.loc_attr = loc(callsite("foo" at "mysource.cc":10:8))})
1007-
10081005
// CHECK-LABEL: func @dialect_attribute_with_type
10091006
func @dialect_attribute_with_type() {
10101007
// CHECK-NEXT: foo = #foo.attr : i32

mlir/test/IR/wrapping_op.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: mlir-opt -allow-unregistered-dialect %s | FileCheck %s
2-
// RUN: mlir-opt -allow-unregistered-dialect -mlir-print-op-generic -mlir-print-debuginfo %s | FileCheck %s --check-prefix=CHECK-GENERIC
2+
// RUN: mlir-opt -allow-unregistered-dialect -mlir-print-op-generic -mlir-print-debuginfo -mlir-print-local-scope %s | FileCheck %s --check-prefix=CHECK-GENERIC
33

44
// CHECK-LABEL: func @wrapping_op
55
// CHECK-GENERIC: "func"

mlir/test/Transforms/inlining.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: mlir-opt %s -inline="disable-simplify" | FileCheck %s
2-
// RUN: mlir-opt %s -inline="disable-simplify" -mlir-print-debuginfo | FileCheck %s --check-prefix INLINE-LOC
2+
// RUN: mlir-opt %s -inline="disable-simplify" -mlir-print-debuginfo -mlir-print-local-scope | FileCheck %s --check-prefix INLINE-LOC
33
// RUN: mlir-opt %s -inline | FileCheck %s --check-prefix INLINE_SIMPLIFY
44

55
// Inline a function that takes an argument.

mlir/test/Transforms/location-snapshot.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t' -mlir-print-debuginfo %s | FileCheck %s -DFILE=%/t
2-
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t tag='tagged'' -mlir-print-debuginfo %s | FileCheck %s --check-prefix=TAG -DFILE=%/t
1+
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t' -mlir-print-local-scope -mlir-print-debuginfo %s | FileCheck %s -DFILE=%/t
2+
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t tag='tagged'' -mlir-print-local-scope -mlir-print-debuginfo %s | FileCheck %s --check-prefix=TAG -DFILE=%/t
33

44
// CHECK: func @function(
55
// CHECK-NEXT: loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})

mlir/test/Transforms/strip-debuginfo.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt -allow-unregistered-dialect %s -mlir-print-debuginfo -strip-debuginfo | FileCheck %s
1+
// RUN: mlir-opt -allow-unregistered-dialect %s -mlir-print-debuginfo -mlir-print-local-scope -strip-debuginfo | FileCheck %s
22
// This test verifies that debug locations are stripped.
33

44
#set0 = affine_set<(d0) : (1 == 0)>

mlir/test/mlir-tblgen/pattern.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt -test-patterns -mlir-print-debuginfo %s | FileCheck %s
1+
// RUN: mlir-opt -test-patterns -mlir-print-debuginfo -mlir-print-local-scope %s | FileCheck %s
22

33
// CHECK-LABEL: verifyFusedLocs
44
func @verifyFusedLocs(%arg0 : i32) -> i32 {

0 commit comments

Comments
 (0)