This is an archive of the discontinued LLVM Phabricator instance.

[BOLT] Introduce MetadataRewriter interface
ClosedPublic

Authored by maksfb on Jun 28 2023, 5:04 PM.

Details

Summary

Introduce the MetadataRewriter interface to handle updates for various
types of auxiliary data stored in a binary file.

To implement metadata processing using this new interface, all metadata
rewriters should derive from the RewriterBase class and implement
one or more of the following methods, depending on the timing of metadata
read and write operations:

  • preCFGInitializer()
  • postCFGInitializer() // TBD
  • preEmitFinalizer() // TBD
  • postEmitFinalizer()

By adopting this approach, we aim to simplify the RewriteInstance class
and improve its scalability to accommodate new extensions of file formats,
including various metadata types of the Linux Kernel.

Diff Detail

Event Timeline

maksfb created this revision.Jun 28 2023, 5:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2023, 5:04 PM
maksfb requested review of this revision.Jun 28 2023, 5:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2023, 5:04 PM
Amir accepted this revision.Jun 28 2023, 5:38 PM

Looks good and nicely factored.

bolt/include/bolt/Rewrite/RewriteManager.h
48 ↗(On Diff #535567)

Why a global instance and not a RewriteInstance member variable?

This revision is now accepted and ready to land.Jun 28 2023, 5:38 PM
jobnoorman added inline comments.Jun 29 2023, 12:13 AM
bolt/include/bolt/Rewrite/RewriteManager.h
48 ↗(On Diff #535567)

I would also recommend to not make this a singleton. It is (in theory) possible to have multiple RewriteInstance objects but having a global RewriteManager would make this (more) difficult.

bolt/include/bolt/Rewrite/RewriterBase.h
23 ↗(On Diff #535567)

Is this only supposed to be used for rewriting metadata? If so, maybe this needs a more specific name?

For example, at some point I was considering to rewrite instructions pre-CFG to support RISC-V linker relaxation. Would this interface have been suitable for that? (Let's ignore for now whether that's actually a good idea to do :))

Amir added inline comments.Jun 29 2023, 5:50 AM
bolt/include/bolt/Rewrite/RewriteManager.h
48 ↗(On Diff #535567)

Yes, in fact, llvm-boltdiff constructs two RI objects.

maksfb updated this revision to Diff 536014.Jun 29 2023, 3:00 PM

Get rid of the singleton and some cleanup.

maksfb marked 3 inline comments as done.Jun 29 2023, 3:01 PM
maksfb added inline comments.Jun 29 2023, 8:55 PM
bolt/include/bolt/Rewrite/RewriterBase.h
23 ↗(On Diff #535567)

How about MetadataRewriterBase? I'm thinking the instances/implementations should be generic enough, but not modify the instructions directly. I.e. they can annotate instructions and introduce pseudo instructions to the stream, but otherwise keep the disassembly intact.

The pre-CFG modification you are talking about is better suited for a BinaryPass that modifies BinaryFunction code. We just have to teach the BinaryPassManager to operate on pre-CFG state.

jobnoorman added inline comments.Jun 30 2023, 5:02 AM
bolt/include/bolt/Rewrite/RewriterBase.h
23 ↗(On Diff #535567)

How about MetadataRewriterBase?

I'd say that's better. It would make me think twice about using it to rewrite instructions :)

maksfb updated this revision to Diff 537599.Jul 5 2023, 11:59 PM
maksfb retitled this revision from [BOLT] Introduce Rewriter interface to [BOLT] Introduce MetadataRewriter interface.
maksfb edited the summary of this revision. (Show Details)

Rename the new interface to MetadataRewriter.

jobnoorman accepted this revision.Jul 6 2023, 12:30 AM
This revision was automatically updated to reflect the committed changes.