Skip to content

[mlir] fix references of attributes which are not defined earlier #134364

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

Merged
merged 1 commit into from
Apr 8, 2025

Conversation

tdanyluk
Copy link
Contributor

@tdanyluk tdanyluk commented Apr 4, 2025

If an attribute is not defined earlier in the same file, but just referenced from its dialect directly, then currently not the correct check is being emited.

What would it emit for #toy.shape<[1, 2, 3]>:
Earlier:
// CHECK: #[['?']]<[1, 2, 3]>
Now:
// CHECK: #toy.shape<[1, 2, 3]>

@llvmbot llvmbot added the mlir label Apr 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2025

@llvm/pr-subscribers-mlir

Author: None (tdanyluk)

Changes

If an attribute is not defined earlier in the same file, but just referenced from its dialect directly, then currently not the correct check is being emited.

What would it emit for this:
Earlier:
// CHECK: #[['?']]<[1, 2, 3]>
Now:
// CHECK: #toy.shape<[1, 2, 3]>


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

1 Files Affected:

  • (modified) mlir/utils/generate-test-checks.py (+5-6)
diff --git a/mlir/utils/generate-test-checks.py b/mlir/utils/generate-test-checks.py
index 749bfa13fe734..f15d0fee0ea11 100755
--- a/mlir/utils/generate-test-checks.py
+++ b/mlir/utils/generate-test-checks.py
@@ -145,10 +145,9 @@ def generate_name(self, source_attribute_name):
         return attribute_name
 
     # Get the saved substitution name for the given attribute name. If no name
-    # has been generated for the given attribute yet, the source attribute name
-    # itself is returned.
+    # has been generated for the given attribute yet, None is returned.
     def get_name(self, source_attribute_name):
-        return self.map[source_attribute_name] if source_attribute_name in self.map else '?'
+        return self.map[source_attribute_name] if source_attribute_name in self.map else None
 
 # Return the number of SSA results in a line of type
 #   %0, %1, ... = ...
@@ -227,9 +226,9 @@ def process_attribute_references(line, attribute_namer):
     components = ATTR_RE.split(line)
     for component in components:
         m = ATTR_RE.match(component)
-        if m:
-            output_line += '#[[' + attribute_namer.get_name(m.group(1)) + ']]'
-            output_line += component[len(m.group()):]
+        attribute_name = attribute_namer.get_name(m.group(1)) if m else None
+        if attribute_name:
+            output_line += f'#[[{attribute_name}]]{component[len(m.group()):]}'
         else:
             output_line += component
     return output_line

@tdanyluk tdanyluk force-pushed the tdanyluk/generate-test-checks branch from e28309f to 1c81160 Compare April 4, 2025 10:58
Copy link

github-actions bot commented Apr 4, 2025

✅ With the latest revision this PR passed the Python code formatter.

@ftynse ftynse changed the title Fix references of attributes which are not defined earlier [mlir] fix references of attributes which are not defined earlier Apr 4, 2025
@ftynse
Copy link
Member

ftynse commented Apr 4, 2025

Is there a way to test this?

@tdanyluk tdanyluk force-pushed the tdanyluk/generate-test-checks branch from 1c81160 to d2f4c5c Compare April 4, 2025 12:19
@tdanyluk
Copy link
Contributor Author

tdanyluk commented Apr 4, 2025

Fair question, but there are currently no tests for generate-test-checks.py and adding them would probably take too much of my working time.

If you just want to try this change, you can make a file (test-input.txt):

{
    #my_attr = my.attr<[1, 2, 3]>
    %0 = toy.reshape %arg0 {shape = #my_attr} : (tensor<3xf32>) -> tensor<3xf32>
    %0 = toy.reshape %arg0 {shape = #toy.shape<[1, 2, 3]>} : (tensor<3xf32>) -> tensor<3xf32>
}

cat test-input.txt | python3 generate-test-checks.py --source test-input.txt

Earlier version would output:

// CHECK-LABEL:     #[[$ATTR_0]] = my.attr<[1, 2, 3]>
// CHECK:           %[[VAL_0:.*]] = toy.reshape %[[VAL_1:.*]] {shape = #[[$ATTR_0]]} : (tensor<3xf32>) -> tensor<3xf32>
// CHECK:           %[[VAL_0]] = toy.reshape %[[VAL_1]] {shape = #[[?]]<[1, 2, 3]>} : (tensor<3xf32>) -> tensor<3xf32>

New version would output:

// CHECK-LABEL:     #[[$ATTR_0]] = my.attr<[1, 2, 3]>
// CHECK:           %[[VAL_0:.*]] = toy.reshape %[[VAL_1:.*]] {shape = #[[$ATTR_0]]} : (tensor<3xf32>) -> tensor<3xf32>
// CHECK:           %[[VAL_0]] = toy.reshape %[[VAL_1]] {shape = #toy.shape<[1, 2, 3]>} : (tensor<3xf32>) -> tensor<3xf32>

So the case where the attribute is defined locally is unchanged, but the case where it's not is different.

Copy link
Contributor

@sergey-kozub sergey-kozub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: self.map.get(source_attribute_name) is an equivalent (lines 151-153, not sure how the UI lost the context)

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG overall

If an attribute is not defined earlier in the same file, but just referenced
from its dialect directly, then currently not the correct check is being emited.

What would it emit for #toy.shape<[1, 2, 3]>:
Earlier:
// CHECK: #[['?']]<[1, 2, 3]>
Now:
// CHECK: #toy.shape<[1, 2, 3]>
@tdanyluk tdanyluk force-pushed the tdanyluk/generate-test-checks branch from fe10b27 to fc643cc Compare April 8, 2025 15:28
@joker-eph joker-eph merged commit 76d2e08 into llvm:main Apr 8, 2025
6 of 10 checks passed
GleasonK added a commit that referenced this pull request Apr 11, 2025
…checks.py (#134327)

A few additions:

- Lines with `{{`: These can show up if serializing non-MLIR info into
string attrs `my.attr = {{proto}, {...}}`. String escape the opening
`{{`, given that check lines are generated this has no effect on
`{{.*}}` etc in generated lines.
- File split line: Normally these are skipped because of their indent
level, but if using `--starts_from_scope=0` to generate checks for the
`module {...} {` line, and since MLIR opt tools emit file split lines by
default, some `CHECK: // -----` lines were emit.
- (edit removed this, fixed by
#134364) AttrAliases: I'm not
sure if I'm missing something for the attribute parser to work
correctly, but I was getting many `#[[?]]` for all dialect attrs. Only
use the attr aliasing if there's a match.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Apr 11, 2025
…erate-test-checks.py (#134327)

A few additions:

- Lines with `{{`: These can show up if serializing non-MLIR info into
string attrs `my.attr = {{proto}, {...}}`. String escape the opening
`{{`, given that check lines are generated this has no effect on
`{{.*}}` etc in generated lines.
- File split line: Normally these are skipped because of their indent
level, but if using `--starts_from_scope=0` to generate checks for the
`module {...} {` line, and since MLIR opt tools emit file split lines by
default, some `CHECK: // -----` lines were emit.
- (edit removed this, fixed by
llvm/llvm-project#134364) AttrAliases: I'm not
sure if I'm missing something for the attribute parser to work
correctly, but I was getting many `#[[?]]` for all dialect attrs. Only
use the attr aliasing if there's a match.
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…checks.py (llvm#134327)

A few additions:

- Lines with `{{`: These can show up if serializing non-MLIR info into
string attrs `my.attr = {{proto}, {...}}`. String escape the opening
`{{`, given that check lines are generated this has no effect on
`{{.*}}` etc in generated lines.
- File split line: Normally these are skipped because of their indent
level, but if using `--starts_from_scope=0` to generate checks for the
`module {...} {` line, and since MLIR opt tools emit file split lines by
default, some `CHECK: // -----` lines were emit.
- (edit removed this, fixed by
llvm#134364) AttrAliases: I'm not
sure if I'm missing something for the attribute parser to work
correctly, but I was getting many `#[[?]]` for all dialect attrs. Only
use the attr aliasing if there's a match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants