[llvm] [Kaleidoscope] Fix ForExprAST::codegen (PR #88207)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 10 10:11:15 PDT 2024
================
@@ -637,75 +639,113 @@ expression for the loop value:
return nullptr;
With this out of the way, the next step is to set up the LLVM basic
-block for the start of the loop body. In the case above, the whole loop
-body is one block, but remember that the body code itself could consist
+blocks that make up the for-loop statement. In the case above, the whole
+loop body is one block, but remember that the body code itself could consist
of multiple blocks (e.g. if it contains an if/then/else or a for/in
expression).
.. code-block:: c++
- // Make the new basic block for the loop header, inserting after current
- // block.
+ // Make new basic blocks for loop condition, loop body and end-loop code.
Function *TheFunction = Builder->GetInsertBlock()->getParent();
- BasicBlock *PreheaderBB = Builder->GetInsertBlock();
- BasicBlock *LoopBB =
- BasicBlock::Create(*TheContext, "loop", TheFunction);
+ BasicBlock *PreLoopBB = Builder->GetInsertBlock();
+ BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
+ TheFunction);
+ BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop");
+ BasicBlock *EndLoopBB = BasicBlock::Create(*TheContext, "endloop");
- // Insert an explicit fall through from the current block to the LoopBB.
- Builder->CreateBr(LoopBB);
+ // Insert an explicit fall through from current block to LoopConditionBB.
+ Builder->CreateBr(LoopConditionBB);
This code is similar to what we saw for if/then/else. Because we will
need it to create the Phi node, we remember the block that falls through
-into the loop. Once we have that, we create the actual block that starts
-the loop and create an unconditional branch for the fall-through between
-the two blocks.
+into the loop condition check. Once we have that, we create the actual block
+that determines if control should enter the loop and attach it directly to the
+end of our parent function. The other two blocks (``LoopBB`` and ``EndLoopBB``)
+are created, but aren't inserted into the function, similarly to our previous
+work on if/then/else. These will be used to complete our loop IR later on.
+
+We also create an unconditional branch into the loop condition block from the
+pre-loop code.
.. code-block:: c++
- // Start insertion in LoopBB.
- Builder->SetInsertPoint(LoopBB);
+ // Start insertion in LoopConditionBB.
+ Builder->SetInsertPoint(LoopConditionBB);
// Start the PHI node with an entry for Start.
- PHINode *Variable = Builder->CreatePHI(Type::getDoubleTy(*TheContext),
- 2, VarName);
- Variable->addIncoming(StartVal, PreheaderBB);
+ PHINode *Variable =
+ Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, VarName);
+ Variable->addIncoming(StartVal, PreLoopBB);
Now that the "preheader" for the loop is set up, we switch to emitting
----------------
Logikable wrote:
Since PreheaderBB was renamed, we can pick a different name for "preheader". This name appears again later.
https://github.com/llvm/llvm-project/pull/88207
More information about the llvm-commits
mailing list