diff --git a/bolt/include/bolt/Rewrite/MetadataManager.h b/bolt/include/bolt/Rewrite/MetadataManager.h --- a/bolt/include/bolt/Rewrite/MetadataManager.h +++ b/bolt/include/bolt/Rewrite/MetadataManager.h @@ -33,6 +33,9 @@ /// CFG is not yet built. void runInitializersPreCFG(); + /// Execute metadata initializers after CFG was constructed for functions. + void runInitializersPostCFG(); + /// Run finalization step of rewriters after code has been emitted. void runFinalizersAfterEmit(); }; diff --git a/bolt/include/bolt/Rewrite/MetadataRewriter.h b/bolt/include/bolt/Rewrite/MetadataRewriter.h --- a/bolt/include/bolt/Rewrite/MetadataRewriter.h +++ b/bolt/include/bolt/Rewrite/MetadataRewriter.h @@ -49,6 +49,9 @@ /// contents of the section. Functions are in pre-cfg state. virtual Error preCFGInitializer() { return Error::success(); } + /// Run the rewriter once the functions are in CFG state. + virtual Error postCFGInitializer() { return Error::success(); } + /// Finalize section contents based on the new context after the new code is /// emitted. virtual Error postEmitFinalizer() { return Error::success(); } diff --git a/bolt/include/bolt/Rewrite/RewriteInstance.h b/bolt/include/bolt/Rewrite/RewriteInstance.h --- a/bolt/include/bolt/Rewrite/RewriteInstance.h +++ b/bolt/include/bolt/Rewrite/RewriteInstance.h @@ -171,6 +171,9 @@ /// Process metadata in special sections before CFG is built for functions. void processMetadataPreCFG(); + /// Process metadata in special sections after CFG is built for functions. + void processMetadataPostCFG(); + /// Update debug and other auxiliary information in the file. void updateMetadata(); diff --git a/bolt/lib/Rewrite/MetadataManager.cpp b/bolt/lib/Rewrite/MetadataManager.cpp --- a/bolt/lib/Rewrite/MetadataManager.cpp +++ b/bolt/lib/Rewrite/MetadataManager.cpp @@ -32,6 +32,18 @@ } } +void MetadataManager::runInitializersPostCFG() { + for (auto &Rewriter : Rewriters) { + LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking " << Rewriter->getName() + << " after CFG construction\n"); + if (Error E = Rewriter->postCFGInitializer()) { + errs() << "BOLT-ERROR: while running " << Rewriter->getName() + << " in CFG state: " << toString(std::move(E)) << '\n'; + exit(1); + } + } +} + void MetadataManager::runFinalizersAfterEmit() { for (auto &Rewriter : Rewriters) { LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking " << Rewriter->getName() diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -722,6 +722,8 @@ postProcessFunctions(); + processMetadataPostCFG(); + if (opts::DiffOnly) return Error::success(); @@ -2920,6 +2922,10 @@ processProfileDataPreCFG(); } +void RewriteInstance::processMetadataPostCFG() { + MetadataManager.runInitializersPostCFG(); +} + void RewriteInstance::processProfileDataPreCFG() { if (!ProfileReader) return;