Jianfei Hu
2012-Apr-22 12:36 UTC
[LLVMdev] Problem about the type of Function's arguement in llvm
in the tutorial of official llvm doc, chapter 3, it deals with arguement of function as follow: for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size(); ++AI, ++Idx) { AI->setName(Args[Idx]); // NamedValues is map<string, Value*> NamedValues[Args[Idx]] = AI; and when it try to get the value of arguement, it simply does: Value *VariableExprAST::Codegen() { Value *V = NamedValues[Name]; return V ? V : ErrorV("Unknown variable name"); } It means that we can access value of arguement directly. However, in llvm.org/demo and the chapter 6 of tutorial, it deals with arguemnt: 1. allocate memory for arguement 2. then store the arguemnt into the memory. 3. CreateLoad() to load the arguement when accessing the value of arguement. I have several problems: Which way should I follow to get the vale of arguement? When I tried to use CreateLoad(arguement), the program gives a segmentation error. Does this result from the wrong type of argument, unsuitable to be the params of CreateLoad()? If it is, what's the type of Function->arg_begin() ? (The source code is too nested for me to find the original type of that) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120422/62c5a10e/attachment.html>
Hongbin Zheng
2012-Apr-22 14:16 UTC
[LLVMdev] Problem about the type of Function's arguement in llvm
hi On Sun, Apr 22, 2012 at 8:36 PM, Jianfei Hu <hujianfei258 at gmail.com> wrote:> in the tutorial of official llvm doc, chapter 3, it deals with arguement of > function as follow: > > for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size(); > ++AI, ++Idx) { > AI->setName(Args[Idx]); > > // NamedValues is map<string, Value*> > NamedValues[Args[Idx]] = AI; > > and when it try to get the value of arguement, it simply does: > > Value *VariableExprAST::Codegen() { > Value *V = NamedValues[Name]; > return V ? V : ErrorV("Unknown variable name"); > } > > It means that we can access value of arguement directly.Yes, it is possible to access the values (virtual registers) direclty, but you may need to construct SSA form by yourself.> > However, in llvm.org/demo and the chapter 6 of tutorial, it deals with > arguemnt: > 1. allocate memory for arguement > 2. then store the arguemnt into the memory. > 3. CreateLoad() to load the arguement when accessing the value of arguement. > > I have several problems: > Which way should I follow to get the vale of arguement?If you do not want to construct SSA from by yourself, you probably should follow this way. You can schedule the "mem2reg"[1] after you build the IR, it will promote alloca/load/store into scalar virtual registers for you. (I remember the tutorial had a discussion about this.)> > When I tried to use CreateLoad(arguement), the program gives a segmentation > error. > Does this result from the wrong type of argument, unsuitable to be the > params of CreateLoad()?Could you paste your code? Had you initialize the IRBuilder with insert position?> If it is, what's the type of Function->arg_begin() ? (The source code is too > nested for me to find the original type of that) >best regards ether [1]http://llvm.org/docs/doxygen/html/Mem2Reg_8cpp_source.html
Jianfei Hu
2012-Apr-23 05:23 UTC
[LLVMdev] Problem about the type of Function's arguement in llvm
I read the tutorial doc and some info of SSA, finally understand it. Thanks for your help. And the segmentation error of loading arguementation occurs, (gdb error info) Program received signal SIGSEGV, Segmentation fault. 0x0855bb68 in llvm::LoadInst::LoadInst(llvm::Value*, char const*, bool, llvm::Instruction*) () code is like follows: //#include necessary header files int main(){ InitializeNativeTarget(); LLVMContext Context; module = new Module("Program", Context); vector<Type *> argslist; argslist.push_back(Type::getDoubleTy(Context)); FunctionType *funType = FunctionType::get(Type::getDoubleTy(Context), argslist, false); Function *fun= cast<Function> ( module->getOrInsertFunction("fun", funType)); BasicBlock * block = BasicBlock::Create(Context, "mainBlock", fun); IRBuilder<> builder(block); Function::arg_iterator itr = fun->arg_begin(); builder.CreateLoad(itr); Value *result = ConstantFP::get(Type::getDoubleTy(getGlobalContext()), 1.0); builder.CreateRet(result); vector<GenericValue> args; ExecutionEngine *e = EngineBuilder(module).create(); outs()<<*module<<"\n"; outs().flush(); return 0; } I'm confused that arg_iterator itr ( Function::arg_iterator itr fun->arg_begin(); ) can be used in CreateStore() function, however, it can not be used as the arguments of CreateLoad() function. 2012/4/22 Hongbin Zheng <etherzhhb at gmail.com>> hi > > On Sun, Apr 22, 2012 at 8:36 PM, Jianfei Hu <hujianfei258 at gmail.com> > wrote: > > in the tutorial of official llvm doc, chapter 3, it deals with > arguement of > > function as follow: > > > > for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size(); > > ++AI, ++Idx) { > > AI->setName(Args[Idx]); > > > > // NamedValues is map<string, Value*> > > NamedValues[Args[Idx]] = AI; > > > > and when it try to get the value of arguement, it simply does: > > > > Value *VariableExprAST::Codegen() { > > Value *V = NamedValues[Name]; > > return V ? V : ErrorV("Unknown variable name"); > > } > > > > It means that we can access value of arguement directly. > Yes, it is possible to access the values (virtual registers) direclty, > but you may need to construct SSA form by yourself. > > > > However, in llvm.org/demo and the chapter 6 of tutorial, it deals with > > arguemnt: > > 1. allocate memory for arguement > > 2. then store the arguemnt into the memory. > > 3. CreateLoad() to load the arguement when accessing the value of > arguement. > > > > I have several problems: > > Which way should I follow to get the vale of arguement? > If you do not want to construct SSA from by yourself, you probably > should follow this way. You can schedule the "mem2reg"[1] after you > build the IR, it will promote alloca/load/store into scalar virtual > registers for you. (I remember the tutorial had a discussion about > this.) > > > > When I tried to use CreateLoad(arguement), the program gives a > segmentation > > error. > > Does this result from the wrong type of argument, unsuitable to be the > > params of CreateLoad()? > Could you paste your code? Had you initialize the IRBuilder with > insert position? > > If it is, what's the type of Function->arg_begin() ? (The source code is > too > > nested for me to find the original type of that) > > > > best regards > ether > [1]http://llvm.org/docs/doxygen/html/Mem2Reg_8cpp_source.html >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120423/16666c4e/attachment.html>
Possibly Parallel Threads
- [LLVMdev] Problem about the type of Function's arguement in llvm
- [LLVMdev] Problem about the type of Function's arguement in llvm
- [LLVMdev] Kaleidoscope toy4 failure seg fault on llvm::ExecutionEngine::getTargetData (this=0x0)
- [LLVMdev] Kaleidoscope toy4 failure seg fault on llvm::ExecutionEngine::getTargetData (this=0x0)
- [LLVMdev] Kaleidoscope toy4 failure seg fault on llvm::ExecutionEngine::getTargetData (this=0x0)