[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
-code for the loop body. To begin with, we move the insertion point and
-create the PHI node for the loop induction variable. Since we already
+code for the loop condition check. To begin with, we move the insertion point
+and create the Phi node for the loop induction variable. Since we already
know the incoming value for the starting value, we add it to the Phi
-node. Note that the Phi will eventually get a second value for the
+node. Note that the Phi node will eventually get a second value for the
backedge, but we can't set it up yet (because it doesn't exist!).
.. code-block:: c++
- // Within the loop, the variable is defined equal to the PHI node. If it
+ // Within the loop, the variable is defined equal to the PHI node. If it
// shadows an existing variable, we have to restore it, so save it now.
Value *OldVal = NamedValues[VarName];
NamedValues[VarName] = Variable;
- // Emit the body of the loop. This, like any other expr, can change the
- // current BB. Note that we ignore the value computed by the body, but don't
- // allow an error.
- if (!Body->codegen())
+ // Compute the end condition.
+ Value *EndCond = End->codegen();
----------------
Logikable wrote:
Perhaps we can also rename "End" to "Cond".
https://github.com/llvm/llvm-project/pull/88207
More information about the llvm-commits
mailing list