[LLVMdev] How to const char* Value for function argument

Dmitry N. Mikushin maemarcus at gmail.com
Thu Sep 22 03:22:45 PDT 2011


Thank you, James!

2011/9/22 James Molloy <James.Molloy at arm.com>:
> Hi Dimitry,
>
> This makes sense if you think about it from the perspective that the string you want passing must be passed at runtime, and so can't use a const char * from compile time.
>
> You need to make the string visible in the compiled image, and use that as the argument. A string is an array of 8-bit integers, so you need to create a ConstantArray.
>
> Value *v = ConstantArray::get(Context, callee->getName(), /*AddNull=*/true);
>
> Cheers,
>
> James
>
> -----Original Message-----
> From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Dmitry N. Mikushin
> Sent: 22 September 2011 11:06
> To: LLVM-Dev
> Subject: [LLVMdev] How to const char* Value for function argument
>
> Hi,
>
> I'm trying to replace function call with call to
> wrapper(function_name, num_args, ...), where varargs hold args of
> original call.
>
>        Function* launch = Function::Create(
>                TypeBuilder<int(const char*, int, ...), false>::get(context),
>                GlobalValue::ExternalLinkage, "kernelgen_launch_", m2);
>
>                                {
>                                        CallInst* call = dyn_cast<CallInst>(cast<Value>(I));
>                                        if (!call) continue;
>                                        Function* callee = call->getCalledFunction();
>                                        if (!callee && !callee->isDeclaration()) continue;
>                                        if (callee->getName() != func2.getName()) continue;
>
>                                        SmallVector<Value*, 16> callargs(call->op_begin(), call->op_end());
>                                        callargs.insert(callargs.begin(),
>                                                ConstantInt::get(Type::getInt32Ty(context),
>                                                        call->getNumArgOperands()));
>                                        callargs.insert(callargs.begin(), callee->getName());
>                                        CallInst* newcall = CallInst::Create(launch, callargs, "", call);
>                                        newcall->takeName(call);
>                                        newcall->setCallingConv(call->getCallingConv());
>                                        newcall->setAttributes(call->getAttributes());
>                                        newcall->setDebugLoc(call->getDebugLoc());
>                                        call->replaceAllUsesWith(newcall);
>
>                                        found = true;
>                                        break;
>                                }
>
> The line callargs.insert(callargs.begin(), callee->getName()); is
> invalid: instead of StringRef I need to supply a Value* for the second
> argument. How to construct value for const char* ?
>
> Thanks,
> - D.
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
> -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium.  Thank you.
>
>




More information about the llvm-dev mailing list