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

Asit Dhal via llvm-dev llvm-dev at lists.llvm.org
Fri Nov 25 05:44:50 PST 2016


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-v
> 128: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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161125/694439d2/attachment.html>


More information about the llvm-dev mailing list