Skip to content

Commit 18adc24

Browse files
committed
remove mutex/file writes in favor of toolsubst+llvm::dbgs
1 parent a431eb5 commit 18adc24

File tree

8 files changed

+75
-108
lines changed

8 files changed

+75
-108
lines changed

mlir/CMakeLists.txt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,6 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
202202
mlir_configure_python_dev_packages()
203203
endif()
204204

205-
#-------------------------------------------------------------------------------
206-
# MLIR Pattern Catalog Generator Configuration
207-
#
208-
# When enabled, causes all rewriter patterns to dump their type names and the
209-
# names of affected operations, which can be used to build a search index
210-
# mapping operations to patterns.
211-
#-------------------------------------------------------------------------------
212-
213-
set(MLIR_ENABLE_CATALOG_GENERATOR 0 CACHE BOOL
214-
"Enables construction of a catalog of rewrite patterns.")
215-
216-
if (MLIR_ENABLE_CATALOG_GENERATOR)
217-
message(STATUS "Enabling MLIR pattern catalog generator")
218-
add_definitions(-DMLIR_ENABLE_CATALOG_GENERATOR)
219-
endif()
220-
221205
set(CMAKE_INCLUDE_CURRENT_DIR ON)
222206

223207
include_directories(BEFORE
@@ -338,4 +322,3 @@ endif()
338322
if(MLIR_STANDALONE_BUILD)
339323
llvm_distribution_add_targets()
340324
endif()
341-

mlir/cmake/modules/MLIRConfig.cmake.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ set(MLIR_IRDL_TO_CPP_EXE "@MLIR_CONFIG_IRDL_TO_CPP_EXE@")
1616
set(MLIR_INSTALL_AGGREGATE_OBJECTS "@MLIR_INSTALL_AGGREGATE_OBJECTS@")
1717
set(MLIR_ENABLE_BINDINGS_PYTHON "@MLIR_ENABLE_BINDINGS_PYTHON@")
1818
set(MLIR_ENABLE_EXECUTION_ENGINE "@MLIR_ENABLE_EXECUTION_ENGINE@")
19-
set(MLIR_ENABLE_CATALOG_GENERATOR "@MLIR_ENABLE_CATALOG_GENERATOR@")
2019

2120
set_property(GLOBAL PROPERTY MLIR_ALL_LIBS "@MLIR_ALL_LIBS@")
2221
set_property(GLOBAL PROPERTY MLIR_DIALECT_LIBS "@MLIR_DIALECT_LIBS@")

mlir/include/mlir/IR/PatternMatch.h

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -476,76 +476,20 @@ class RewriterBase : public OpBuilder {
476476
};
477477

478478
struct CatalogingListener : public RewriterBase::ForwardingListener {
479-
CatalogingListener(OpBuilder::Listener *listener, StringRef patternName,
480-
raw_ostream &os, std::mutex &writeMutex)
481-
: RewriterBase::ForwardingListener(listener), patternName(patternName),
482-
os(os), writeMutex(writeMutex) {}
483-
484-
void notifyOperationInserted(Operation *op, InsertPoint previous) override {
485-
{
486-
std::lock_guard<std::mutex> lock(writeMutex);
487-
os << patternName << " | notifyOperationInserted"
488-
<< " | " << op->getName() << "\n";
489-
os.flush();
490-
}
491-
ForwardingListener::notifyOperationInserted(op, previous);
492-
}
493-
494-
void notifyOperationModified(Operation *op) override {
495-
{
496-
std::lock_guard<std::mutex> lock(writeMutex);
497-
os << patternName << " | notifyOperationModified"
498-
<< " | " << op->getName() << "\n";
499-
os.flush();
500-
}
501-
ForwardingListener::notifyOperationModified(op);
502-
}
503-
504-
void notifyOperationReplaced(Operation *op, Operation *newOp) override {
505-
{
506-
std::lock_guard<std::mutex> lock(writeMutex);
507-
os << patternName << " | notifyOperationReplaced (with op)"
508-
<< " | " << op->getName() << " | " << newOp->getName() << "\n";
509-
os.flush();
510-
}
511-
ForwardingListener::notifyOperationReplaced(op, newOp);
479+
CatalogingListener(OpBuilder::Listener *listener, StringRef patternName)
480+
: RewriterBase::ForwardingListener(listener), patternName(patternName) {
512481
}
513482

483+
void notifyOperationInserted(Operation *op, InsertPoint previous) override;
484+
void notifyOperationModified(Operation *op) override;
485+
void notifyOperationReplaced(Operation *op, Operation *newOp) override;
514486
void notifyOperationReplaced(Operation *op,
515-
ValueRange replacement) override {
516-
{
517-
std::lock_guard<std::mutex> lock(writeMutex);
518-
os << patternName << " | notifyOperationReplaced (with values)"
519-
<< " | " << op->getName() << "\n";
520-
os.flush();
521-
}
522-
ForwardingListener::notifyOperationReplaced(op, replacement);
523-
}
524-
525-
void notifyOperationErased(Operation *op) override {
526-
{
527-
std::lock_guard<std::mutex> lock(writeMutex);
528-
os << patternName << " | notifyOperationErased"
529-
<< " | " << op->getName() << "\n";
530-
os.flush();
531-
}
532-
ForwardingListener::notifyOperationErased(op);
533-
}
534-
535-
void notifyPatternBegin(const Pattern &pattern, Operation *op) override {
536-
{
537-
std::lock_guard<std::mutex> lock(writeMutex);
538-
os << patternName << " | notifyPatternBegin"
539-
<< " | " << op->getName() << "\n";
540-
os.flush();
541-
}
542-
ForwardingListener::notifyPatternBegin(pattern, op);
543-
}
487+
ValueRange replacement) override;
488+
void notifyOperationErased(Operation *op) override;
489+
void notifyPatternBegin(const Pattern &pattern, Operation *op) override;
544490

545491
private:
546492
StringRef patternName;
547-
raw_ostream &os;
548-
std::mutex &writeMutex;
549493
};
550494

551495
/// Move the blocks that belong to "region" before the given position in

mlir/lib/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_mlir_library(MLIRIR
1818
BuiltinDialectBytecode.cpp
1919
BuiltinTypes.cpp
2020
BuiltinTypeInterfaces.cpp
21+
CatalogingListener.cpp
2122
Diagnostics.cpp
2223
Dialect.cpp
2324
DialectResourceBlobManager.cpp

mlir/lib/IR/CatalogingListener.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "mlir/IR/PatternMatch.h"
2+
#include "llvm/Support/Debug.h"
3+
4+
#define DEBUG_TYPE "generate-pattern-catalog"
5+
6+
using namespace mlir;
7+
8+
void RewriterBase::CatalogingListener::notifyOperationInserted(
9+
Operation *op, InsertPoint previous) {
10+
LLVM_DEBUG(llvm::dbgs() << patternName << " | notifyOperationInserted"
11+
<< " | " << op->getName() << "\n");
12+
ForwardingListener::notifyOperationInserted(op, previous);
13+
}
14+
15+
void RewriterBase::CatalogingListener::notifyOperationModified(Operation *op) {
16+
LLVM_DEBUG(llvm::dbgs() << patternName << " | notifyOperationModified"
17+
<< " | " << op->getName() << "\n");
18+
ForwardingListener::notifyOperationModified(op);
19+
}
20+
21+
void RewriterBase::CatalogingListener::notifyOperationReplaced(
22+
Operation *op, Operation *newOp) {
23+
LLVM_DEBUG(llvm::dbgs() << patternName
24+
<< " | notifyOperationReplaced (with op)"
25+
<< " | " << op->getName() << " | " << newOp->getName()
26+
<< "\n");
27+
ForwardingListener::notifyOperationReplaced(op, newOp);
28+
}
29+
30+
void RewriterBase::CatalogingListener::notifyOperationReplaced(
31+
Operation *op, ValueRange replacement) {
32+
LLVM_DEBUG(llvm::dbgs() << patternName
33+
<< " | notifyOperationReplaced (with values)"
34+
<< " | " << op->getName() << "\n");
35+
ForwardingListener::notifyOperationReplaced(op, replacement);
36+
}
37+
38+
void RewriterBase::CatalogingListener::notifyOperationErased(Operation *op) {
39+
LLVM_DEBUG(llvm::dbgs() << patternName << " | notifyOperationErased"
40+
<< " | " << op->getName() << "\n");
41+
ForwardingListener::notifyOperationErased(op);
42+
}
43+
44+
void RewriterBase::CatalogingListener::notifyPatternBegin(
45+
const Pattern &pattern, Operation *op) {
46+
LLVM_DEBUG(llvm::dbgs() << patternName << " | notifyPatternBegin"
47+
<< " | " << op->getName() << "\n");
48+
ForwardingListener::notifyPatternBegin(pattern, op);
49+
}

mlir/lib/Rewrite/PatternApplicator.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,8 @@
1515
#include "ByteCode.h"
1616
#include "llvm/Support/Debug.h"
1717

18-
#ifdef MLIR_ENABLE_CATALOG_GENERATOR
19-
#include "llvm/Support/FileSystem.h"
20-
#include "llvm/Support/raw_ostream.h"
21-
#include <mutex>
22-
#endif
23-
2418
#define DEBUG_TYPE "pattern-application"
2519

26-
#ifdef MLIR_ENABLE_CATALOG_GENERATOR
27-
static std::mutex catalogWriteMutex;
28-
#endif
29-
3020
using namespace mlir;
3121
using namespace mlir::detail;
3222

@@ -162,16 +152,6 @@ LogicalResult PatternApplicator::matchAndRewrite(
162152
unsigned anyIt = 0, anyE = anyOpPatterns.size();
163153
unsigned pdlIt = 0, pdlE = pdlMatches.size();
164154
LogicalResult result = failure();
165-
#ifdef MLIR_ENABLE_CATALOG_GENERATOR
166-
std::error_code ec;
167-
llvm::raw_fd_ostream catalogOs("pattern_catalog.txt", ec,
168-
llvm::sys::fs::OF_Append);
169-
if (ec) {
170-
op->emitError("Failed to open pattern catalog file: " + ec.message());
171-
return failure();
172-
}
173-
#endif
174-
175155
do {
176156
// Find the next pattern with the highest benefit.
177157
const Pattern *bestPattern = nullptr;
@@ -229,19 +209,18 @@ LogicalResult PatternApplicator::matchAndRewrite(
229209
const auto *pattern =
230210
static_cast<const RewritePattern *>(bestPattern);
231211

232-
#ifdef MLIR_ENABLE_CATALOG_GENERATOR
212+
#ifndef NDEBUG
233213
OpBuilder::Listener *oldListener = rewriter.getListener();
234214
RewriterBase::CatalogingListener *catalogingListener =
235-
new RewriterBase::CatalogingListener(
236-
oldListener, pattern->getDebugName(), catalogOs,
237-
catalogWriteMutex);
215+
new RewriterBase::CatalogingListener(oldListener,
216+
pattern->getDebugName());
238217
rewriter.setListener(catalogingListener);
239218
#endif
240219
result = pattern->matchAndRewrite(op, rewriter);
241220
LLVM_DEBUG(llvm::dbgs()
242221
<< "\"" << bestPattern->getDebugName() << "\" result "
243222
<< succeeded(result) << "\n");
244-
#ifdef MLIR_ENABLE_CATALOG_GENERATOR
223+
#ifndef NDEBUG
245224
rewriter.setListener(oldListener);
246225
delete catalogingListener;
247226
#endif

mlir/test/lit.cfg.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ def find_real_python_interpreter():
301301
ToolSubst("mlir-opt", "mlir-opt --verify-roundtrip", unresolved="fatal"),
302302
]
303303
)
304+
elif "MLIR_GENERATE_PATTERN_CATALOG" in os.environ:
305+
tools.extend([
306+
ToolSubst(
307+
"mlir-opt",
308+
"mlir-opt --debug-only=generate-pattern-catalog --mlir-disable-threading",
309+
unresolved="fatal"
310+
),
311+
ToolSubst(
312+
"FileCheck",
313+
"FileCheck --dump-input=always",
314+
unresolved="fatal"
315+
),
316+
])
304317
else:
305318
tools.extend(["mlir-opt"])
306319

utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ expand_template(
4444
"@MLIR_ENABLE_SPIRV_CPU_RUNNER@": "0",
4545
"@MLIR_ENABLE_VULKAN_RUNNER@": "0",
4646
"@MLIR_ENABLE_BINDINGS_PYTHON@": "0",
47-
"@MLIR_ENABLE_CATALOG_GENERATOR@": "0",
4847
"@MLIR_RUN_AMX_TESTS@": "0",
4948
"@MLIR_RUN_ARM_SVE_TESTS@": "0",
5049
"@MLIR_RUN_ARM_SME_TESTS@": "0",

0 commit comments

Comments
 (0)