From 6bbd45dec7f3aef58accb935c685e802d6972dec Mon Sep 17 00:00:00 2001 From: Paschalis Mpeis Date: Fri, 21 Mar 2025 15:55:09 +0000 Subject: [PATCH] [NFC][BOLT] Refactor ForcePatch option (#127812) Move force-patch flag to CommandLineOpts and add details on PatchEntries. --- bolt/include/bolt/Utils/CommandLineOpts.h | 1 + bolt/lib/Passes/PatchEntries.cpp | 21 +++++++++++---------- bolt/lib/Rewrite/MachORewriteInstance.cpp | 6 +++--- bolt/lib/Utils/CommandLineOpts.cpp | 6 ++++++ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bolt/include/bolt/Utils/CommandLineOpts.h b/bolt/include/bolt/Utils/CommandLineOpts.h index fefd7969ef6f..19f8c6b2646d 100644 --- a/bolt/include/bolt/Utils/CommandLineOpts.h +++ b/bolt/include/bolt/Utils/CommandLineOpts.h @@ -37,6 +37,7 @@ extern llvm::cl::opt BucketsPerLine; extern llvm::cl::opt DiffOnly; extern llvm::cl::opt EnableBAT; extern llvm::cl::opt EqualizeBBCounts; +extern llvm::cl::opt ForcePatch; extern llvm::cl::opt RemoveSymtab; extern llvm::cl::opt ExecutionCountThreshold; extern llvm::cl::opt HeatmapBlock; diff --git a/bolt/lib/Passes/PatchEntries.cpp b/bolt/lib/Passes/PatchEntries.cpp index 4ce9c09b311d..8a2f0a39a56c 100644 --- a/bolt/lib/Passes/PatchEntries.cpp +++ b/bolt/lib/Passes/PatchEntries.cpp @@ -6,27 +6,28 @@ // //===----------------------------------------------------------------------===// // -// This file implements the PatchEntries class that is used for patching -// the original function entry points. +// This file implements the PatchEntries class that is used for patching the +// original function entry points. This ensures that only the new/optimized code +// executes and that the old code is never used. This is necessary due to +// current BOLT limitations of not being able to duplicate all function's +// associated metadata (e.g., .eh_frame, exception ranges, debug info, +// jump-tables). +// +// NOTE: A successful run of 'scanExternalRefs' can relax this requirement as +// it also ensures that old code is never executed. // //===----------------------------------------------------------------------===// #include "bolt/Passes/PatchEntries.h" +#include "bolt/Utils/CommandLineOpts.h" #include "bolt/Utils/NameResolver.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/CommandLine.h" namespace opts { - extern llvm::cl::OptionCategory BoltCategory; - extern llvm::cl::opt Verbosity; - -llvm::cl::opt - ForcePatch("force-patch", - llvm::cl::desc("force patching of original entry points"), - llvm::cl::Hidden, llvm::cl::cat(BoltCategory)); -} +} // namespace opts namespace llvm { namespace bolt { diff --git a/bolt/lib/Rewrite/MachORewriteInstance.cpp b/bolt/lib/Rewrite/MachORewriteInstance.cpp index 2f05b03290ba..335b7b42ddde 100644 --- a/bolt/lib/Rewrite/MachORewriteInstance.cpp +++ b/bolt/lib/Rewrite/MachORewriteInstance.cpp @@ -20,6 +20,7 @@ #include "bolt/Rewrite/JITLinkLinker.h" #include "bolt/Rewrite/RewriteInstance.h" #include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h" +#include "bolt/Utils/CommandLineOpts.h" #include "bolt/Utils/Utils.h" #include "llvm/MC/MCObjectStreamer.h" #include "llvm/Support/Errc.h" @@ -32,9 +33,8 @@ namespace opts { using namespace llvm; extern cl::opt AlignText; -//FIXME! Upstream change -//extern cl::opt CheckOverlappingElements; -extern cl::opt ForcePatch; +// FIXME! Upstream change +// extern cl::opt CheckOverlappingElements; extern cl::opt Instrument; extern cl::opt InstrumentCalls; extern cl::opt JumpTables; diff --git a/bolt/lib/Utils/CommandLineOpts.cpp b/bolt/lib/Utils/CommandLineOpts.cpp index 17f090aa61ee..ad714371436e 100644 --- a/bolt/lib/Utils/CommandLineOpts.cpp +++ b/bolt/lib/Utils/CommandLineOpts.cpp @@ -80,6 +80,12 @@ cl::opt EqualizeBBCounts( "in non-LBR and shrink wrapping)"), cl::ZeroOrMore, cl::init(false), cl::Hidden, cl::cat(BoltOptCategory)); +llvm::cl::opt ForcePatch( + "force-patch", + llvm::cl::desc("force patching of original entry points to ensure " + "execution follows only the new/optimized code."), + llvm::cl::Hidden, llvm::cl::cat(BoltCategory)); + cl::opt RemoveSymtab("remove-symtab", cl::desc("Remove .symtab section"), cl::cat(BoltCategory));