[llvm-dev] Translation of custom attribute (defined for variables) from clang to llvm

Naveed Ul Mustafa via llvm-dev llvm-dev at lists.llvm.org
Fri Nov 25 06:04:11 PST 2016


Hi Asit,

thanks for the reply.

But I guess I was not clear in my question. Actually, i dont want to use 
__ATTRIBUTE__((ANNOTATE("MOVIATTR"))), since in documentation it is 
stated that "This intrinsic allows annotation of local variables with 
arbitrary strings. This can be useful for special purpose optimizations 
that want to look for these annotations. These have no other defined 
use; they are ignored by code generation and optimization." 
http://llvm.org/docs/LangRef.html#llvm-var-annotation-intrinsic

Thats why I want to use my custom defined attribute (i.e. 
moviAttr(unsigned int))) so that i can use it later for code generation.

Syntax of adding my custom attribute in code snippet above is correct, 
since clang doesnt generate any error.

do you think there is anyother reason for my custom attribute to not 
appear in IR?



---
Naveed Ul Mustafa

On 2016-11-25 13:44, Asit Dhal wrote:
> Hi,
> 
> I think, the syntax is wrong.
> You can try this way..
> 
> #DEFINE NEWATTR __ATTRIBUTE__((ANNOTATE("MOVIATTR")))
> 
> int main()
> {
>         NEWATTR volatile unsigned int a = 5;
>         VOLATILE UNSIGNED INT *P;
>         p = &a;
>         return (a+*p);
> }
> 
> And it generates
> 
> ; ModuleID = 'test2.cpp'
> target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
> target triple = "x86_64-pc-linux-gnu"
> 
> @.str = private unnamed_addr constant [9 x i8] c"moviAttr\00", section
> "llvm.metadata"
> @.str1 = private unnamed_addr constant [10 x i8] c"test2.cpp\00",
> section "llvm.metadata"
> 
> ; Function Attrs: nounwind uwtable
> define i32 @main() #0 {
>   %1 = alloca i32, align 4
>   %a = alloca i32, align 4
>   %p = alloca i32*, align 8
>   store i32 0, i32* %1
>   %2 = bitcast i32* %a to i8*
>   CALL VOID @LLVM.VAR.ANNOTATION(I8* %2, I8* GETELEMENTPTR INBOUNDS
> ([9 X I8]* @.STR, I32 0, I32 0), I8* GETELEMENTPTR INBOUNDS ([10 X
> I8]* @.STR1, I32 0, I32 0), I32 5)
>   store volatile i32 5, i32* %a, align 4
>   store i32* %a, i32** %p, align 8
>   %3 = load volatile i32* %a, align 4
>   %4 = load i32** %p, align 8
>   %5 = load volatile i32* %4, align 4
>   %6 = add i32 %3, %5
>   ret i32 %6
> }
> 
> ; Function Attrs: nounwind
> DECLARE VOID @LLVM.VAR.ANNOTATION(I8*, I8*, I8*, I32) #1
> 
> attributes #0 = { nounwind uwtable "less-precise-fpmad"="false"
> "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"
> "no-infs-fp-math"="false" "no-nans-fp-math"="false"
> "stack-protector-buffer-size"="8" "unsafe-fp-math"="false"
> "use-soft-float"="false" }
> attributes #1 = { nounwind }
> 
> !llvm.ident = !{!0}
> 
> !0 = !{!"Ubuntu clang version 3.6.0-2ubuntu1~trusty1
> (tags/RELEASE_360/final) (based on LLVM 3.6.0)"}
> 
> On Fri, Nov 25, 2016 at 12:52 PM, Naveed Ul Mustafa via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
> 
>> Hi All,
>> 
>> I need your guidance about a custom attribute. I have defined one
>> for variables. It is accepted in the source code (without any
>> warnings from clang), for example in following snippet.
>> 
>> #define NEWATTR __attribute__((moviAttr(1)))
>> 
>> int main()
>> {
>> NEWATTR volatile unsigned int a = 5;
>> volatile unsigned int *p;
>> p = &a;
>> return (a+*p);
>> }
>> 
>> and actually when I Dump the declaration, after adding the attribute
>> in handleMoviAttr() function in SemaDeclAttr.cpp as shown in
>> following snippet
>> 
>> D->addAttr(::new (S.Context)moviAttrAttr(Attr.getRange(), S.Context,
>> Val, Attr.getAttributeSpellingListIndex()));
>> D->dump();
>> 
>> It is dumped by clang with following output
>> 
>> VarDecl 0x440c698 <file.c:6:17, line:10:32> col:32 a 'volatile
>> unsigned int'
>> `-moviAttrAttr 0x440c6d0 <line:6:32, col:42> 1
>> 
>> HOWEVER, the attribute doesnt appear in IR of the program. IR is
>> shown below.
>> 
>> *** IR Dump Before Pre-ISel Intrinsic Lowering ***; ModuleID =
>> 'file.bc'
>> source_filename = "file.c"
>> target datalayout =
>> 
> "e-m:e-p:32:32-f64:64-i64:64-v128:64-v64:64-v32:32-v16:16-n8:16:32-S64"
>> target triple = "shave"
>> 
>> ; Function Attrs: nounwind
>> define i32 @main() local_unnamed_addr #0 {
>> %1 = alloca i32, align 4
>> %2 = bitcast i32* %1 to i8*
>> call void @llvm.lifetime.start(i64 4, i8* %2)
>> store volatile i32 5, i32* %1, align 4
>> %3 = load volatile i32, i32* %1, align 4
>> %4 = load volatile i32, i32* %1, align 4
>> %5 = add i32 %4, %3
>> call void @llvm.lifetime.end(i64 4, i8* %2)
>> ret i32 %5
>> }
>> 
>> ; Function Attrs: argmemonly nounwind
>> declare void @llvm.lifetime.start(i64, i8* nocapture) #1
>> 
>> ; Function Attrs: argmemonly nounwind
>> declare void @llvm.lifetime.end(i64, i8* nocapture) #1
>> 
>> attributes #0 = { nounwind "disable-tail-calls"="false"
>> "less-precise-fpmad"="false" "no-frame-pointer-elim"="true"
>> "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false"
>> "no-jump-tables"="false" "no-nans-fp-math"="false"
>> "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8"
>> "target-cpu"="myriad2.2" "unsafe-fp-math"="false"
>> "use-soft-float"="false" }
>> attributes #1 = { argmemonly nounwind "target-cpu"="myriad2.2" }
>> 
>> Can someone help me please to solve the problem so that custom
>> attribute also appears in the IR.
>> 
>> Thanks in advance
>> 
>> --
>> Naveed Ul Mustafa
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev [1]
> 
> 
> 
> Links:
> ------
> [1] http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


More information about the llvm-dev mailing list