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.

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

Thank you, James!