Skip to content

Commit

Permalink
Support btf_tag annotations
Browse files Browse the repository at this point in the history
Fixes #188. Fixes #201.
  • Loading branch information
RyanGlScott committed Apr 10, 2023
1 parent 22c4fdc commit 2203ae1
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 7 deletions.
10 changes: 10 additions & 0 deletions disasm-test/tests/btf-tag-dicompositetype.at-least-llvm14.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
;; https://github.com/llvm/llvm-project/blob/0b32dca12ef4d82af71f86a70c49806e5b81ead2/llvm/test/Bitcode/attr-btf_tag-dicomposite.ll

!3 = !DIFile(filename: "struct.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t", file: !3, line: 1, size: 32, elements: !7, annotations: !10)
!7 = !{!8}
!8 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !6, file: !3, line: 1, baseType: !9, size: 32)
!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!10 = !{!11, !12}
!11 = !{!"btf_tag", !"a"}
!12 = !{!"btf_tag", !"b"}
10 changes: 10 additions & 0 deletions disasm-test/tests/btf-tag-diderivedtype.at-least-llvm14.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
;; Adapted from https://github.com/llvm/llvm-project/blob/430e22388173c96da6777fccb9735a6e8ee3ea1c/llvm/test/Bitcode/attr-btf_tag-field.ll

!1 = !DIFile(filename: "attr-btf_tag-field.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t1", file: !1, line: 7, size: 32, elements: !15)
!15 = !{!16}
!16 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !14, file: !1, line: 8, baseType: !12, size: 32, annotations: !17)
!17 = !{!18, !19}
!18 = !{!"btf_tag", !"tag1"}
!19 = !{!"btf_tag", !"tag2"}
15 changes: 15 additions & 0 deletions disasm-test/tests/btf-tag-diglobalvariable.at-least-llvm14.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; Adapted from https://github.com/llvm/llvm-project/blob/30c288489ae51a3e0819241f367eeae6df2b09e7/llvm/test/Bitcode/attr-btf_tag-diglobalvariable.ll

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "g1", scope: !2, file: !3, line: 7, type: !6, isLocal: false, isDefinition: true, annotations: !10)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 47af5574a87dc298b5c6c36ff6a969c8c77c8499)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "t.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
!4 = !{}
!5 = !{!0}
!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t1", file: !3, line: 4, size: 32, elements: !7)
!7 = !{!8}
!8 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !6, file: !3, line: 5, baseType: !9, size: 32)
!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!10 = !{!11, !12}
!11 = !{!"btf_tag", !"tag1"}
!12 = !{!"btf_tag", !"tag2"}
14 changes: 14 additions & 0 deletions disasm-test/tests/btf-tag-dilocalvariable.at-least-llvm14.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;; Adapted from https://github.com/llvm/llvm-project/blob/1bebc31c617d1a0773f1d561f02dd17c5e83b23b/llvm/test/Bitcode/attr-btf_tag-parameter.ll

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git c9e3139e00bcef23b236a02890b909a130d1b3d9)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "func.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
!2 = !{}
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
!9 = !DISubroutineType(types: !10)
!10 = !{!11, !11}
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!12 = !{!13}
!13 = !DILocalVariable(name: "a", arg: 1, scope: !8, file: !1, line: 1, type: !11, annotations: !14)
!14 = !{!15, !16}
!15 = !{!"btf_tag", !"a"}
!16 = !{!"btf_tag", !"b"}
15 changes: 15 additions & 0 deletions disasm-test/tests/btf-tag-disubprogram.at-least-llvm14.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; Adapted from https://github.com/llvm/llvm-project/blob/d383df32c0d5bcdc8c160ecdd7174399aa3c5395/llvm/test/Bitcode/attr-btf_tag-disubprogram.ll

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git a6dd9d402a04d53403664bbb47771f2573c7ade0)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "func.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
!2 = !{}
!7 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git a6dd9d402a04d53403664bbb47771f2573c7ade0)"}
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12, annotations: !14)
!9 = !DISubroutineType(types: !10)
!10 = !{!11, !11}
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!12 = !{!13}
!13 = !DILocalVariable(name: "a", arg: 1, scope: !8, file: !1, line: 1, type: !11)
!14 = !{!15, !16}
!15 = !{!"btf_tag", !"a"}
!16 = !{!"btf_tag", !"b"}
2 changes: 1 addition & 1 deletion llvm-pretty
29 changes: 23 additions & 6 deletions src/Data/LLVM/BitCode/IR/Metadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
(addDebugInfo isDistinct (DebugInfoFile diFile)) pm

17 -> label "METADATA_DERIVED_TYPE" $ do
assertRecordSizeBetween 12 13
assertRecordSizeBetween 12 14
ctx <- getContext
isDistinct <- parseField r 0 nonzero
didt <- DIDerivedType
Expand All @@ -610,11 +610,14 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
<*> (if length (recordFields r) <= 12
then pure Nothing
else Just <$> parseField r 12 numeric) -- didtDwarfAddressSpace
<*> (if length (recordFields r) <= 13
then pure Nothing
else mdForwardRefOrNull ctx mt <$> parseField r 13 numeric) -- didtAnnotations
return $! updateMetadataTable
(addDebugInfo isDistinct (DebugInfoDerivedType didt)) pm

18 -> label "METADATA_COMPOSITE_TYPE" $ do
assertRecordSizeBetween 16 21
assertRecordSizeBetween 16 22
ctx <- getContext
isDistinct <- parseField r 0 nonzero
dict <- DICompositeType
Expand Down Expand Up @@ -648,6 +651,9 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
<*> (if length (recordFields r) <= 20
then pure Nothing
else mdForwardRefOrNull ctx mt <$> parseField r 20 numeric) -- dictRank
<*> (if length (recordFields r) <= 21
then pure Nothing
else mdForwardRefOrNull ctx mt <$> parseField r 21 numeric) -- dictAnnotations
return $! updateMetadataTable
(addDebugInfo isDistinct (DebugInfoCompositeType dict)) pm

Expand Down Expand Up @@ -790,6 +796,10 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
| not hasSPFlags = recordSize >= 21
| otherwise = True

hasAnnotations
| not hasSPFlags = False
| otherwise = recordSize >= 19

-- Some additional sanity checking
when (not hasSPFlags && hasUnit)
(assertRecordSizeBetween 19 21)
Expand Down Expand Up @@ -828,6 +838,7 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
<*> (mdForwardRefOrNull ctx mt <$> parseField r (14 + offsetB) numeric) -- dispDeclaration
<*> (mdForwardRefOrNull ctx mt <$> parseField r (15 + offsetB) numeric) -- dispVariables
<*> (optFwdRef hasThrownTypes (17 + offsetB)) -- dispThrownTypes
<*> (optFwdRef hasAnnotations (18 + offsetB)) -- dispAnnotations

-- TODO: in the LLVM parser, it then goes into the metadata table
-- and updates function entries to point to subprograms. Is that
Expand Down Expand Up @@ -916,7 +927,7 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
(addDebugInfo isDistinct (DebugInfoTemplateValueParameter ditvp)) pm

27 -> label "METADATA_GLOBAL_VAR" $ do
assertRecordSizeIn [11, 12]
assertRecordSizeBetween 11 13
ctx <- getContext
field0 <- parseField r 0 numeric
let isDistinct = testBit field0 0
Expand All @@ -933,9 +944,12 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
<*> parseField r 8 nonzero -- digvIsDefinition
<*> (mdForwardRefOrNull ctx mt <$> parseField r 9 numeric) -- digvVariable
<*> (mdForwardRefOrNull ctx mt <$> parseField r 10 numeric) -- digvDeclaration
<*> if length (recordFields r) > 11
then Just <$> parseField r 11 numeric -- digvAlignment
else pure Nothing
<*> (if length (recordFields r) > 11
then Just <$> parseField r 11 numeric -- digvAlignment
else pure Nothing)
<*> (if length (recordFields r) > 12
then mdForwardRefOrNull ctx mt <$> parseField r 12 numeric -- digvAnnotations
else pure Nothing)
return $! updateMetadataTable
(addDebugInfo isDistinct (DebugInfoGlobalVariable digv)) pm

Expand Down Expand Up @@ -975,6 +989,9 @@ parseMetadataEntry vt mt pm (fromEntry -> Just r) =
<*> parseField r (adj 6) numeric -- dilvArg
<*> parseField r (adj 7) numeric -- dilvFlags
<*> pure alignInBits -- dilvAlignment
<*> (if hasAlignment && length (recordFields r) > 9
then mdForwardRefOrNull ctx mt <$> parseField r 9 numeric -- dilvAnnotations
else pure Nothing)
return $! updateMetadataTable
(addDebugInfo isDistinct (DebugInfoLocalVariable dilv)) pm

Expand Down

0 comments on commit 2203ae1

Please sign in to comment.