[LLVMdev] Problem about the type of Function's arguement in llvm
Hongbin Zheng
etherzhhb at gmail.com
Mon Apr 23 04:27:32 PDT 2012
On Mon, Apr 23, 2012 at 1:23 PM, Jianfei Hu <hujianfei258 at gmail.com> wrote:
> 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.
You cannot load from the argument in because the type of the argument
is double instead of double*, in fact, the C equivalence of the LLVM
IR create by you is something looks like this:
double fun(double a) {
c = *a; // Illegal, a is not a pointer
...
}
To get the value of the argument, you can use the Argument object
directly (the first way in your first mail) or follow the second way
described in you first mail:
> 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.
best regards
ether
>
>
> 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
>
>
More information about the llvm-dev
mailing list