[LLVMdev] noob IR builder question

Alan Nilsson alan.n at mac.com
Tue Mar 10 15:15:23 PDT 2015


I am trying to get a handle on IR builder, at least some basics.  I ran through a tutorial here: Create a working compiler with the LLVM framework, Part 1 <http://www.ibm.com/developerworks/library/os-createcompilerllvm1/>, 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.

I’ve added the following snippets:

	Value *c1 = ConstantFP::get(context, APFloat(1.0));
	Value *c2 = ConstantFP::get(context, APFloat(3.0));
	builder.CreateFAdd(c1, c2, "addtmp”);

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:

 1 	using namespace llvm;
 2 
 3 	llvm::Module *module;
 4 	llvm::IRBuilder<> builder(getGlobalContext());
 5 
 6 	int main(int argc, const char * argv[]) {
 7 		llvm::LLVMContext & context = llvm::getGlobalContext();
 8 		module = new llvm::Module("calc", context);
 9 	
10 
11 		llvm::FunctionType *funcType = llvm::FunctionType::get(builder.getVoidTy(), false);
12 		llvm::Function *mainFunc = 
13 			llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", module);
14 		llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entrypoint", mainFunc);
15 		builder.SetInsertPoint(entry);
16 
17 		llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world!\n");
18 
19 		std::vector<llvm::Type *> putsArgs;
20 		putsArgs.push_back(builder.getInt8Ty()->getPointerTo());
21 		llvm::ArrayRef<llvm::Type*>  argsRef(putsArgs);
22 
23 		llvm::FunctionType *putsType = 
24 			llvm::FunctionType::get(builder.getInt32Ty(), argsRef, false);
25 		llvm::Constant *putsFunc = module->getOrInsertFunction("puts", putsType);
26 
27 		Value *a;
28 		builder.CreateAlloca(builder.getDoubleTy(), a, "a");
29 		
30 		Value *c1 = ConstantFP::get(context, APFloat(1.0));
31 		Value *c2 = ConstantFP::get(context, APFloat(3.0));
32 		builder.CreateFAdd(c1, c2, "addtmp");
33 
34 		builder.CreateCall(putsFunc, helloWorld);
35 		builder.CreateRetVoid();
36 		module->dump();
37 		return 0;
38 	}

Produces:

; ModuleID = 'calc'

@0 = private unnamed_addr constant [14 x i8] c"hello world!\0A\00"

define void @main() {
entrypoint:
 %a = alloca double
 %0 = call i32 @puts(i8* getelementptr inbounds ([14 x i8]* @0, i32 0, i32 0))
 ret void
}

declare i32 @puts(i8*)

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.

(llvm 3.5 downloaded from llvm.org <http://llvm.org/> on OSX)

thanks
alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150310/37880b05/attachment.html>


More information about the llvm-dev mailing list