[llvm] [Kaleidoscope] Fix ForExprAST::codegen (PR #88207)

Vlad Mishel via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 10 19:10:15 PDT 2024


https://github.com/vmishelcs updated https://github.com/llvm/llvm-project/pull/88207

>From 3f21a33bec221cda6dab8164eaf3055cc6bd931b Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Mon, 8 Apr 2024 01:11:29 -0700
Subject: [PATCH 01/12] Implemented a fix for ForExprAST::codegen in Ch-5 code

---
 llvm/examples/Kaleidoscope/Chapter5/toy.cpp | 95 ++++++++++++---------
 1 file changed, 56 insertions(+), 39 deletions(-)

diff --git a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
index 48936bddb1d4f2..d76834d1830cf3 100644
--- a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
@@ -682,53 +682,78 @@ Value *IfExprAST::codegen() {
 }
 
 // Output for-loop as:
+// preloop:
 //   ...
 //   start = startexpr
-//   goto loop
+//   goto loopcondition
+// loopcondition:
+//   variable = phi [start, loopheader], [nextvariable, loop]
+//   endcond = endexpr
+//   br endcond, loop, endloop
 // loop:
-//   variable = phi [start, loopheader], [nextvariable, loopend]
 //   ...
 //   bodyexpr
 //   ...
-// loopend:
 //   step = stepexpr
 //   nextvariable = variable + step
-//   endcond = endexpr
-//   br endcond, loop, endloop
-// outloop:
+//   goto loopcondition
+// endloop:
+//   ...
 Value *ForExprAST::codegen() {
   // Emit the start code first, without 'variable' in scope.
   Value *StartVal = Start->codegen();
   if (!StartVal)
     return nullptr;
 
-  // Make the new basic block for the loop header, inserting after current
-  // block.
+  // Make new basic blocks for pre-loop, 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);
 
-  // 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);
+  Variable->addIncoming(StartVal, PreLoopBB);
 
-  // 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();
+  if (!EndCond)
+    return nullptr;
+
+  // Convert condition to a bool by comparing non-equal to 0.0.
+  EndCond = Builder->CreateFCmpONE(
+      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "endcond");
+
+  // Insert the conditional branch that either continues the loop, or exits the
+  // loop.
+  Builder->CreateCondBr(EndCond, LoopBB, EndLoopBB);
+
+  // Attach the basic block that will soon hold the loop body to the end of the
+  // parent function.
+  TheFunction->insert(TheFunction->end(), LoopBB);
+
+  // Emit the loop body within the LoopBB. 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.
+  Builder->SetInsertPoint(LoopBB);
+  if (!Body->codegen()) {
     return nullptr;
+  }
 
   // Emit the step value.
   Value *StepVal = nullptr;
@@ -743,28 +768,20 @@ Value *ForExprAST::codegen() {
 
   Value *NextVar = Builder->CreateFAdd(Variable, StepVal, "nextvar");
 
-  // Compute the end condition.
-  Value *EndCond = End->codegen();
-  if (!EndCond)
-    return nullptr;
-
-  // Convert condition to a bool by comparing non-equal to 0.0.
-  EndCond = Builder->CreateFCmpONE(
-      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
-
-  // Create the "after loop" block and insert it.
-  BasicBlock *LoopEndBB = Builder->GetInsertBlock();
-  BasicBlock *AfterBB =
-      BasicBlock::Create(*TheContext, "afterloop", TheFunction);
+  // Add a new entry to the PHI node for the backedge.
+  LoopBB = Builder->GetInsertBlock();
+  Variable->addIncoming(NextVar, LoopBB);
 
-  // Insert the conditional branch into the end of LoopEndBB.
-  Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
+  // Create the unconditional branch that returns to LoopConditionBB to
+  // determine if we should continue looping.
+  Builder->CreateBr(LoopConditionBB);
 
-  // Any new code will be inserted in AfterBB.
-  Builder->SetInsertPoint(AfterBB);
+  // Append EndLoopBB after the loop body. We go to this basic block if the
+  // loop condition says we should not loop anymore.
+  TheFunction->insert(TheFunction->end(), EndLoopBB);
 
-  // Add a new entry to the PHI node for the backedge.
-  Variable->addIncoming(NextVar, LoopEndBB);
+  // Any new code will be inserted after the loop.
+  Builder->SetInsertPoint(EndLoopBB);
 
   // Restore the unshadowed variable.
   if (OldVal)

>From 63d526b50216f891322e5fabe8e6aaee3f2b54ca Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Mon, 8 Apr 2024 20:06:15 -0700
Subject: [PATCH 02/12] Updated `LangImpl05.rst` to use correct Ch5 code

---
 .../MyFirstLanguageFrontend/LangImpl05.rst    | 196 ++++++++++--------
 1 file changed, 112 insertions(+), 84 deletions(-)

diff --git a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
index 0039547e8c7baf..8edd4d6db694d5 100644
--- a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
+++ b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
@@ -598,22 +598,24 @@ this dump is generated with optimizations disabled for clarity):
     define double @printstar(double %n) {
     entry:
       ; initial value = 1.0 (inlined into phi)
-      br label %loop
+      br label %loopcond
 
-    loop:       ; preds = %loop, %entry
+    loopcond:   ; preds = %loop, %entry
       %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ]
+      ; termination test
+      %cmptmp = fcmp ult double %i, %n
+      %booltmp = uitofp i1 %cmptmp to double
+      %endcond = fcmp one double %booltmp, 0.000000e+00
+      br i1 %endcond, label %loop, label %endloop
+
+    loop:       ; preds = %loopcond
       ; body
       %calltmp = call double @putchard(double 4.200000e+01)
       ; increment
       %nextvar = fadd double %i, 1.000000e+00
+      br label %loopcond
 
-      ; termination test
-      %cmptmp = fcmp ult double %i, %n
-      %booltmp = uitofp i1 %cmptmp to double
-      %loopcond = fcmp one double %booltmp, 0.000000e+00
-      br i1 %loopcond, label %loop, label %afterloop
-
-    afterloop:      ; preds = %loop
+    endloop:    ; preds = %loopcond
       ; loop always returns 0.0
       ret double 0.000000e+00
     }
@@ -637,75 +639,114 @@ 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 pre-loop, 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.
+
+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();
+      if (!EndCond)
         return nullptr;
 
-Now the code starts to get more interesting. Our 'for' loop introduces a
-new variable to the symbol table. This means that our symbol table can
-now contain either function arguments or loop variables. To handle this,
-before we codegen the body of the loop, we add the loop variable as the
-current value for its name. Note that it is possible that there is a
-variable of the same name in the outer scope. It would be easy to make
+Our for-loop introduces a new variable to the symbol table. This means that
+our symbol table can now contain either function arguments or loop variables.
+To handle this, before we codegen the remainder of the loop, we add the loop
+variable as the current value for its name. Note that it is possible there
+is a variable of the same name in the outer scope. It would be easy to make
 this an error (emit an error and return null if there is already an
 entry for VarName) but we choose to allow shadowing of variables. In
 order to handle this correctly, we remember the Value that we are
 potentially shadowing in ``OldVal`` (which will be null if there is no
-shadowed variable).
+shadowed variable). This allows the loop body (which we will codegen soon) to
+use the loop variable: any references to it will naturally find it in the 
+symbol table.
+
+Once the loop variable is set into the symbol table, we codegen the condition
+that determines if we can enter into the loop (or continue looping, depending
+on if we are arriving from the "preheader" or the loop body).
+
+.. code-block:: c++
+
+      // Convert condition to a bool by comparing non-equal to 0.0.
+      EndCond = Builder->CreateFCmpONE(
+          EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "endcond");
+
+      // Insert the conditional branch that either continues the loop, or exits the
+      // loop.
+      Builder->CreateCondBr(EndCond, LoopBB, EndLoopBB);
 
-Once the loop variable is set into the symbol table, the code
-recursively codegen's the body. This allows the body to use the loop
-variable: any references to it will naturally find it in the symbol
-table.
+As with if/then/else, after emitting the condition, we compare that value to
+zero to get a truth value as a 1-bit (bool) value. Next we emit the conditional
+branch that chooses if we enter the loop body, or move on to the post-loop code.
+
+.. code-block:: c++
+
+      // Attach the basic block that will soon hold the loop body to the end of the
+      // parent function.
+      TheFunction->insert(TheFunction->end(), LoopBB);
+
+      // Emit the loop body within the LoopBB. 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.
+      Builder->SetInsertPoint(LoopBB);
+      if (!Body->codegen()) {
+        return nullptr;
+      }
+
+Next, we insert our basic block that will soon hold the loop body to the end of 
+the specified function. We recursively codegen the body, remembering to update
+the IRBuilder's insert point to the basic block that is supposed to hold the
+loop body beforehand.
 
 .. code-block:: c++
 
@@ -723,51 +764,42 @@ table.
       Value *NextVar = Builder->CreateFAdd(Variable, StepVal, "nextvar");
 
 Now that the body is emitted, we compute the next value of the iteration
-variable by adding the step value, or 1.0 if it isn't present.
-'``NextVar``' will be the value of the loop variable on the next
-iteration of the loop.
+variable by adding the step value, or 1.0 if it isn't present. ``NextVar``
+will be the value of the loop variable on the next iteration of the loop.
 
 .. code-block:: c++
 
-      // Compute the end condition.
-      Value *EndCond = End->codegen();
-      if (!EndCond)
-        return nullptr;
+      // Add a new entry to the PHI node for the backedge.
+      LoopBB = Builder->GetInsertBlock();
+      Variable->addIncoming(NextVar, LoopBB);
 
-      // Convert condition to a bool by comparing non-equal to 0.0.
-      EndCond = Builder->CreateFCmpONE(
-          EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
+      // Create the unconditional branch that returns to LoopConditionBB to
+      // determine if we should continue looping.
+      Builder->CreateBr(LoopConditionBB);
 
-Finally, we evaluate the exit value of the loop, to determine whether
-the loop should exit. This mirrors the condition evaluation for the
-if/then/else statement.
+Here, similarly to how we did it in our implementation of the if/then/else
+statement, we get an up-to-date value for ``LoopBB`` (because it's possible
+that the loop body has changed the basic block that the Builder is emitting
+into) and use it to add a backedge to our Phi node. This backedge denotes
+the value of the incremented loop variable. We also create an unconditional
+branch back to the basic block that performs the check if we should continue
+looping. This completes the loop body code.
 
 .. code-block:: c++
 
-      // Create the "after loop" block and insert it.
-      BasicBlock *LoopEndBB = Builder->GetInsertBlock();
-      BasicBlock *AfterBB =
-          BasicBlock::Create(*TheContext, "afterloop", TheFunction);
+      // Append EndLoopBB after the loop body. We go to this basic block if the
+      // loop condition says we should not loop anymore.
+      TheFunction->insert(TheFunction->end(), EndLoopBB);
 
-      // Insert the conditional branch into the end of LoopEndBB.
-      Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
+      // Any new code will be inserted after the loop.
+      Builder->SetInsertPoint(EndLoopBB);
 
-      // Any new code will be inserted in AfterBB.
-      Builder->SetInsertPoint(AfterBB);
-
-With the code for the body of the loop complete, we just need to finish
-up the control flow for it. This code remembers the end block (for the
-phi node), then creates the block for the loop exit ("afterloop"). Based
-on the value of the exit condition, it creates a conditional branch that
-chooses between executing the loop again and exiting the loop. Any
-future code is emitted in the "afterloop" block, so it sets the
-insertion position to it.
+Finally, we append the post-loop basic block created earlier (denoted by 
+``EndLoopBB``) to the parent function, and update the IRBuilder's insert point
+such that any new subsequent code is generated in that post-loop basic block.
 
 .. code-block:: c++
 
-      // Add a new entry to the PHI node for the backedge.
-      Variable->addIncoming(NextVar, LoopEndBB);
-
       // Restore the unshadowed variable.
       if (OldVal)
         NamedValues[VarName] = OldVal;
@@ -778,12 +810,9 @@ insertion position to it.
       return Constant::getNullValue(Type::getDoubleTy(*TheContext));
     }
 
-The final code handles various cleanups: now that we have the "NextVar"
-value, we can add the incoming value to the loop PHI node. After that,
-we remove the loop variable from the symbol table, so that it isn't in
-scope after the for loop. Finally, code generation of the for loop
-always returns 0.0, so that is what we return from
-``ForExprAST::codegen()``.
+The final bit of code handles some clean-ups: We remove the loop variable from
+the symbol table so that it isn't in scope after the for-loop, and return a 0.0
+value.
 
 With this, we conclude the "adding control flow to Kaleidoscope" chapter
 of the tutorial. In this chapter we added two control flow constructs,
@@ -811,4 +840,3 @@ Here is the code:
    :language: c++
 
 `Next: Extending the language: user-defined operators <LangImpl06.html>`_
-

>From 81f62daec97b9dc1b1a2a22da314e2ef2a40e12b Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Mon, 8 Apr 2024 20:30:54 -0700
Subject: [PATCH 03/12] Updated Kaleidoscope Ch-6 code listing

---
 llvm/examples/Kaleidoscope/Chapter6/toy.cpp | 95 ++++++++++++---------
 1 file changed, 56 insertions(+), 39 deletions(-)

diff --git a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
index ebe4322287b21f..c29f1e719990e3 100644
--- a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
@@ -794,53 +794,78 @@ Value *IfExprAST::codegen() {
 }
 
 // Output for-loop as:
+// preloop:
 //   ...
 //   start = startexpr
-//   goto loop
+//   goto loopcondition
+// loopcondition:
+//   variable = phi [start, loopheader], [nextvariable, loop]
+//   endcond = endexpr
+//   br endcond, loop, endloop
 // loop:
-//   variable = phi [start, loopheader], [nextvariable, loopend]
 //   ...
 //   bodyexpr
 //   ...
-// loopend:
 //   step = stepexpr
 //   nextvariable = variable + step
-//   endcond = endexpr
-//   br endcond, loop, endloop
-// outloop:
+//   goto loopcondition
+// endloop:
+//   ...
 Value *ForExprAST::codegen() {
   // Emit the start code first, without 'variable' in scope.
   Value *StartVal = Start->codegen();
   if (!StartVal)
     return nullptr;
 
-  // Make the new basic block for the loop header, inserting after current
-  // block.
+  // Make new basic blocks for pre-loop, 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);
 
-  // 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);
+  Variable->addIncoming(StartVal, PreLoopBB);
 
-  // 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();
+  if (!EndCond)
+    return nullptr;
+
+  // Convert condition to a bool by comparing non-equal to 0.0.
+  EndCond = Builder->CreateFCmpONE(
+      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "endcond");
+
+  // Insert the conditional branch that either continues the loop, or exits the
+  // loop.
+  Builder->CreateCondBr(EndCond, LoopBB, EndLoopBB);
+
+  // Attach the basic block that will soon hold the loop body to the end of the
+  // parent function.
+  TheFunction->insert(TheFunction->end(), LoopBB);
+
+  // Emit the loop body within the LoopBB. 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.
+  Builder->SetInsertPoint(LoopBB);
+  if (!Body->codegen()) {
     return nullptr;
+  }
 
   // Emit the step value.
   Value *StepVal = nullptr;
@@ -855,28 +880,20 @@ Value *ForExprAST::codegen() {
 
   Value *NextVar = Builder->CreateFAdd(Variable, StepVal, "nextvar");
 
-  // Compute the end condition.
-  Value *EndCond = End->codegen();
-  if (!EndCond)
-    return nullptr;
-
-  // Convert condition to a bool by comparing non-equal to 0.0.
-  EndCond = Builder->CreateFCmpONE(
-      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
-
-  // Create the "after loop" block and insert it.
-  BasicBlock *LoopEndBB = Builder->GetInsertBlock();
-  BasicBlock *AfterBB =
-      BasicBlock::Create(*TheContext, "afterloop", TheFunction);
+  // Add a new entry to the PHI node for the backedge.
+  LoopBB = Builder->GetInsertBlock();
+  Variable->addIncoming(NextVar, LoopBB);
 
-  // Insert the conditional branch into the end of LoopEndBB.
-  Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
+  // Create the unconditional branch that returns to LoopConditionBB to
+  // determine if we should continue looping.
+  Builder->CreateBr(LoopConditionBB);
 
-  // Any new code will be inserted in AfterBB.
-  Builder->SetInsertPoint(AfterBB);
+  // Append EndLoopBB after the loop body. We go to this basic block if the
+  // loop condition says we should not loop anymore.
+  TheFunction->insert(TheFunction->end(), EndLoopBB);
 
-  // Add a new entry to the PHI node for the backedge.
-  Variable->addIncoming(NextVar, LoopEndBB);
+  // Any new code will be inserted after the loop.
+  Builder->SetInsertPoint(EndLoopBB);
 
   // Restore the unshadowed variable.
   if (OldVal)

>From 19fdb0f3b19dcbef565d22568250a11078dc3357 Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Mon, 8 Apr 2024 21:57:32 -0700
Subject: [PATCH 04/12] Changed some comments in Ch5 for clarity

---
 .../docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst | 5 ++---
 llvm/examples/Kaleidoscope/Chapter5/toy.cpp              | 9 ++++-----
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
index 8edd4d6db694d5..128d178d42bdd2 100644
--- a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
+++ b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
@@ -646,8 +646,7 @@ expression).
 
 .. code-block:: c++
 
-      // Make new basic blocks for pre-loop, loop condition, loop body and end-loop
-      // code. 
+      // Make new basic blocks for loop condition, loop body and end-loop code.
       Function *TheFunction = Builder->GetInsertBlock()->getParent();
       BasicBlock *PreLoopBB = Builder->GetInsertBlock();
       BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
@@ -664,7 +663,7 @@ 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.
+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.
diff --git a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
index d76834d1830cf3..74f4a91e6ff7b0 100644
--- a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
@@ -685,8 +685,8 @@ Value *IfExprAST::codegen() {
 // preloop:
 //   ...
 //   start = startexpr
-//   goto loopcondition
-// loopcondition:
+//   goto loopcond
+// loopcond:
 //   variable = phi [start, loopheader], [nextvariable, loop]
 //   endcond = endexpr
 //   br endcond, loop, endloop
@@ -696,7 +696,7 @@ Value *IfExprAST::codegen() {
 //   ...
 //   step = stepexpr
 //   nextvariable = variable + step
-//   goto loopcondition
+//   goto loopcond
 // endloop:
 //   ...
 Value *ForExprAST::codegen() {
@@ -705,8 +705,7 @@ Value *ForExprAST::codegen() {
   if (!StartVal)
     return nullptr;
 
-  // Make new basic blocks for pre-loop, loop condition, loop body and end-loop
-  // code. 
+  // Make new basic blocks for loop condition, loop body and end-loop code. 
   Function *TheFunction = Builder->GetInsertBlock()->getParent();
   BasicBlock *PreLoopBB = Builder->GetInsertBlock();
   BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",

>From 7426bf29e7efc0da1d5793ec1bd92047d8e7a37a Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Mon, 8 Apr 2024 22:43:52 -0700
Subject: [PATCH 05/12] Fixed ForExprAST::codegen in Ch7 and updated rst

---
 .../MyFirstLanguageFrontend/LangImpl07.rst    | 16 ++-
 llvm/examples/Kaleidoscope/Chapter7/toy.cpp   | 97 +++++++++++--------
 2 files changed, 62 insertions(+), 51 deletions(-)

diff --git a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
index 8fd4c39d3ff47b..0bb0a0835b8376 100644
--- a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
+++ b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
@@ -384,17 +384,16 @@ the unabridged code):
 
       // Store the value into the alloca.
       Builder->CreateStore(StartVal, Alloca);
-      ...
 
-      // Compute the end condition.
-      Value *EndCond = End->codegen();
-      if (!EndCond)
-        return nullptr;
+      // If the loop variable shadows an existing variable, we have to restore it,
+      // so save it now. Set VarName to refer to our recently created alloca.
+      AllocaInst *OldVal = NamedValues[VarName];
+      NamedValues[VarName] = Alloca;
+      ...
 
-      // Reload, increment, and restore the alloca.  This handles the case where
-      // the body of the loop mutates the variable.
+      // Load, increment and store the new loop variable.
       Value *CurVar = Builder->CreateLoad(Alloca->getAllocatedType(), Alloca,
-                                          VarName.c_str());
+                                          VarName);
       Value *NextVar = Builder->CreateFAdd(CurVar, StepVal, "nextvar");
       Builder->CreateStore(NextVar, Alloca);
       ...
@@ -883,4 +882,3 @@ Here is the code:
    :language: c++
 
 `Next: Compiling to Object Code <LangImpl08.html>`_
-
diff --git a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
index 68208c4f3394ab..a5b8aad3ca2417 100644
--- a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
@@ -905,20 +905,21 @@ Value *IfExprAST::codegen() {
 //   ...
 //   start = startexpr
 //   store start -> var
-//   goto loop
+//   goto loopcond
+// loopcond:
+//   endcond = endexpr
+//   br endcond, loop, endloop
 // loop:
 //   ...
 //   bodyexpr
 //   ...
-// loopend:
 //   step = stepexpr
-//   endcond = endexpr
-//
 //   curvar = load var
 //   nextvar = curvar + step
 //   store nextvar -> var
-//   br endcond, loop, endloop
-// outloop:
+//   goto loopcond
+// endloop:
+//   ...
 Value *ForExprAST::codegen() {
   Function *TheFunction = Builder->GetInsertBlock()->getParent();
 
@@ -933,26 +934,47 @@ Value *ForExprAST::codegen() {
   // Store the value into the alloca.
   Builder->CreateStore(StartVal, Alloca);
 
-  // Make the new basic block for the loop header, inserting after current
-  // block.
-  BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop", TheFunction);
+  // If the loop variable shadows an existing variable, we have to restore it,
+  // so save it now. Set VarName to refer to our recently created alloca.
+  AllocaInst *OldVal = NamedValues[VarName];
+  NamedValues[VarName] = Alloca;
 
-  // Insert an explicit fall through from the current block to the LoopBB.
-  Builder->CreateBr(LoopBB);
+  // Make new basic blocks for loop condition, loop body and end-loop code.
+  BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
+      TheFunction);
+  BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop");
+  BasicBlock *EndLoopBB = BasicBlock::Create(*TheContext, "endloop");
 
-  // Start insertion in LoopBB.
-  Builder->SetInsertPoint(LoopBB);
+  // Insert an explicit fall through from current block to LoopConditionBB.
+  Builder->CreateBr(LoopConditionBB);
 
-  // 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.
-  AllocaInst *OldVal = NamedValues[VarName];
-  NamedValues[VarName] = Alloca;
+  // Start insertion in LoopConditionBB.
+  Builder->SetInsertPoint(LoopConditionBB);
+
+  // Compute the end condition.
+  Value *EndCond = End->codegen();
+  if (!EndCond)
+    return nullptr;
 
-  // 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())
+  // Convert condition to a bool by comparing non-equal to 0.0.
+  EndCond = Builder->CreateFCmpONE(
+      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "endcond");
+
+  // Insert the conditional branch that either continues the loop, or exits the
+  // loop.
+  Builder->CreateCondBr(EndCond, LoopBB, EndLoopBB);
+
+  // Attach the basic block that will soon hold the loop body to the end of the
+  // parent function.
+  TheFunction->insert(TheFunction->end(), LoopBB);
+
+  // Emit the loop body within the LoopBB. 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.
+  Builder->SetInsertPoint(LoopBB);
+  if (!Body->codegen()) {
     return nullptr;
+  }
 
   // Emit the step value.
   Value *StepVal = nullptr;
@@ -965,31 +987,22 @@ Value *ForExprAST::codegen() {
     StepVal = ConstantFP::get(*TheContext, APFloat(1.0));
   }
 
-  // Compute the end condition.
-  Value *EndCond = End->codegen();
-  if (!EndCond)
-    return nullptr;
-
-  // Reload, increment, and restore the alloca.  This handles the case where
-  // the body of the loop mutates the variable.
-  Value *CurVar =
-      Builder->CreateLoad(Alloca->getAllocatedType(), Alloca, VarName.c_str());
+  // Load, increment and store the new loop variable.
+  Value *CurVar = Builder->CreateLoad(Alloca->getAllocatedType(), Alloca,
+                                      VarName);
   Value *NextVar = Builder->CreateFAdd(CurVar, StepVal, "nextvar");
   Builder->CreateStore(NextVar, Alloca);
 
-  // Convert condition to a bool by comparing non-equal to 0.0.
-  EndCond = Builder->CreateFCmpONE(
-      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
-
-  // Create the "after loop" block and insert it.
-  BasicBlock *AfterBB =
-      BasicBlock::Create(*TheContext, "afterloop", TheFunction);
+  // Create the unconditional branch that returns to LoopConditionBB to
+  // determine if we should continue looping.
+  Builder->CreateBr(LoopConditionBB);
 
-  // Insert the conditional branch into the end of LoopEndBB.
-  Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
+  // Append EndLoopBB after the loop body. We go to this basic block if the
+  // loop condition says we should not loop anymore.
+  TheFunction->insert(TheFunction->end(), EndLoopBB);
 
-  // Any new code will be inserted in AfterBB.
-  Builder->SetInsertPoint(AfterBB);
+  // Any new code will be inserted after the loop.
+  Builder->SetInsertPoint(EndLoopBB);
 
   // Restore the unshadowed variable.
   if (OldVal)
@@ -1104,7 +1117,7 @@ Function *FunctionAST::codegen() {
     verifyFunction(*TheFunction);
 
     // Run the optimizer on the function.
-    TheFPM->run(*TheFunction, *TheFAM);
+    // TheFPM->run(*TheFunction, *TheFAM);
 
     return TheFunction;
   }

>From 792faa513154c6cdda136a6471365794d49c74dd Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Mon, 8 Apr 2024 22:46:11 -0700
Subject: [PATCH 06/12] Uncommented function optimization pass

---
 llvm/examples/Kaleidoscope/Chapter7/toy.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
index a5b8aad3ca2417..11b63091c47889 100644
--- a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
@@ -1117,7 +1117,7 @@ Function *FunctionAST::codegen() {
     verifyFunction(*TheFunction);
 
     // Run the optimizer on the function.
-    // TheFPM->run(*TheFunction, *TheFAM);
+    TheFPM->run(*TheFunction, *TheFAM);
 
     return TheFunction;
   }

>From d006bd10a17801954495bd351eeec8a6e6acf976 Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Tue, 9 Apr 2024 15:52:35 -0700
Subject: [PATCH 07/12] Updated Kaleidoscope code for Ch8 and Ch9

---
 llvm/examples/Kaleidoscope/Chapter8/toy.cpp | 95 ++++++++++++---------
 llvm/examples/Kaleidoscope/Chapter9/toy.cpp | 95 ++++++++++++---------
 2 files changed, 108 insertions(+), 82 deletions(-)

diff --git a/llvm/examples/Kaleidoscope/Chapter8/toy.cpp b/llvm/examples/Kaleidoscope/Chapter8/toy.cpp
index ae2f9c7059e5fb..4b2d2aeebff64c 100644
--- a/llvm/examples/Kaleidoscope/Chapter8/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter8/toy.cpp
@@ -893,20 +893,21 @@ Value *IfExprAST::codegen() {
 //   ...
 //   start = startexpr
 //   store start -> var
-//   goto loop
+//   goto loopcond
+// loopcond:
+//   endcond = endexpr
+//   br endcond, loop, endloop
 // loop:
 //   ...
 //   bodyexpr
 //   ...
-// loopend:
 //   step = stepexpr
-//   endcond = endexpr
-//
 //   curvar = load var
 //   nextvar = curvar + step
 //   store nextvar -> var
-//   br endcond, loop, endloop
-// outloop:
+//   goto loopcond
+// endloop:
+//   ...
 Value *ForExprAST::codegen() {
   Function *TheFunction = Builder->GetInsertBlock()->getParent();
 
@@ -921,26 +922,47 @@ Value *ForExprAST::codegen() {
   // Store the value into the alloca.
   Builder->CreateStore(StartVal, Alloca);
 
-  // Make the new basic block for the loop header, inserting after current
-  // block.
-  BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop", TheFunction);
+  // If the loop variable shadows an existing variable, we have to restore it,
+  // so save it now. Set VarName to refer to our recently created alloca.
+  AllocaInst *OldVal = NamedValues[VarName];
+  NamedValues[VarName] = Alloca;
 
-  // Insert an explicit fall through from the current block to the LoopBB.
-  Builder->CreateBr(LoopBB);
+  // Make new basic blocks for loop condition, loop body and end-loop code.
+  BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
+      TheFunction);
+  BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop");
+  BasicBlock *EndLoopBB = BasicBlock::Create(*TheContext, "endloop");
 
-  // Start insertion in LoopBB.
-  Builder->SetInsertPoint(LoopBB);
+  // Insert an explicit fall through from current block to LoopConditionBB.
+  Builder->CreateBr(LoopConditionBB);
 
-  // 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.
-  AllocaInst *OldVal = NamedValues[VarName];
-  NamedValues[VarName] = Alloca;
+  // Start insertion in LoopConditionBB.
+  Builder->SetInsertPoint(LoopConditionBB);
+
+  // Compute the end condition.
+  Value *EndCond = End->codegen();
+  if (!EndCond)
+    return nullptr;
 
-  // 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())
+  // Convert condition to a bool by comparing non-equal to 0.0.
+  EndCond = Builder->CreateFCmpONE(
+      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "endcond");
+
+  // Insert the conditional branch that either continues the loop, or exits the
+  // loop.
+  Builder->CreateCondBr(EndCond, LoopBB, EndLoopBB);
+
+  // Attach the basic block that will soon hold the loop body to the end of the
+  // parent function.
+  TheFunction->insert(TheFunction->end(), LoopBB);
+
+  // Emit the loop body within the LoopBB. 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.
+  Builder->SetInsertPoint(LoopBB);
+  if (!Body->codegen()) {
     return nullptr;
+  }
 
   // Emit the step value.
   Value *StepVal = nullptr;
@@ -953,31 +975,22 @@ Value *ForExprAST::codegen() {
     StepVal = ConstantFP::get(*TheContext, APFloat(1.0));
   }
 
-  // Compute the end condition.
-  Value *EndCond = End->codegen();
-  if (!EndCond)
-    return nullptr;
-
-  // Reload, increment, and restore the alloca.  This handles the case where
-  // the body of the loop mutates the variable.
-  Value *CurVar = Builder->CreateLoad(Type::getDoubleTy(*TheContext), Alloca,
-                                      VarName.c_str());
+  // Load, increment and store the new loop variable.
+  Value *CurVar = Builder->CreateLoad(Alloca->getAllocatedType(), Alloca,
+                                      VarName);
   Value *NextVar = Builder->CreateFAdd(CurVar, StepVal, "nextvar");
   Builder->CreateStore(NextVar, Alloca);
 
-  // Convert condition to a bool by comparing non-equal to 0.0.
-  EndCond = Builder->CreateFCmpONE(
-      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
-
-  // Create the "after loop" block and insert it.
-  BasicBlock *AfterBB =
-      BasicBlock::Create(*TheContext, "afterloop", TheFunction);
+  // Create the unconditional branch that returns to LoopConditionBB to
+  // determine if we should continue looping.
+  Builder->CreateBr(LoopConditionBB);
 
-  // Insert the conditional branch into the end of LoopEndBB.
-  Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
+  // Append EndLoopBB after the loop body. We go to this basic block if the
+  // loop condition says we should not loop anymore.
+  TheFunction->insert(TheFunction->end(), EndLoopBB);
 
-  // Any new code will be inserted in AfterBB.
-  Builder->SetInsertPoint(AfterBB);
+  // Any new code will be inserted after the loop.
+  Builder->SetInsertPoint(EndLoopBB);
 
   // Restore the unshadowed variable.
   if (OldVal)
diff --git a/llvm/examples/Kaleidoscope/Chapter9/toy.cpp b/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
index 51457a3c22ade3..a17830e240d294 100644
--- a/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
@@ -1063,20 +1063,21 @@ Value *IfExprAST::codegen() {
 //   ...
 //   start = startexpr
 //   store start -> var
-//   goto loop
+//   goto loopcond
+// loopcond:
+//   endcond = endexpr
+//   br endcond, loop, endloop
 // loop:
 //   ...
 //   bodyexpr
 //   ...
-// loopend:
 //   step = stepexpr
-//   endcond = endexpr
-//
 //   curvar = load var
 //   nextvar = curvar + step
 //   store nextvar -> var
-//   br endcond, loop, endloop
-// outloop:
+//   goto loopcond
+// endloop:
+//   ...
 Value *ForExprAST::codegen() {
   Function *TheFunction = Builder->GetInsertBlock()->getParent();
 
@@ -1093,26 +1094,47 @@ Value *ForExprAST::codegen() {
   // Store the value into the alloca.
   Builder->CreateStore(StartVal, Alloca);
 
-  // Make the new basic block for the loop header, inserting after current
-  // block.
-  BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop", TheFunction);
+  // If the loop variable shadows an existing variable, we have to restore it,
+  // so save it now. Set VarName to refer to our recently created alloca.
+  AllocaInst *OldVal = NamedValues[VarName];
+  NamedValues[VarName] = Alloca;
 
-  // Insert an explicit fall through from the current block to the LoopBB.
-  Builder->CreateBr(LoopBB);
+  // Make new basic blocks for loop condition, loop body and end-loop code.
+  BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
+      TheFunction);
+  BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop");
+  BasicBlock *EndLoopBB = BasicBlock::Create(*TheContext, "endloop");
 
-  // Start insertion in LoopBB.
-  Builder->SetInsertPoint(LoopBB);
+  // Insert an explicit fall through from current block to LoopConditionBB.
+  Builder->CreateBr(LoopConditionBB);
 
-  // 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.
-  AllocaInst *OldVal = NamedValues[VarName];
-  NamedValues[VarName] = Alloca;
+  // Start insertion in LoopConditionBB.
+  Builder->SetInsertPoint(LoopConditionBB);
+
+  // Compute the end condition.
+  Value *EndCond = End->codegen();
+  if (!EndCond)
+    return nullptr;
 
-  // 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())
+  // Convert condition to a bool by comparing non-equal to 0.0.
+  EndCond = Builder->CreateFCmpONE(
+      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "endcond");
+
+  // Insert the conditional branch that either continues the loop, or exits the
+  // loop.
+  Builder->CreateCondBr(EndCond, LoopBB, EndLoopBB);
+
+  // Attach the basic block that will soon hold the loop body to the end of the
+  // parent function.
+  TheFunction->insert(TheFunction->end(), LoopBB);
+
+  // Emit the loop body within the LoopBB. 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.
+  Builder->SetInsertPoint(LoopBB);
+  if (!Body->codegen()) {
     return nullptr;
+  }
 
   // Emit the step value.
   Value *StepVal = nullptr;
@@ -1125,31 +1147,22 @@ Value *ForExprAST::codegen() {
     StepVal = ConstantFP::get(*TheContext, APFloat(1.0));
   }
 
-  // Compute the end condition.
-  Value *EndCond = End->codegen();
-  if (!EndCond)
-    return nullptr;
-
-  // Reload, increment, and restore the alloca.  This handles the case where
-  // the body of the loop mutates the variable.
-  Value *CurVar = Builder->CreateLoad(Type::getDoubleTy(*TheContext), Alloca,
-                                      VarName.c_str());
+  // Load, increment and store the new loop variable.
+  Value *CurVar = Builder->CreateLoad(Alloca->getAllocatedType(), Alloca,
+                                      VarName);
   Value *NextVar = Builder->CreateFAdd(CurVar, StepVal, "nextvar");
   Builder->CreateStore(NextVar, Alloca);
 
-  // Convert condition to a bool by comparing non-equal to 0.0.
-  EndCond = Builder->CreateFCmpONE(
-      EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
-
-  // Create the "after loop" block and insert it.
-  BasicBlock *AfterBB =
-      BasicBlock::Create(*TheContext, "afterloop", TheFunction);
+  // Create the unconditional branch that returns to LoopConditionBB to
+  // determine if we should continue looping.
+  Builder->CreateBr(LoopConditionBB);
 
-  // Insert the conditional branch into the end of LoopEndBB.
-  Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
+  // Append EndLoopBB after the loop body. We go to this basic block if the
+  // loop condition says we should not loop anymore.
+  TheFunction->insert(TheFunction->end(), EndLoopBB);
 
-  // Any new code will be inserted in AfterBB.
-  Builder->SetInsertPoint(AfterBB);
+  // Any new code will be inserted after the loop.
+  Builder->SetInsertPoint(EndLoopBB);
 
   // Restore the unshadowed variable.
   if (OldVal)

>From a650f396d8f1a955462ab9d4021fb71c020e95b0 Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Tue, 9 Apr 2024 16:43:18 -0700
Subject: [PATCH 08/12] Fixed Kaleidoscope tests for Ch5 and Ch7

---
 llvm/test/Examples/Kaleidoscope/Chapter5.test | 1 -
 llvm/test/Examples/Kaleidoscope/Chapter7.test | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/test/Examples/Kaleidoscope/Chapter5.test b/llvm/test/Examples/Kaleidoscope/Chapter5.test
index f9509197f06685..06af785a673341 100644
--- a/llvm/test/Examples/Kaleidoscope/Chapter5.test
+++ b/llvm/test/Examples/Kaleidoscope/Chapter5.test
@@ -16,5 +16,4 @@ for i = 1, i < 5, 1.0 in
 # CHECK: 2.0
 # CHECK: 3.0
 # CHECK: 4.0
-# CHECK: 5.0
 # CHECK: Evaluated to 0.000000
diff --git a/llvm/test/Examples/Kaleidoscope/Chapter7.test b/llvm/test/Examples/Kaleidoscope/Chapter7.test
index 4c77ebc42169dd..a99029631817d6 100644
--- a/llvm/test/Examples/Kaleidoscope/Chapter7.test
+++ b/llvm/test/Examples/Kaleidoscope/Chapter7.test
@@ -13,4 +13,4 @@ def fibi(x)
   b;
 
 fibi(10);
-# CHECK: Evaluated to 55.000000
+# CHECK: Evaluated to 34.000000

>From 889c38029479376a077becc0e052430fb8753c02 Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Wed, 10 Apr 2024 18:44:48 -0700
Subject: [PATCH 09/12] [Kaleidoscope] Changed variable names to be more clear
 in Ch 5

---
 .../MyFirstLanguageFrontend/LangImpl05.rst    | 50 +++++++++----------
 llvm/examples/Kaleidoscope/Chapter5/toy.cpp   | 22 ++++----
 2 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
index 128d178d42bdd2..fec267036eea65 100644
--- a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
+++ b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
@@ -447,8 +447,7 @@ from a starting value, while the condition ("i < n" in this case) is
 true, incrementing by an optional step value ("1.0" in this case). If
 the step value is omitted, it defaults to 1.0. While the loop is true,
 it executes its body expression. Because we don't have anything better
-to return, we'll just define the loop as always returning 0.0. In the
-future when we have mutable variables, it will get more useful.
+to return, we'll just define the loop as always returning 0.0.
 
 As before, let's talk about the changes that we need to Kaleidoscope to
 support this.
@@ -493,13 +492,13 @@ variable name and the constituent expressions in the node.
     /// ForExprAST - Expression class for for/in.
     class ForExprAST : public ExprAST {
       std::string VarName;
-      std::unique_ptr<ExprAST> Start, End, Step, Body;
+      std::unique_ptr<ExprAST> Start, Cond, Step, Body;
 
     public:
       ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
-                 std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
+                 std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Step,
                  std::unique_ptr<ExprAST> Body)
-        : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
+        : VarName(VarName), Start(std::move(Start)), Cond(std::move(Cond)),
           Step(std::move(Step)), Body(std::move(Body)) {}
 
       Value *codegen() override;
@@ -537,8 +536,8 @@ value to null in the AST node:
         return LogError("expected ',' after for start value");
       getNextToken();
 
-      auto End = ParseExpression();
-      if (!End)
+      auto Cond = ParseExpression();
+      if (!Cond)
         return nullptr;
 
       // The step value is optional.
@@ -559,7 +558,7 @@ value to null in the AST node:
         return nullptr;
 
       return std::make_unique<ForExprAST>(IdName, std::move(Start),
-                                           std::move(End), std::move(Step),
+                                           std::move(Cond), std::move(Step),
                                            std::move(Body));
     }
 
@@ -646,9 +645,9 @@ expression).
 
 .. code-block:: c++
 
-      // Make new basic blocks for loop condition, loop body and end-loop code.
+      // Make new basic blocks for loop condition, loop body, and end-loop code.
       Function *TheFunction = Builder->GetInsertBlock()->getParent();
-      BasicBlock *PreLoopBB = Builder->GetInsertBlock();
+      BasicBlock *EntryBB = Builder->GetInsertBlock();
       BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
           TheFunction);
       BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop");
@@ -659,14 +658,15 @@ expression).
 
 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 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.
+into the loop condition check (denoted by ``EntryBB``). 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 (denoted by
+``LoopConditionBB``). 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.
+"entry" code.
 
 .. code-block:: c++
 
@@ -676,14 +676,14 @@ pre-loop code.
       // Start the PHI node with an entry for Start.
       PHINode *Variable =
           Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, VarName);
-      Variable->addIncoming(StartVal, PreLoopBB);
+      Variable->addIncoming(StartVal, EntryBB);
 
-Now that the "preheader" for the loop is set up, we switch to emitting
-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 node will eventually get a second value for the
-backedge, but we can't set it up yet (because it doesn't exist!).
+Now that ``EntryBB`` is set up, we switch to emitting 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 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++
 
@@ -693,7 +693,7 @@ backedge, but we can't set it up yet (because it doesn't exist!).
       NamedValues[VarName] = Variable;
 
       // Compute the end condition.
-      Value *EndCond = End->codegen();
+      Value *EndCond = Cond->codegen();
       if (!EndCond)
         return nullptr;
 
@@ -712,7 +712,7 @@ symbol table.
 
 Once the loop variable is set into the symbol table, we codegen the condition
 that determines if we can enter into the loop (or continue looping, depending
-on if we are arriving from the "preheader" or the loop body).
+on if we are arriving from the "entry" basic block or the loop body).
 
 .. code-block:: c++
 
diff --git a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
index 74f4a91e6ff7b0..2c29e4efd0808e 100644
--- a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
@@ -199,13 +199,13 @@ class IfExprAST : public ExprAST {
 /// ForExprAST - Expression class for for/in.
 class ForExprAST : public ExprAST {
   std::string VarName;
-  std::unique_ptr<ExprAST> Start, End, Step, Body;
+  std::unique_ptr<ExprAST> Start, Cond, Step, Body;
 
 public:
   ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
-             std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
+             std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Step,
              std::unique_ptr<ExprAST> Body)
-      : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
+      : VarName(VarName), Start(std::move(Start)), Cond(std::move(Cond)),
         Step(std::move(Step)), Body(std::move(Body)) {}
 
   Value *codegen() override;
@@ -387,8 +387,8 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
     return LogError("expected ',' after for start value");
   getNextToken();
 
-  auto End = ParseExpression();
-  if (!End)
+  auto Cond = ParseExpression();
+  if (!Cond)
     return nullptr;
 
   // The step value is optional.
@@ -408,7 +408,7 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
   if (!Body)
     return nullptr;
 
-  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
+  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(Cond),
                                        std::move(Step), std::move(Body));
 }
 
@@ -682,7 +682,7 @@ Value *IfExprAST::codegen() {
 }
 
 // Output for-loop as:
-// preloop:
+// entry:
 //   ...
 //   start = startexpr
 //   goto loopcond
@@ -705,9 +705,9 @@ Value *ForExprAST::codegen() {
   if (!StartVal)
     return nullptr;
 
-  // Make new basic blocks for loop condition, loop body and end-loop code. 
+  // Make new basic blocks for loop condition, loop body, and end-loop code. 
   Function *TheFunction = Builder->GetInsertBlock()->getParent();
-  BasicBlock *PreLoopBB = Builder->GetInsertBlock();
+  BasicBlock *EntryBB = Builder->GetInsertBlock();
   BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
       TheFunction);
   BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop");
@@ -722,7 +722,7 @@ Value *ForExprAST::codegen() {
   // Start the PHI node with an entry for Start.
   PHINode *Variable =
       Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, VarName);
-  Variable->addIncoming(StartVal, PreLoopBB);
+  Variable->addIncoming(StartVal, EntryBB);
 
   // 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.
@@ -730,7 +730,7 @@ Value *ForExprAST::codegen() {
   NamedValues[VarName] = Variable;
 
   // Compute the end condition.
-  Value *EndCond = End->codegen();
+  Value *EndCond = Cond->codegen();
   if (!EndCond)
     return nullptr;
 

>From ae424474cc054c72bbfde98435049430f68852fe Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Wed, 10 Apr 2024 19:02:33 -0700
Subject: [PATCH 10/12] [Kaleidoscope] Updated code in Ch6-9 to match code from
 Ch5

---
 llvm/examples/Kaleidoscope/Chapter6/toy.cpp | 29 ++++++++++-----------
 llvm/examples/Kaleidoscope/Chapter7/toy.cpp | 14 +++++-----
 llvm/examples/Kaleidoscope/Chapter8/toy.cpp | 14 +++++-----
 llvm/examples/Kaleidoscope/Chapter9/toy.cpp | 18 ++++++-------
 4 files changed, 37 insertions(+), 38 deletions(-)

diff --git a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
index c29f1e719990e3..f2be6d1418fd81 100644
--- a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
@@ -219,13 +219,13 @@ class IfExprAST : public ExprAST {
 /// ForExprAST - Expression class for for/in.
 class ForExprAST : public ExprAST {
   std::string VarName;
-  std::unique_ptr<ExprAST> Start, End, Step, Body;
+  std::unique_ptr<ExprAST> Start, Cond, Step, Body;
 
 public:
   ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
-             std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
+             std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Step,
              std::unique_ptr<ExprAST> Body)
-      : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
+      : VarName(VarName), Start(std::move(Start)), Cond(std::move(Cond)),
         Step(std::move(Step)), Body(std::move(Body)) {}
 
   Value *codegen() override;
@@ -421,8 +421,8 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
     return LogError("expected ',' after for start value");
   getNextToken();
 
-  auto End = ParseExpression();
-  if (!End)
+  auto Cond = ParseExpression();
+  if (!Cond)
     return nullptr;
 
   // The step value is optional.
@@ -442,7 +442,7 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
   if (!Body)
     return nullptr;
 
-  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
+  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(Cond),
                                        std::move(Step), std::move(Body));
 }
 
@@ -794,11 +794,11 @@ Value *IfExprAST::codegen() {
 }
 
 // Output for-loop as:
-// preloop:
+// entry:
 //   ...
 //   start = startexpr
-//   goto loopcondition
-// loopcondition:
+//   goto loopcond
+// loopcond:
 //   variable = phi [start, loopheader], [nextvariable, loop]
 //   endcond = endexpr
 //   br endcond, loop, endloop
@@ -808,7 +808,7 @@ Value *IfExprAST::codegen() {
 //   ...
 //   step = stepexpr
 //   nextvariable = variable + step
-//   goto loopcondition
+//   goto loopcond
 // endloop:
 //   ...
 Value *ForExprAST::codegen() {
@@ -817,10 +817,9 @@ Value *ForExprAST::codegen() {
   if (!StartVal)
     return nullptr;
 
-  // Make new basic blocks for pre-loop, loop condition, loop body and end-loop
-  // code. 
+  // Make new basic blocks for loop condition, loop body, and end-loop code. 
   Function *TheFunction = Builder->GetInsertBlock()->getParent();
-  BasicBlock *PreLoopBB = Builder->GetInsertBlock();
+  BasicBlock *EntryBB = Builder->GetInsertBlock();
   BasicBlock *LoopConditionBB = BasicBlock::Create(*TheContext, "loopcond",
       TheFunction);
   BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop");
@@ -835,7 +834,7 @@ Value *ForExprAST::codegen() {
   // Start the PHI node with an entry for Start.
   PHINode *Variable =
       Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, VarName);
-  Variable->addIncoming(StartVal, PreLoopBB);
+  Variable->addIncoming(StartVal, EntryBB);
 
   // 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.
@@ -843,7 +842,7 @@ Value *ForExprAST::codegen() {
   NamedValues[VarName] = Variable;
 
   // Compute the end condition.
-  Value *EndCond = End->codegen();
+  Value *EndCond = Cond->codegen();
   if (!EndCond)
     return nullptr;
 
diff --git a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
index 11b63091c47889..b04716c51323ca 100644
--- a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
@@ -227,13 +227,13 @@ class IfExprAST : public ExprAST {
 /// ForExprAST - Expression class for for/in.
 class ForExprAST : public ExprAST {
   std::string VarName;
-  std::unique_ptr<ExprAST> Start, End, Step, Body;
+  std::unique_ptr<ExprAST> Start, Cond, Step, Body;
 
 public:
   ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
-             std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
+             std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Step,
              std::unique_ptr<ExprAST> Body)
-      : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
+      : VarName(VarName), Start(std::move(Start)), Cond(std::move(Cond)),
         Step(std::move(Step)), Body(std::move(Body)) {}
 
   Value *codegen() override;
@@ -443,8 +443,8 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
     return LogError("expected ',' after for start value");
   getNextToken();
 
-  auto End = ParseExpression();
-  if (!End)
+  auto Cond = ParseExpression();
+  if (!Cond)
     return nullptr;
 
   // The step value is optional.
@@ -464,7 +464,7 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
   if (!Body)
     return nullptr;
 
-  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
+  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(Cond),
                                        std::move(Step), std::move(Body));
 }
 
@@ -952,7 +952,7 @@ Value *ForExprAST::codegen() {
   Builder->SetInsertPoint(LoopConditionBB);
 
   // Compute the end condition.
-  Value *EndCond = End->codegen();
+  Value *EndCond = Cond->codegen();
   if (!EndCond)
     return nullptr;
 
diff --git a/llvm/examples/Kaleidoscope/Chapter8/toy.cpp b/llvm/examples/Kaleidoscope/Chapter8/toy.cpp
index 4b2d2aeebff64c..cc81eee2b6d3e3 100644
--- a/llvm/examples/Kaleidoscope/Chapter8/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter8/toy.cpp
@@ -223,13 +223,13 @@ class IfExprAST : public ExprAST {
 /// ForExprAST - Expression class for for/in.
 class ForExprAST : public ExprAST {
   std::string VarName;
-  std::unique_ptr<ExprAST> Start, End, Step, Body;
+  std::unique_ptr<ExprAST> Start, Cond, Step, Body;
 
 public:
   ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
-             std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
+             std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Step,
              std::unique_ptr<ExprAST> Body)
-      : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
+      : VarName(VarName), Start(std::move(Start)), Cond(std::move(Cond)),
         Step(std::move(Step)), Body(std::move(Body)) {}
 
   Value *codegen() override;
@@ -439,8 +439,8 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
     return LogError("expected ',' after for start value");
   getNextToken();
 
-  auto End = ParseExpression();
-  if (!End)
+  auto Cond = ParseExpression();
+  if (!Cond)
     return nullptr;
 
   // The step value is optional.
@@ -460,7 +460,7 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
   if (!Body)
     return nullptr;
 
-  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
+  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(Cond),
                                        std::move(Step), std::move(Body));
 }
 
@@ -940,7 +940,7 @@ Value *ForExprAST::codegen() {
   Builder->SetInsertPoint(LoopConditionBB);
 
   // Compute the end condition.
-  Value *EndCond = End->codegen();
+  Value *EndCond = Cond->codegen();
   if (!EndCond)
     return nullptr;
 
diff --git a/llvm/examples/Kaleidoscope/Chapter9/toy.cpp b/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
index a17830e240d294..d303e63fde0e5f 100644
--- a/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
@@ -312,19 +312,19 @@ class IfExprAST : public ExprAST {
 /// ForExprAST - Expression class for for/in.
 class ForExprAST : public ExprAST {
   std::string VarName;
-  std::unique_ptr<ExprAST> Start, End, Step, Body;
+  std::unique_ptr<ExprAST> Start, Cond, Step, Body;
 
 public:
   ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
-             std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
+             std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Step,
              std::unique_ptr<ExprAST> Body)
-      : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
+      : VarName(VarName), Start(std::move(Start)), Cond(std::move(Cond)),
         Step(std::move(Step)), Body(std::move(Body)) {}
   Value *codegen() override;
   raw_ostream &dump(raw_ostream &out, int ind) override {
     ExprAST::dump(out << "for", ind);
-    Start->dump(indent(out, ind) << "Cond:", ind + 1);
-    End->dump(indent(out, ind) << "End:", ind + 1);
+    Start->dump(indent(out, ind) << "Start:", ind + 1);
+    Cond->dump(indent(out, ind) << "Cond:", ind + 1);
     Step->dump(indent(out, ind) << "Step:", ind + 1);
     Body->dump(indent(out, ind) << "Body:", ind + 1);
     return out;
@@ -551,8 +551,8 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
     return LogError("expected ',' after for start value");
   getNextToken();
 
-  auto End = ParseExpression();
-  if (!End)
+  auto Cond = ParseExpression();
+  if (!Cond)
     return nullptr;
 
   // The step value is optional.
@@ -572,7 +572,7 @@ static std::unique_ptr<ExprAST> ParseForExpr() {
   if (!Body)
     return nullptr;
 
-  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
+  return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(Cond),
                                        std::move(Step), std::move(Body));
 }
 
@@ -1112,7 +1112,7 @@ Value *ForExprAST::codegen() {
   Builder->SetInsertPoint(LoopConditionBB);
 
   // Compute the end condition.
-  Value *EndCond = End->codegen();
+  Value *EndCond = Cond->codegen();
   if (!EndCond)
     return nullptr;
 

>From f18126c4dd7d1529f8ee3b682a4ac7071f196801 Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Wed, 10 Apr 2024 19:04:20 -0700
Subject: [PATCH 11/12] [Kaleidoscope] Updated Ch7 test

---
 llvm/test/Examples/Kaleidoscope/Chapter7.test | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Examples/Kaleidoscope/Chapter7.test b/llvm/test/Examples/Kaleidoscope/Chapter7.test
index a99029631817d6..9d66701dab34a9 100644
--- a/llvm/test/Examples/Kaleidoscope/Chapter7.test
+++ b/llvm/test/Examples/Kaleidoscope/Chapter7.test
@@ -6,11 +6,11 @@ def binary : 1 (x y) y;
 
 def fibi(x)
   var a = 1, b = 1, c in
-  (for i = 3, i < x in
+  (for i = 3, i < x + 1 in
      c = a + b :
      a = b :
      b = c) :
   b;
 
 fibi(10);
-# CHECK: Evaluated to 34.000000
+# CHECK: Evaluated to 55.000000

>From c09a128a775469b9f3183e1ca0d8a7bf9542ea5c Mon Sep 17 00:00:00 2001
From: Vladimir Mishel <vmishel at outlook.com>
Date: Wed, 10 Apr 2024 19:06:42 -0700
Subject: [PATCH 12/12] [Kaleidoscope] Updated Ch7 "Iterative fib" example

---
 llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
index 0bb0a0835b8376..f28c823a1f982c 100644
--- a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
+++ b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
@@ -287,7 +287,7 @@ them. Here's a motivating example that shows how we could use these:
     # Iterative fib.
     def fibi(x)
       var a = 1, b = 1, c in
-      (for i = 3, i < x in
+      (for i = 3, i < x + 1 in
          c = a + b :
          a = b :
          b = c) :



More information about the llvm-commits mailing list