This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Fix block merging with the result of a terminator
ClosedPublic

Authored by zero9178 on Mar 18 2022, 3:30 AM.

Details

Summary

When the current implementation merges two blocks that have operands defined outside of their block respectively, it will merge these by adding a block argument in the resulting merged block and adding successor arguments to the predecessors.
There is a special case where this is incorrect however: If one of predecessors terminator produce the operand, inserting the block argument and updating the predecessor would lead to the terminator using its own result as successor argument.
IR Example:

  %0 = "test.producing_br"()[^bb1, ^bb2] {
        operand_segment_sizes = dense<0> : vector<2 x i32>
	} : () -> i32

^bb1:
  "test.br"(%0)[^bb4] : (i32) -> ()

where ^bb1 is then merged with another block would lead to:

%0 = "test.producing_br"(%0)[^bb1, ^bb2]

This patch fixes that issue during clustering by making sure that if the operand is from an outside block, that it is not produced by the terminator of a predecessor.

(Note: The tests use "test.br" instead of cf.br to avoid other canonicalizations changing the IR and not causing the block merging)

Diff Detail

Event Timeline

zero9178 created this revision.Mar 18 2022, 3:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2022, 3:30 AM
zero9178 requested review of this revision.Mar 18 2022, 3:30 AM
ftynse accepted this revision.Mar 21 2022, 2:17 AM
This revision is now accepted and ready to land.Mar 21 2022, 2:17 AM
This revision was automatically updated to reflect the committed changes.
rriddle added inline comments.Mar 28 2022, 7:45 AM
mlir/lib/Transforms/Utils/RegionUtils.cpp
536

Can we remove the mlir:: here?

mlir/test/lib/Dialect/Test/TestDialect.cpp
351

Drop the trivial braces.

zero9178 marked 2 inline comments as done.Mar 28 2022, 7:56 AM

Thanks for noticing! I corrected the code-style issues in https://github.com/llvm/llvm-project/commit/a7865228b30dc019c0d13d0c81247d90fcad325b