Skip to content

Commit fbf6c8a

Browse files
eopXDeopXD
authored andcommitted
[LoopVersioning] Allow versionLoop to create plain branch inst when no runtime check is specified
After this function call, the LLVM IR would look like the following: ``` if (true) /* NonVersionedLoop */ else /* VersionedLoop */ ``` Reviewed By: Whitney Differential Revision: https://reviews.llvm.org/D104631
1 parent 68cb111 commit fbf6c8a

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

llvm/include/llvm/Transforms/Utils/LoopVersioning.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class LoopVersioning {
7575
/// loop may alias (i.e. one of the memchecks failed).
7676
Loop *getNonVersionedLoop() { return NonVersionedLoop; }
7777

78+
/// Returns the basic block that contains the runtime check BranchInst
79+
BasicBlock *getRuntimeCheckBB() { return RuntimeCheckBB; }
80+
81+
/// Returns the runtime check BranchInst
82+
BranchInst *getRuntimeCheckBI() { return RuntimeCheckBI; }
83+
7884
/// Annotate memory instructions in the versioned loop with no-alias
7985
/// metadata based on the memchecks issued.
8086
///
@@ -115,6 +121,12 @@ class LoopVersioning {
115121
/// loop may alias (memchecks failed).
116122
Loop *NonVersionedLoop;
117123

124+
/// The basic Block that stores the BranchInst to Versioned / NonVersioned
125+
BasicBlock *RuntimeCheckBB;
126+
127+
/// The branch instruction to Versioned / NonVersioned
128+
BranchInst *RuntimeCheckBI;
129+
118130
/// This maps the instructions from VersionedLoop to their counterpart
119131
/// in NonVersionedLoop.
120132
ValueToValueMapTy VMap;

llvm/lib/Transforms/Utils/LoopVersioning.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
using namespace llvm;
3232

33+
#define LVER_OPTION "loop-versioning"
34+
#define DEBUG_TYPE LVER_OPTION
35+
3336
static cl::opt<bool>
3437
AnnotateNoAlias("loop-version-annotate-no-alias", cl::init(true),
3538
cl::Hidden,
@@ -84,8 +87,11 @@ void LoopVersioning::versionLoop(
8487
} else
8588
RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck;
8689

87-
assert(RuntimeCheck && "called even though we don't need "
88-
"any runtime checks");
90+
if (!RuntimeCheck) {
91+
LLVM_DEBUG(dbgs() << DEBUG_TYPE " No MemRuntimeCheck or SCEVRuntimeCheck found"
92+
<< ", set RuntimeCheck to False\n");
93+
RuntimeCheck = ConstantInt::getFalse(RuntimeCheckBB->getContext());
94+
}
8995

9096
// Rename the block to make the IR more readable.
9197
RuntimeCheckBB->setName(VersionedLoop->getHeader()->getName() +
@@ -109,8 +115,8 @@ void LoopVersioning::versionLoop(
109115

110116
// Insert the conditional branch based on the result of the memchecks.
111117
Instruction *OrigTerm = RuntimeCheckBB->getTerminator();
112-
BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
113-
VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
118+
RuntimeCheckBI = BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
119+
VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
114120
OrigTerm->eraseFromParent();
115121

116122
// The loops merge in the original exit block. This is now dominated by the
@@ -125,6 +131,9 @@ void LoopVersioning::versionLoop(
125131
assert(NonVersionedLoop->isLoopSimplifyForm() &&
126132
VersionedLoop->isLoopSimplifyForm() &&
127133
"The versioned loops should be in simplify form.");
134+
135+
// RuntimeCheckBB and RuntimeCheckBI is recorded
136+
assert(RuntimeCheckBB && RuntimeCheckBI);
128137
}
129138

130139
void LoopVersioning::addPHINodes(
@@ -324,9 +333,6 @@ class LoopVersioningLegacyPass : public FunctionPass {
324333
};
325334
}
326335

327-
#define LVER_OPTION "loop-versioning"
328-
#define DEBUG_TYPE LVER_OPTION
329-
330336
char LoopVersioningLegacyPass::ID;
331337
static const char LVer_name[] = "Loop Versioning";
332338

0 commit comments

Comments
 (0)