Skip to content

Commit aa1968c

Browse files
authored
[BOLT] Add metadata pre-emit finalization interface (#79925)
Some metadata needs to be updated/finalized before the binary context is emitted into the binary. Add the interface and use it for Linux ORC update invocation.
1 parent 6b33047 commit aa1968c

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

bolt/include/bolt/Rewrite/MetadataManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class MetadataManager {
3636
/// Execute metadata initializers after CFG was constructed for functions.
3737
void runInitializersPostCFG();
3838

39+
/// Run finalization step of rewriters before the binary is emitted.
40+
void runFinalizersPreEmit();
41+
3942
/// Run finalization step of rewriters after code has been emitted.
4043
void runFinalizersAfterEmit();
4144
};

bolt/include/bolt/Rewrite/MetadataRewriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class MetadataRewriter {
5252
/// Run the rewriter once the functions are in CFG state.
5353
virtual Error postCFGInitializer() { return Error::success(); }
5454

55+
/// Run the pass before the binary is emitted.
56+
virtual Error preEmitFinalizer() { return Error::success(); }
57+
5558
/// Finalize section contents based on the new context after the new code is
5659
/// emitted.
5760
virtual Error postEmitFinalizer() { return Error::success(); }

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ class RewriteInstance {
183183
/// Process metadata in special sections after CFG is built for functions.
184184
void processMetadataPostCFG();
185185

186+
/// Make changes to metadata before the binary is emitted.
187+
void finalizeMetadataPreEmit();
188+
186189
/// Update debug and other auxiliary information in the file.
187190
void updateMetadata();
188191

bolt/lib/Rewrite/LinuxKernelRewriter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,18 @@ class LinuxKernelRewriter final : public MetadataRewriter {
163163
return Error::success();
164164
}
165165

166-
Error postEmitFinalizer() override {
167-
updateLKMarkers();
168-
166+
Error preEmitFinalizer() override {
169167
if (Error E = rewriteORCTables())
170168
return E;
171169

172170
return Error::success();
173171
}
172+
173+
Error postEmitFinalizer() override {
174+
updateLKMarkers();
175+
176+
return Error::success();
177+
}
174178
};
175179

176180
Error LinuxKernelRewriter::markInstructions() {

bolt/lib/Rewrite/MetadataManager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ void MetadataManager::runInitializersPostCFG() {
4444
}
4545
}
4646

47+
void MetadataManager::runFinalizersPreEmit() {
48+
for (auto &Rewriter : Rewriters) {
49+
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking " << Rewriter->getName()
50+
<< " before emitting binary context\n");
51+
if (Error E = Rewriter->preEmitFinalizer()) {
52+
errs() << "BOLT-ERROR: while running " << Rewriter->getName()
53+
<< " before emit: " << toString(std::move(E)) << '\n';
54+
exit(1);
55+
}
56+
}
57+
}
58+
4759
void MetadataManager::runFinalizersAfterEmit() {
4860
for (auto &Rewriter : Rewriters) {
4961
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invoking " << Rewriter->getName()

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ Error RewriteInstance::run() {
742742

743743
runOptimizationPasses();
744744

745+
finalizeMetadataPreEmit();
746+
745747
emitAndLink();
746748

747749
updateMetadata();
@@ -3416,6 +3418,10 @@ void RewriteInstance::emitAndLink() {
34163418
}
34173419
}
34183420

3421+
void RewriteInstance::finalizeMetadataPreEmit() {
3422+
MetadataManager.runFinalizersPreEmit();
3423+
}
3424+
34193425
void RewriteInstance::updateMetadata() {
34203426
MetadataManager.runFinalizersAfterEmit();
34213427

0 commit comments

Comments
 (0)