-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Simplify and refactor contractUncondEdges as a utility in TFCanonicalizeCFG. #19546
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,29 @@ void ConditionalSESERegion::print(llvm::raw_ostream &OS, unsigned indent) const | |
// CFG Canonicalization Implementation | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Our partitioning and other transformations can leave around lots of | ||
// unconditional branches between blocks that formerly had control edges. Go | ||
// through and merge those to make later passes simpler. | ||
bool tf::contractUncondBranches(SILFunction *fn, DominanceInfo* DI, SILLoopInfo *LI) { | ||
bool changed = false; | ||
// Iterate carefully to avoid invalidating iterators: we mutate the block list | ||
// while we walk it. | ||
for (auto bbi = fn->begin(), e = fn->end(); bbi != e;) { | ||
auto *bb = &*bbi; | ||
if (mergeBasicBlockWithSuccessor(bb, DI, LI)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the old code does not use mergeBasicBlockWithSuccessor() -- it may be worth mentioning this "less obvious refactoring" in PR description. From a quick read of that func impl, it seems to be equivalent to the old code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the description. |
||
// The block was merged with this successor. Therefore, revisit this node: | ||
// we have new successor(s) and may need to contract them as well. Also, | ||
// bbi may be invalidated at this point. | ||
changed = true; | ||
bbi = SILFunction::iterator(bb); | ||
} else { | ||
// Move to the next block if this was not merged. | ||
++bbi; | ||
} | ||
} | ||
return changed; | ||
} | ||
|
||
namespace { | ||
class SESERegionBuilder { | ||
DominanceInfo DI; | ||
|
@@ -1156,8 +1179,8 @@ namespace { | |
/// The entry point to the transformation. | ||
void run() override { | ||
auto fn = getFunction(); | ||
contractUncondBranches(fn, /*DI*/nullptr, /*LI*/ nullptr); | ||
auto region = canonicalizeCFGForXLA(fn); | ||
|
||
llvm::outs() << "--- XLA CFG Canonicalize: " << fn->getName() << "\n"; | ||
region->print(llvm::outs()); | ||
llvm::outs() << "\n--- XLA CFG Canonicalize end\n"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// RUN: %target-sil-opt -tf-xla-cfg-canonicalize -tf-ensure-single-loop-exit -assume-parsing-unqualified-ownership-sil %s -o /dev/null | %FileCheck %s | ||
|
||
import Builtin | ||
import Swift | ||
import TensorFlow | ||
|
||
// Straight line blocks | ||
sil @$straightLine : $@convention(thin) (Builtin.Int32) -> Builtin.Int32 { | ||
bb0(%0 : $Builtin.Int32): | ||
br bb1(%0 : $Builtin.Int32) | ||
|
||
bb1(%1: $Builtin.Int32): | ||
br bb2(%1 : $Builtin.Int32) | ||
|
||
bb2(%2: $Builtin.Int32): | ||
br bb3(%2 : $Builtin.Int32) | ||
|
||
bb3(%3 : $Builtin.Int32): | ||
return %3 : $Builtin.Int32 | ||
} | ||
// CHECK-LABEL: --- XLA CFG Canonicalize: $straightLine | ||
// CHECK: block bb0 | ||
// CHECK: --- XLA CFG Canonicalize end | ||
// CHECK: sil @$straightLine : $@convention(thin) (Builtin.Int32) -> Builtin.Int32 { | ||
// CHECK: bb0(%0 : $Builtin.Int32): | ||
// CHECK-NEXT: return %0 : $Builtin.Int32 | ||
// CHECK: } | ||
|
||
|
||
// Conditionals | ||
sil @$condRegion : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32 { | ||
bb0(%0 : $Builtin.Int32, %1 : $Builtin.Int32): | ||
%2 = builtin "cmp_slt_Int32"(%0 : $Builtin.Int32, %1 : $Builtin.Int32) : $Builtin.Int1 | ||
cond_br %2, bb1, bb4 | ||
|
||
bb1: | ||
br bb2(%0 : $Builtin.Int32) | ||
|
||
bb2(%3 : $Builtin.Int32): | ||
br bb3(%3 : $Builtin.Int32) | ||
|
||
bb3(%4 : $Builtin.Int32): | ||
br bb5(%4 : $Builtin.Int32) | ||
|
||
bb4: | ||
br bb5(%1 : $Builtin.Int32) | ||
|
||
bb5(%5 : $Builtin.Int32): | ||
return %5 : $Builtin.Int32 | ||
} | ||
// CHECK-LABEL: --- XLA CFG Canonicalize: $condRegion | ||
// CHECK:[sequence | ||
// CHECK: {condition Header: bb0 | ||
// CHECK: block bb1 | ||
// CHECK: block bb2} | ||
// CHECK: block bb3] | ||
// CHECK: --- XLA CFG Canonicalize end | ||
// CHECK: sil @$condRegion : $@convention(thin) {{.*}} { | ||
// CHECK: bb0(%0 : $Builtin.Int32, %1 : $Builtin.Int32): | ||
// CHECK: cond_br %2, bb1, bb2 | ||
// CHECK: bb1: | ||
// CHECK: br bb3(%0 : $Builtin.Int32) | ||
// CHECK: bb2: | ||
// CHECK: br bb3(%1 : $Builtin.Int32) | ||
// CHECK: bb3([[A:%.*]] : $Builtin.Int32): | ||
// CHECK: return [[A]] : $Builtin.Int32 | ||
// CHECK: } | ||
|
||
// Simple loop | ||
sil @$simpleLoop : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32 { | ||
bb0(%0 : $Builtin.Int32, %1 : $Builtin.Int32): | ||
br bb1(%0 : $Builtin.Int32) | ||
|
||
bb1(%2 : $Builtin.Int32): | ||
%3 = builtin "cmp_slt_Int32"(%2 : $Builtin.Int32, %1 : $Builtin.Int32) : $Builtin.Int1 | ||
cond_br %3, bb3, bb2 | ||
|
||
bb2: | ||
br bb6(%2 : $Builtin.Int32) | ||
|
||
bb3: | ||
br bb4(%2 : $Builtin.Int32) | ||
|
||
bb4(%4 : $Builtin.Int32): | ||
br bb5(%4 : $Builtin.Int32) | ||
|
||
bb5(%5 : $Builtin.Int32): | ||
br bb1(%5 : $Builtin.Int32) | ||
|
||
bb6(%6 : $Builtin.Int32): | ||
return %6 : $Builtin.Int32 | ||
} | ||
// CHECK-LABEL:--- XLA CFG Canonicalize: $simpleLoop | ||
// CHECK:[sequence | ||
// CHECK: <while Preheader: bb0, Header: bb1, exit: bb2 | ||
// CHECK: block bb3> | ||
// CHECK: block bb2] | ||
// CHECK:--- XLA CFG Canonicalize end | ||
// CHECK:sil @$simpleLoop : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32 { | ||
// CHECK:bb0(%0 : $Builtin.Int32, %1 : $Builtin.Int32): | ||
// CHECK: br bb1(%0 : $Builtin.Int32) | ||
// CHECK:bb1(%3 : $Builtin.Int32): | ||
// CHECK: %4 = builtin "cmp_slt_Int32"(%3 : $Builtin.Int32, %1 : $Builtin.Int32) : $Builtin.Int1 | ||
// CHECK: cond_br %4, bb3, bb2 | ||
// CHECK:bb2: | ||
// CHECK: return %3 : $Builtin.Int32 | ||
// CHECK:bb3: | ||
// CHECK: br bb1(%3 : $Builtin.Int32) | ||
// CHECK:} | ||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will there be a caller where DI and LI are not NULL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I am preparing a PR that passes non-null for DI & LI.