[llvm-dev] Is there a way to map from coerced parameter to original parameter

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] Is there a way to map from coerced parameter to original parameter

Jonas Paulsson via llvm-dev
Hello.

Some parameters of the function would be changed according to ABI rules by compiler
I would like to mapping from coerced parameters of the function(by compiler(clang)) to C code.

For example,

In C
typedef struct data_info
{
    char *buf;
    size_t size;
} data_info;

data_info foo(data_info src, int result)
{
    data_info dst;
    dst.buf = (char *)malloc(src.size);
    memcpy((dst.buf, src.buf, src.size);
    return dst;
}

In IR (with reg2mem, no opt)

define {i8 *, i64} @foo(i8*, i64, i32*) #0 {
    %4 = alloc %struct.dat_info, align 8
    %5 = alloc %struct.dat_info, align 8
    %6 = bitcast %struct.data_info* %6 to {i8 *, i64}
    %7 = getelementptr inbounds {i8 *, i64}, {i8 *, i64}* %6, i32 0, i32 0
    %8 = getelementptr inbounds {i8 *, i64}, {i8 *, i64}* %6, i32 0, i32 1
 .....
}

The foo function has 2 parameter in C code, but IR code has 3 parameters.
There is mismatch between C code and IR code.

I want to mapping parameters from IR to C code like in the below

1st parameter (i8 *) in IR code-> 1st field of the first parameter of the function in C
2nd (i64) -> 2nd field of the 1st parameter of the function in C.
3rd (i32 *) -> 2nd parameter of the function in C.

Is there any class or tools for it in LLVM.
Please advise the way/idea to map between original parameter in C code and coerced parameter in IR code

_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Is there a way to map from coerced parameter to original parameter

Jonas Paulsson via llvm-dev
Hi,

I do not think there is a way guaranteed to work.

If the changes due to the ABI are actually not important for you I
recommend compiling for a target like wasm instead. The mapping IR
argument -> C argument becomes simpler. (Though the optimization caveat
still exists.)

If you cannot change the target:
Debug metadata might help but I'm not sure how much. One other way that
could be explored would be to involve clang. I doubt that this code path
exists. The idea is to ask clang from the LLVM pass, e.g., via a custom
analysis remark, what the mapping is. Similarly you could emit the
mapping from the beginning as metadata. However, all approaches might be
prone to errors once you start optimizing the code.

I hope this helps,
  Johannes


P.S.

This question came up multiple times over the years. I don't have a link
to an older discussion handy but you might be able to find it via
google.


On 01/16, ZIN MOON via llvm-dev wrote:

> Hello.
>
> Some parameters of the function would be changed according to ABI rules by
> compiler
> I would like to mapping from coerced parameters of the function(by
> compiler(clang)) to C code.
>
> For example,
>
> In C
> typedef struct data_info
> {
>     char *buf;
>     size_t size;
> } data_info;
>
> data_info foo(data_info src, int result)
> {
>     data_info dst;
>     dst.buf = (char *)malloc(src.size);
>     memcpy((dst.buf, src.buf, src.size);
>     return dst;
> }
>
> In IR (with reg2mem, no opt)
>
> define {i8 *, i64} @foo(i8*, i64, i32*) #0 {
>     %4 = alloc %struct.dat_info, align 8
>     %5 = alloc %struct.dat_info, align 8
>     %6 = bitcast %struct.data_info* %6 to {i8 *, i64}
>     %7 = getelementptr inbounds {i8 *, i64}, {i8 *, i64}* %6, i32 0, i32 0
>     %8 = getelementptr inbounds {i8 *, i64}, {i8 *, i64}* %6, i32 0, i32 1
>  .....
> }
>
> The foo function has 2 parameter in C code, but IR code has 3 parameters.
> There is mismatch between C code and IR code.
>
> I want to mapping parameters from IR to C code like in the below
>
> 1st parameter (i8 *) in IR code-> 1st field of the first parameter of the
> function in C
> 2nd (i64) -> 2nd field of the 1st parameter of the function in C.
> 3rd (i32 *) -> 2nd parameter of the function in C.
>
> Is there any class or tools for it in LLVM.
> Please advise the way/idea to map between original parameter in C code and
> coerced parameter in IR code

> _______________________________________________
> LLVM Developers mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


--

Johannes Doerfert
Researcher

Argonne National Laboratory
Lemont, IL 60439, USA

[hidden email]

_______________________________________________
LLVM Developers mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

signature.asc (235 bytes) Download Attachment