core: Fix location in func block arguments#5690
core: Fix location in func block arguments#5690ZahidWakeel-synthara wants to merge 10 commits intoxdslproject:mainfrom
Conversation
|
@superlopuh @JosseVanDelm @math-fehr Please take a look at this, thanks. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5690 +/- ##
==========================================
- Coverage 86.34% 86.34% -0.01%
==========================================
Files 406 406
Lines 57723 57744 +21
Branches 6706 6710 +4
==========================================
+ Hits 49843 49860 +17
- Misses 6322 6325 +3
- Partials 1558 1559 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
xdsl/dialects/utils/format.py
Outdated
| # If a location was parsed, reject attrs that appear after it, | ||
| # including empty dictionaries (e.g. `%arg: i32 loc(...) {}`). | ||
| if has_loc: | ||
| arg_attr_pos = parser.pos | ||
| parser.parse_optional_dictionary_attr_dict() | ||
| if parser.pos != arg_attr_pos: | ||
| parser.raise_error( | ||
| "Expected function argument attributes before location." | ||
| ) | ||
|
|
||
| # Reject duplicate locations (e.g. `%arg: i32 ... loc(...) loc(...)`). | ||
| if has_loc and parser.parse_optional_location() is not None: | ||
| parser.raise_error( | ||
| "Expected at most one location in function argument." | ||
| ) |
There was a problem hiding this comment.
Does MLIR have similar errors? I feel like this is a little overkill, as long as a parse error is raised for incorrect format that seems good enough to me, we can't realistically handle all combinations of wrong orders of elements of the IR.
There was a problem hiding this comment.
@superlopuh I think we can remove the duplicate location check, but should definitely keep the attr before location check as MLIR strictly enforces it.
There was a problem hiding this comment.
Sorry what I mean is that if this check is removed, it should still raise a parse error, right? Just with a different error message? For the same test.
There was a problem hiding this comment.
@superlopuh Yes right, but the existing error message is not super clear.
There was a problem hiding this comment.
Fair enough, let's keep the nicer message. I think we're safe testing it with just one test though, as opposed to the current empty and not-empty dictionary.
There was a problem hiding this comment.
Previously the empty dict test case was failing, so I had to take care of parser pos before & after parsing attr dict. Hence I added the empty dict test case.
3dfe34b to
dee68cf
Compare
JosseVanDelm
left a comment
There was a problem hiding this comment.
Awesome! For me this pull request is good to go, if you address the following points:
- Please add an extra check test to see if the location information is properly printed. You can do that within the current filecheck tests by using
--check-prefix=CHECKPREFIX, see the inline suggestion. - Also there are quite a few other users of the
print_func_op_like, adding tests for all of them is probably overkill, but could you please also add a test case (+interop test case) for at least onellvm.funcoperation?
Thank you so much!
Cheers!
| @@ -1,2 +1,2 @@ | |||
| // RUN: xdsl-opt --print-op-generic %s | mlir-opt --mlir-print-op-generic --allow-unregistered-dialect | xdsl-opt | filecheck %s | |||
| // RUN: xdsl-opt %s | mlir-opt --mlir-print-op-generic --allow-unregistered-dialect | xdsl-opt | filecheck %s | |||
There was a problem hiding this comment.
I would add an extra check here to actually check the print with debuginfo, something like:
// RUN: xdsl-opt %s --print-debuginfo | mlir-opt --mlir-print-op-generic --allow-unregistered-dialect | xdsl-opt | filecheck %s --check-prefix=DEBUGINFO
--check-prefix=DEBUGINFO allows you to add extra "check-like" lines like
// DEBUGINFO: check the debuginfo print here
// DEBUGINFO-NEXT: check the debuginfo print on the next line here
There was a problem hiding this comment.
@JosseVanDelm @superlopuh In the PR, currently locations attached with func block args are only parsed but not propagated, so applying xdsl-opt driver on such IR results in unknown locations.
builtin.module {
func.func private @decl_only(%arg0: i32 loc("decl.mlir":1:1))
func.func @with_body(%arg0: i32 loc("body.mlir":2:3)) {
func.return
}
}On running with xdsl-opt --print-debuginfo
builtin.module {
func.func private @decl_only(i32) -> ()
func.func @with_body(%arg0 : i32 loc(unknown)) {
func.return
}
}Function declarations behaviour is similar to how it behaves in MLIR, but function with body should preserve its arg locations which is not the case.
I have implemented a quick fix here: 1436496
Let me know if we can go ahead with this or not.
There was a problem hiding this comment.
This looks great, and looks like it would be good to add as a separate PR before merging this one
There was a problem hiding this comment.
- Added this RUN command.
// RUN: xdsl-opt %s --print-debuginfo | mlir-opt --mlir-print-op-generic --allow-unregistered-dialect | xdsl-opt--print-debuginfo| filecheck %s --check-prefix=DEBUGINFO - I tried to mimic MLIR's behaviour as much as possible except one scenario.
func.func private @f_decl_unnamed_attr_then_loc(i32 {test.arg_name = "x"} loc("model.mlir":7:9))
// XDSL-CHECK: func.func private @f_decl_unnamed_attr_then_loc(i32) -> ()
// MLIR-CHECK: func.func private @f_decl_unnamed_attr_then_loc(i32 {test.arg_name = "x"})- Locations are dropped for
declarativefunctions in MLIR, while in xDSL, both attributes & locations are dropped. I don't need this behaviour currently in xDSL hence didn't fix it as this required changes in multiple places. - If this sounds good, we can go for merge and I will push another PR for propagating locations for func block args. Currently known locations turn to unknown locations after parsing. See the
func_ops.mlirtests for reference.
func.func private @f_named_attr_then_loc(%arg0: i32 {test.arg_name = "x"} loc("file.mlir":5:7)) {
func.return
}
// DEBUGINFO: func.func private @f_named_attr_then_loc(%{{.*}} : i32 {test.arg_name = "x"} loc(unknown))There was a problem hiding this comment.
how much more code would it be to add it to this PR?
There was a problem hiding this comment.
I think at this stage we should probably use this PR to reach the final state that we want to merge, and then either merge it all in one go or pull out independent changes, depending on how the code ends up looking.
|
|
||
| func.func private @f(%arg0: i32 loc(unknown) {test.arg_name = "x"}) | ||
|
|
||
| // CHECK: Expected function argument attributes before location. |
There was a problem hiding this comment.
I get a different error message in MLIR:
➜ snax-mlir git:(main) ✗ mlir-opt test_loc.mlir
test_loc.mlir:5:45: error: expected ')'
func.func private @f(%arg0: i32 loc(unknown) {test.arg_name = "x"})
^
But I like your implementation
| @@ -1 +1 @@ | |||
| // RUN: XDSL_ROUNDTRIP | |||
There was a problem hiding this comment.
Please also add an extra line for debuginfo print testing here (see other comment in interop tests)
There was a problem hiding this comment.
Since mlir-opt doesn't recognize xdsl based dialects like test, I get this error -
<stdin>:13:33: error: unknown type!
func.func @arg_rec(%0 : !test.type<"int"> loc(unknown)) -> !test.type<"int"> {Hence I added this RUN command:
// RUN: xdsl-opt %s --print-debuginfo | filecheck %s --check-prefix=DEBUGINFO
There was a problem hiding this comment.
please add --allow-unregistered-dialect
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
dee68cf to
180dfe0
Compare
Signed-off-by: Zahid Wakeel <zahid.wakeel@synthara.ai>
func.func block arguments having locations info are not parsed in xDSL and hence throws parsing errors.
xDSL raises parsing error because it doesn't expect location to be there inside func.func block arguments.
while the same mlir code is parsed by mlir tools without any parsing errors. MLIR also expects the location to be in a specific order like
%arg: <type> {attr} loc(...)mlir supports this kind of location format for instance -
while if the order of location format is something like
%arg: <type> loc(...) {attr}, then it is not supported in MLIR -