[llvm-dev] Kaleidoscope tutorial: some little problems need to fix

Jiang Ma via llvm-dev llvm-dev at lists.llvm.org
Mon Mar 23 20:31:48 PDT 2020

Hi all,
I have finished Kaleidoscope tutorial a few days ago. It's really a
great tutorial that help me a lot.
But, there seems to be some little problems need to be fixed. Although
I have fixed most of them, I'll just list them here as I'm new to
LLVM, and I'm not sure whether my solutions are correct.
The 'for' expression works differently from mainstream languages.  For
example, you probably won't get any print when using mainstream
languages such as C.
for i = 1, i < 1, 1.0 in
However, Kaleidoscope will print '1'. I'm not sure if it is a real
bug, but it really seems strange.

There are memory leaks in IfExprAST::codegen.
When ThenV == nullptr, we return and leak ElseBB/MergeBB (because they
have not been inserted into TheFunction at that point).
Value *IfExprAST::codegen() {
// Create blocks for the then and else cases. Insert the 'then' block at the
// end of the function.
BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
Value *ThenV = Then->codegen();
if (!ThenV)
   return nullptr;

Debug infos for Kaleidoscope are generally misleading.
The root cause is that currently we call emitLocation too early. Take
BinaryExprAST for example.

Value *BinaryExprAST::codegen() {
------------------------------------------------> we are saying below
instructions belongs to  this binary_expr
Value *L = LHS->codegen();
Value *R = RHS->codegen();----------------------------------------------------->
However, after this point, following instructions belongs to RHS.
if (!L || !R)
return nullptr;

switch (Op) {
case '+':
return Builder.CreateFAdd(L, R,
"addtmp");---------------------------------> So, the add instruction
belong to RHS expression in the debug info.

Variables in Var/For do not got their debug infos.

some more little improvements :
use map.find instead of map[] (as the latter insert non-existed pair silently)
implement language logics in parser instead of LLVM-IR generator.(for
example, uninitialized variable will be set to 0. We do this in
xxx::codegen now. In my opinion, we should do this when we build AST)
This tutorial is a great start point, thanks very much for all who
contribute to it. Hope my emails could also make it better.
For anyone who are interested in my solutions, my code have been
uploaded to https://github.com/mjblog/llvm_study_Kaleidoscope/tree/master/src
.  Beware, I'm new to LLVM and a C++ newbie... You have been warned :)

More information about the llvm-dev mailing list