Skip to content

Commit

Permalink
[CodeExtractor] Preserve topological order for the return blocks.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D108673
  • Loading branch information
Vyacheslav Zakharin committed Aug 25, 2021
1 parent 85eedf7 commit 2e192ab
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 104 deletions.
16 changes: 8 additions & 8 deletions clang/test/OpenMP/cancel_codegen.cpp
Expand Up @@ -1560,8 +1560,6 @@ for (int i = 0; i < argc; ++i) {
// CHECK3-NEXT: store i32 [[TMP0]], i32* [[TID_ADDR_LOCAL]], align 4
// CHECK3-NEXT: [[TID:%.*]] = load i32, i32* [[TID_ADDR_LOCAL]], align 4
// CHECK3-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK3: omp.par.outlined.exit.exitStub:
// CHECK3-NEXT: ret void
// CHECK3: omp.par.region:
// CHECK3-NEXT: [[TMP1:%.*]] = load float, float* @flag, align 4
// CHECK3-NEXT: [[TOBOOL:%.*]] = fcmp une float [[TMP1]], 0.000000e+00
Expand Down Expand Up @@ -1607,6 +1605,8 @@ for (int i = 0; i < argc; ++i) {
// CHECK3-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB]]
// CHECK3: .split:
// CHECK3-NEXT: br label [[TMP3]]
// CHECK3: omp.par.outlined.exit.exitStub:
// CHECK3-NEXT: ret void
//
//
// CHECK3-LABEL: define {{[^@]+}}@.omp_task_entry.
Expand Down Expand Up @@ -2187,8 +2187,6 @@ for (int i = 0; i < argc; ++i) {
// CHECK4-NEXT: store i32 [[TMP0]], i32* [[TID_ADDR_LOCAL]], align 4
// CHECK4-NEXT: [[TID:%.*]] = load i32, i32* [[TID_ADDR_LOCAL]], align 4
// CHECK4-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK4: omp.par.outlined.exit.exitStub:
// CHECK4-NEXT: ret void
// CHECK4: omp.par.region:
// CHECK4-NEXT: [[TMP1:%.*]] = load float, float* @flag, align 4
// CHECK4-NEXT: [[TOBOOL:%.*]] = fcmp une float [[TMP1]], 0.000000e+00
Expand Down Expand Up @@ -2234,6 +2232,8 @@ for (int i = 0; i < argc; ++i) {
// CHECK4-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB]]
// CHECK4: .split:
// CHECK4-NEXT: br label [[TMP3]]
// CHECK4: omp.par.outlined.exit.exitStub:
// CHECK4-NEXT: ret void
//
//
// CHECK4-LABEL: define {{[^@]+}}@.omp_task_entry.
Expand Down Expand Up @@ -4054,8 +4054,6 @@ for (int i = 0; i < argc; ++i) {
// CHECK9-NEXT: store i32 [[TMP0]], i32* [[TID_ADDR_LOCAL]], align 4
// CHECK9-NEXT: [[TID:%.*]] = load i32, i32* [[TID_ADDR_LOCAL]], align 4
// CHECK9-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK9: omp.par.outlined.exit.exitStub:
// CHECK9-NEXT: ret void
// CHECK9: omp.par.region:
// CHECK9-NEXT: [[TMP1:%.*]] = load float, float* @flag, align 4
// CHECK9-NEXT: [[TOBOOL:%.*]] = fcmp une float [[TMP1]], 0.000000e+00
Expand Down Expand Up @@ -4101,6 +4099,8 @@ for (int i = 0; i < argc; ++i) {
// CHECK9-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB]]
// CHECK9: .split:
// CHECK9-NEXT: br label [[TMP3]]
// CHECK9: omp.par.outlined.exit.exitStub:
// CHECK9-NEXT: ret void
//
//
// CHECK9-LABEL: define {{[^@]+}}@.omp_task_entry.
Expand Down Expand Up @@ -4681,8 +4681,6 @@ for (int i = 0; i < argc; ++i) {
// CHECK10-NEXT: store i32 [[TMP0]], i32* [[TID_ADDR_LOCAL]], align 4
// CHECK10-NEXT: [[TID:%.*]] = load i32, i32* [[TID_ADDR_LOCAL]], align 4
// CHECK10-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK10: omp.par.outlined.exit.exitStub:
// CHECK10-NEXT: ret void
// CHECK10: omp.par.region:
// CHECK10-NEXT: [[TMP1:%.*]] = load float, float* @flag, align 4
// CHECK10-NEXT: [[TOBOOL:%.*]] = fcmp une float [[TMP1]], 0.000000e+00
Expand Down Expand Up @@ -4728,6 +4726,8 @@ for (int i = 0; i < argc; ++i) {
// CHECK10-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB]]
// CHECK10: .split:
// CHECK10-NEXT: br label [[TMP3]]
// CHECK10: omp.par.outlined.exit.exitStub:
// CHECK10-NEXT: ret void
//
//
// CHECK10-LABEL: define {{[^@]+}}@.omp_task_entry.
Expand Down
18 changes: 9 additions & 9 deletions clang/test/OpenMP/parallel_codegen.cpp
Expand Up @@ -753,8 +753,6 @@ int main (int argc, char **argv) {
// CHECK3-NEXT: store i32 [[TMP0]], i32* [[TID_ADDR_LOCAL]], align 4
// CHECK3-NEXT: [[TID:%.*]] = load i32, i32* [[TID_ADDR_LOCAL]], align 4
// CHECK3-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK3: omp.par.outlined.exit.exitStub:
// CHECK3-NEXT: ret void
// CHECK3: omp.par.region:
// CHECK3-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[VLA]], i64 1
// CHECK3-NEXT: [[TMP1:%.*]] = load i32, i32* [[ARRAYIDX]], align 4
Expand All @@ -765,6 +763,8 @@ int main (int argc, char **argv) {
// CHECK3-NEXT: br label [[OMP_PAR_PRE_FINALIZE:%.*]]
// CHECK3: omp.par.pre_finalize:
// CHECK3-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB:%.*]]
// CHECK3: omp.par.outlined.exit.exitStub:
// CHECK3-NEXT: ret void
//
//
// CHECK3-LABEL: define {{[^@]+}}@_Z3fooIiEvT_
Expand Down Expand Up @@ -809,8 +809,6 @@ int main (int argc, char **argv) {
// CHECK3-NEXT: [[TMP1:%.*]] = load i64, i64* [[DOTRELOADED]], align 8
// CHECK3-NEXT: [[VAR:%.*]] = alloca double*, align 8
// CHECK3-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK3: omp.par.outlined.exit.exitStub:
// CHECK3-NEXT: ret void
// CHECK3: omp.par.region:
// CHECK3-NEXT: [[TMP2:%.*]] = load i8**, i8*** [[ARGC_ADDR]], align 8
// CHECK3-NEXT: call void @_Z3fooIPPcEvT_(i8** [[TMP2]])
Expand All @@ -821,6 +819,8 @@ int main (int argc, char **argv) {
// CHECK3-NEXT: br label [[OMP_PAR_PRE_FINALIZE:%.*]]
// CHECK3: omp.par.pre_finalize:
// CHECK3-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB:%.*]]
// CHECK3: omp.par.outlined.exit.exitStub:
// CHECK3-NEXT: ret void
//
//
// CHECK3-LABEL: define {{[^@]+}}@_Z3fooIPPcEvT_
Expand Down Expand Up @@ -877,8 +877,6 @@ int main (int argc, char **argv) {
// CHECK4-NEXT: store i32 [[TMP0]], i32* [[TID_ADDR_LOCAL]], align 4
// CHECK4-NEXT: [[TID:%.*]] = load i32, i32* [[TID_ADDR_LOCAL]], align 4
// CHECK4-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK4: omp.par.outlined.exit.exitStub:
// CHECK4-NEXT: ret void
// CHECK4: omp.par.region:
// CHECK4-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[VLA]], i64 1, !dbg [[DBG34:![0-9]+]]
// CHECK4-NEXT: [[TMP1:%.*]] = load i32, i32* [[ARRAYIDX]], align 4, !dbg [[DBG34]]
Expand All @@ -889,6 +887,8 @@ int main (int argc, char **argv) {
// CHECK4-NEXT: br label [[OMP_PAR_PRE_FINALIZE:%.*]], !dbg [[DBG34]]
// CHECK4: omp.par.pre_finalize:
// CHECK4-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB:%.*]], !dbg [[DBG34]]
// CHECK4: omp.par.outlined.exit.exitStub:
// CHECK4-NEXT: ret void
//
//
// CHECK4-LABEL: define {{[^@]+}}@_Z3fooIiEvT_
Expand Down Expand Up @@ -935,8 +935,6 @@ int main (int argc, char **argv) {
// CHECK4-NEXT: [[TMP1:%.*]] = load i64, i64* [[DOTRELOADED]], align 8
// CHECK4-NEXT: [[VAR:%.*]] = alloca double*, align 8
// CHECK4-NEXT: br label [[OMP_PAR_REGION:%.*]]
// CHECK4: omp.par.outlined.exit.exitStub:
// CHECK4-NEXT: ret void
// CHECK4: omp.par.region:
// CHECK4-NEXT: [[TMP2:%.*]] = load i8**, i8*** [[ARGC_ADDR]], align 8, !dbg [[DBG57:![0-9]+]]
// CHECK4-NEXT: call void @_Z3fooIPPcEvT_(i8** [[TMP2]]), !dbg [[DBG57]]
Expand All @@ -948,6 +946,8 @@ int main (int argc, char **argv) {
// CHECK4-NEXT: br label [[OMP_PAR_PRE_FINALIZE:%.*]], !dbg [[DBG66:![0-9]+]]
// CHECK4: omp.par.pre_finalize:
// CHECK4-NEXT: br label [[OMP_PAR_OUTLINED_EXIT_EXITSTUB:%.*]], !dbg [[DBG66]]
// CHECK4: omp.par.outlined.exit.exitStub:
// CHECK4-NEXT: ret void
//
//
// CHECK4-LABEL: define {{[^@]+}}@_Z3fooIPPcEvT_
Expand All @@ -958,4 +958,4 @@ int main (int argc, char **argv) {
// CHECK4-NEXT: call void @llvm.dbg.declare(metadata i8*** [[ARGC_ADDR]], metadata [[META70:![0-9]+]], metadata !DIExpression()), !dbg [[DBG71:![0-9]+]]
// CHECK4-NEXT: ret void, !dbg [[DBG71]]
//
//
//
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/IPO/IROutliner.cpp
Expand Up @@ -434,7 +434,6 @@ Function *IROutliner::createFunction(Module &M, OutlinableGroup &Group,
static BasicBlock *moveFunctionData(Function &Old, Function &New) {
Function::iterator CurrBB, NextBB, FinalBB;
BasicBlock *NewEnd = nullptr;
std::vector<Instruction *> DebugInsts;
for (CurrBB = Old.begin(), FinalBB = Old.end(); CurrBB != FinalBB;
CurrBB = NextBB) {
NextBB = std::next(CurrBB);
Expand All @@ -444,6 +443,8 @@ static BasicBlock *moveFunctionData(Function &Old, Function &New) {
if (isa<ReturnInst>(I))
NewEnd = &(*CurrBB);

std::vector<Instruction *> DebugInsts;

for (Instruction &Val : *CurrBB) {
// We must handle the scoping of called functions differently than
// other outlined instructions.
Expand Down
7 changes: 6 additions & 1 deletion llvm/lib/Transforms/Utils/CodeExtractor.cpp
Expand Up @@ -1389,12 +1389,17 @@ void CodeExtractor::moveCodeToFunction(Function *newFunction) {
Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList();
Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList();

auto newFuncIt = newFunction->front().getIterator();
for (BasicBlock *Block : Blocks) {
// Delete the basic block from the old function, and the list of blocks
oldBlocks.remove(Block);

// Insert this basic block into the new function
newBlocks.push_back(Block);
// Insert the original blocks after the entry block created
// for the new function. The entry block may be followed
// by a set of exit blocks at this point, but these exit
// blocks better be placed at the end of the new function.
newFuncIt = newBlocks.insertAfter(newFuncIt, Block);
}
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/test/Transforms/CodeExtractor/LoopExtractor.ll
Expand Up @@ -42,8 +42,6 @@ declare void @foo()
; CHECK-LABEL: define internal void @test3.loop.1()
; CHECK-NEXT: newFuncRoot:
; CHECK-NEXT: br label %loop.1
; CHECK: exit.exitStub:
; CHECK-NEXT: ret void
; CHECK: loop.1:
; CHECK-NEXT: %index.1 = phi i32 [ %next.1, %loop.1.loop.1_crit_edge ], [ 10, %newFuncRoot ]
; CHECK-NEXT: tail call void @foo()
Expand All @@ -52,12 +50,12 @@ declare void @foo()
; CHECK-NEXT: br i1 %repeat.1, label %loop.1.loop.1_crit_edge, label %exit.exitStub
; CHECK: loop.1.loop.1_crit_edge:
; CHECK-NEXT: br label %loop.1
; CHECK: exit.exitStub:
; CHECK-NEXT: ret void

; CHECK-LABEL: define internal void @test3.loop.0()
; CHECK-NEXT: newFuncRoot:
; CHECK-NEXT: br label %loop.0
; CHECK: loop.0.loop.1_crit_edge.exitStub:
; CHECK-NEXT: ret void
; CHECK: loop.0:
; CHECK-NEXT: %index.0 = phi i32 [ 10, %newFuncRoot ], [ %next.0, %loop.0.loop.0_crit_edge ]
; CHECK-NEXT: tail call void @foo()
Expand All @@ -66,3 +64,5 @@ declare void @foo()
; CHECK-NEXT: br i1 %repeat.0, label %loop.0.loop.0_crit_edge, label %loop.0.loop.1_crit_edge.exitStub
; CHECK: loop.0.loop.0_crit_edge:
; CHECK-NEXT: br label %loop.0
; CHECK: loop.0.loop.1_crit_edge.exitStub:
; CHECK-NEXT: ret void
4 changes: 2 additions & 2 deletions llvm/test/Transforms/CodeExtractor/LoopExtractor_crash.ll
Expand Up @@ -37,10 +37,10 @@ declare void @foo()
; CHECK-LABEL: define internal void @test.loopentry()
; CHECK-NEXT: newFuncRoot:
; CHECK-NEXT: br label %loopentry
; CHECK: loopexit.exitStub:
; CHECK-NEXT: ret void
; CHECK: loopentry:
; CHECK-NEXT: br i1 false, label %loopbody, label %loopexit.exitStub
; CHECK: loopbody:
; CHECK-NEXT: call void @foo()
; CHECK-NEXT: br label %loopentry
; CHECK: loopexit.exitStub:
; CHECK-NEXT: ret void
8 changes: 4 additions & 4 deletions llvm/test/Transforms/CodeExtractor/LoopExtractor_infinite.ll
Expand Up @@ -35,13 +35,13 @@ exit.1: ; preds = %loopentry
; CHECK-LABEL: define internal i1 @test.loopentry()
; CHECK-NEXT: newFuncRoot:
; CHECK-NEXT: br label %loopentry
; CHECK: exit.1.exitStub:
; CHECK-NEXT: ret i1 true
; CHECK: exit.0.exitStub:
; CHECK-NEXT: ret i1 false
; CHECK: loopentry:
; CHECK-NEXT: br i1 true, label %exit.1.exitStub, label %loopexit
; CHECK: loopexit:
; CHECK-NEXT: br i1 false, label %loopexit.loopentry_crit_edge, label %exit.0.exitStub
; CHECK: loopexit.loopentry_crit_edge:
; CHECK-NEXT: br label %loopentry
; CHECK: exit.1.exitStub:
; CHECK-NEXT: ret i1 true
; CHECK: exit.0.exitStub:
; CHECK-NEXT: ret i1 false
6 changes: 3 additions & 3 deletions llvm/test/Transforms/CodeExtractor/live_shrink_unsafe.ll
Expand Up @@ -72,13 +72,13 @@ bb:
; CHECK: newFuncRoot:
; CHECK-NEXT: br label %bb3

; CHECK: bb4.exitStub:
; CHECK-NEXT: ret void

; CHECK: bb3:
; CHECK-NOT: lifetime.ed
; CHECK: br label %bb4.exitStub

; CHECK: bb4.exitStub:
; CHECK-NEXT: ret void



!llvm.module.flags = !{!0}
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/CodeExtractor/unreachable-block.ll
Expand Up @@ -9,11 +9,11 @@
; CHECK-LABEL: define internal void @tinkywinky.1.ontrue() {
; CHECK-NEXT: newFuncRoot:
; CHECK-NEXT: br label %ontrue
; CHECK: onfalse{{.*}}:
; CHECK-NEXT: ret void
; CHECK: ontrue:
; CHECK-NEXT: call void @patatino()
; CHECK-NEXT: br label %onfalse{{.*}}
; CHECK: onfalse{{.*}}:
; CHECK-NEXT: ret void
; CHECK-NEXT: }

declare void @patatino()
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Transforms/HotColdSplit/multiple-exits.ll
Expand Up @@ -56,16 +56,16 @@ return: ; preds = %exit2, %exit1

; CHECK-LABEL: define {{.*}}@foo.cold.1(
; CHECK: br
; CHECK: [[exit1Stub:.*]]:
; CHECK-NEXT: ret i1 true
; CHECK: [[returnStub:.*]]:
; CHECK-NEXT: ret i1 false
; CHECK: call {{.*}}@sink
; CHECK-NEXT: [[cmp:%.*]] = icmp
; CHECK-NEXT: br i1 [[cmp]], label %[[exit1Stub]], label %exit2
; CHECK-NEXT: br i1 [[cmp]], label %[[exit1Stub:.*]], label %exit2
; CHECK-LABEL: exit2:
; CHECK-NEXT: call {{.*}}@sideeffect(i32 2)
; CHECK-NEXT: br label %[[returnStub]]
; CHECK-NEXT: br label %[[returnStub:.*]]
; CHECK: [[exit1Stub]]:
; CHECK-NEXT: ret i1 true
; CHECK: [[returnStub]]:
; CHECK-NEXT: ret i1 false

declare void @sink(...) cold

Expand Down
Expand Up @@ -86,11 +86,6 @@ entry:
}

; CHECK: define internal void @outlined_ir_func_0(i32* [[ARG0:%.*]], i32* [[ARG1:%.*]], i32* [[ARG2:%.*]], i32* [[ARG3:%.*]], i32* [[ARG4:%.*]], i32 [[ARG5:%.*]]) #1 {
; CHECK: _after_outline.exitStub:
; CHECK-NEXT: switch i32 [[ARG5]], label [[BLOCK:%.*]] [
; CHECK-NEXT: i32 0, label %[[BLOCK_0:.*]]
; CHECK-NEXT: i32 1, label %[[BLOCK_1:.*]]

; CHECK: entry_to_outline:
; CHECK-NEXT: store i32 2, i32* [[ARG0]], align 4
; CHECK-NEXT: store i32 3, i32* [[ARG1]], align 4
Expand All @@ -101,6 +96,11 @@ entry:
; CHECK-NEXT: store i32 [[ADD]], i32* [[ARG2]], align 4
; CHECK-NEXT: [[TMP2:%.*]] = load i32, i32* [[ARG2]], align 4

; CHECK: _after_outline.exitStub:
; CHECK-NEXT: switch i32 [[ARG5]], label [[BLOCK:%.*]] [
; CHECK-NEXT: i32 0, label %[[BLOCK_0:.*]]
; CHECK-NEXT: i32 1, label %[[BLOCK_1:.*]]

; CHECK: [[BLOCK_0]]:
; CHECK-NEXT: store i32 [[ADD]], i32* [[ARG3]], align 4
; CHECK-NEXT: store i32 [[TMP2]], i32* [[ARG4]], align 4
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Transforms/IROutliner/outlining-same-output-blocks.ll
Expand Up @@ -84,15 +84,15 @@ entry:
}

; CHECK: define internal void @outlined_ir_func_0(i32* [[ARG0:%.*]], i32* [[ARG1:%.*]], i32* [[ARG2:%.*]], i32* [[ARG3:%.*]], i32* [[ARG4:%.*]]) #1 {
; CHECK: entry_after_outline.exitStub:
; CHECK-NEXT: store i32 [[ADD:%.*]], i32* [[ARG3]], align 4
; CHECK-NEXT: store i32 [[TMP2:%.*]], i32* [[ARG4]], align 4

; CHECK: entry_to_outline:
; CHECK-NEXT: store i32 2, i32* [[ARG0]], align 4
; CHECK-NEXT: store i32 3, i32* [[ARG1]], align 4
; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARG0]], align 4
; CHECK-NEXT: [[TMP1:%.*]] = load i32, i32* [[ARG1]], align 4
; CHECK-NEXT: [[ADD]] = add i32 [[TMP0]], [[TMP1]]
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[TMP0]], [[TMP1]]
; CHECK-NEXT: store i32 [[ADD]], i32* [[ARG2]], align 4
; CHECK-NEXT: [[TMP2]] = load i32, i32* [[ARG2]], align 4
; CHECK-NEXT: [[TMP2:%.*]] = load i32, i32* [[ARG2]], align 4

; CHECK: entry_after_outline.exitStub:
; CHECK-NEXT: store i32 [[ADD]], i32* [[ARG3]], align 4
; CHECK-NEXT: store i32 [[TMP2]], i32* [[ARG4]], align 4

0 comments on commit 2e192ab

Please sign in to comment.