diff --git a/llvm/test/CMakeLists.txt b/llvm/test/CMakeLists.txt index 6127b76db06b7..1a07e95181194 100644 --- a/llvm/test/CMakeLists.txt +++ b/llvm/test/CMakeLists.txt @@ -138,6 +138,7 @@ set(LLVM_TEST_DEPENDS not obj2yaml opt + opt-printplugin sancov sanstats split-file diff --git a/llvm/test/Feature/load_staticextension.ll b/llvm/test/Feature/load_staticextension.ll new file mode 100644 index 0000000000000..11b754e161256 --- /dev/null +++ b/llvm/test/Feature/load_staticextension.ll @@ -0,0 +1,15 @@ +; REQUIRES: x86-registered-target +; RUN: opt-printplugin %s -passes="printpass" -disable-output 2>&1 | FileCheck %s + +; REQUIRES: plugins + +; 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 +} + diff --git a/llvm/tools/opt/CMakeLists.txt b/llvm/tools/opt/CMakeLists.txt index 8d031b2cc57c7..abbfa24535044 100644 --- a/llvm/tools/opt/CMakeLists.txt +++ b/llvm/tools/opt/CMakeLists.txt @@ -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) +add_subdirectory(driver) +add_subdirectory(test) diff --git a/llvm/tools/opt/driver/CMakeLists.txt b/llvm/tools/opt/driver/CMakeLists.txt new file mode 100644 index 0000000000000..2b0d2722c00f5 --- /dev/null +++ b/llvm/tools/opt/driver/CMakeLists.txt @@ -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) diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/driver/opt.cpp similarity index 100% rename from llvm/tools/opt/opt.cpp rename to llvm/tools/opt/driver/opt.cpp diff --git a/llvm/tools/opt/lib/CMakeLists.txt b/llvm/tools/opt/lib/CMakeLists.txt new file mode 100644 index 0000000000000..bdcc6507c43c1 --- /dev/null +++ b/llvm/tools/opt/lib/CMakeLists.txt @@ -0,0 +1,9 @@ +# We don't want to link this into libLLVM +add_llvm_library(LLVMOptDriver + STATIC + NewPMDriver.cpp + optdriver.cpp + PARTIAL_SOURCES_INTENDED + DEPENDS + intrinsics_gen +) diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/lib/NewPMDriver.cpp similarity index 97% rename from llvm/tools/opt/NewPMDriver.cpp rename to llvm/tools/opt/lib/NewPMDriver.cpp index fdfb4df53273f..7c76e8a1e80b7 100644 --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/lib/NewPMDriver.cpp @@ -57,12 +57,12 @@ cl::opt VerifyEachDebugInfoPreserve( cl::desc("Start each pass with collecting and end it with checking of " "debug info preservation.")); -cl::opt - 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 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("")); } // namespace llvm @@ -163,7 +163,6 @@ static cl::opt DisablePipelineVerification( "-print-pipeline-passes can be used to create a pipeline."), cl::Hidden); - static cl::opt PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden, cl::desc("The kind of profile guided optimization"), @@ -174,8 +173,8 @@ static cl::opt "Use instrumented profile to guide PGO."), clEnumValN(SampleUse, "pgo-sample-use-pipeline", "Use sampled profile to guide PGO."))); -static cl::opt ProfileFile("profile-file", - cl::desc("Path to the profile."), cl::Hidden); +static cl::opt + ProfileFile("profile-file", cl::desc("Path to the profile."), cl::Hidden); static cl::opt MemoryProfileFile("memory-profile-file", cl::desc("Path to the memory profile."), cl::Hidden); @@ -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); } diff --git a/llvm/tools/opt/NewPMDriver.h b/llvm/tools/opt/lib/NewPMDriver.h similarity index 97% rename from llvm/tools/opt/NewPMDriver.h rename to llvm/tools/opt/lib/NewPMDriver.h index 19cabd15436eb..c9d66855f39e9 100644 --- a/llvm/tools/opt/NewPMDriver.h +++ b/llvm/tools/opt/lib/NewPMDriver.h @@ -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); diff --git a/llvm/tools/opt/optdriver.cpp b/llvm/tools/opt/lib/optdriver.cpp similarity index 100% rename from llvm/tools/opt/optdriver.cpp rename to llvm/tools/opt/lib/optdriver.cpp diff --git a/llvm/tools/opt/test/CMakeLists.txt b/llvm/tools/opt/test/CMakeLists.txt new file mode 100644 index 0000000000000..6a2a369519257 --- /dev/null +++ b/llvm/tools/opt/test/CMakeLists.txt @@ -0,0 +1,8 @@ +add_llvm_tool(opt-printplugin + opt-printplugin.cpp + DEPENDS + intrinsics_gen + SUPPORT_PLUGINS + ) +target_link_libraries(opt-printplugin PRIVATE LLVMOptDriver) +export_executable_symbols_for_plugins(opt-printplugin) diff --git a/llvm/tools/opt/test/opt-printplugin.cpp b/llvm/tools/opt/test/opt-printplugin.cpp new file mode 100644 index 0000000000000..386a23e4f1d81 --- /dev/null +++ b/llvm/tools/opt/test/opt-printplugin.cpp @@ -0,0 +1,67 @@ +//===- opt-printplugin.cpp - The LLVM Modular Optimizer +//-------------------------------===// +// +// 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 + +using namespace llvm; + +namespace { + +class PrintPass final : public llvm::AnalysisInfoMixin { + friend struct llvm::AnalysisInfoMixin; + +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) { + if (Name == "printpass") { + PM.addPass(PrintPass()); + return true; + } + return false; + }); +} + +} // namespace + +extern "C" int optMain(int argc, char **argv, + llvm::ArrayRef> + PassBuilderCallbacks); + +int main(int argc, char **argv) { + std::function plugins[] = {registerPlugin}; + return optMain(argc, argv, plugins); +} diff --git a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel index c3efe178d6ed6..d541fecf73b1a 100644 --- a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel @@ -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({ @@ -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(