Skip to content

Commit dfb6c16

Browse files
authored
---
yaml --- r: 221183 b: refs/heads/tensorflow-merge c: 175eb5e h: refs/heads/master i: 221181: 3c86bb8 221179: 36eb32f 221175: 0845776 221167: 91b1b6d 221151: 30fea0d 221119: 197e1cd 221055: b3fa5f1 220927: fdf1979 220671: 807e010 220159: 82ba960 219135: 94af4e4 217087: ea6baf1 212991: 1aeddf8
1 parent bcb438a commit dfb6c16

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-06-14-a: 888f12b456d2b9b0e35518c1972bb
906906
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-06-15-a: 683450174579052b46e32f3b8f7fcc6fe8127db9
907907
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-06-16-a: 5d81d51e075adbc6128d5612ac1d9a43186d0396
908908
refs/heads/move-debugging-executables-into-its-own-section: 968835a1ac02f692a0f5d8ebd60949ab6b0f14bc
909-
refs/heads/tensorflow-merge: fa69bf24de77e0f654d3345eda6ad934b363ad93
909+
refs/heads/tensorflow-merge: 175eb5e2d1374eb43bd8e83d5162b88862da6156
910910
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-06-17-a: b947b41f2e23855c11bd483879813857b41e63ec
911911
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-06-18-a: 76370219d53fcd9b9d7440166a992c9fd21909f7
912912
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-06-19-a: 171b02e41ec9c3c8b592cecf7da70ab560d06cc5

branches/tensorflow-merge/lib/SILOptimizer/Mandatory/TFCanonicalizeCFG.cpp

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,13 @@ class SingleExitLoopTransformer {
316316
/// Create a new latch block and clone all the arguments from new header.
317317
SILBasicBlock *createNewLatch(SILBasicBlock *newHeader);
318318

319-
// Patch the edges that go to the header and exit blocks to the new header or
320-
// latch block as appropriate. Also, return the map consisting of indices
321-
// assigned to the exit blocks.
319+
/// Replace the preheader->header edge with preheader->newHeader edge
320+
/// and update the arguments of preheader to match that of newheader.
321+
void patchPreheader(SILBasicBlock *newHeader);
322+
323+
/// Patch the edges that go to the header and exit blocks to the new header or
324+
/// latch block as appropriate. Also, return the map consisting of indices
325+
/// assigned to the exit blocks.
322326
llvm::DenseMap<SILBasicBlock *, intmax_t>
323327
patchEdges(SILBasicBlock *newHeader, SILBasicBlock *latchBlock);
324328

@@ -368,9 +372,18 @@ void SingleExitLoopTransformer::initialize() {
368372
loop->getExitBlocks(exitBlocks);
369373
// All the exiting edges need to be rewired.
370374
loop->getExitEdges(edgesToFix);
371-
// All the edges to the header need to be rewired.
372-
for (SILBasicBlock *headerPred : header->getPredecessorBlocks()) {
373-
edgesToFix.emplace_back(headerPred, header);
375+
edgesToFix.emplace_back(latch, header);
376+
// Split critical edges in edgesToFix before we do any transformations.
377+
for (auto &edge : edgesToFix) {
378+
SILBasicBlock *src = const_cast<SILBasicBlock *>(edge.first);
379+
SILBasicBlock *tgt = const_cast<SILBasicBlock *>(edge.second);
380+
SILBasicBlock *splitBlock = splitIfCriticalEdge(src, tgt, /*DT*/ nullptr, LI);
381+
if (splitBlock != nullptr) {
382+
// If the edge was critical then splitBlock would have been inserted
383+
// between src and tgt as follows: src -> splitBlock -> tgt. Therefore,
384+
// update src as the new edge to patch would be splitBlock -> tgt.
385+
edge.first = splitBlock;
386+
}
374387
}
375388

376389
for (const SILBasicBlock *bb : loop->getBlocks()) {
@@ -502,6 +515,30 @@ SingleExitLoopTransformer::createNewLatch(SILBasicBlock *newHeader) {
502515
return latchBlock;
503516
}
504517

518+
void SingleExitLoopTransformer::patchPreheader(SILBasicBlock *newHeader) {
519+
// Update edge
520+
replaceBranchTarget(preheader->getTerminator(), header, newHeader,
521+
/*preserveArgs*/ true);
522+
SILBuilder builder(preheader->getTerminator());
523+
SILLocation location(
524+
getUserSourceLocation(preheader->getTerminator()->getDebugLocation()));
525+
// Add arguments corresponding to escaping arguments.
526+
// State from within the loop is not available in the preheader.
527+
// Simply pass in an undef. This will never be accessed at runtime.
528+
SmallVector<SILValue, 8> newArgs;
529+
for (const SILValue &escapingValue : escapingValues) {
530+
newArgs.push_back(getUndef(escapingValue->getType()));
531+
}
532+
// `exitIndex` to identify the block to which we exit from the loop.
533+
newArgs.push_back(createTFIntegerConst(*deviceInfo, builder, location,
534+
/*bitwidth*/ 32,
535+
/*exitIndex*/ 0));
536+
// `stayInLoop` flag
537+
newArgs.push_back(createTFIntegerConst(*deviceInfo, builder, location,
538+
/*bitwidth*/ 1, true));
539+
appendArguments(preheader->getTerminator(), newHeader, newArgs);
540+
}
541+
505542
llvm::DenseMap<SILBasicBlock *, intmax_t>
506543
SingleExitLoopTransformer::patchEdges(SILBasicBlock *newHeader,
507544
SILBasicBlock *latchBlock) {
@@ -537,14 +574,7 @@ SingleExitLoopTransformer::patchEdges(SILBasicBlock *newHeader,
537574
for (const auto &edge : edgesToFix) {
538575
SILBasicBlock *src = const_cast<SILBasicBlock *>(edge.first);
539576
SILBasicBlock *tgt = const_cast<SILBasicBlock *>(edge.second);
540-
SILBasicBlock *newTgt = (src == preheader) ? newHeader : latchBlock;
541-
SILBasicBlock *splitBlock = splitIfCriticalEdge(src, tgt, /*DT*/ nullptr, LI);
542-
if (splitBlock != nullptr) {
543-
// If the edge was critical then splitBlock would have been inserted
544-
// between src and tgt as follows: src -> splitBlock -> tgt. Therefore,
545-
// update src as the new edge to patch would be splitBlock -> tgt.
546-
src = splitBlock;
547-
}
577+
SILBasicBlock *newTgt = latchBlock;
548578
bool stayInLoop = loop->contains(tgt);
549579
replaceBranchTarget(src->getTerminator(), tgt, newTgt,
550580
/*preserveArgs=*/stayInLoop);
@@ -564,13 +594,8 @@ SingleExitLoopTransformer::patchEdges(SILBasicBlock *newHeader,
564594
for (const SILValue &escapingValue : escapingValues) {
565595
if (DI->properlyDominates(escapingValue, src->getTerminator())) {
566596
newArgs.push_back(escapingValue);
567-
} else if (src != preheader) {
568-
// newHeader arguments are available if src is not the preheader.
569-
newArgs.push_back(newHeader->getArgument(argIndex));
570597
} else {
571-
// State from within the loop is not available in the preheader.
572-
// Simply pass in an undef. This will never be accessed at runtime.
573-
newArgs.push_back(getUndef(escapingValue->getType()));
598+
newArgs.push_back(newHeader->getArgument(argIndex));
574599
}
575600
++argIndex;
576601
}
@@ -690,6 +715,8 @@ bool SingleExitLoopTransformer::transform() {
690715
// Create a new latch block.
691716
SILBasicBlock *latchBlock = createNewLatch(newHeader);
692717

718+
patchPreheader(newHeader);
719+
693720
// Patch the edges and return the map consisting of indices assigned
694721
// to the exit blocks.
695722
llvm::DenseMap<SILBasicBlock *, intmax_t> exitIndices =

0 commit comments

Comments
 (0)