Skip to content

[BPF] Treat class records as structs in BTF and CO-RE - #226723

Closed
maxskokov wants to merge 1 commit into
llvm:mainfrom
maxskokov:bpf-btf-class-type
Closed

maxskokov wants to merge 1 commit into
llvm:mainfrom
maxskokov:bpf-btf-class-type

Conversation

@maxskokov

Copy link
Copy Markdown

A C++ record declared with the class keyword has DW_TAG_class_type in the debug info, and the BPF backend only handles DW_TAG_structure_type:

class C { public: int x; int y; };
C c;
unsigned long f(C *p) { return (unsigned long)__builtin_preserve_access_index(&p->y); }
  • BTF: since [BPF] Support for DW_TAG_variant_part in BTF generation  #155783 (LLVM 22.1), visitCompositeType() hits llvm_unreachable() on a class, so compiling C++ for BPF with -g crashes as soon as a class is used. Before, the class was emitted as type 0 (clang 19: Holder.c gets type_id=0).
  • CO-RE: the field offset is only computed for DW_TAG_structure_type, so &p->y above is relocated as offset 0 instead of 4 (@"llvm.C:0:0$0:1" with clang 19 and main).

This PR handles DW_TAG_class_type wherever DW_TAG_structure_type is handled, through one helper, so a class becomes a BTF struct (not a union) and is relocated like one. On its own it fixes classes whose elements are all data members; a class with methods, base classes or static data members also needs #226717, so neither PR makes C++ classes work without the other.

Testing: new tests check that a class and a declared-only class become STRUCT and FWD in BTF, that a map definition declared as a class gets the type its value points to in full rather than as FWD, as for a struct, and that a CO-RE access and the field info byte offset of a bit-field in a class are right; both fail on main. The kernel BPF selftests (999 C programs) compile to byte-identical code, .BTF and .BTF.ext. All BPF lit tests pass.

This is the other half of #226717: that PR handles base classes, static data members and methods in C++ records, this one records declared with class. They touch the same lines, so whichever lands second needs a rebase, and two checks from #226717 then need isStructTag() as well: getBTFRecordElements() in BPFCORE.h, and the struct/union check before the field index mapping in computeBaseAndAccessKey().

This fixes a crash that is a regression since 22.1 (#155783); once this lands I'd like to request a backport to 23.x together with #226717.

Assisted-by: Claude Code (Claude Opus 5.5)

A C++ record declared with the class keyword has DW_TAG_class_type in
the debug info. Since llvm#155783, visitCompositeType() hits
llvm_unreachable() on it; before, the class type was emitted as type 0.
The CO-RE pass only computes the field offset for DW_TAG_structure_type,
so an access into a class got the offset 0: for "class C { public: int
x, y; }", &c->y was relocated as offset 0 instead of 4.

Handle DW_TAG_class_type wherever DW_TAG_structure_type is handled, so
that a class becomes a BTF struct (not a union) and is relocated like
one. A class with methods, base classes or static data members also
needs llvm#226717.

Assisted-by: Claude Code (Claude Opus 5.5)
@github-actions

Copy link
Copy Markdown

Hello @maxskokov 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@maxskokov

Copy link
Copy Markdown
Author

I've read the policies, including the AI Tool Use Policy. AI tool use is noted in the description with an Assisted-by line, and I've reviewed and tested the change myself.

@maxskokov

Copy link
Copy Markdown
Author

cc @tamird @yonghong-song @eddyz87

@maxskokov

Copy link
Copy Markdown
Author

ping

@maxskokov

Copy link
Copy Markdown
Author

Superseded by #233715.

@maxskokov maxskokov closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant