Author: Manuel Jacob <[email protected]>
Branch: llvm-translation-backend
Changeset: r81728:0edd0ee170a8
Date: 2016-01-13 02:16 +0100
http://bitbucket.org/pypy/pypy/changeset/0edd0ee170a8/
Log: Implement a LLVM plugin pass which internalizes hidden symbols.
"Internalizes" means that it'll set internal linkage, which is
comparable to adding the static keyword in C.
This should be correct because the LLVM backend links all source
files into one module before compiling.
diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -95,4 +95,4 @@
^.git/
^release/
^rpython/_cache$
-^rpython/translator/llvm/PyPyGC.so$
+^rpython/translator/llvm/.*\.so$
diff --git a/rpython/translator/llvm/InternalizeHiddenSymbols.cpp
b/rpython/translator/llvm/InternalizeHiddenSymbols.cpp
new file mode 100644
--- /dev/null
+++ b/rpython/translator/llvm/InternalizeHiddenSymbols.cpp
@@ -0,0 +1,47 @@
+#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+
+using namespace llvm;
+
+namespace {
+struct InternalizeHiddenSymbols : public ModulePass {
+ static char ID;
+
+ InternalizeHiddenSymbols() : ModulePass(ID) {}
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {}
+
+ bool runOnModule(Module &M) override;
+
+ const char *getPassName() const override {
+ return "Set internal linkage on hidden symbols.";
+ }
+};
+}
+
+char InternalizeHiddenSymbols::ID = 0;
+
+static bool InternalizeIfHidden(GlobalValue &GV) {
+ if (GV.getVisibility() != GlobalValue::HiddenVisibility)
+ return false;
+ GV.setLinkage(GlobalValue::InternalLinkage);
+ return true;
+}
+
+bool InternalizeHiddenSymbols::runOnModule(Module &M) {
+ bool Changed = false;
+
+ for (auto &GV : M.globals())
+ Changed |= InternalizeIfHidden(GV);
+ for (auto &F : M.functions())
+ Changed |= InternalizeIfHidden(F);
+
+ return Changed;
+}
+
+static RegisterStandardPasses RegisterMyPass(
+ PassManagerBuilder::EP_ModuleOptimizerEarly,
+ [](const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) {
+ PM.add(new InternalizeHiddenSymbols());
+ });
diff --git a/rpython/translator/llvm/genllvm.py
b/rpython/translator/llvm/genllvm.py
--- a/rpython/translator/llvm/genllvm.py
+++ b/rpython/translator/llvm/genllvm.py
@@ -1953,7 +1953,9 @@
# optimize this module
optimized_file = self.work_dir.join('output_optimized.' +
('ll' if llvm_assembly else 'bc'))
- opt_args = ['opt', '-O3', linked_file, '-o', optimized_file]
+ opt_args = ['opt', '-load',
+ self._compile_llvm_plugin('InternalizeHiddenSymbols.cpp'),
+ '-O3', linked_file, '-o', optimized_file]
self._execute(opt_args + (['-S'] if llvm_assembly else []))
# compile object file
@@ -1984,14 +1986,14 @@
self._execute(link_args + [object_file, '-o', output_file])
return output_file
- def _compile_llvmgcroot(self):
+ def _compile_llvm_plugin(self, relative_filename):
this_file = local(__file__)
- gc_cpp = this_file.new(basename='PyPyGC.cpp')
- gc_lib = this_file.new(purebasename='PyPyGC',
- ext=self.translator.platform.so_ext)
+ plugin_cpp = this_file.new(basename=relative_filename)
+ plugin_so = plugin_cpp.new(ext=self.translator.platform.so_ext)
cflags = cmdexec('llvm-config --cxxflags').strip() + ' -fno-rtti'
- cmdexec('clang {} -shared {} -o {}'.format(cflags, gc_cpp, gc_lib))
- return gc_lib
+ cmdexec('clang {} -shared {} -o {}'.format(cflags, plugin_cpp,
+ plugin_so))
+ return plugin_so
def compile(self, exe_name):
return self._compile()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit