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>
----- Original Message -----> From: "Alan Nilsson" <alan.n at mac.com> > To: "LLVM Developers Mailing List" <llvmdev at cs.uiuc.edu> > Sent: Tuesday, March 10, 2015 5:15:23 PM > Subject: [LLVMdev] noob IR builder question > > > 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 , 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”);The IRBuilder has a builtin constant folder, and since you're not actually doing anything with the value returned, the constant that is created just goes away. -Hal> > > 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 on OSX) > > > thanks > alan > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-- Hal Finkel Assistant Computational Scientist Leadership Computing Facility Argonne National Laboratory
The IRBuilder class does some basic constant folding. Look at the `Folder` member variable in the class definition. On Tue, Mar 10, 2015 at 3:15 PM, Alan Nilsson <alan.n at mac.com> wrote:> 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, 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 on OSX) > > thanks > alan > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
> From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] > On Behalf Of Alan Nilsson > Subject: [LLVMdev] noob IR builder question> 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.Because you don't use the result from the CreateFAdd(), it gets eliminated as dead code. Try passing the result on a function call or as the return value for the function you're generating. - Chuck
Maybe what you wanted was Value* c3 = builder.CreateFAdd(...); builder.CreateStore(a, c3); ? On 10 March 2015 at 22:33, Hal Finkel <hfinkel at anl.gov> wrote:> ----- Original Message ----- >> From: "Alan Nilsson" <alan.n at mac.com> >> To: "LLVM Developers Mailing List" <llvmdev at cs.uiuc.edu> >> Sent: Tuesday, March 10, 2015 5:15:23 PM >> Subject: [LLVMdev] noob IR builder question >> >> >> 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 , 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”); > > The IRBuilder has a builtin constant folder, and since you're not actually doing anything with the value returned, the constant that is created just goes away. > > -Hal > >> >> >> 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 on OSX) >> >> >> thanks >> alan >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> > > -- > Hal Finkel > Assistant Computational Scientist > Leadership Computing Facility > Argonne National Laboratory > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Maybe Matching Threads
- [LLVMdev] Is it valid to add parameters to a function once it is created
- [LLVMdev] Best strategy to add a parameter to a function
- [LLVMdev] LLVM Basic Program Compilation
- [LLVMdev] regression? Or did I do something wrong again?
- [LLVMdev] Seeing a crash with ConstantFP::get