-
-
Notifications
You must be signed in to change notification settings - Fork 262
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
Add LLVM-pass plugin support to LDC. Commandline flag: -plugin=...
.
#2554
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
816f2ca
Add LLVM-pass plugin support to LDC. Commandline flag: `-plugin=...`.
JohanEngelen 3b6ba09
Fix makefile
JohanEngelen 838177a
Fix pass for different LLVM versions.
JohanEngelen 6f871c0
Add --export-dynamic to ldc2 link flags.
JohanEngelen 6292afb
Path to plugin needs to be specified, apparently cwd is not included …
JohanEngelen 7f58041
More fixup.
JohanEngelen 9db6d0e
Make plugin support opt-in. Default enabled only on macOS.
JohanEngelen bfc8277
...and add a CMake message about building with/without plugin support.
JohanEngelen a1df496
Add message when plugins are _not_ enabled.
JohanEngelen 35528af
Plugins default enabled on Linux+Apple.
JohanEngelen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//===-- driver/plugins.cpp -------------------------------------*- C++ -*-===// | ||
// | ||
// LDC – the LLVM D compiler | ||
// | ||
// This file is distributed under the BSD-style LDC license. See the LICENSE | ||
// file for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Implements functionality related to plugins (`-plugin=...`). | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "driver/plugins.h" | ||
|
||
#if LDC_ENABLE_PLUGINS | ||
|
||
#include "errors.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/DynamicLibrary.h" | ||
|
||
namespace { | ||
namespace cl = llvm::cl; | ||
|
||
cl::list<std::string> | ||
pluginFiles("plugin", cl::CommaSeparated, cl::desc("Plugins to load."), | ||
cl::value_desc("<dynamic_library.so, lib2.so>")); | ||
|
||
} // anonymous namespace | ||
|
||
/// Loads all plugins. The static constructor of each plugin should take care of | ||
/// the plugins registering themself with the rest of LDC/LLVM. | ||
void loadAllPlugins() { | ||
for (auto &filename : pluginFiles) { | ||
std::string errorString; | ||
if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(filename.c_str(), | ||
&errorString)) { | ||
error(Loc(), "Error loading plugin '%s': %s", filename.c_str(), | ||
errorString.c_str()); | ||
} | ||
} | ||
} | ||
|
||
#else // #if LDC_ENABLE_PLUGINS | ||
|
||
void loadAllPlugins() {} | ||
|
||
#endif // LDC_ENABLE_PLUGINS |
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,15 @@ | ||
//===-- driver/plugins.h ---------------------------------------*- C++ -*-===// | ||
// | ||
// LDC – the LLVM D compiler | ||
// | ||
// This file is distributed under the BSD-style LDC license. See the LICENSE | ||
// file for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LDC_DRIVER_PLUGINS_H | ||
#define LDC_DRIVER_PLUGINS_H | ||
|
||
void loadAllPlugins(); | ||
|
||
#endif // LDC_DRIVER_PLUGINS_H |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
# ROOT_DIR = directory where Makefile sits | ||
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) | ||
ROOT_DIR := $(dir $(MAKEFILE_PATH)) | ||
|
||
LLVM_CONFIG ?= llvm-config | ||
|
||
CXXFLAGS ?= -O3 | ||
CXXFLAGS += $(shell $(LLVM_CONFIG) --cxxflags) -fno-rtti -fpic | ||
# Remove all warning flags (they may or may not be supported by the compiler) | ||
CXXFLAGS := $(filter-out -W%,$(CXXFLAGS)) | ||
CXXFLAGS := $(filter-out -fcolor-diagnostics,$(CXXFLAGS)) | ||
|
||
ifeq "$(shell uname)" "Darwin" | ||
CXXFLAGS += -Wl,-flat_namespace -Wl,-undefined,suppress | ||
endif | ||
|
||
PASSLIB = addFuncEntryCallPass | ||
|
||
all: $(PASSLIB) | ||
|
||
$(PASSLIB): $(ROOT_DIR)$(PASSLIB).cpp | ||
$(CXX) $(CXXFLAGS) -shared $< -o $@.so | ||
|
||
.NOTPARALLEL: clean | ||
|
||
clean: | ||
rm -f $(PASSLIB).so |
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,60 @@ | ||
//===-- addFuncEntryCallPass.cpp - Optimize druntime calls ----------------===// | ||
// | ||
// LDC – the LLVM D compiler | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See the LICENSE file for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Pass.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/LegacyPassManager.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/Transforms/IPO/PassManagerBuilder.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
class FuncEntryCallPass : public FunctionPass { | ||
|
||
Constant *funcToCallUponEntry = nullptr; | ||
|
||
public: | ||
static char ID; | ||
FuncEntryCallPass() : FunctionPass(ID) {} | ||
|
||
bool doInitialization(Module &M) override; | ||
bool runOnFunction(Function &F) override; | ||
}; | ||
} | ||
|
||
char FuncEntryCallPass::ID = 0; | ||
|
||
bool FuncEntryCallPass::doInitialization(Module &M) { | ||
// Add fwd declaration of the `void __test_funcentrycall(void)` function. | ||
auto functionType = FunctionType::get(Type::getVoidTy(M.getContext()), false); | ||
funcToCallUponEntry = | ||
M.getOrInsertFunction("__test_funcentrycall", functionType); | ||
return true; | ||
} | ||
|
||
bool FuncEntryCallPass::runOnFunction(Function &F) { | ||
// Add call to `__test_funcentrycall(void)` at the start of _every_ function | ||
// (this includes e.g. `ldc.register_dso`!) | ||
llvm::BasicBlock &block = F.getEntryBlock(); | ||
IRBuilder<> builder(&block, block.begin()); | ||
builder.CreateCall(funcToCallUponEntry); | ||
return true; | ||
} | ||
|
||
static void addFuncEntryCallPass(const PassManagerBuilder &, | ||
legacy::PassManagerBase &PM) { | ||
PM.add(new FuncEntryCallPass()); | ||
} | ||
// Registration of the plugin's pass is done by the plugin's static constructor. | ||
static RegisterStandardPasses | ||
RegisterFuncEntryCallPass0(PassManagerBuilder::EP_EnabledOnOptLevel0, | ||
addFuncEntryCallPass); |
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 @@ | ||
// REQUIRES: Plugins | ||
|
||
// RUN: make -f %S/Makefile | ||
// RUN: %ldc -c -output-ll -plugin=./addFuncEntryCallPass.so -of=%t.ll %s | ||
// RUN: FileCheck %s < %t.ll | ||
|
||
// CHECK: define {{.*}}testfunction | ||
int testfunction(int i) | ||
{ | ||
// CHECK-NEXT: call {{.*}}__test_funcentrycall | ||
return i * 2; | ||
} | ||
|
||
// CHECK-DAG: declare {{.*}}__test_funcentrycall |
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,7 @@ | ||
import os | ||
import platform | ||
import re | ||
|
||
if (config.plugins_supported): | ||
config.available_features.add('Plugins') | ||
config.environment['LLVM_CONFIG'] = os.path.join(config.llvm_tools_dir, 'llvm-config') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a default of 'off' means that few people will try it, perhaps you should add a status "Building LDC without plugin support"?