giving metadata a function parameter as an operand

Hi,

Is it possible to give an instruction metadata that accepts function argument as operand? The 2 functions are in the same module so the metadata operand can be resolved. Here is how I imagine it should look like:

define i32 @f(i32 %v) {
%v0 = call i32 @f1 (i32 %v, metadata !0) ; this ok
ret i32 %v0
}

define i32 @f1 (i32 %v, metadata %md) { ; and this is ok too
%t0 = add i32 %v, 1, !md !%md
; ^ here it says it expects an integer
ret i32 %t0
}

!0 = metadata !{i32 4}

Thanks, Anat

From: "Anat Shemer" <anat.shemer@intel.com>
To: llvmdev@cs.uiuc.edu
Sent: Tuesday, February 12, 2013 6:33:41 AM
Subject: [LLVMdev] giving metadata a function parameter as an operand

Hi,

Is it possible to give an instruction metadata that accepts function
argument as operand? The 2 functions are in the same module so the
metadata operand can be resolved. Here is how I imagine it should
look like:

define i32 @f(i32 %v) {
%v0 = call i32 @f1 (i32 %v, metadata !0) ; this ok
ret i32 %v0
}

define i32 @f1 (i32 %v, metadata %md) { ; and this is ok too
%t0 = add i32 %v, 1, !md !%md
; ^ here it says it expects an integer
ret i32 %t0
}

!0 = metadata !{i32 4}

Can you explain your motivation for wanting this?

-Hal

constant (= known at compile time) metadata nodes can be attached to
instructions. This makes sense, because metadata attached to
instructions are a complile-time property that should be available to
LLVM optimization passes and backends. What are the semantics of
having this value only known at runtime? Perhaps there's a better way
to achieve what you're trying to do.

Eli

Hi,

Thanks for looking into this.

The function f1 is inlined in the function f. Once this happens %md is a constant.

I do this because I would like to inline f1 with different metadata values depending on the call site. There is a finite number of possible metadata values, practically 2 at the moment.

One alternative that I currently have in mind is to duplicate the function f1, where the metadata is the only difference.

Another alternative has the advantage of duplicating only the instruction that has the metadata attached but is more complex. (The idea is to transfer %md to f1 as i32 and duplicate the instruction with the metadata in f1 in separate basic blocks such that each copy has different metadata value. Additional control flow will check the value of %md and jump to the right block accordingly. This solution hopes that future passes will remove the redundant blocks once f1 is inlined and %md becomes a constant.)

Both these options require code duplication with all the consequences of maintainability.

And of course it's possible to write a pass that will handle this case.

But first I look for an existing solution.

Thanks, Anat