[PATCH] D143967: [DebugInfo][BPF] Add 'btf:type_tag' annotation in DWARF

Eduard Zingerman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 13 15:53:30 PDT 2023


eddyz87 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
eddyz87 updated this revision to Diff 497835.
eddyz87 added a comment.
eddyz87 retitled this revision from "[DebugInfo][BPF] 'annotations' on a target type to represent btf_type_tag" to "[DebugInfo][BPF] Add 'btf_type_tag:v2' annotation in DWARF".
eddyz87 edited the summary of this revision.
eddyz87 updated this revision to Diff 499281.
eddyz87 edited the summary of this revision.
eddyz87 updated this revision to Diff 502033.
eddyz87 edited the summary of this revision.
eddyz87 retitled this revision from "[DebugInfo][BPF] Add 'btf_type_tag:v2' annotation in DWARF" to "[DebugInfo][BPF] Add 'btf:type_tag' annotation in DWARF".
eddyz87 updated this revision to Diff 502321.
eddyz87 updated this revision to Diff 503198.
eddyz87 updated this revision to Diff 504493.
eddyz87 published this revision for review.
eddyz87 added reviewers: yonghong-song, echristo, dblaikie.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

- Removed DWARF tag 0x6001, use 'btf_type_tag:v2' name instead.
- Removed CopyBTFTypeTags pass, generate 'btf_type_tag' and 'btf_type_tag:v2' in `CGDebugInfo` instead.
- Added tests for BTF deduplication logic for debug info types that differ only in annotations.


yonghong-song added a comment.

When I tried to build llvm with the patch, I hit the following errors:

  [2198/3751] Building CXX object lib/Target/BPF/CMakeFiles/LLVMBPFCodeGen.dir/BTFDebug.cpp.o
  FAILED: lib/Target/BPF/CMakeFiles/LLVMBPFCodeGen.dir/BTFDebug.cpp.o 
  /bin/c++ -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GNU_SOURCE -D_LIBCPP_ENABLE_ASSERTIONS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Ilib/Target/BPF -I../lib/Target/BPF -Iinclude -I../include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wno-comment -Wno-misleading-indentation -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fvisibility=hidden  -fno-exceptions -fno-rtti -UNDEBUG -std=c++17 -MD -MT lib/Target/BPF/CMakeFiles/LLVMBPFCodeGen.dir/BTFDebug.cpp.o -MF lib/Target/BPF/CMakeFiles/LLVMBPFCodeGen.dir/BTFDebug.cpp.o.d -o lib/Target/BPF/CMakeFiles/LLVMBPFCodeGen.dir/BTFDebug.cpp.o -c ../lib/Target/BPF/BTFDebug.cpp
  ../lib/Target/BPF/BTFDebug.cpp: In function ‘llvm::DINodeArray lookupAnnotations(const llvm::DIType*)’:
  ../lib/Target/BPF/BTFDebug.cpp:516:21: error: ‘const class llvm::DIBasicType’ has no member named ‘getAnnotations’; did you mean ‘getEncoding’?
       Annots = SubTy->getAnnotations();
                       ^~~~~~~~~~~~~~
                       getEncoding
  ../lib/Target/BPF/BTFDebug.cpp:522:21: error: ‘const class llvm::DISubroutineType’ has no member named ‘getAnnotations’
       Annots = SubTy->getAnnotations();
                       ^~~~~~~~~~~~~~
  [2272/3751] Building CXX object lib/Passes/CMakeFiles/LLVMPasses.dir/PassBuilderPipelines.cpp.o
  In file included from ../lib/Passes/PassBuilderPipelines.cpp:113:
  ../include/llvm/Transforms/Scalar/SROA.h:95:7: warning: ‘llvm::SROAPass’ declared with greater visibility than the type of its field ‘llvm::SROAPass::SelectsToRewrite’ [-Wattributes]
   class SROAPass : public PassInfoMixin<SROAPass> {
         ^~~~~~~~
  [2279/3751] Building CXX object lib/Passes/CMakeFiles/LLVMPasses.dir/PassBuilder.cpp.o
  In file included from ../lib/Passes/PassBuilder.cpp:214:
  ../include/llvm/Transforms/Scalar/SROA.h:95:7: warning: ‘llvm::SROAPass’ declared with greater visibility than the type of its field ‘llvm::SROAPass::SelectsToRewrite’ [-Wattributes]
   class SROAPass : public PassInfoMixin<SROAPass> {
         ^~~~~~~~
  ninja: build stopped: subcommand failed.
  [yhs at devbig309.ftw3 ~/work/llvm-project/llvm/build (main)]$

The top commit is

  959216f9b1f1  [AMDGPU] MIR-Tests for Multiplication using KBA

Could you take a look?

My cmake command line:

  cmake .. -DCMAKE_BUILD_TYPE=Release -G Ninja \
      -DLLVM_ENABLE_PROJECTS="clang;lld" \
      -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
      -DLLVM_ENABLE_ASSERTIONS=ON \
      -DLLVM_ENABLE_ZLIB=ON \
      -DCMAKE_INSTALL_PREFIX=$PWD/install


eddyz87 added a comment.

> ../lib/Target/BPF/BTFDebug.cpp: In function ‘llvm::DINodeArray lookupAnnotations(const llvm::DIType*)’:
> ../lib/Target/BPF/BTFDebug.cpp:516:21: error: ‘const class llvm::DIBasicType’ has no member named ‘getAnnotations’; did you mean ‘getEncoding’?
>
>   Annots = SubTy->getAnnotations();
>                   ^~~~~~~~~~~~~~
>                   getEncoding
>
> ../lib/Target/BPF/BTFDebug.cpp:522:21: error: ‘const class llvm::DISubroutineType’ has no member named ‘getAnnotations’
>
>   Annots = SubTy->getAnnotations();
>                   ^~~~~~~~~~~~~~

Hi Yonghong,

Looking at the error it seems that you applied only the changes from D143967 <https://reviews.llvm.org/D143967>, however it depends on D143966 <https://reviews.llvm.org/D143966>. I organized these two as a stack to simplify the review:

- D143966 <https://reviews.llvm.org/D143966> adds annotations attribute for `DIBasicType` and `DISubroutineType`;
- D143967 <https://reviews.llvm.org/D143967> adds logic for `btf_type_tag:v2` generation.

Anyways, I've rebased the revisions against current main (dad3741e3bdc <https://reviews.llvm.org/rGdad3741e3bdc6e126c5b3adec9242d7b2d926282> [PowerPC] clang-format isPPC64ELFv2ABI() function. NFC) and see one failing test for `ninja check-all` target:

  Failed Tests (1):
    Clang :: CodeGen/dwarf-version.c

However, it fails without my changes as well.
I'll push the rebased revisions shortly.


eddyz87 added a comment.

Rebase


eddyz87 added a comment.

Rebase, change for tag name from btf_type_tag:v2 to btf:type_tag.


eddyz87 added a comment.

Another rebase, hope to fix windows build issue.


eddyz87 added a comment.

Fix support for cyclic types

Update to handle situations like below:

  struct bravo {
    struct bravo __tag(a) * a;
  } g;

During DI construction annotations are now represneted by
"annotation placeholders", special kind of derived type.
These placeholders are replaced by actual types with
proper "annotations" field in CGDebugInfo::finalize().


eddyz87 added a comment.

- BTFDebug changes moved to a separate revision (to simplify the review);
- Added test case for circular type reference handling.


This commit is a follow-up for discussion at Kernel BPF mailing list <https://lore.kernel.org/bpf/87r0w9jjoq.fsf@oracle.com/>, it changes the way `__attribute__((btf_type_tag("...")))`s are represented in DWARF.

Prior to this commit type tags could only be attached to pointers. Such attachments associated the tags with a pointee type. E.g. for the following C code:

  int __attribute__((btf_type_tag("tag1"))) *g;

Generated DWARF looked as follows:

  0x0000001e:   DW_TAG_variable
                  DW_AT_name      ("g")
                  DW_AT_type      (0x00000029 "int *")
  
  0x00000029:   DW_TAG_pointer_type
                  DW_AT_type      (0x00000032 "int")
  
  0x0000002e:     DW_TAG_LLVM_annotation
                    DW_AT_name    ("btf_type_tag")
                    DW_AT_const_value     ("tag1")
  
  0x00000032:   DW_TAG_base_type
                  DW_AT_name      ("int")

The goal of this commit is to allow attachment of type tags to the tagged types instead. E.g. for the same example DWARF should look as follows:

  0x0000001e:   DW_TAG_variable
                  DW_AT_name      ("g")
                  DW_AT_type      (0x00000029 "int *")
  
  0x00000029:   DW_TAG_pointer_type
                  DW_AT_type      (0x00000032 "int")
  
  0x00000032:   DW_TAG_base_type
                  DW_AT_name      ("int")
  
  0x00000036:     DW_TAG_LLVM_annotation
                    DW_AT_name    ("btf:type_tag")
                    DW_AT_const_value     ("tag1")

A new tag name, `btf:type_tag`, is used so that DWARF consumers could distinguish between old and new attachment semantics.

In order to preserve backwards compatibility both `btf_type_tag` and `btf:type_tag` are generated. `btf_type_tag` might be deprecated in the future.

The commit includes the following changes:

- New overload for `CGDebugInfo::CreateType` method is added. This overload is responsible for creation of `DIBasicType`, `DICompositeType`, `DIDerivedType`, `DISubroutineType` entries with `annotations` fields describing attached BTF type tags. Generated annotations use `btf:type_tag` field name.

  llvm::DIType *CGDebugInfo::CreateType(const BTFTagAttributedType *Ty,
                                        llvm::DIFile *Unit);



- `ASTContext::getBTFTagAttributedType()` is updated to ensure that `BTFTagAttributedType` always wraps `QualType` w/o local constant/volatile/restricted qualifiers. This simplifies debug info generation.

- `BTFDebug` is updated to handle BTF type tags attached to non-pointer types:
  - The logic for `BTFTypeTypeTag` chains creation is moved to `BTFDebug::addType()`;
  - Special logic is added to avoid duplicate BTF entries for tagged and un-tagged type variants. Debug information generated for this example contains three instances of `DICompositeType(name: "foo")` with different `annotations` fields, however single BTF definition for structure "foo" should be generated. Field `BTFDebug::DIDedupMap` and method `BTFDebug::lookupType()` are responsible for this logic. E.g. this is useful in the following case:

  #define __tag1 __attribute__((btf_type_tag("tag1")))
  #define __tag2 __attribute__((btf_type_tag("tag2")))
  
  struct foo {};
  
  struct bar {
    struct foo __tag1 aa;
    struct foo __tag2 bb;
    struct foo cc;
  };


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143967

Files:
  clang/lib/AST/ASTContext.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/test/CodeGen/attr-btf_type_tag-circular.c
  clang/test/CodeGen/attr-btf_type_tag-func-ptr.c
  clang/test/CodeGen/attr-btf_type_tag-func.c
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c
  clang/test/CodeGen/attr-btf_type_tag-typedef-field.c
  clang/test/CodeGen/attr-btf_type_tag-var.c
  clang/test/CodeGen/attr-btf_type_tag-void.c
  llvm/include/llvm/IR/DIBuilder.h
  llvm/lib/IR/DIBuilder.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143967.504493.patch
Type: text/x-patch
Size: 27686 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230313/70e42cf2/attachment-0001.bin>


More information about the cfe-commits mailing list