llvm get annotations

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

llvm get annotations

Alexandru Ionut Diaconescu
Hello everyone !

I followed http://stackoverflow.com/questions/4976298/modern-equivalent-of-llvm-annotationmanager in order to get annotations from my target bytecode. All the examples that I give in the following is related to the code from that link. I have `__attribute__((annotate("DS"))) int f=0;` into the target C++ program and the related IR code:

    @.str = private unnamed_addr constant [3 x i8] c"DS\00", section "llvm.metadata"
    @llvm.global.annotations = appending global [1 x { i8*, i8*, i8*, i32 }] [{ i8*, i8*, i8*, i32 } { i8* bitcast (i32* @f to i8*), i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8]* @.str1, i32 0, i32 0), i32 18 }], section "llvm.metadata"

I have two problems following the examples from http://stackoverflow.com/questions/4976298/modern-equivalent-of-llvm-annotationmanager :

1. Cannot use `annotatedValue->getUnderlyingObject()` since Value class has not this method. Instead, I found in LLVM the following methods: `llvm::GetUnderlyingObject()`,`getUnderlyingObjectFromInt(), getUnderlyingObjects()`.  

 I hacked the method `llvm::GetUnderlyingObject()`...that is with Capital G (different from `getUnderlyingObject()`) and call `myGetUnderlyingObject(annotatedValue,0,6)` as mentioned at http://llvm.org/docs/doxygen/html/namespacellvm.html#ace6c957db39858291c6ce01c38e78480 .

2. Since I write my code into runOnFunction(), I have problems with `getGlobalVariableString(std::string name)` function from the stackoverflow link. So I have to integrate the following part of code into runOnFunction():

    std::string name = gv->getName().str();

// assumption: the zeroth operand of a Value::GlobalVariableVal is the actual Value

    //Module* module = ; //can I use Module& llvm::CallGraph::getModule() ?
    Value *v = module->getNamedValue(name)->getOperand(0);
    if(v->getValueID() == Value::ConstantArrayVal)
    {
        ConstantArray *ca = (ConstantArray *)v;    
        std::cout << "    argument " << a->getType()->getDescription() << " "<<a->getName().str()
            << " has annotation \"" << ca->getAsString() << "\"\n";
    }


Is there an easier way to get the simple DS annotation of @f ?

Thank you for any suggestion!
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: llvm get annotations

Sebastian Dreßler
Hi Alexandru,

On 02/27/2013 03:15 PM, Alexandru Ionut Diaconescu wrote:
> [...]
> Is there an easier way to get the simple DS annotation of @f ?
>

I used to annotate C / C++ functions with e.g.
__attribute__((annotate("kernel"))) and the retrieved the name of the
function with the following code:

/*****************************************************/

GlobalValue *gv;
Value *a = gv->getOperand(0);

ConstantStruct *annoStruct =
    dynamic_cast<ConstantStruct>(
        dynamic_cast<ConstantArray>(a)->getOperand(0);
    );

GlobalVariable *gvar = dynamic_cast<GlobalVariable>(
    dynamic_cast<ConstantExpr>(
        annoStruct->getOperand(1)
    )->getOperand(0)
);

std::string str = dynamic_cast<ConstantDataArray>(
    gvar->getOperand(0)
)->getAsString();

if (str.find("kernel")) {
    [...]
}

/*****************************************************/

Inside the last if one can find the function that belongs to the annotation.

IMO its a bit ugly, but it works for me ;)

Cheers,
Sebastian

--
Mit freundlichen Grüßen / Kind regards

Sebastian Dreßler

Zuse Institute Berlin (ZIB)
Takustraße 7
D-14195 Berlin-Dahlem
Germany

[hidden email]
Phone: +49 30 84185-261

http://www.zib.de/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: llvm get annotations

Alexandru Ionut Diaconescu

Hi Sebastian,

Thanks for the response.

I already did this :

I cast the entire annotated expression to Value*. Then, in order to avoid ugly things like getAsString(), I check if V->getValueID() == Value::ConstantArrayVal in order to cast it to ConstantArray. Because it contains only array[0], I cast array0>getOperand(0) to ConstantStruct. Therefore, from ConstantStruct you can get all the four operands. Now, as a TODO, is only to get the names of @f, @str from every field.

But right now I have only a small step to do. ConstantStruct->getOperand(0) gives me   i8* bitcast (i32* @f to i8*)  . Do you know how I can get the name @f from this? I cannot convert it again to ConstantStruct and follow the same procedure, it will give me a segfault.

Thank you again. Your advice was very helpful :)


On Fri, Mar 1, 2013 at 9:22 AM, Sebastian Dreßler <[hidden email]> wrote:
Hi,

On 02/28/2013 03:55 PM, Alexandru Ionut Diaconescu wrote:
> Hi Sebastian,
>
> I think I am very close to get the annotation, but I need to parse a
> Value*, which is of type  {[1 x { i8*, i8*, i8*, i32 }]. In one of those
> fields is the variable that I'm interested into and the string keeping the
> annotation. But I don't know how to parse .... use_begin() gives me the
> same Value*....do you know how I can parse inside the element?
>

This is an Array Type (http://llvm.org/docs/LangRef.html#array-type)
which contains a struct. I assume, that you could cast to ConstantArray
which is a Value and then use getOperand(0) and cast its result to
ConstantStruct, like I've done in the sample code. Then you have the
struct and you can traverse it further.


Cheers,
Sebastian

> Thank you :) !
>
>
>
>
>
> On Wed, Feb 27, 2013 at 4:44 PM, Alexandru Ionut Diaconescu <
> [hidden email]> wrote:
>
>> Hi Sebastian,
>>
>> Thank you for the dummy file. I will try to understand and use it. I guess
>> I have some problems with C++11. If I cannot solve by simple "parsing" my
>> code for @llvm.global.annotations, I will ask you again.
>>
>> Cheers,
>> Alex
>>
>>
>> On Wed, Feb 27, 2013 at 4:34 PM, Sebastian Dreßler <[hidden email]>wrote:
>>
>>> Hi Alexandru,
>>>
>>> On 02/27/2013 04:20 PM, Alexandru Ionut Diaconescu wrote:
>>>> [...]
>>>> Do you have a larger piece of code (maybe you have some dummy/test
>>>> examples) from your previous work with annotations? It would be very
>>> useful
>>>> :D
>>>>
>>>
>>> I've attached an example. But please be aware of:
>>>
>>> 1. It uses C++11 and cast (previously I simply replaced cast<> with
>>> dynamic_cast<> which does not seem to work everywhere, so sorry for that).
>>>
>>> 2. assert is not used in the style as it should be used, so pleas do not
>>> adopt this style.
>>>
>>>
>>> If some things are still unclear, keep asking ;)
>>>
>>> Cheers,
>>> Sebastian
>>>
>>>
>>> --
>>> Mit freundlichen Grüßen / Kind regards
>>>
>>> Sebastian Dreßler
>>>
>>> Zuse Institute Berlin (ZIB)
>>> Takustraße 7
>>> D-14195 Berlin-Dahlem
>>> Germany
>>>
>>> [hidden email]
>>> Phone: <a href="tel:%2B49%2030%2084185-261" value="+493084185261" target="_blank">+49 30 84185-261
>>>
>>> http://www.zib.de/
>>>
>>
>>
>>
>> --
>> Best regards,
>> Alexandru Ionut Diaconescu
>>
>
>
>


--
Mit freundlichen Grüßen / Kind regards

Sebastian Dreßler

Zuse Institute Berlin (ZIB)
Takustraße 7
D-14195 Berlin-Dahlem
Germany

[hidden email]
Phone: <a href="tel:%2B49%2030%2084185-261" value="+493084185261" target="_blank">+49 30 84185-261

http://www.zib.de/



--
Best regards,
Alexandru Ionut Diaconescu

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: llvm get annotations

Alexandru Ionut Diaconescu
Hi, I solved it. From the ConstantStruct you can call getOperand() multiple times, so "mine" as deep as you can.

On Fri, Mar 1, 2013 at 1:41 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:

Hi Sebastian,

Thanks for the response.

I already did this :

I cast the entire annotated expression to Value*. Then, in order to avoid ugly things like getAsString(), I check if V->getValueID() == Value::ConstantArrayVal in order to cast it to ConstantArray. Because it contains only array[0], I cast array0>getOperand(0) to ConstantStruct. Therefore, from ConstantStruct you can get all the four operands. Now, as a TODO, is only to get the names of @f, @str from every field.

But right now I have only a small step to do. ConstantStruct->getOperand(0) gives me   i8* bitcast (i32* @f to i8*)  . Do you know how I can get the name @f from this? I cannot convert it again to ConstantStruct and follow the same procedure, it will give me a segfault.

Thank you again. Your advice was very helpful :)



On Fri, Mar 1, 2013 at 9:22 AM, Sebastian Dreßler <[hidden email]> wrote:
Hi,

On 02/28/2013 03:55 PM, Alexandru Ionut Diaconescu wrote:
> Hi Sebastian,
>
> I think I am very close to get the annotation, but I need to parse a
> Value*, which is of type  {[1 x { i8*, i8*, i8*, i32 }]. In one of those
> fields is the variable that I'm interested into and the string keeping the
> annotation. But I don't know how to parse .... use_begin() gives me the
> same Value*....do you know how I can parse inside the element?
>

This is an Array Type (http://llvm.org/docs/LangRef.html#array-type)
which contains a struct. I assume, that you could cast to ConstantArray
which is a Value and then use getOperand(0) and cast its result to
ConstantStruct, like I've done in the sample code. Then you have the
struct and you can traverse it further.


Cheers,
Sebastian

> Thank you :) !
>
>
>
>
>
> On Wed, Feb 27, 2013 at 4:44 PM, Alexandru Ionut Diaconescu <
> [hidden email]> wrote:
>
>> Hi Sebastian,
>>
>> Thank you for the dummy file. I will try to understand and use it. I guess
>> I have some problems with C++11. If I cannot solve by simple "parsing" my
>> code for @llvm.global.annotations, I will ask you again.
>>
>> Cheers,
>> Alex
>>
>>
>> On Wed, Feb 27, 2013 at 4:34 PM, Sebastian Dreßler <[hidden email]>wrote:
>>
>>> Hi Alexandru,
>>>
>>> On 02/27/2013 04:20 PM, Alexandru Ionut Diaconescu wrote:
>>>> [...]
>>>> Do you have a larger piece of code (maybe you have some dummy/test
>>>> examples) from your previous work with annotations? It would be very
>>> useful
>>>> :D
>>>>
>>>
>>> I've attached an example. But please be aware of:
>>>
>>> 1. It uses C++11 and cast (previously I simply replaced cast<> with
>>> dynamic_cast<> which does not seem to work everywhere, so sorry for that).
>>>
>>> 2. assert is not used in the style as it should be used, so pleas do not
>>> adopt this style.
>>>
>>>
>>> If some things are still unclear, keep asking ;)
>>>
>>> Cheers,
>>> Sebastian
>>>
>>>
>>> --
>>> Mit freundlichen Grüßen / Kind regards
>>>
>>> Sebastian Dreßler
>>>
>>> Zuse Institute Berlin (ZIB)
>>> Takustraße 7
>>> D-14195 Berlin-Dahlem
>>> Germany
>>>
>>> [hidden email]
>>> Phone: <a href="tel:%2B49%2030%2084185-261" value="+493084185261" target="_blank">+49 30 84185-261
>>>
>>> http://www.zib.de/
>>>
>>
>>
>>
>> --
>> Best regards,
>> Alexandru Ionut Diaconescu
>>
>
>
>


--
Mit freundlichen Grüßen / Kind regards

Sebastian Dreßler

Zuse Institute Berlin (ZIB)
Takustraße 7
D-14195 Berlin-Dahlem
Germany

[hidden email]
Phone: <a href="tel:%2B49%2030%2084185-261" value="+493084185261" target="_blank">+49 30 84185-261

http://www.zib.de/



--
Best regards,
Alexandru Ionut Diaconescu



--
Best regards,
Alexandru Ionut Diaconescu

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: llvm get annotations

Sebastian Dreßler
Hi,

On 03/01/2013 02:46 PM, Alexandru Ionut Diaconescu wrote:
> Hi, I solved it. From the ConstantStruct you can call getOperand() multiple
> times, so "mine" as deep as you can.
>

Great that you figured it out. And I'm glad, that I could help you.


Cheers,
Sebastian


> On Fri, Mar 1, 2013 at 1:41 PM, Alexandru Ionut Diaconescu <
> [hidden email]> wrote:
>
>>
>> Hi Sebastian,
>>
>> Thanks for the response.
>>
>> I already did this :
>>
>> I cast the entire annotated expression to Value*. Then, in order to avoid
>> ugly things like getAsString(), I check if V->getValueID() ==
>> Value::ConstantArrayVal in order to cast it to ConstantArray. Because it
>> contains only array[0], I cast array0>getOperand(0) to ConstantStruct.
>> Therefore, from ConstantStruct you can get all the four operands. Now, as
>> a TODO, is only to get the names of @f, @str from every field.
>>
>> But right now I have only a small step to do.
>> ConstantStruct->getOperand(0) gives me   i8* bitcast (i32* @f to i8*)  . Do
>> you know how I can get the name @f from this? I cannot convert it again to
>> ConstantStruct and follow the same procedure, it will give me a segfault.
>>
>> Thank you again. Your advice was very helpful :)
>>
>>
>>
>> On Fri, Mar 1, 2013 at 9:22 AM, Sebastian Dreßler <[hidden email]> wrote:
>>
>>> Hi,
>>>
>>> On 02/28/2013 03:55 PM, Alexandru Ionut Diaconescu wrote:
>>>> Hi Sebastian,
>>>>
>>>> I think I am very close to get the annotation, but I need to parse a
>>>> Value*, which is of type  {[1 x { i8*, i8*, i8*, i32 }]. In one of those
>>>> fields is the variable that I'm interested into and the string keeping
>>> the
>>>> annotation. But I don't know how to parse .... use_begin() gives me the
>>>> same Value*....do you know how I can parse inside the element?
>>>>
>>>
>>> This is an Array Type (http://llvm.org/docs/LangRef.html#array-type)
>>> which contains a struct. I assume, that you could cast to ConstantArray
>>> which is a Value and then use getOperand(0) and cast its result to
>>> ConstantStruct, like I've done in the sample code. Then you have the
>>> struct and you can traverse it further.
>>>
>>>
>>> Cheers,
>>> Sebastian
>>>
>>>> Thank you :) !
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Wed, Feb 27, 2013 at 4:44 PM, Alexandru Ionut Diaconescu <
>>>> [hidden email]> wrote:
>>>>
>>>>> Hi Sebastian,
>>>>>
>>>>> Thank you for the dummy file. I will try to understand and use it. I
>>> guess
>>>>> I have some problems with C++11. If I cannot solve by simple "parsing"
>>> my
>>>>> code for @llvm.global.annotations, I will ask you again.
>>>>>
>>>>> Cheers,
>>>>> Alex
>>>>>
>>>>>
>>>>> On Wed, Feb 27, 2013 at 4:34 PM, Sebastian Dreßler <[hidden email]
>>>> wrote:
>>>>>
>>>>>> Hi Alexandru,
>>>>>>
>>>>>> On 02/27/2013 04:20 PM, Alexandru Ionut Diaconescu wrote:
>>>>>>> [...]
>>>>>>> Do you have a larger piece of code (maybe you have some dummy/test
>>>>>>> examples) from your previous work with annotations? It would be very
>>>>>> useful
>>>>>>> :D
>>>>>>>
>>>>>>
>>>>>> I've attached an example. But please be aware of:
>>>>>>
>>>>>> 1. It uses C++11 and cast (previously I simply replaced cast<> with
>>>>>> dynamic_cast<> which does not seem to work everywhere, so sorry for
>>> that).
>>>>>>
>>>>>> 2. assert is not used in the style as it should be used, so pleas do
>>> not
>>>>>> adopt this style.
>>>>>>
>>>>>>
>>>>>> If some things are still unclear, keep asking ;)
>>>>>>
>>>>>> Cheers,
>>>>>> Sebastian
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Mit freundlichen Grüßen / Kind regards
>>>>>>
>>>>>> Sebastian Dreßler
>>>>>>
>>>>>> Zuse Institute Berlin (ZIB)
>>>>>> Takustraße 7
>>>>>> D-14195 Berlin-Dahlem
>>>>>> Germany
>>>>>>
>>>>>> [hidden email]
>>>>>> Phone: +49 30 84185-261
>>>>>>
>>>>>> http://www.zib.de/
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Best regards,
>>>>> Alexandru Ionut Diaconescu
>>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Mit freundlichen Grüßen / Kind regards
>>>
>>> Sebastian Dreßler
>>>
>>> Zuse Institute Berlin (ZIB)
>>> Takustraße 7
>>> D-14195 Berlin-Dahlem
>>> Germany
>>>
>>> [hidden email]
>>> Phone: +49 30 84185-261
>>>
>>> http://www.zib.de/
>>>
>>
>>
>>
>> --
>> Best regards,
>> Alexandru Ionut Diaconescu
>>
>
>
>


--
Mit freundlichen Grüßen / Kind regards

Sebastian Dreßler

Zuse Institute Berlin (ZIB)
Takustraße 7
D-14195 Berlin-Dahlem
Germany

[hidden email]
Phone: +49 30 84185-261

http://www.zib.de/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev