Skip to content

Commit

Permalink
NFC: Pass PassInstrumentations by unique_ptr instead of raw pointer.
Browse files Browse the repository at this point in the history
This makes the ownership model explicit, and removes potential user errors.

PiperOrigin-RevId: 269122834
  • Loading branch information
River707 authored and tensorflower-gardener committed Sep 15, 2019
1 parent cb1bcba commit bbe65b4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
3 changes: 2 additions & 1 deletion mlir/g3doc/WritingAPass.md
Expand Up @@ -393,7 +393,8 @@ PassManager pm;

// Add the instrumentation to the pass manager.
unsigned domInfoCount;
pm.addInstrumentation(new DominanceCounterInstrumentation(domInfoCount));
pm.addInstrumentation(
std::make_unique<DominanceCounterInstrumentation>(domInfoCount));

// Run the pass manager on a module.
Module m = ...;
Expand Down
5 changes: 2 additions & 3 deletions mlir/include/mlir/Pass/PassInstrumentation.h
Expand Up @@ -112,9 +112,8 @@ class PassInstrumentor {
/// See PassInstrumentation::runAfterAnalysis for details.
void runAfterAnalysis(llvm::StringRef name, AnalysisID *id, Operation *op);

/// Add the given instrumentation to the collection. This takes ownership over
/// the given pointer.
void addInstrumentation(PassInstrumentation *pi);
/// Add the given instrumentation to the collection.
void addInstrumentation(std::unique_ptr<PassInstrumentation> pi);

private:
std::unique_ptr<detail::PassInstrumentorImpl> impl;
Expand Down
5 changes: 2 additions & 3 deletions mlir/include/mlir/Pass/PassManager.h
Expand Up @@ -125,9 +125,8 @@ class PassManager : public OpPassManager {
// Instrumentations
//===--------------------------------------------------------------------===//

/// Add the provided instrumentation to the pass manager. This takes ownership
/// over the given pointer.
void addInstrumentation(PassInstrumentation *pi);
/// Add the provided instrumentation to the pass manager.
void addInstrumentation(std::unique_ptr<PassInstrumentation> pi);

/// Add an instrumentation to print the IR before and after pass execution.
/// * 'shouldPrintBeforePass' and 'shouldPrintAfterPass' correspond to filter
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Pass/IRPrinting.cpp
Expand Up @@ -127,7 +127,7 @@ void PassManager::enableIRPrinting(
std::function<bool(Pass *)> shouldPrintBeforePass,
std::function<bool(Pass *)> shouldPrintAfterPass, bool printModuleScope,
raw_ostream &out) {
addInstrumentation(new IRPrinterInstrumentation(
addInstrumentation(std::make_unique<IRPrinterInstrumentation>(
std::move(shouldPrintBeforePass), std::move(shouldPrintAfterPass),
printModuleScope, out));
}
15 changes: 7 additions & 8 deletions mlir/lib/Pass/Pass.cpp
Expand Up @@ -465,13 +465,12 @@ void PassManager::disableMultithreading(bool disable) {
getImpl().disableThreads = disable;
}

/// Add the provided instrumentation to the pass manager. This takes ownership
/// over the given pointer.
void PassManager::addInstrumentation(PassInstrumentation *pi) {
/// Add the provided instrumentation to the pass manager.
void PassManager::addInstrumentation(std::unique_ptr<PassInstrumentation> pi) {
if (!instrumentor)
instrumentor.reset(new PassInstrumentor());

instrumentor->addInstrumentation(pi);
instrumentor->addInstrumentation(std::move(pi));
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -605,11 +604,11 @@ void PassInstrumentor::runAfterAnalysis(llvm::StringRef name, AnalysisID *id,
instr->runAfterAnalysis(name, id, op);
}

/// Add the given instrumentation to the collection. This takes ownership over
/// the given pointer.
void PassInstrumentor::addInstrumentation(PassInstrumentation *pi) {
/// Add the given instrumentation to the collection.
void PassInstrumentor::addInstrumentation(
std::unique_ptr<PassInstrumentation> pi) {
llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
impl->instrumentations.emplace_back(pi);
impl->instrumentations.emplace_back(std::move(pi));
}

constexpr AnalysisID mlir::detail::PreservedAnalyses::allAnalysesID;
2 changes: 1 addition & 1 deletion mlir/lib/Pass/PassTiming.cpp
Expand Up @@ -476,6 +476,6 @@ void PassManager::enableTiming(PassTimingDisplayMode displayMode) {
// Check if pass timing is already enabled.
if (passTiming)
return;
addInstrumentation(new PassTiming(displayMode));
addInstrumentation(std::make_unique<PassTiming>(displayMode));
passTiming = true;
}

0 comments on commit bbe65b4

Please sign in to comment.