<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span style="font-family: LucidaGrande;" class="">I am trying to get a handle on IR builder, at least some basics. I ran through a tutorial here: </span><a href="http://www.ibm.com/developerworks/library/os-createcompilerllvm1/" class="" style="font-family: LucidaGrande;">Create a working compiler with the LLVM framework, Part 1</a><span style="font-family: LucidaGrande;" class="">, and it worked well enough. I have some simple code that creates a hello world IR. I am trying to now bring in some concepts from the Kaleidoscope tutorial, namely adding expressions the the IR that is dumped out.</span><div class="" style="font-family: LucidaGrande;"><br class=""></div><div class="" style="font-family: LucidaGrande;">I’ve added the following snippets:</div><div class="" style="font-family: LucidaGrande;"><br class=""></div><div class="" style="font-family: LucidaGrande;"><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>Value *c1 = ConstantFP::get(context, APFloat(1.0));<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>Value *c2 = ConstantFP::get(context, APFloat(3.0));<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>builder.CreateFAdd(c1, c2, "addtmp”);</font></div><div class="" style="font-family: LucidaGrande;"><br class=""></div><div class="" style="font-family: LucidaGrande;">expecting to see an add instruction in the dump, but nothing is added. This seems to be pretty strait from the kaleidoscope examples but I am missing something here I am sure. Here is the full program:</div><div class="" style="font-family: LucidaGrande;"><br class=""></div><div class="" style="font-family: LucidaGrande;"><div class=""><font face="Monaco" class=""> 1 <span class="Apple-tab-span" style="white-space: pre;"> </span>using namespace llvm;</font></div><div class=""><font face="Monaco" class=""> 2 </font></div><div class=""><font face="Monaco" class=""> 3 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::Module *module;</font></div><div class=""><font face="Monaco" class=""> 4 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::IRBuilder<> builder(getGlobalContext());</font></div><div class=""><font face="Monaco" class=""> 5 </font></div><div class=""><font face="Monaco" class=""> 6 <span class="Apple-tab-span" style="white-space: pre;"> </span>int main(int argc, const char * argv[]) {</font></div><div class=""><font face="Monaco" class=""> 7 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::LLVMContext & context = llvm::getGlobalContext();</font></div><div class=""><font face="Monaco" class=""> 8 <span class="Apple-tab-span" style="white-space: pre;"> </span>module = new llvm::Module("calc", context);</font></div><div class=""><font face="Monaco" class=""> 9 <span class="Apple-tab-span" style="white-space: pre;"> </span></font></div><div class=""><font face="Monaco" class="">10 </font></div><div class=""><font face="Monaco" class="">11 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::FunctionType *funcType = llvm::FunctionType::get(builder.getVoidTy(), false);</font></div><div class=""><font face="Monaco" class="">12 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::Function *mainFunc = </font></div><div class=""><font face="Monaco" class="">13 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", module);</font></div><div class=""><font face="Monaco" class="">14 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entrypoint", mainFunc);</font></div><div class=""><font face="Monaco" class="">15 <span class="Apple-tab-span" style="white-space: pre;"> </span>builder.SetInsertPoint(entry);</font></div><div class=""><font face="Monaco" class="">16 </font></div><div class=""><font face="Monaco" class="">17 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world!\n");</font></div><div class=""><font face="Monaco" class="">18 </font></div><div class=""><font face="Monaco" class="">19 <span class="Apple-tab-span" style="white-space: pre;"> </span>std::vector<llvm::Type *> putsArgs;</font></div><div class=""><font face="Monaco" class="">20 <span class="Apple-tab-span" style="white-space: pre;"> </span>putsArgs.push_back(builder.getInt8Ty()->getPointerTo());</font></div><div class=""><font face="Monaco" class="">21 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::ArrayRef<llvm::Type*> argsRef(putsArgs);</font></div><div class=""><font face="Monaco" class="">22 </font></div><div class=""><font face="Monaco" class="">23 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::FunctionType *putsType = </font></div><div class=""><font face="Monaco" class="">24 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::FunctionType::get(builder.getInt32Ty(), argsRef, false);</font></div><div class=""><font face="Monaco" class="">25 <span class="Apple-tab-span" style="white-space: pre;"> </span>llvm::Constant *putsFunc = module->getOrInsertFunction("puts", putsType);</font></div><div class=""><font face="Monaco" class="">26 </font></div><div class=""><font face="Monaco" class="">27 <span class="Apple-tab-span" style="white-space: pre;"> </span>Value *a;</font></div><div class=""><font face="Monaco" class="">28 <span class="Apple-tab-span" style="white-space: pre;"> </span>builder.CreateAlloca(builder.getDoubleTy(), a, "a");</font></div><div class=""><font face="Monaco" class="">29 <span class="Apple-tab-span" style="white-space: pre;"> </span></font></div><div class=""><font face="Monaco" class="">30 <span class="Apple-tab-span" style="white-space: pre;"> </span>Value *c1 = ConstantFP::get(context, APFloat(1.0));</font></div><div class=""><font face="Monaco" class="">31 <span class="Apple-tab-span" style="white-space: pre;"> </span>Value *c2 = ConstantFP::get(context, APFloat(3.0));</font></div><div class=""><font face="Monaco" class="">32 <span class="Apple-tab-span" style="white-space: pre;"> </span>builder.CreateFAdd(c1, c2, "addtmp");</font></div><div class=""><font face="Monaco" class="">33 </font></div><div class=""><font face="Monaco" class="">34 <span class="Apple-tab-span" style="white-space: pre;"> </span>builder.CreateCall(putsFunc, helloWorld);</font></div><div class=""><font face="Monaco" class="">35 <span class="Apple-tab-span" style="white-space: pre;"> </span>builder.CreateRetVoid();</font></div><div class=""><font face="Monaco" class="">36 <span class="Apple-tab-span" style="white-space: pre;"> </span>module->dump();</font></div><div class=""><font face="Monaco" class="">37 <span class="Apple-tab-span" style="white-space: pre;"> </span>return 0;</font></div><div class=""><font face="Monaco" class="">38 <span class="Apple-tab-span" style="white-space: pre;"> </span>}</font></div><div class=""><br class=""></div><div class="">Produces:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class="">; ModuleID = 'calc'<br class=""><br class="">@0 = private unnamed_addr constant [14 x i8] c"hello world!\0A\00"<br class=""><br class="">define void @main() {<br class="">entrypoint:<br class=""> %a = alloca double<br class=""> %0 = call i32 @puts(i8* getelementptr inbounds ([14 x i8]* @0, i32 0, i32 0))<br class=""> ret void<br class="">}<br class=""></font><br class=""><font face="Monaco" class="">declare i32 @puts(i8*)</font></div><div class=""><br class=""></div><div class="">Lines 27-32 are the code I have added beyond the demo at the mentioned website. As shown, the alloca shows up but no fadd instruction. I would appreciate if someone could help me understand this and point out what I am missing. Open to reading if someone can point me to more documentation as well.</div><div class=""><br class=""></div><div class="">(llvm 3.5 downloaded from <a href="http://llvm.org" class="">llvm.org</a> on OSX)</div><div class=""><br class=""></div><div class="">thanks</div><div class="">alan</div></div></body></html>