[llvm-dev] Cast between struct

98 views
Skip to first unread message

Jie Zhou via llvm-dev

unread,
May 6, 2020, 8:17:17 PM5/6/20
to Joerg Sonnenberger via llvm-dev
Dear All,

In my development, a function returns a struct {i8*, i64}, and on the
call site I need to assign the return value to a struct of type {some_struct*, i64}.
One way to do it is to call the mutateType() method of llvm::Value on
the return value of the function call; however, I’m concerned that this mutateType()
method might be too disruptive (it’s discouraged to use it by the documentation).
Since LLVM does not support cast between structs. Is there any way I can safely
“cast” a value of one struct type to another?

Thanks,
- Jie
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Krzysztof Parzyszek via llvm-dev

unread,
May 6, 2020, 9:54:22 PM5/6/20
to Jie Zhou, llvm...@lists.llvm.org
You can
1. extract individual elements of {i8*, i64},
2. bitcast the first to some_struct*,
3. insert them into a new struct of type {some_struct*, i64}.


--
Krzysztof Parzyszek kpar...@quicinc.com AI tools development

Jie Zhou via llvm-dev

unread,
May 6, 2020, 9:59:13 PM5/6/20
to Krzysztof Parzyszek, llvm...@lists.llvm.org

> On May 6, 2020, at 21:53, Krzysztof Parzyszek <kpar...@quicinc.com> wrote:
>
> You can
> 1. extract individual elements of {i8*, i64},
> 2. bitcast the first to some_struct*,
> 3. insert them into a new struct of type {some_struct*, i64}.

Thanks for the help. I’m thinking the same way. Now I’m stuck with
how to create a struct; I know how to create a llvm::StructType but
I’m not clear of how to create a Value whose type is struct). It seems that
llvm disallowed directly create a Value object; all Values are created from
one of its numerous descendants. Can you give me some guidance here?

Thanks,
- Jie

>
>
> --
> Krzysztof Parzyszek kpar...@quicinc.com AI tools development
>
>> -----Original Message-----
>> From: llvm-dev <llvm-dev...@lists.llvm.org> On Behalf Of Jie Zhou via
>> llvm-dev
>> Sent: Wednesday, May 6, 2020 7:17 PM
>> To: Joerg Sonnenberger via llvm-dev <llvm...@lists.llvm.org>
>> Subject: [EXT] [llvm-dev] Cast between struct
>>
>> Dear All,
>>
>> In my development, a function returns a struct {i8*, i64}, and on the call
>> site I need to assign the return value to a struct of type {some_struct*,
>> i64}.
>> One way to do it is to call the mutateType() method of llvm::Value on the
>> return value of the function call; however, I’m concerned that this
>> mutateType() method might be too disruptive (it’s discouraged to use it by
>> the documentation).
>> Since LLVM does not support cast between structs. Is there any way I can
>> safely “cast” a value of one struct type to another?
>>
>> Thanks,
>> - Jie
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm...@lists.llvm.org

>> https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwIGaQ&c=kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=KAtyTEI8n3FritxDpKpR7rv3VjdmUs0luiVKZLb_bNI&m=uYmm3cX0yLTO1MaEx_UXqc7gisr4jIhk9jA-eFWAjAA&s=KKOI1Vd0TktBj4Yu95cYeygctxjDWhOm9DF1NgxRQAs&e=

Krzysztof Parzyszek via llvm-dev

unread,
May 6, 2020, 10:14:31 PM5/6/20
to Jie Zhou, llvm...@lists.llvm.org
> -----Original Message-----
>
> Thanks for the help. I’m thinking the same way. Now I’m stuck with how to
> create a struct; I know how to create a llvm::StructType but I’m not clear of
> how to create a Value whose type is struct). It seems that llvm disallowed
> directly create a Value object; all Values are created from one of its
> numerous descendants. Can you give me some guidance here?

The "trick" is to start with "undef" (which always exists for any type).

-Krzysztof

Jie Zhou via llvm-dev

unread,
May 7, 2020, 5:38:40 PM5/7/20
to Krzysztof Parzyszek, llvm...@lists.llvm.org

On May 6, 2020, at 22:14, Krzysztof Parzyszek <kpar...@quicinc.com> wrote:

-----Original Message-----

Thanks for the help. I’m thinking the same way. Now I’m stuck with how to
create a struct; I know how to create a llvm::StructType but I’m not clear of
how to create a Value whose type is struct). It seems that llvm disallowed
directly create a Value object; all Values are created from one of its
numerous descendants. Can you give me some guidance here?

The "trick" is to start with "undef" (which always exists for any type).

Thanks very much for the direction. This UndefValue method works for me.

For anyone who’s interested in more details, here is the code snippet that
“cast” a struct of type {i8*, i64} to a struct of type {some_struct*, i64}:

// V is a struct of type {i8*, i64}, and RetIRTy is type {some_struct*, i64}.
llvm::UndefValue *newRet = llvm::UndefValue::get(RetIRTy);        
llvm::Value *genericPtr = Builder.CreateExtractValue(V, 0);       
llvm::Value *ID = Builder.CreateExtractValue(V, 1);               
llvm::Value *concretePtr = Builder.CreateBitCast                  
  (genericPtr, cast<llvm::StructType>(RetIRTy)->getElementType(0));
llvm::Value *insertPtr = Builder.CreateInsertValue(newRet, concretePtr, 0);
V = Builder.CreateInsertValue(insertPtr, ID, 1);

- Jie

-Krzysztof


Reply all
Reply to author
Forward
0 new messages