-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Driver][Frontend] Introduce
load-pass-plugin
option
Allow dynamic loading of LLVM passes via `load-pass-plugin` option passed to the Swift compiler driver.
- Loading branch information
1 parent
d011771
commit 177dc53
Showing
13 changed files
with
200 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
add_swift_unittest(SwiftDriverTests | ||
set(SWIFT_DRIVER_TESTS_SOURCE_FILES | ||
FineGrainedDependencyGraphTests.cpp | ||
MockingFineGrainedDependencyGraphs.cpp | ||
UnitTestSourceFileDepGraphFactory.cpp | ||
) | ||
|
||
set(SWIFT_DRIVER_TESTS_LIBRARIES | ||
swiftAST | ||
swiftClangImporter | ||
swiftSema | ||
swiftDriver | ||
) | ||
|
||
# PassPlugin not supported on Windows | ||
if(NOT SWIFT_HOST_VARIANT STREQUAL "windows") | ||
list(APPEND SWIFT_DRIVER_TESTS_SOURCE_FILES | ||
PassPluginTest.cpp) | ||
list(APPEND SWIFT_DRIVER_TESTS_LIBRARIES | ||
LLVMTestingSupport) | ||
add_subdirectory(PassPluginInput) | ||
endif() | ||
|
||
add_swift_unittest(SwiftDriverTests | ||
${SWIFT_DRIVER_TESTS_SOURCE_FILES}) | ||
|
||
target_link_libraries(SwiftDriverTests PRIVATE | ||
swiftAST | ||
swiftClangImporter | ||
swiftSema | ||
swiftDriver) | ||
${SWIFT_DRIVER_TESTS_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_library(InputTestPlugin SHARED | ||
TestPlugin.cpp) | ||
|
||
# Put PLUGIN next to the unit test executable. | ||
set_output_directory(InputTestPlugin | ||
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../ | ||
LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../ | ||
) | ||
|
||
llvm_map_components_to_libnames(LLVM_LIBRARIES Core) | ||
target_link_libraries(InputTestPlugin PRIVATE ${LLVM_LIBRARIES}) | ||
|
||
set_target_properties(InputTestPlugin PROPERTIES FOLDER "Tests") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//===---------------------- TestPlugin.cpp ----------------------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Passes/PassBuilder.h" | ||
#include "llvm/Passes/PassPlugin.h" | ||
|
||
#include "../TestPlugin.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
struct TestPluginPass : PassInfoMixin<TestPluginPass> { | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) { | ||
return PreservedAnalyses::all(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
PassPluginLibraryInfo getTestPluginInfo() { | ||
return {LLVM_PLUGIN_API_VERSION, TEST_PLUGIN_NAME, TEST_PLUGIN_VERSION, | ||
[](PassBuilder &PB) { | ||
PB.registerPipelineParsingCallback( | ||
[](StringRef Name, ModulePassManager &PM, | ||
ArrayRef<PassBuilder::PipelineElement> InnerPipeline) { | ||
if (Name == "plugin-pass") { | ||
PM.addPass(TestPluginPass()); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
}}; | ||
} | ||
|
||
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo | ||
llvmGetPassPluginInfo() { | ||
return getTestPluginInfo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//===-------------------- PassPluginTest.cpp --------------------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Config/config.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Passes/PassBuilder.h" | ||
#include "llvm/Passes/PassPlugin.h" | ||
#include "llvm/Testing/Support/Error.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include "TestPlugin.h" | ||
|
||
using namespace llvm; | ||
|
||
static std::string LibPath(const std::string &Name) { | ||
const auto &Argvs = testing::internal::GetArgvs(); | ||
const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PassPluginTest"; | ||
std::string Path = sys::fs::getMainExecutable(Argv0, nullptr); | ||
llvm::SmallString<256> Buf{sys::path::parent_path(Path)}; | ||
sys::path::append(Buf, (Name + LLVM_PLUGIN_EXT).c_str()); | ||
return std::string(Buf.str()); | ||
} | ||
|
||
TEST(PassPluginTest, LoadPlugin) { | ||
auto PluginPath = LibPath("libInputTestPlugin"); | ||
ASSERT_NE("", PluginPath); | ||
|
||
Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath); | ||
ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath; | ||
|
||
ASSERT_EQ(TEST_PLUGIN_NAME, Plugin->getPluginName()); | ||
ASSERT_EQ(TEST_PLUGIN_VERSION, Plugin->getPluginVersion()); | ||
|
||
PassBuilder PB; | ||
ModulePassManager PM; | ||
ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Failed()); | ||
|
||
Plugin->registerPassBuilderCallbacks(PB); | ||
ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Succeeded()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===----------------------- TestPlugin.h -----------------------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#define TEST_PLUGIN_NAME "TestPlugin" | ||
#define TEST_PLUGIN_VERSION "0.1-unit" |