retrieve information from a macro inlined as x86_64 asm in LLVM IR

Hi LLVM devs,

I work on a pass that creates multiple versions of a code region and switches between them. The decision about the version to be selected is taken by an external runtime system. I insert callbacks to this system in my code to pass the control of the execution. All callbacks are written in inline assembly, in macros.
The problem comes when I need to send some information from the runtime system, back to the code.
I create one local variable :

lim = new AllocaInst(…);

and I want the value of %lim to be set by the runtime system. For this, I want to create a macro that takes as parameter the address of this variable, such that the runtime system can put the value it computes in this address, at runtime. (Similar to sending a parameter by reference. )
If I send the address of the Value* lim as a param to the macro, it puts the hardcoded address (as expected).

std::stringstream out;
out.str(std::string()); out << &lim;

InlineAsm* IA = InlineAsm::get(asmFuncTy, “camus_macro_decision” + out.str() , “=r”, true);
CallInst* cmp = CallInst::Create(IA, …);

creates the call:
camus_macro_decision 0x7fffdf3172f0

I tried also by creating a pointer to the Value* lim, without any success.

I know this is not the good approach, but can you suggest a solution to retrieve information from the macro?

Thank you,
Alexandra

Hi LLVM devs,

I work on a pass that creates multiple versions of a code region and switches between them. The decision about the version to be selected is taken by an external runtime system. I insert callbacks to this system in my code to pass the control of the execution. All callbacks are written in inline assembly, in macros.
The problem comes when I need to send some information from the runtime system, back to the code.
I create one local variable :

lim = new AllocaInst(…);

and I want the value of %lim to be set by the runtime system. For this, I want to create a macro that takes as parameter the address of this variable, such that the runtime system can put the value it computes in this address, at runtime. (Similar to sending a parameter by reference. )
If I send the address of the Value* lim as a param to the macro, it puts the hardcoded address (as expected).

std::stringstream out;
out.str(std::string()); out << &lim;

This is very confused. ‘lim’ is a local variable containing a pointer to an AllocaInst; taking its address will give you a pointer into the stack frame of the compiler, which is generally not even in the same process as where the code will run.

The value represented by an AllocaInst is the address of the allocated memory; just use that as an operand to the inline assembly. That said…

creates the call:
camus_macro_decision 0x7fffdf3172f0

…this is not valid x86-64 assembly at all. If you’re just trying to call a function, passing the address of your local variable, just use an LLVM call instruction, like so:

llvm::Constant *fn = module.getOrInsertFunction(“camus_macro_decision”, fnType);
builder.CreateCall(fn, lim);

Note that you probably want the alloca to be inserted in the entry block; see the documentation for more on that.

John.