[llvm] fbf6c8a - [LoopVersioning] Allow versionLoop to create plain branch inst when no runtime check is specified

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 16 00:41:35 PST 2021


Author: Yueh-Ting Chen
Date: 2021-12-16T00:41:29-08:00
New Revision: fbf6c8ac1589a4be68ee549257f1d528937ac582

URL: https://github.com/llvm/llvm-project/commit/fbf6c8ac1589a4be68ee549257f1d528937ac582
DIFF: https://github.com/llvm/llvm-project/commit/fbf6c8ac1589a4be68ee549257f1d528937ac582.diff

LOG: [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

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Utils/LoopVersioning.h
    llvm/lib/Transforms/Utils/LoopVersioning.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Utils/LoopVersioning.h b/llvm/include/llvm/Transforms/Utils/LoopVersioning.h
index 4a8831ed45b25..abf5acc377e4e 100644
--- a/llvm/include/llvm/Transforms/Utils/LoopVersioning.h
+++ b/llvm/include/llvm/Transforms/Utils/LoopVersioning.h
@@ -75,6 +75,12 @@ class LoopVersioning {
   /// loop may alias (i.e. one of the memchecks failed).
   Loop *getNonVersionedLoop() { return NonVersionedLoop; }
 
+  /// Returns the basic block that contains the runtime check BranchInst
+  BasicBlock *getRuntimeCheckBB() { return RuntimeCheckBB; }
+
+  /// Returns the runtime check BranchInst
+  BranchInst *getRuntimeCheckBI() { return RuntimeCheckBI; }
+
   /// Annotate memory instructions in the versioned loop with no-alias
   /// metadata based on the memchecks issued.
   ///
@@ -115,6 +121,12 @@ class LoopVersioning {
   /// loop may alias (memchecks failed).
   Loop *NonVersionedLoop;
 
+  /// The basic Block that stores the BranchInst to Versioned / NonVersioned
+  BasicBlock *RuntimeCheckBB;
+
+  /// The branch instruction to Versioned / NonVersioned
+  BranchInst *RuntimeCheckBI;
+
   /// This maps the instructions from VersionedLoop to their counterpart
   /// in NonVersionedLoop.
   ValueToValueMapTy VMap;

diff  --git a/llvm/lib/Transforms/Utils/LoopVersioning.cpp b/llvm/lib/Transforms/Utils/LoopVersioning.cpp
index 771b7d25b0f20..15a40cac8c110 100644
--- a/llvm/lib/Transforms/Utils/LoopVersioning.cpp
+++ b/llvm/lib/Transforms/Utils/LoopVersioning.cpp
@@ -30,6 +30,9 @@
 
 using namespace llvm;
 
+#define LVER_OPTION "loop-versioning"
+#define DEBUG_TYPE LVER_OPTION
+
 static cl::opt<bool>
     AnnotateNoAlias("loop-version-annotate-no-alias", cl::init(true),
                     cl::Hidden,
@@ -84,8 +87,11 @@ void LoopVersioning::versionLoop(
   } else
     RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck;
 
-  assert(RuntimeCheck && "called even though we don't need "
-                         "any runtime checks");
+  if (!RuntimeCheck) {
+    LLVM_DEBUG(dbgs() << DEBUG_TYPE " No MemRuntimeCheck or SCEVRuntimeCheck found"
+                      << ", set RuntimeCheck to False\n");
+    RuntimeCheck = ConstantInt::getFalse(RuntimeCheckBB->getContext());
+  }
 
   // Rename the block to make the IR more readable.
   RuntimeCheckBB->setName(VersionedLoop->getHeader()->getName() +
@@ -109,8 +115,8 @@ void LoopVersioning::versionLoop(
 
   // Insert the conditional branch based on the result of the memchecks.
   Instruction *OrigTerm = RuntimeCheckBB->getTerminator();
-  BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
-                     VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
+  RuntimeCheckBI = BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
+                           VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
   OrigTerm->eraseFromParent();
 
   // The loops merge in the original exit block.  This is now dominated by the
@@ -125,6 +131,9 @@ void LoopVersioning::versionLoop(
   assert(NonVersionedLoop->isLoopSimplifyForm() &&
          VersionedLoop->isLoopSimplifyForm() &&
          "The versioned loops should be in simplify form.");
+
+  // RuntimeCheckBB and RuntimeCheckBI is recorded
+  assert(RuntimeCheckBB && RuntimeCheckBI);
 }
 
 void LoopVersioning::addPHINodes(
@@ -324,9 +333,6 @@ class LoopVersioningLegacyPass : public FunctionPass {
 };
 }
 
-#define LVER_OPTION "loop-versioning"
-#define DEBUG_TYPE LVER_OPTION
-
 char LoopVersioningLegacyPass::ID;
 static const char LVer_name[] = "Loop Versioning";
 


        


More information about the llvm-commits mailing list