Skip to content

[Opt] Enable statically-linked plugin support #79227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ set(LLVM_TEST_DEPENDS
not
obj2yaml
opt
opt-printplugin
sancov
sanstats
split-file
Expand Down
15 changes: 15 additions & 0 deletions llvm/test/Feature/load_staticextension.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; REQUIRES: x86-registered-target
; RUN: opt-printplugin %s -passes="printpass" -disable-output 2>&1 | FileCheck %s

; REQUIRES: plugins
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this actually requires plugins, that's only necessary for shared library plugins IIUC


; CHECK: [PrintPass] Found function: somefunk

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@junk = global i32 0

define ptr @somefunk() {
ret ptr @junk
}

24 changes: 3 additions & 21 deletions llvm/tools/opt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,6 @@ set(LLVM_LINK_COMPONENTS
Passes
)

# We don't want to link this into libLLVM
add_llvm_library(LLVMOptDriver
STATIC
NewPMDriver.cpp
optdriver.cpp
PARTIAL_SOURCES_INTENDED
DEPENDS
intrinsics_gen
)

add_llvm_tool(opt
PARTIAL_SOURCES_INTENDED
opt.cpp
DEPENDS
intrinsics_gen
SUPPORT_PLUGINS

)
target_link_libraries(opt PRIVATE LLVMOptDriver)

export_executable_symbols_for_plugins(opt)
add_subdirectory(lib)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(can't comment above due to github limitations) should LLVM_LINK_COMPONENTS go down to llvm/tools/opt/lib/CMakeLists.txt?

add_subdirectory(driver)
add_subdirectory(test)
8 changes: 8 additions & 0 deletions llvm/tools/opt/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_llvm_tool(opt
opt.cpp
DEPENDS
intrinsics_gen
SUPPORT_PLUGINS
)
target_link_libraries(opt PRIVATE LLVMOptDriver)
export_executable_symbols_for_plugins(opt)
File renamed without changes.
9 changes: 9 additions & 0 deletions llvm/tools/opt/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# We don't want to link this into libLLVM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for my understanding, how do we tell the build system not to link this into libLLVM?

add_llvm_library(LLVMOptDriver
STATIC
NewPMDriver.cpp
optdriver.cpp
PARTIAL_SOURCES_INTENDED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need PARTIAL_SOURCES_INTENDED anymore

DEPENDS
intrinsics_gen
)
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ cl::opt<bool> VerifyEachDebugInfoPreserve(
cl::desc("Start each pass with collecting and end it with checking of "
"debug info preservation."));

cl::opt<std::string>
VerifyDIPreserveExport("verify-di-preserve-export",
cl::desc("Export debug info preservation failures into "
"specified (JSON) file (should be abs path as we use"
" append mode to insert new JSON objects)"),
cl::value_desc("filename"), cl::init(""));
cl::opt<std::string> VerifyDIPreserveExport(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you commit the formatting changes ahead of time

"verify-di-preserve-export",
cl::desc("Export debug info preservation failures into "
"specified (JSON) file (should be abs path as we use"
" append mode to insert new JSON objects)"),
cl::value_desc("filename"), cl::init(""));

} // namespace llvm

Expand Down Expand Up @@ -163,7 +163,6 @@ static cl::opt<bool> DisablePipelineVerification(
"-print-pipeline-passes can be used to create a pipeline."),
cl::Hidden);


static cl::opt<PGOKind>
PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
cl::desc("The kind of profile guided optimization"),
Expand All @@ -174,8 +173,8 @@ static cl::opt<PGOKind>
"Use instrumented profile to guide PGO."),
clEnumValN(SampleUse, "pgo-sample-use-pipeline",
"Use sampled profile to guide PGO.")));
static cl::opt<std::string> ProfileFile("profile-file",
cl::desc("Path to the profile."), cl::Hidden);
static cl::opt<std::string>
ProfileFile("profile-file", cl::desc("Path to the profile."), cl::Hidden);
static cl::opt<std::string>
MemoryProfileFile("memory-profile-file",
cl::desc("Path to the memory profile."), cl::Hidden);
Expand Down Expand Up @@ -411,8 +410,7 @@ bool llvm::runPassPipeline(
} else if (VerifyEachDebugInfoPreserve) {
Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
Debugify.setOrigDIVerifyBugsReportFilePath(
VerifyDIPreserveExport);
Debugify.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
Debugify.registerCallbacks(PIC, MAM);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ enum OutputKind {
OK_OutputThinLTOBitcode,
};
enum VerifierKind { VK_NoVerifier, VK_VerifyOut, VK_VerifyEachPass };
enum PGOKind {
NoPGO,
InstrGen,
InstrUse,
SampleUse
};
enum PGOKind { NoPGO, InstrGen, InstrUse, SampleUse };
enum CSPGOKind { NoCSPGO, CSInstrGen, CSInstrUse };
}
} // namespace opt_tool

void printPasses(raw_ostream &OS);

Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions llvm/tools/opt/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_llvm_tool(opt-printplugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this add_llvm_example? then test will need REQUIRES: examples

opt-printplugin.cpp
DEPENDS
intrinsics_gen
SUPPORT_PLUGINS
)
target_link_libraries(opt-printplugin PRIVATE LLVMOptDriver)
export_executable_symbols_for_plugins(opt-printplugin)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this for an example/test binary?

67 changes: 67 additions & 0 deletions llvm/tools/opt/test/opt-printplugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//===- opt-printplugin.cpp - The LLVM Modular Optimizer
//-------------------------------===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting

//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Example static opt plugin which simply prints the names of all the functions
// within the generated LLVM code.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/ArrayRef.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Support/Registry.h"
#include "llvm/Support/raw_ostream.h"

#include <functional>

using namespace llvm;

namespace {

class PrintPass final : public llvm::AnalysisInfoMixin<PrintPass> {
friend struct llvm::AnalysisInfoMixin<PrintPass>;

private:
static llvm::AnalysisKey key;

public:
using Result = llvm::PreservedAnalyses;

Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) {
for (auto &F : M)
llvm::outs() << "[PrintPass] Found function: " << F.getName() << "\n";
return llvm::PreservedAnalyses::all();
}
static bool isRequired() { return true; }
};

void registerPlugin(PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, llvm::ModulePassManager &PM,
ArrayRef<llvm::PassBuilder::PipelineElement>) {
if (Name == "printpass") {
PM.addPass(PrintPass());
return true;
}
return false;
});
}

} // namespace

extern "C" int optMain(int argc, char **argv,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optMain() shouldn't be extern "C" since it uses C++ classes. this mirrors the llvm driver support, e.g. llvm/cmake/modules/llvm-driver-template.cpp.in

llvm::ArrayRef<std::function<void(llvm::PassBuilder &)>>
PassBuilderCallbacks);

int main(int argc, char **argv) {
std::function<void(llvm::PassBuilder &)> plugins[] = {registerPlugin};
return optMain(argc, argv, plugins);
}
9 changes: 6 additions & 3 deletions utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4817,8 +4817,8 @@ cc_binary(
cc_library(
name = "opt-driver",
srcs = glob([
"tools/opt/*.cpp",
"tools/opt/*.h",
"tools/opt/lib/*.cpp",
"tools/opt/lib/*.h",
]),
copts = llvm_copts,
linkopts = select({
Expand Down Expand Up @@ -4855,7 +4855,10 @@ cc_library(
cc_binary(
name = "opt",
stamp = 0,
deps = [":opt-driver"]
deps = [":opt-driver"],
srcs = glob([
"tools/opt/driver/opt.cpp",
]),
)

gentbl(
Expand Down