This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][Affine] Fix generateUnrolledLoop utility
ClosedPublic

Authored by akshaybaviskar on Mar 26 2023, 11:04 PM.

Details

Summary

generateUnrolledLoop was assuming that the yielded value is always generated in
the Block corresponding to the loop being unrolled. Thus, it was updating the
last yielded values with it's cloned value. However, if the yielded value is not
generated in the same Block then the cloned value and it's corresponding mapping
won't exist, resulting in a crash. Fix this.

Diff Detail

Event Timeline

akshaybaviskar requested review of this revision.Mar 26 2023, 11:04 PM
bondhugula requested changes to this revision.Mar 26 2023, 11:47 PM
bondhugula added inline comments.
mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
1044–1048

This would be wrong. You can't assume getDefiningOp always returns non-null. This will crash if the yielded value is a BlockArgument.

This revision now requires changes to proceed.Mar 26 2023, 11:47 PM
bondhugula added inline comments.Mar 26 2023, 11:49 PM
mlir/test/Dialect/SCF/loop-unroll.mlir
360

Missing test cases where the yield value is a block argument - one of the iter_args itself for example which can easily happen.

360–363

Please drop the %{{.*}} = part -- it's not doing any additional testing.

Fix generateUnrolledLoop utility

generateUnrolledLoop was assuming that the yielded value is always generated in
the Block corresponding to the loop being unrolled. Thus, it was updating the
last yielded values with it's cloned value. However, if the yielded value is not
generated in the same Block then the cloned value and it's corresponding mapping
won't exist, resulting in a crash. Fix this.

akshaybaviskar marked 2 inline comments as done.Mar 27 2023, 3:00 AM
akshaybaviskar added inline comments.
mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
1044–1048

Thanks for pointing out. I have fixed it and updated the test cases. Please review.

Mostly related to code comments and redundancy.

mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
1040

If the yielded value

1040

An SSA value can never be updated. Please rephrase.

1041

Nit: it's -> its

1041–1042

Again, please rewrite. An SSA value can't be updated.

1044

Please avoid != nullptr -- it's not consistent with the style used and is also more verbose. In addition, the more idiomatic way would be: !..isa<BlockArgument>(). You don't need to obtain the defining op pointer.

1044

The check also has redundancy/repetition between the two clauses. Instead:

Operation *defOp = ...;
if (defOp && defOp->getBlock() == ...)

Please add [MLIR][Affine] to the revision title.

bondhugula retitled this revision from Fix generateUnrolledLoop utility to [MLIR][Affine] Fix generateUnrolledLoop utility.Mar 27 2023, 7:44 PM
bondhugula edited the summary of this revision. (Show Details)

[MLIR][Affine] Fix generateUnrolledLoop utility

generateUnrolledLoop was assuming that the yielded value is always generated in
the Block corresponding to the loop being unrolled. Thus, it was updating the
last yielded values with it's cloned value. However, if the yielded value is not
generated in the same Block or if it is a BlockArgument then the cloned value
and it's corresponding mapping won't exist, resulting in a crash. Fix this.

Differential Revision: https://reviews.llvm.org/D146931

akshaybaviskar marked 6 inline comments as done.

Fix generateUnrolledLoop utility

generateUnrolledLoop was assuming that the yielded value is always generated in
the Block corresponding to the loop being unrolled. Thus, it was updating the
last yielded values with it's cloned value. However, if the yielded value is not
generated in the same Block then the cloned value and it's corresponding mapping
won't exist, resulting in a crash. Fix this.

Differential Revision: https://reviews.llvm.org/D146931

akshaybaviskar added inline comments.Mar 28 2023, 2:46 AM
mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
1040

Update. Please re-review.

This revision is now accepted and ready to land.Mar 28 2023, 9:11 AM
bondhugula added inline comments.Mar 28 2023, 9:12 AM
mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
1044–1048

This will eliminate an extra check:

Operation *defOp = ...;
if (defOp && defOp->getBlock() == ...)

Fix generateUnrolledLoop utility

generateUnrolledLoop was assuming that the yielded value is always generated in
the Block corresponding to the loop being unrolled. Thus, it was updating the
last yielded values with it's cloned value. However, if the yielded value is not
generated in the same Block then the cloned value and it's corresponding mapping
won't exist, resulting in a crash. Fix this.

Differential Revision: https://reviews.llvm.org/D146931

akshaybaviskar marked 2 inline comments as done.Mar 28 2023, 10:35 AM
akshaybaviskar retitled this revision from [MLIR][Affine] Fix generateUnrolledLoop utility to Fix generateUnrolledLoop utility.
akshaybaviskar edited the summary of this revision. (Show Details)

Fix generateUnrolledLoop utility

generateUnrolledLoop was assuming that the yielded value is always generated in
the Block corresponding to the loop being unrolled. Thus, it was updating the
last yielded values with it's cloned value. However, if the yielded value is not
generated in the same Block then the cloned value and it's corresponding mapping
won't exist, resulting in a crash. Fix this.

git diff --check HEAD~
mlir/test/Dialect/SCF/loop-unroll.mlir:8: trailing whitespace.
+// RUN: mlir-opt %s --affine-loop-unroll --split-input-file | FileCheck %s

Please fix.

bondhugula added inline comments.Apr 4 2023, 9:35 AM
mlir/test/Dialect/SCF/loop-unroll.mlir
8

Trailing white space here.

Fix generateUnrolledLoop utility

generateUnrolledLoop was assuming that the yielded value is always generated in
the Block corresponding to the loop being unrolled. Thus, it was updating the
last yielded values with it's cloned value. However, if the yielded value is not
generated in the same Block then the cloned value and it's corresponding mapping
won't exist, resulting in a crash. Fix this.

akshaybaviskar marked an inline comment as done.Apr 5 2023, 5:47 AM
akshaybaviskar retitled this revision from Fix generateUnrolledLoop utility to [MLIR][Affine] Fix generateUnrolledLoop utility.
This revision was landed with ongoing or failed builds.Apr 6 2023, 8:49 AM
This revision was automatically updated to reflect the committed changes.