Skip to content

Fix vectorchain.shuffle op. - #19

Draft
KFAFSP wants to merge 2 commits into
torch-spyre:mainfrom
KFAFSP:kfaf/vectorchain_shuffle
Draft

KFAFSP wants to merge 2 commits into
torch-spyre:mainfrom
KFAFSP:kfaf/vectorchain_shuffle

Conversation

@KFAFSP

@KFAFSP KFAFSP commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

I want to keep a lid on technical debt by preventing the introduction of new issues. This PR is in response to PR #18, which shows a lot of that. In particular:

  • Please respect .clang-format, even when the CI does not check it in this repo yet.
  • Do not introduce builders that don't defer to a default builder if at all possible. Do not hard code attribute names. User property style construction.
  • Do not introduce custom assembly formats that aren't declarative if at all possible.
  • Do not introduce assembly formats that aren't decidable without look-ahead > 1.
  • Prefer encoding invariants in ODS instead of C++ if possible.

I picked a sane assembly format, which will probably break downstream users.

There are more invariants that can be extracted in ODS (element type equivalence), but that is an overall tech debt issue that isn't new with this PR and can be left for later.

Signed-off-by: Karl F. A. Friebel <karl.friebel@ibm.com>
Signed-off-by: Karl F. A. Friebel <karl.friebel@ibm.com>

@msdataei msdataei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with use of assembly format and build functions in ODS. It is more flexible.
And of course clang-format, which I missed.

Thanks for working on this and making it better.

int64_t element_value = element.getInt();
if (element_value < min_element || element_value > max_element) {
for (auto index : llvm::map_range(indices, [](Attribute attr) {
return cast<IntegerAttr>(attr).getValue().getSExtValue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't it better to have an explicit error message?

@KFAFSP KFAFSP Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This invariant was moved to ODS. It's still there and will generate an error.

https://github.com/KFAFSP/dataflow-scheduler-mlir-dialects/blob/bf2807a9ad1bb7b83b7ddf6121736f121fec9967/include/dataflow-scheduler/Dialect/VectorChain/VectorChain.td#L463

EDIT: Whoops, link did not go where I wanted it to.

// CHECK-LABEL: func.func @shuffle_splat(
// CHECK-SAME: %arg0: vector<1xf16>) -> vector<64xf16> {
// CHECK-NEXT: %0 = vectorchain.shuffle input(%arg0) {indices = [0 : i32], repetition = 64 : i32} : vector<1xf16>, vector<64xf16>
// CHECK-NEXT: %0 = vectorchain.shuffle input(%arg0) {indices = [0 : i32], repetition = 64 : i32} : (vector<1xf16>) -> vector<64xf16>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess "(...) ->" notation will break some tests on other repos.

@KFAFSP KFAFSP Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is what I fear. But the problem is this:

Previously, there was one operand, one optional operand, and one result. Now there are multiple variadic ones. That means munching commas can't decide when we've reached the end and expect the return type.

The commonly accepted default in MLIR is to separate operands from results with an arrow ->. The default format of ops without custom assembly formats goes one step further and just uses the functional-type. That seemed like the easiest solution to me if we ever want to change anything about the op.

We could also not have parens around the operands, we could put the types inside the optional operands blocks, we could parenthesise all operand groups, ... There are other solutions but this is the simplest one. Since all of them would break downstream users, I suggested the easiest one.

I also want to highlight that we don't have to make this change. If we have a good reason for keeping the custom format (which is the case with many ops that declare regions), then we should absolutely do that. This should be documented, use custom directives instead of member functions if possible, and then also use the property-style builder (state.getOrAddProperties<Properties>()) instead of the attribute dictionary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is also at least one op with an invalid assembly format (optional attr dict followed by an optional region) in DFIR. These kinds of mistakes can't happen when using the declarative format, since that is checked by ODS.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this parens and -> is better format than putting the type inside the optional operands blocks. I think type next to the operands will shift the some of the attribute of shuffle op further right and it makes it harder to read. I think we can update shuffle op to -> notation on other repos.

@KFAFSP
KFAFSP marked this pull request as draft August 10, 2026 13:34
@KFAFSP

KFAFSP commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

I'm putting this on ice because the downstream users are doing even more inconsistent things, including sometimes treating the indices attribute as 64-bit. Fixing this is going to touch on hundreds of lines and I'm not doing that right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants