llvm.org GIT mirror llvm / 218bad2
Revert "[PM] Add pass run listeners to the pass manager." Revert the current implementation and C API. New implementation and C APIs are in the works. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208904 91177308-0d34-0410-b5e6-96231b3b80d8 Juergen Ributzka 5 years ago
11 changed file(s) with 9 addition(s) and 208 deletion(s). Raw diff Collapse all Expand all
2020
2121 namespace llvm {
2222
23 class BasicBlock;
24 class DebugLoc;
25 class DiagnosticInfo;
26 class Function;
27 class Instruction;
2823 class LLVMContextImpl;
29 class Module;
30 class Pass;
31 struct PassRunListener;
32 template class SmallVectorImpl;
33 class SMDiagnostic;
3424 class StringRef;
3525 class Twine;
26 class Instruction;
27 class Module;
28 class SMDiagnostic;
29 class DiagnosticInfo;
30 template class SmallVectorImpl;
31 class Function;
32 class DebugLoc;
3633
3734 /// This is an important class for using LLVM in a threaded context. It
3835 /// (opaquely) owns and manages the core "global" data of LLVM's core
138135 void emitOptimizationRemark(const char *PassName, const Function &Fn,
139136 const DebugLoc &DLoc, const Twine &Msg);
140137
141 /// \brief Notify that we finished running a pass.
142 void notifyPassRun(Pass *P, Module *M, Function *F = nullptr,
143 BasicBlock *BB = nullptr);
144 /// \brief Register the given PassRunListener to receive notifyPassRun()
145 /// callbacks whenever a pass ran. The context will take ownership of the
146 /// listener and free it when the context is destroyed.
147 void addRunListener(PassRunListener *L);
148 /// \brief Unregister a PassRunListener so that it no longer receives
149 /// notifyPassRun() callbacks. Remove and free the listener from the context.
150 void removeRunListener(PassRunListener *L);
151138 private:
152139 LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
153140 void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
2828 #ifndef LLVM_PASS_H
2929 #define LLVM_PASS_H
3030
31 #include "llvm/Support/CBindingWrapping.h"
3231 #include "llvm/Support/Compiler.h"
33 #include "llvm-c/Core.h"
3432 #include
3533
3634 namespace llvm {
370368 /// @brief This is the storage for the -time-passes option.
371369 extern bool TimePassesIsEnabled;
372370
373 // Create wrappers for C Binding types (see CBindingWrapping.h).
374 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
375
376371 } // End llvm namespace
377372
378373 // Include support files that contain important APIs commonly used by Passes,
3030 namespace llvm {
3131
3232 class TargetMachine;
33 class LLVMContext;
3433 //===---------------------------------------------------------------------------
3534 /// PassInfo class - An instance of this class exists for every pass known by
3635 /// the system, and can be obtained from a live Pass by calling its
355354 virtual void passEnumerate(const PassInfo *) {}
356355 };
357356
358 //===---------------------------------------------------------------------------
359 /// PassRunListener class - This class is meant to be derived from by
360 /// clients that are interested in which and when passes are run at runtime.
361 struct PassRunListener {
362 /// PassRunListener ctor - Add the current object to the list of
363 /// PassRunListeners...
364 PassRunListener(LLVMContext *);
365
366 virtual ~PassRunListener();
367
368 /// Callback function - This functions is invoked whenever a pass has run.
369 virtual void passRun(LLVMContext *, Pass *, Module *, Function *,
370 BasicBlock *) {}
371 };
372
373357
374358 } // End llvm namespace
375359
111111 */
112112 typedef struct LLVMOpaqueModuleProvider *LLVMModuleProviderRef;
113113
114 /** @see llvm::Pass */
115 typedef struct LLVMOpaquePass *LLVMPassRef;
116
117114 /** @see llvm::PassManagerBase */
118115 typedef struct LLVMOpaquePassManager *LLVMPassManagerRef;
119116
120117 /** @see llvm::PassRegistry */
121118 typedef struct LLVMOpaquePassRegistry *LLVMPassRegistryRef;
122
123 /** @see llvm::PassRunListener */
124 typedef struct LLVMOpaquePassRunListener *LLVMPassRunListenerRef;
125
126 /** @see llvm::LLVMPassRunListener */
127 typedef void (*LLVMPassRunListenerHandlerTy)(LLVMContextRef, LLVMPassRef,
128 LLVMModuleRef, LLVMValueRef,
129 LLVMBasicBlockRef);
130119
131120 /**
132121 * Used to get the users and usees of a Value.
524513 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
525514 unsigned SLen);
526515 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen);
527
528 LLVMPassRunListenerRef LLVMAddPassRunListener(LLVMContextRef,
529 LLVMPassRunListenerHandlerTy);
530 void LLVMRemovePassRunListener(LLVMContextRef, LLVMPassRunListenerRef);
531516
532517 /**
533518 * @}
27792764 */
27802765
27812766 /**
2782 * @defgroup LLVMCCorePass Pass
2783 *
2784 * @{
2785 */
2786
2787 const char *LLVMGetPassName(LLVMPassRef);
2788
2789 /**
2790 * @}
2791 */
2792
2793 /**
27942767 * @defgroup LLVMCCorePassRegistry Pass Registry
27952768 *
27962769 * @{
2626 #include "llvm/IR/IntrinsicInst.h"
2727 #include "llvm/IR/LLVMContext.h"
2828 #include "llvm/IR/Module.h"
29 #include "llvm/Pass.h"
3029 #include "llvm/PassManager.h"
3130 #include "llvm/Support/Debug.h"
3231 #include "llvm/Support/ErrorHandling.h"
4342 using namespace llvm;
4443
4544 #define DEBUG_TYPE "ir"
46
47 namespace {
48 struct LLVMPassRunListener : PassRunListener {
49 LLVMPassRunListenerHandlerTy Callback;
50
51 LLVMPassRunListener(LLVMContext *Context, LLVMPassRunListenerHandlerTy Fn)
52 : PassRunListener(Context), Callback(Fn) {}
53 void passRun(LLVMContext *C, Pass *P, Module *M, Function *F,
54 BasicBlock *BB) override {
55 Callback(wrap(C), wrap(P), wrap(M), wrap(F), wrap(BB));
56 }
57 };
58 // Create wrappers for C Binding types (see CBindingWrapping.h).
59 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassRunListener, LLVMPassRunListenerRef)
60 } // end anonymous namespace
6145
6246 void llvm::initializeCore(PassRegistry &Registry) {
6347 initializeDominatorTreeWrapperPassPass(Registry);
149133 return severity;
150134 }
151135
152 LLVMPassRunListenerRef LLVMAddPassRunListener(LLVMContextRef Context,
153 LLVMPassRunListenerHandlerTy Fn) {
154 return wrap(new LLVMPassRunListener(unwrap(Context), Fn));
155 }
156
157 void LLVMRemovePassRunListener(LLVMContextRef Context,
158 LLVMPassRunListenerRef Listener) {
159 unwrap(Context)->removeRunListener(unwrap(Listener));
160 }
136
161137
162138
163139 /*===-- Operations on modules ---------------------------------------------===*/
26702646 delete unwrap(MemBuf);
26712647 }
26722648
2673 /*===-- Pass -------------------------------------------------------------===*/
2674
2675 const char *LLVMGetPassName(LLVMPassRef P) {
2676 return unwrap(P)->getPassName();
2677 }
2678
26792649 /*===-- Pass Registry -----------------------------------------------------===*/
26802650
26812651 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
213213 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
214214 Names[I->second] = I->first();
215215 }
216
217 //===----------------------------------------------------------------------===//
218 // Pass Run Listeners
219 //===----------------------------------------------------------------------===//
220 /// Notify that we finished running a pass.
221 void LLVMContext::notifyPassRun(Pass *P, Module *M, Function *F, BasicBlock *BB)
222 {
223 pImpl->notifyPassRun(this, P, M, F, BB);
224 }
225 /// Register the given PassRunListener to receive notifyPassRun() callbacks
226 /// whenever a pass ran. The context will take ownership of the listener and
227 /// free it when the context is destroyed.
228 void LLVMContext::addRunListener(PassRunListener *L) {
229 pImpl->addRunListener(L);
230 }
231 /// Unregister a PassRunListener so that it no longer receives notifyPassRun()
232 /// callbacks. Remove and free the listener from the context.
233 void LLVMContext::removeRunListener(PassRunListener *L) {
234 pImpl->removeRunListener(L);
235 }
1414 #include "llvm/ADT/STLExtras.h"
1515 #include "llvm/IR/Attributes.h"
1616 #include "llvm/IR/Module.h"
17 #include "llvm/PassSupport.h"
1817 #include "llvm/Support/CommandLine.h"
1918 #include "llvm/Support/Regex.h"
2019 #include
2120 using namespace llvm;
22
23 /// Notify that we finished running a pass.
24 void LLVMContextImpl::notifyPassRun(LLVMContext *C, Pass *P, Module *M,
25 Function *F, BasicBlock *BB) {
26 for (auto const &L : RunListeners)
27 L->passRun(C, P, M, F, BB);
28 }
29 /// Register the given PassRunListener to receive notifyPassRun()
30 /// callbacks whenever a pass ran.
31 void LLVMContextImpl::addRunListener(PassRunListener *L) {
32 RunListeners.push_back(L);
33 }
34 /// Unregister a PassRunListener so that it no longer receives
35 /// notifyPassRun() callbacks.
36 void LLVMContextImpl::removeRunListener(PassRunListener *L) {
37 auto I = std::find(RunListeners.begin(), RunListeners.end(), L);
38 assert(I != RunListeners.end() && "RunListener not registered!");
39 delete *I;
40 RunListeners.erase(I);
41 }
4221
4322 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
4423 : TheTrueVal(nullptr), TheFalseVal(nullptr),
198177
199178 // Destroy MDStrings.
200179 DeleteContainerSeconds(MDStringCache);
201
202 // Destroy all run listeners.
203 for (auto &L : RunListeners)
204 delete L;
205 RunListeners.clear();
206180 }
207181
208182 // ConstantsContext anchors
3939 class LLVMContext;
4040 class Type;
4141 class Value;
42 struct PassRunListener;
4342
4443 struct DenseMapAPIntKeyInfo {
4544 struct KeyTy {
368367 typedef DenseMap PrefixDataMapTy;
369368 PrefixDataMapTy PrefixDataMap;
370369
371 /// \brief List of listeners to notify about a pass run.
372 SmallVector RunListeners;
373
374370 /// \brief Return true if the given pass name should emit optimization
375371 /// remarks.
376372 bool optimizationRemarksEnabledFor(const char *PassName) const;
377373
378374 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
379375 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
380
381 /// \brief Notify that we finished running a pass.
382 void notifyPassRun(LLVMContext *, Pass *, Module *, Function *, BasicBlock *);
383 /// \brief Register the given PassRunListener to receive notifyPassRun()
384 /// callbacks whenever a pass ran. The context will take ownership of the
385 /// listener and free it when the context is destroyed.
386 void addRunListener(PassRunListener *);
387 /// \brief Unregister a PassRunListener so that it no longer receives
388 /// notifyPassRun() callbacks. Remove and free the listener from the context.
389 void removeRunListener(PassRunListener *);
390
376
391377 LLVMContextImpl(LLVMContext &C);
392378 ~LLVMContextImpl();
393379 };
1515 #include "llvm/IR/LegacyPassManager.h"
1616 #include "llvm/IR/LegacyPassManagers.h"
1717 #include "llvm/IR/LegacyPassNameParser.h"
18 #include "llvm/IR/LLVMContext.h"
1918 #include "llvm/IR/Module.h"
2019 #include "llvm/Support/CommandLine.h"
2120 #include "llvm/Support/Debug.h"
13131312 TimeRegion PassTimer(getPassTimer(BP));
13141313
13151314 LocalChanged |= BP->runOnBasicBlock(*I);
1316
1317 F.getContext().notifyPassRun(BP, F.getParent(), &F, &*I);
13181315 }
13191316
13201317 Changed |= LocalChanged;
15531550 removeNotPreservedAnalysis(FP);
15541551 recordAvailableAnalysis(FP);
15551552 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
1556
1557 F.getContext().notifyPassRun(FP, F.getParent(), &F);
15581553 }
15591554 return Changed;
15601555 }
16341629 removeNotPreservedAnalysis(MP);
16351630 recordAvailableAnalysis(MP);
16361631 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
1637
1638 M.getContext().notifyPassRun(MP, &M);
16391632 }
16401633
16411634 // Finalize module passes
1616 #include "llvm/IR/Function.h"
1717 #include "llvm/IR/IRPrintingPasses.h"
1818 #include "llvm/IR/LegacyPassNameParser.h"
19 #include "llvm/IR/LLVMContext.h"
2019 #include "llvm/PassRegistry.h"
2120 #include "llvm/Support/Debug.h"
2221 #include "llvm/Support/raw_ostream.h"
242241 PassRegistry::getPassRegistry()->enumerateWith(this);
243242 }
244243
245 //===----------------------------------------------------------------------===//
246 // PassRunListener implementation
247 //
248
249 // PassRunListener ctor - Add the current object to the list of
250 // PassRunListeners...
251 PassRunListener::PassRunListener(LLVMContext *C) {
252 C->addRunListener(this);
253 }
254
255 PassRunListener::~PassRunListener() {}
256
257244 PassNameParser::~PassNameParser() {}
258245
259246 //===----------------------------------------------------------------------===//
2727
2828 static bool didCallAllocateCodeSection;
2929 static bool didAllocateCompactUnwindSection;
30 static bool didCallPassRunListener;
3130
3231 static uint8_t *roundTripAllocateCodeSection(void *object, uintptr_t size,
3332 unsigned alignment,
6261
6362 static void roundTripDestroy(void *object) {
6463 delete static_cast(object);
65 }
66
67 static void passRunListenerCallback(LLVMContextRef C, LLVMPassRef P,
68 LLVMModuleRef M, LLVMValueRef F,
69 LLVMBasicBlockRef BB) {
70 didCallPassRunListener = true;
7164 }
7265
7366 namespace {
148141 virtual void SetUp() {
149142 didCallAllocateCodeSection = false;
150143 didAllocateCompactUnwindSection = false;
151 didCallPassRunListener = false;
152144 Module = 0;
153145 Function = 0;
154146 Engine = 0;
436428 EXPECT_TRUE(MM->UsedCodeSize > 0);
437429 EXPECT_TRUE(MM->UsedDataSizeRW > 0);
438430 }
439
440 TEST_F(MCJITCAPITest, pass_run_listener) {
441 SKIP_UNSUPPORTED_PLATFORM;
442
443 buildSimpleFunction();
444 buildMCJITOptions();
445 buildMCJITEngine();
446 LLVMContextRef C = LLVMGetGlobalContext();
447 LLVMAddPassRunListener(C, passRunListenerCallback);
448 buildAndRunPasses();
449
450 union {
451 void *raw;
452 int (*usable)();
453 } functionPointer;
454 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
455
456 EXPECT_EQ(42, functionPointer.usable());
457 EXPECT_TRUE(didCallPassRunListener);
458 }