Skip to content

[mlir] Remove extra whitespace during printout in prop-dict (#145695) #145908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from

Conversation

weirdsmiley
Copy link
Member

Using character representation and removal of extra whitespace keeps printout consistent across cases.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jun 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 26, 2025

@llvm/pr-subscribers-mlir-core

Author: Manas (weirdsmiley)

Changes

Using character representation and removal of extra whitespace keeps printout consistent across cases.


Full diff: https://github.com/llvm/llvm-project/pull/145908.diff

1 Files Affected:

  • (modified) mlir/lib/IR/AsmPrinter.cpp (+1-1)
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index c7cc6a02ad208..d9bd6444dda79 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -2848,7 +2848,7 @@ void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
       os << " attributes";
 
     // Otherwise, print them all out in braces.
-    os << " {";
+    os << '{';
     interleaveComma(filteredAttrs,
                     [&](NamedAttribute attr) { printNamedAttribute(attr); });
     os << '}';

@llvmbot
Copy link
Member

llvmbot commented Jun 26, 2025

@llvm/pr-subscribers-mlir

Author: Manas (weirdsmiley)

Changes

Using character representation and removal of extra whitespace keeps printout consistent across cases.


Full diff: https://github.com/llvm/llvm-project/pull/145908.diff

1 Files Affected:

  • (modified) mlir/lib/IR/AsmPrinter.cpp (+1-1)
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index c7cc6a02ad208..d9bd6444dda79 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -2848,7 +2848,7 @@ void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
       os << " attributes";
 
     // Otherwise, print them all out in braces.
-    os << " {";
+    os << '{';
     interleaveComma(filteredAttrs,
                     [&](NamedAttribute attr) { printNamedAttribute(attr); });
     os << '}';

@weirdsmiley weirdsmiley requested a review from River707 June 26, 2025 14:55
@weirdsmiley
Copy link
Member Author

I notice this fails CI. It wouldn't be a correct fix, because it removes whitespace from all cases which isn't ideal. I will send a v2.

@weirdsmiley
Copy link
Member Author

@River707 a question about the formatting: the issue only talks about keeping things consistent. So do we want to remove the whitespace from single attribute case or put extra whitespace in all cases?

Visually, that would be: we can either make prop-dict look like <{ in all cases (one or more attributes) or to make it < { (add a whitespace) in all cases?

@joker-eph
Copy link
Collaborator

Why do we have this extra whitespace in the first place?
Was this actually needed in some cases?

@weirdsmiley
Copy link
Member Author

weirdsmiley commented Jun 26, 2025

@joker-eph yes there are cases like this, outside of <> where removing whitespace would lead to something like the following.

expected:
; CHECK: module attributes {

if whitespace removed:
; CHECK: module attributes{

There are more test cases like this (and probably others, total failed cases are 367). I got this from failed test cases (ninja check-mlir) after applying my patch.

I think that adding a whitespace in all cases would be consistent and will have a straightforward fix.

@joker-eph
Copy link
Collaborator

I think that adding a whitespace in all cases would be consistent and will have a straightforward fix.

I don't see what is "consistent" about adding an extra whitespace actually: it does not follow any existing convention and seems totally out-of-place to me.

@joker-eph
Copy link
Collaborator

joker-eph commented Jun 26, 2025

This API has a bool withKeyword parameter, why not add a bool addSpacePrefix = true option?

@weirdsmiley
Copy link
Member Author

weirdsmiley commented Jun 26, 2025

  1. For the "consistency" part, the issue raised here has been that under cases where there is one attribute with elidedProps, then printer puts a whitespace, while if there are more than one attribute then it won't. For example, this test shows both cases which are different during printing.
// CHECK: test.format_optional_prop_dict < {a = ["foo"]}>
//                                        ^ extra whitespace
test.format_optional_prop_dict <{a = ["foo"]}>

// CHECK: test.format_optional_prop_dict < {b = 2 : i32}>
//                                        ^ extra whitespace
test.format_optional_prop_dict <{b = 2 : i32}>

// CHECK: test.format_optional_prop_dict <{a = ["foo"], b = 2 : i32}>
//                                        ^ no whitespace
  1. We can add option bool addSpacePrefix = true. But if user sets it to false then do we expect to put no whitespaces in prefix? That would result in above said situations like module attributes{.

@joker-eph
Copy link
Collaborator

That would result in above said situations like module attributes{.

It wouldn't because the flag wouldn't be false in this context.

@joker-eph
Copy link
Collaborator

// CHECK: test.format_optional_prop_dict <{a = ["foo"], b = 2 : i32}>
// ^ no whitespace

What is the codepath that gets us there? Maybe it is just a matter of calling the right API instead of this one.

@joker-eph
Copy link
Collaborator

Here is a fix for this bug: #145962

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants