llvm.org GIT mirror llvm / 4ed42e9
Force RegisterStandardPasses to construct std::function in the IPO library. Summary: Fixes an issue using RegisterStandardPasses from a statically linked object before PassManagerBuilder::addGlobalExtension is called from a dynamic library. Reviewers: efriedma, theraven Reviewed By: efriedma Subscribers: mehdi_amini, mgorny, llvm-commits Differential Revision: https://reviews.llvm.org/D33515 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305303 91177308-0d34-0410-b5e6-96231b3b80d8 Frederich Munch 2 years ago
5 changed file(s) with 50 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
5959 public:
6060 /// Extensions are passed the builder itself (so they can see how it is
6161 /// configured) as well as the pass manager to add stuff to.
62 typedef std::function
63 legacy::PassManagerBase &PM)>
64 ExtensionFn;
62 typedef void ExtensionProc(const PassManagerBuilder &Builder,
63 legacy::PassManagerBase &PM);
64 typedef std::function ExtensionFn;
65
6566 enum ExtensionPointTy {
6667 /// EP_EarlyAsPossible - This extension point allows adding passes before
6768 /// any other transformations, allowing them to see the code as it is coming
178179 /// Adds an extension that will be used by all PassManagerBuilder instances.
179180 /// This is intended to be used by plugins, to register a set of
180181 /// optimisations to run automatically.
181 static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
182 static void addGlobalExtension(ExtensionPointTy Ty, ExtensionProc Fn);
182183 void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
183184
184185 private:
207208 /// used by optimizer plugins to allow all front ends to transparently use
208209 /// them. Create a static instance of this class in your plugin, providing a
209210 /// private function that the PassManagerBuilder can use to add your passes.
211 /// Currently limited to real functions to avoid crashes when used within the
212 /// main executable before a loaded plugin has a chance to use this.
210213 struct RegisterStandardPasses {
211214 RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
212 PassManagerBuilder::ExtensionFn Fn) {
213 PassManagerBuilder::addGlobalExtension(Ty, std::move(Fn));
215 PassManagerBuilder::ExtensionProc Fn) {
216 PassManagerBuilder::addGlobalExtension(Ty, Fn);
214217 }
215218 };
216219
197197 static ManagedStatic
198198 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
199199
200 void PassManagerBuilder::addGlobalExtension(
201 PassManagerBuilder::ExtensionPointTy Ty,
202 PassManagerBuilder::ExtensionFn Fn) {
203 GlobalExtensions->push_back(std::make_pair(Ty, std::move(Fn)));
200 void PassManagerBuilder::addGlobalExtension(ExtensionPointTy Ty,
201 ExtensionProc Fn) {
202 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
204203 }
205204
206205 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
None set(LLVM_LINK_COMPONENTS Support)
0 set(LLVM_LINK_COMPONENTS Support IPO)
11
22 add_library(DynamicLibraryLib STATIC ExportedFuncs.cxx)
33
1111 #include "llvm/Support/FileSystem.h"
1212 #include "llvm/Support/ManagedStatic.h"
1313 #include "llvm/Support/Path.h"
14 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
1415 #include "gtest/gtest.h"
1516
1617 #include "PipSqueak.h"
4950 } Tmp;
5051 Tmp.F = Func;
5152 return Tmp.P;
53 }
54
55 static void
56 NoPassRegistration(const PassManagerBuilder &, legacy::PassManagerBase &) {
57 }
58
59 static RegisterStandardPasses LocalPass(PassManagerBuilder::EP_LoopOptimizerEnd,
60 NoPassRegistration);
61
62 typedef void (*TestPassReg)(void (*)(PassManagerBuilder::ExtensionPointTy,
63 PassManagerBuilder::ExtensionProc));
64
65 TEST(DynamicLibrary, PassRegistration) {
66 std::string Err;
67 llvm_shutdown_obj Shutdown;
68 DynamicLibrary DL =
69 DynamicLibrary::getPermanentLibrary(LibPath().c_str(), &Err);
70 EXPECT_TRUE(DL.isValid());
71 EXPECT_TRUE(Err.empty());
72
73 TestPassReg RP = FuncPtr(DL.getAddressOfSymbol("TestPassReg"));
74 RP(&PassManagerBuilder::addGlobalExtension);
5275 }
5376
5477 static const char *OverloadTestA() { return "OverloadCall"; }
77 //===----------------------------------------------------------------------===//
88
99 #include "PipSqueak.h"
10 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
11
12 #define PIPSQUEAK_TESTA_RETURN "LibCall"
13 #include "ExportedFuncs.cxx"
1014
1115 struct Global {
1216 std::string *Str;
4448 Glb.Vec = &V;
4549 }
4650
47 #define PIPSQUEAK_TESTA_RETURN "LibCall"
48 #include "ExportedFuncs.cxx"
51
52 static void LibPassRegistration(const llvm::PassManagerBuilder &,
53 llvm::legacy::PassManagerBase &) {}
54
55 extern "C" PIPSQUEAK_EXPORT void TestPassReg(
56 void (*addGlobalExtension)(llvm::PassManagerBuilder::ExtensionPointTy,
57 llvm::PassManagerBuilder::ExtensionProc)) {
58 addGlobalExtension(llvm::PassManagerBuilder::EP_EarlyAsPossible,
59 LibPassRegistration);
60 }