Skip to content

Commit 3135984

Browse files
committed
Reland "[CMake/Bazel] Support usage of opt driver as a library (llvm#79205)"
This reverts commit be08be5. The build error was due to a different change, apologies!
1 parent d2d42dc commit 3135984

File tree

6 files changed

+972
-922
lines changed

6 files changed

+972
-922
lines changed

llvm/tools/opt/CMakeLists.txt

+14-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,24 @@ set(LLVM_LINK_COMPONENTS
2929
Passes
3030
)
3131

32-
add_llvm_tool(opt
32+
# We don't want to link this into libLLVM
33+
add_llvm_library(LLVMOptDriver
34+
STATIC
3335
NewPMDriver.cpp
34-
opt.cpp
36+
optdriver.cpp
37+
PARTIAL_SOURCES_INTENDED
38+
DEPENDS
39+
intrinsics_gen
40+
)
3541

42+
add_llvm_tool(opt
43+
PARTIAL_SOURCES_INTENDED
44+
opt.cpp
3645
DEPENDS
3746
intrinsics_gen
3847
SUPPORT_PLUGINS
48+
3949
)
50+
target_link_libraries(opt PRIVATE LLVMOptDriver)
51+
4052
export_executable_symbols_for_plugins(opt)

llvm/tools/opt/NewPMDriver.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ bool llvm::runPassPipeline(
328328
StringRef Arg0, Module &M, TargetMachine *TM, TargetLibraryInfoImpl *TLII,
329329
ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
330330
ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
331-
ArrayRef<PassPlugin> PassPlugins, OutputKind OK, VerifierKind VK,
332-
bool ShouldPreserveAssemblyUseListOrder,
331+
ArrayRef<PassPlugin> PassPlugins,
332+
ArrayRef<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks,
333+
OutputKind OK, VerifierKind VK, bool ShouldPreserveAssemblyUseListOrder,
333334
bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,
334335
bool EmitModuleHash, bool EnableDebugify, bool VerifyDIPreserve,
335336
bool UnifiedLTO) {
@@ -428,6 +429,10 @@ bool llvm::runPassPipeline(
428429
for (auto &PassPlugin : PassPlugins)
429430
PassPlugin.registerPassBuilderCallbacks(PB);
430431

432+
// Load any explicitly specified plugins.
433+
for (auto &PassCallback : PassBuilderCallbacks)
434+
PassCallback(PB);
435+
431436
#define HANDLE_EXTENSION(Ext) \
432437
get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
433438
#include "llvm/Support/Extension.def"

llvm/tools/opt/NewPMDriver.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Support/CommandLine.h"
2424

2525
namespace llvm {
26+
class PassBuilder;
2627
class StringRef;
2728
class Module;
2829
class PassPlugin;
@@ -64,16 +65,17 @@ void printPasses(raw_ostream &OS);
6465
///
6566
/// ThinLTOLinkOut is only used when OK is OK_OutputThinLTOBitcode, and can be
6667
/// nullptr.
67-
bool runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
68-
TargetLibraryInfoImpl *TLII, ToolOutputFile *Out,
69-
ToolOutputFile *ThinLinkOut, ToolOutputFile *OptRemarkFile,
70-
StringRef PassPipeline, ArrayRef<PassPlugin> PassPlugins,
71-
opt_tool::OutputKind OK, opt_tool::VerifierKind VK,
72-
bool ShouldPreserveAssemblyUseListOrder,
73-
bool ShouldPreserveBitcodeUseListOrder,
74-
bool EmitSummaryIndex, bool EmitModuleHash,
75-
bool EnableDebugify, bool VerifyDIPreserve,
76-
bool UnifiedLTO = false);
68+
bool runPassPipeline(
69+
StringRef Arg0, Module &M, TargetMachine *TM, TargetLibraryInfoImpl *TLII,
70+
ToolOutputFile *Out, ToolOutputFile *ThinLinkOut,
71+
ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
72+
ArrayRef<PassPlugin> PassPlugins,
73+
ArrayRef<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks,
74+
opt_tool::OutputKind OK, opt_tool::VerifierKind VK,
75+
bool ShouldPreserveAssemblyUseListOrder,
76+
bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,
77+
bool EmitModuleHash, bool EnableDebugify, bool VerifyDIPreserve,
78+
bool UnifiedLTO = false);
7779
} // namespace llvm
7880

7981
#endif

0 commit comments

Comments
 (0)