Arvind Sudarsanam
2010-Jul-14 22:03 UTC
[LLVMdev] Trying to access the user defined variable name
Dear all,
I have the following source code:
long f(long x, long y)
{
long u;
u = x+y;
return u;
};
After clang (with debug option set) and llvm opt using mem2reg, I get
the following .ll file
***************************************************************************
; ModuleID = '<stdin>'
target datalayout
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
target triple = "i686-pc-linux-gnu"
define i32 @f(i32 %x, i32 %y) nounwind {
entry:
call void @llvm.dbg.value(metadata !{i32 %x}, i64 0, metadata !0)
call void @llvm.dbg.value(metadata !{i32 %y}, i64 0, metadata !7)
%add = add nsw i32 %x, %y, !dbg !8 ; <i32> [#uses=1]
call void @llvm.dbg.value(metadata !{i32 %add}, i64 0, metadata !10), !dbg !8
ret i32 %add, !dbg !11
}
declare void @llvm.dbg.declare(metadata, metadata) nounwind readnone
declare void @llvm.dbg.value(metadata, i64, metadata) nounwind readnone
!0 = metadata !{i32 524545, metadata !1, metadata !"x", metadata !2,
i32 1, metadata !6} ; [ DW_TAG_arg_variable ]
!1 = metadata !{i32 524334, i32 0, metadata !2, metadata !"f",
metadata !"f", metadata !"f", metadata !2, i32 2, metadata
!4, i1
false, i1 true, i32 0, i32 0, null, i1 false, i1 false} ; [
DW_TAG_subprogram ]
!2 = metadata !{i32 524329, metadata !"testadd.c", metadata
!"/home/asudarsanam/perforce/llvmtop/setup", metadata !3} ; [
DW_TAG_file_type ]
!3 = metadata !{i32 524305, i32 0, i32 12, metadata !"testadd.c",
metadata !".", metadata !"clang 2.0", i1 true, i1 false,
metadata !"",
i32 0} ; [ DW_TAG_compile_unit ]
!4 = metadata !{i32 524309, metadata !2, metadata !"", metadata !2,
i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !5, i32 0, null} ; [
DW_TAG_subroutine_type ]
!5 = metadata !{metadata !6}
!6 = metadata !{i32 524324, metadata !2, metadata !"long", metadata
!2, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ]
!7 = metadata !{i32 524545, metadata !1, metadata !"y", metadata !2,
i32 1, metadata !6} ; [ DW_TAG_arg_variable ]
!8 = metadata !{i32 5, i32 4, metadata !9, null}
!9 = metadata !{i32 524299, metadata !1, i32 2, i32 1} ; [
DW_TAG_lexical_block ] !10 = metadata !{i32 524544, metadata !9,
metadata !"u", metadata !2, i32 3, metadata !6} ; [
DW_TAG_auto_variable ]
!11 = metadata !{i32 7, i32 1, metadata !9, null}
******************************************************************
Llvm has replaced the name of the local variable (u) by a temporary
name (%add). However, the debug information contains the information
about the original variable name. **** !10 = metadata !{i32 524544,
metadata !9, metadata !"u", metadata !2, i32 3, metadata !6} ; [
DW_TAG_auto_variable ] ******
I have been trying for a while and have been unsuccessful in accessing
this information (mapping between the temporary name (%add) and the
actual name (u)). What I need is this: When I access the llvm value
that points to the temporary name (%add), I need to identify that it
replaces the actual name (u).
Can anyone help me out please?
Thanks
Regards
Arvind
Devang Patel
2010-Jul-15 20:07 UTC
[LLVMdev] Trying to access the user defined variable name
On Wed, Jul 14, 2010 at 3:03 PM, Arvind Sudarsanam <arvind.sudarsanam at aggiemail.usu.edu> wrote:> Dear all, > > I have the following source code: > > long f(long x, long y) > { > long u; > > u = x+y; > return u; > }; > > After clang (with debug option set) and llvm opt using mem2reg, I get > the following .ll file > *************************************************************************** > ; ModuleID = '<stdin>' > target datalayout > "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32" > target triple = "i686-pc-linux-gnu" > > define i32 @f(i32 %x, i32 %y) nounwind { > entry: > call void @llvm.dbg.value(metadata !{i32 %x}, i64 0, metadata !0) > call void @llvm.dbg.value(metadata !{i32 %y}, i64 0, metadata !7) > %add = add nsw i32 %x, %y, !dbg !8 ; <i32> [#uses=1] > call void @llvm.dbg.value(metadata !{i32 %add}, i64 0, metadata !10), !dbg !8 > ret i32 %add, !dbg !11 > } > > declare void @llvm.dbg.declare(metadata, metadata) nounwind readnone > > declare void @llvm.dbg.value(metadata, i64, metadata) nounwind readnone > > !0 = metadata !{i32 524545, metadata !1, metadata !"x", metadata !2, > i32 1, metadata !6} ; [ DW_TAG_arg_variable ] > !1 = metadata !{i32 524334, i32 0, metadata !2, metadata !"f", > metadata !"f", metadata !"f", metadata !2, i32 2, metadata !4, i1 > false, i1 true, i32 0, i32 0, null, i1 false, i1 false} ; [ > DW_TAG_subprogram ] > !2 = metadata !{i32 524329, metadata !"testadd.c", metadata > !"/home/asudarsanam/perforce/llvmtop/setup", metadata !3} ; [ > DW_TAG_file_type ] > !3 = metadata !{i32 524305, i32 0, i32 12, metadata !"testadd.c", > metadata !".", metadata !"clang 2.0", i1 true, i1 false, metadata !"", > i32 0} ; [ DW_TAG_compile_unit ] > !4 = metadata !{i32 524309, metadata !2, metadata !"", metadata !2, > i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !5, i32 0, null} ; [ > DW_TAG_subroutine_type ] > !5 = metadata !{metadata !6} > !6 = metadata !{i32 524324, metadata !2, metadata !"long", metadata > !2, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ] > !7 = metadata !{i32 524545, metadata !1, metadata !"y", metadata !2, > i32 1, metadata !6} ; [ DW_TAG_arg_variable ] > !8 = metadata !{i32 5, i32 4, metadata !9, null} > !9 = metadata !{i32 524299, metadata !1, i32 2, i32 1} ; [ > DW_TAG_lexical_block ] !10 = metadata !{i32 524544, metadata !9, > metadata !"u", metadata !2, i32 3, metadata !6} ; [ > DW_TAG_auto_variable ] > !11 = metadata !{i32 7, i32 1, metadata !9, null} > ****************************************************************** > > Llvm has replaced the name of the local variable (u) by a temporary > name (%add). However, the debug information contains the information > about the original variable name. **** !10 = metadata !{i32 524544, > metadata !9, metadata !"u", metadata !2, i32 3, metadata !6} ; [ > DW_TAG_auto_variable ] ******Variable name 'u' is intentionally preserved in DW_TAG_auto_variable because user debugging this code does not know anything about temp. names used by compiler.> I have been trying for a while and have been unsuccessful in accessing > this information (mapping between the temporary name (%add) and the > actual name (u)).That's what llvm.dbg.value is doing. call void @llvm.dbg.value(metadata !{i32 %add}, i64 0, metadata !10), !dbg !8 First parameter is the compiler generated temp. and 3rd parameter provides info about the variable, including its name 'u'. - Devang
Arvind Sudarsanam
2010-Jul-15 20:19 UTC
[LLVMdev] Trying to access the user defined variable name
Hi Devang,
Thanks for your reply. You mentioned
" First parameter is the compiler generated temp. and 3rd parameter
provides info about the variable, including its name 'u'."
I did manage to get this far. But, I am finding it difficult to access
this info using LLVM APIs. The third parameter is of type "metadata".
So I was able to access it by casting it to MDNode. After that, I am
assuming that it gives me a handle to the metadata represented as
"!10"
!10 = metadata !{i32 524544, metadata !9, metadata !"u", metadata !2,
i32 3, metadata !6} ; [DW_TAG_auto_variable ]
>From here, I am trying invain to access the variable name.
Is there something wrong in my approach? I feel that I have not
understood the method to access metadata in llvm.
Thanks
Regards
Arvind
On Thu, Jul 15, 2010 at 2:07 PM, Devang Patel <devang.patel at gmail.com>
wrote:> On Wed, Jul 14, 2010 at 3:03 PM, Arvind Sudarsanam
> <arvind.sudarsanam at aggiemail.usu.edu> wrote:
>> Dear all,
>>
>> I have the following source code:
>>
>> long f(long x, long y)
>> {
>> long u;
>>
>> u = x+y;
>> return u;
>> };
>>
>> After clang (with debug option set) and llvm opt using mem2reg, I get
>> the following .ll file
>>
***************************************************************************
>> ; ModuleID = '<stdin>'
>> target datalayout >>
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
>> target triple = "i686-pc-linux-gnu"
>>
>> define i32 @f(i32 %x, i32 %y) nounwind {
>> entry:
>> call void @llvm.dbg.value(metadata !{i32 %x}, i64 0, metadata !0)
>> call void @llvm.dbg.value(metadata !{i32 %y}, i64 0, metadata !7)
>> %add = add nsw i32 %x, %y, !dbg !8 ; <i32>
[#uses=1]
>> call void @llvm.dbg.value(metadata !{i32 %add}, i64 0, metadata !10),
!dbg !8
>> ret i32 %add, !dbg !11
>> }
>>
>> declare void @llvm.dbg.declare(metadata, metadata) nounwind readnone
>>
>> declare void @llvm.dbg.value(metadata, i64, metadata) nounwind readnone
>>
>> !0 = metadata !{i32 524545, metadata !1, metadata !"x",
metadata !2,
>> i32 1, metadata !6} ; [ DW_TAG_arg_variable ]
>> !1 = metadata !{i32 524334, i32 0, metadata !2, metadata
!"f",
>> metadata !"f", metadata !"f", metadata !2, i32 2,
metadata !4, i1
>> false, i1 true, i32 0, i32 0, null, i1 false, i1 false} ; [
>> DW_TAG_subprogram ]
>> !2 = metadata !{i32 524329, metadata !"testadd.c", metadata
>> !"/home/asudarsanam/perforce/llvmtop/setup", metadata !3} ; [
>> DW_TAG_file_type ]
>> !3 = metadata !{i32 524305, i32 0, i32 12, metadata
!"testadd.c",
>> metadata !".", metadata !"clang 2.0", i1 true, i1
false, metadata !"",
>> i32 0} ; [ DW_TAG_compile_unit ]
>> !4 = metadata !{i32 524309, metadata !2, metadata !"",
metadata !2,
>> i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !5, i32 0, null} ; [
>> DW_TAG_subroutine_type ]
>> !5 = metadata !{metadata !6}
>> !6 = metadata !{i32 524324, metadata !2, metadata !"long",
metadata
>> !2, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ]
>> !7 = metadata !{i32 524545, metadata !1, metadata !"y",
metadata !2,
>> i32 1, metadata !6} ; [ DW_TAG_arg_variable ]
>> !8 = metadata !{i32 5, i32 4, metadata !9, null}
>> !9 = metadata !{i32 524299, metadata !1, i32 2, i32 1} ; [
>> DW_TAG_lexical_block ] !10 = metadata !{i32 524544, metadata !9,
>> metadata !"u", metadata !2, i32 3, metadata !6} ; [
>> DW_TAG_auto_variable ]
>> !11 = metadata !{i32 7, i32 1, metadata !9, null}
>> ******************************************************************
>>
>> Llvm has replaced the name of the local variable (u) by a temporary
>> name (%add). However, the debug information contains the information
>> about the original variable name. **** !10 = metadata !{i32 524544,
>> metadata !9, metadata !"u", metadata !2, i32 3, metadata !6}
; [
>> DW_TAG_auto_variable ] ******
>
> Variable name 'u' is intentionally preserved in
DW_TAG_auto_variable
> because user debugging this code does not know anything about temp.
> names used by compiler.
>
>> I have been trying for a while and have been unsuccessful in accessing
>> this information (mapping between the temporary name (%add) and the
>> actual name (u)).
>
> That's what llvm.dbg.value is doing.
>
> call void @llvm.dbg.value(metadata !{i32 %add}, i64 0, metadata !10), !dbg
!8
>
> First parameter is the compiler generated temp. and 3rd parameter
> provides info about the variable, including its name 'u'.
>
> -
> Devang
>
--
Arvind Sudarsanam
Utah State University
Phone: (435) 512-7769
E-mail: theonemorpheus at gmail.com
Web-site: http://cc.usu.edu/~asudarsanam