Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions mlir/examples/transform/Ch4/lib/MyExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

#include "MyExtension.h"
#include "mlir/Dialect/Transform/IR/TransformDialect.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"

#define DEBUG_TYPE_MATCHER "transform-matcher"
#define DBGS_MATCHER() (llvm::dbgs() << "[" DEBUG_TYPE_MATCHER "] ")
#define DEBUG_MATCHER(x) DEBUG_WITH_TYPE(DEBUG_TYPE_MATCHER, x)
#define DEBUG_TYPE "transform-matcher"

#define GET_OP_CLASSES
#include "MyExtension.cpp.inc"
Expand Down Expand Up @@ -124,9 +122,8 @@ mlir::transform::HasOperandSatisfyingOp::apply(
// Report failure-to-match for debugging purposes and stop matching this
// operand.
assert(diag.isSilenceableFailure());
DEBUG_MATCHER(DBGS_MATCHER()
<< "failed to match operand #" << operand.getOperandNumber()
<< ": " << diag.getMessage());
LDBG() << "failed to match operand #" << operand.getOperandNumber()
<< ": " << diag.getMessage();
(void)diag.silence();
matchSucceeded = false;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ struct CopyMappingInfo : public MappingInfo {
Status status;
};

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
const CopyMappingInfo &info) {
info.print(os);
return os;
}

} // namespace gpu
} // namespace transform
} // namespace mlir
Expand Down
16 changes: 7 additions & 9 deletions mlir/lib/Dialect/Affine/Transforms/DecomposeAffineOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
#include "mlir/Dialect/Affine/Transforms/Transforms.h"
#include "mlir/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/InterleavedRange.h"

using namespace mlir;
using namespace mlir::affine;

#define DEBUG_TYPE "decompose-affine-ops"
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
#define DBGSNL() (llvm::dbgs() << "\n")

/// Count the number of loops surrounding `operand` such that operand could be
/// hoisted above.
Expand Down Expand Up @@ -115,7 +115,7 @@ FailureOr<AffineApplyOp> mlir::affine::decompose(RewriterBase &rewriter,
return rewriter.notifyMatchFailure(
op, "only add or mul binary expr can be reassociated");

LLVM_DEBUG(DBGS() << "Start decomposeIntoFinerGrainedOps: " << op << "\n");
LDBG() << "Start decomposeIntoFinerGrainedOps: " << op;

// 2. Iteratively extract the RHS subexpressions while the top-level binary
// expr kind remains the same.
Expand All @@ -125,11 +125,11 @@ FailureOr<AffineApplyOp> mlir::affine::decompose(RewriterBase &rewriter,
auto currentBinExpr = dyn_cast<AffineBinaryOpExpr>(remainingExp);
if (!currentBinExpr || currentBinExpr.getKind() != binExpr.getKind()) {
subExpressions.push_back(remainingExp);
LLVM_DEBUG(DBGS() << "--terminal: " << subExpressions.back() << "\n");
LDBG() << "--terminal: " << subExpressions.back();
break;
}
subExpressions.push_back(currentBinExpr.getRHS());
LLVM_DEBUG(DBGS() << "--subExpr: " << subExpressions.back() << "\n");
LDBG() << "--subExpr: " << subExpressions.back();
remainingExp = currentBinExpr.getLHS();
}

Expand All @@ -146,9 +146,7 @@ FailureOr<AffineApplyOp> mlir::affine::decompose(RewriterBase &rewriter,
llvm::stable_sort(subExpressions, [&](AffineExpr e1, AffineExpr e2) {
return getMaxSymbol(e1) < getMaxSymbol(e2);
});
LLVM_DEBUG(
llvm::interleaveComma(subExpressions, DBGS() << "--sorted subexprs: ");
llvm::dbgs() << "\n");
LDBG() << "--sorted subexprs: " << llvm::interleaved(subExpressions);

// 4. Merge sorted subExpressions iteratively, thus achieving reassociation.
auto s0 = getAffineSymbolExpr(0, ctx);
Expand All @@ -162,7 +160,7 @@ FailureOr<AffineApplyOp> mlir::affine::decompose(RewriterBase &rewriter,
Value tmp = createSubApply(rewriter, op, subExpressions[i]);
current = AffineApplyOp::create(rewriter, op.getLoc(), binMap,
ValueRange{current, tmp});
LLVM_DEBUG(DBGS() << "--reassociate into: " << current << "\n");
LDBG() << "--reassociate into: " << current;
}

// 5. Replace original op.
Expand Down
55 changes: 20 additions & 35 deletions mlir/lib/Dialect/Affine/Transforms/SimplifyAffineMinMax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
#include "mlir/Interfaces/ValueBoundsOpInterface.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/ADT/IntEqClasses.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/InterleavedRange.h"

#define DEBUG_TYPE "affine-min-max"
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE << "]: ")

using namespace mlir;
using namespace mlir::affine;
Expand All @@ -39,7 +38,7 @@ static bool simplifyAffineMinMaxOp(RewriterBase &rewriter, AffineOp affineOp) {
ValueRange operands = affineOp.getOperands();
static constexpr bool isMin = std::is_same_v<AffineOp, AffineMinOp>;

LLVM_DEBUG({ DBGS() << "analyzing value: `" << affineOp << "`\n"; });
LDBG() << "analyzing value: `" << affineOp;

// Create a `Variable` list with values corresponding to each of the results
// in the affine affineMap.
Expand All @@ -48,12 +47,9 @@ static bool simplifyAffineMinMaxOp(RewriterBase &rewriter, AffineOp affineOp) {
[&](unsigned i) {
return Variable(affineMap.getSliceMap(i, 1), operands);
});
LLVM_DEBUG({
DBGS() << "- constructed variables are: "
<< llvm::interleaved_array(llvm::map_range(
variables, [](const Variable &v) { return v.getMap(); }))
<< "`\n";
});
LDBG() << "- constructed variables are: "
<< llvm::interleaved_array(llvm::map_range(
variables, [](const Variable &v) { return v.getMap(); }));

// Get the comparison operation.
ComparisonOperator cmpOp =
Expand All @@ -72,10 +68,8 @@ static bool simplifyAffineMinMaxOp(RewriterBase &rewriter, AffineOp affineOp) {
// Initialize the bound.
Variable *bound = &v;

LLVM_DEBUG({
DBGS() << "- inspecting variable: #" << i << ", with map: `" << v.getMap()
<< "`\n";
});
LDBG() << "- inspecting variable: #" << i << ", with map: `" << v.getMap()
<< "`\n";

// Check against the other variables.
for (size_t j = i + 1; j < variables.size(); ++j) {
Expand All @@ -87,29 +81,23 @@ static bool simplifyAffineMinMaxOp(RewriterBase &rewriter, AffineOp affineOp) {
// Get the bound of the equivalence class or itself.
Variable *nv = bounds.lookup_or(jEqClass, &variables[j]);

LLVM_DEBUG({
DBGS() << "- comparing with variable: #" << jEqClass
<< ", with map: " << nv->getMap() << "\n";
});
LDBG() << "- comparing with variable: #" << jEqClass
<< ", with map: " << nv->getMap();

// Compare the variables.
FailureOr<bool> cmpResult =
ValueBoundsConstraintSet::strongCompare(*bound, cmpOp, *nv);

// The variables cannot be compared.
if (failed(cmpResult)) {
LLVM_DEBUG({
DBGS() << "-- classes: #" << i << ", #" << jEqClass
<< " cannot be merged\n";
});
LDBG() << "-- classes: #" << i << ", #" << jEqClass
<< " cannot be merged";
continue;
}

// Join the equivalent classes and update the bound if necessary.
LLVM_DEBUG({
DBGS() << "-- merging classes: #" << i << ", #" << jEqClass
<< ", is cmp(lhs, rhs): " << *cmpResult << "`\n";
});
LDBG() << "-- merging classes: #" << i << ", #" << jEqClass
<< ", is cmp(lhs, rhs): " << *cmpResult << "`";
if (*cmpResult) {
boundedClasses.join(eqClass, jEqClass);
} else {
Expand All @@ -124,8 +112,7 @@ static bool simplifyAffineMinMaxOp(RewriterBase &rewriter, AffineOp affineOp) {

// Return if there's no simplification.
if (bounds.size() >= affineMap.getNumResults()) {
LLVM_DEBUG(
{ DBGS() << "- the affine operation couldn't get simplified\n"; });
LDBG() << "- the affine operation couldn't get simplified";
return false;
}

Expand All @@ -135,21 +122,19 @@ static bool simplifyAffineMinMaxOp(RewriterBase &rewriter, AffineOp affineOp) {
for (auto [k, bound] : bounds)
results.push_back(bound->getMap().getResult(0));

LLVM_DEBUG({
DBGS() << "- starting from map: " << affineMap << "\n";
DBGS() << "- creating new map with: \n";
DBGS() << "--- dims: " << affineMap.getNumDims() << "\n";
DBGS() << "--- syms: " << affineMap.getNumSymbols() << "\n";
DBGS() << "--- res: " << llvm::interleaved_array(results) << "\n";
});
LDBG() << "- starting from map: " << affineMap;
LDBG() << "- creating new map with:";
LDBG() << "--- dims: " << affineMap.getNumDims();
LDBG() << "--- syms: " << affineMap.getNumSymbols();
LDBG() << "--- res: " << llvm::interleaved_array(results);

affineMap =
AffineMap::get(0, affineMap.getNumSymbols() + affineMap.getNumDims(),
results, rewriter.getContext());

// Update the affine op.
rewriter.modifyOpInPlace(affineOp, [&]() { affineOp.setMap(affineMap); });
LLVM_DEBUG({ DBGS() << "- simplified affine op: `" << affineOp << "`\n"; });
LDBG() << "- simplified affine op: `" << affineOp << "`";
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Linalg/TransformOps/GPUHeuristics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/InterleavedRange.h"
#include "llvm/Support/MathExtras.h"
Expand All @@ -21,7 +22,6 @@
using namespace mlir;

#define DEBUG_TYPE "linalg-transforms"
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")

static Attribute linearId0(MLIRContext *ctx) {
return gpu::GPUThreadMappingAttr::get(ctx, gpu::MappingId::LinearDim0);
Expand Down Expand Up @@ -81,7 +81,7 @@ transform::gpu::CopyMappingInfo::CopyMappingInfo(MLIRContext *ctx,
this->threadMapping =
llvm::to_vector(ArrayRef(allThreadMappings)
.take_back(this->smallestBoundingTileSizes.size()));
LLVM_DEBUG(this->print(DBGS()); llvm::dbgs() << "\n");
LDBG() << *this;
}

int64_t transform::gpu::CopyMappingInfo::maxContiguousElementsToTransfer(
Expand Down
8 changes: 3 additions & 5 deletions mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
#include "mlir/Dialect/Transform/IR/TransformTypes.h"
#include "mlir/Dialect/Transform/Interfaces/MatchInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/InterleavedRange.h"

using namespace mlir;

#define DEBUG_TYPE "linalg-transforms"
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")

//===----------------------------------------------------------------------===//
// StructuredMatchOp
Expand All @@ -39,7 +38,7 @@ DiagnosedSilenceableFailure transform::MatchStructuredOp::matchOperation(
return emitSilenceableError() << "expected a Linalg op";
}
// If errors are suppressed, succeed and set all results to empty lists.
LLVM_DEBUG(DBGS() << "optional nested matcher expected a Linalg op");
LDBG() << "optional nested matcher expected a Linalg op";
results.setRemainingToEmpty(cast<TransformOpInterface>(getOperation()));
return DiagnosedSilenceableFailure::success();
}
Expand Down Expand Up @@ -75,8 +74,7 @@ DiagnosedSilenceableFailure transform::MatchStructuredOp::matchOperation(
// When they are defined in this block, we additionally check if we have
// already applied the operation that defines them. If not, the
// corresponding results will be set to empty lists.
LLVM_DEBUG(DBGS() << "optional nested matcher failed: " << diag.getMessage()
<< "\n");
LDBG() << "optional nested matcher failed: " << diag.getMessage();
(void)diag.silence();
SmallVector<OpOperand *> undefinedOperands;
for (OpOperand &terminatorOperand :
Expand Down
18 changes: 7 additions & 11 deletions mlir/lib/Dialect/Transform/Interfaces/TransformInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#define DEBUG_TYPE "transform-dialect"
#define DEBUG_TYPE_FULL "transform-dialect-full"
#define DEBUG_PRINT_AFTER_ALL "transform-dialect-print-top-level-after-all"
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "] ")
#ifndef NDEBUG
#define FULL_LDBG(X) \
DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), DEBUG_TYPE_FULL)
Expand Down Expand Up @@ -818,16 +817,14 @@ void transform::TransformState::compactOpHandles() {

DiagnosedSilenceableFailure
transform::TransformState::applyTransform(TransformOpInterface transform) {
LLVM_DEBUG({
DBGS() << "applying: ";
transform->print(llvm::dbgs(), OpPrintingFlags().skipRegions());
llvm::dbgs() << "\n";
});
LDBG() << "applying: "
<< OpWithFlags(transform, OpPrintingFlags().skipRegions());
FULL_LDBG() << "Top-level payload before application:\n" << *getTopLevel();
auto printOnFailureRAII = llvm::make_scope_exit([this] {
(void)this;
LLVM_DEBUG(DBGS() << "Failing Top-level payload:\n"; getTopLevel()->print(
llvm::dbgs(), mlir::OpPrintingFlags().printGenericOpForm()););
LDBG() << "Failing Top-level payload:\n"
<< OpWithFlags(getTopLevel(),
OpPrintingFlags().printGenericOpForm());
});

// Set current transform op.
Expand Down Expand Up @@ -995,8 +992,7 @@ transform::TransformState::applyTransform(TransformOpInterface transform) {

printOnFailureRAII.release();
DEBUG_WITH_TYPE(DEBUG_PRINT_AFTER_ALL, {
DBGS() << "Top-level payload:\n";
getTopLevel()->print(llvm::dbgs());
LDBG() << "Top-level payload:\n" << *getTopLevel();
});
return result;
}
Expand Down Expand Up @@ -1273,7 +1269,7 @@ void transform::TrackingListener::notifyMatchFailure(
LLVM_DEBUG({
Diagnostic diag(loc, DiagnosticSeverity::Remark);
reasonCallback(diag);
DBGS() << "Match Failure : " << diag.str();
LDBG() << "Match Failure : " << diag.str();
});
}

Expand Down