Skip to content

Commit b88e5ca

Browse files
authored
[MLIR] Adopt LDBG() in EliminateBarriers.cpp (NFC) (llvm#155092)
Also add an extra optional TYPE argument to the LDBG() macro to make it easier to punctually overide DEBUG_TYPE.
1 parent 35110b7 commit b88e5ca

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

llvm/include/llvm/Support/Debug.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class raw_ostream;
4444
/// level, return false.
4545
LLVM_ABI bool isCurrentDebugType(const char *Type, int Level = 0);
4646

47+
/// Overload allowing to swap the order of the Type and Level arguments.
48+
LLVM_ABI inline bool isCurrentDebugType(int Level, const char *Type) {
49+
return isCurrentDebugType(Type, Level);
50+
}
51+
4752
/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
4853
/// option were specified. Note that DebugFlag also needs to be set to true for
4954
/// debug output to be produced.

llvm/include/llvm/Support/DebugLog.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,32 @@ namespace llvm {
3939
// The `level` argument can be a literal integer, or a macro that evaluates to
4040
// an integer.
4141
//
42+
// An optional `type` argument can be provided to control the debug type. The
43+
// default type is DEBUG_TYPE. The `type` argument can be a literal string, or a
44+
// macro that evaluates to a string.
4245
#define LDBG(...) _GET_LDBG_MACRO(__VA_ARGS__)(__VA_ARGS__)
4346

4447
// Helper macros to choose the correct macro based on the number of arguments.
45-
#define LDBG_FUNC_CHOOSER(_f1, _f2, ...) _f2
48+
#define LDBG_FUNC_CHOOSER(_f1, _f2, _f3, ...) _f3
4649
#define LDBG_FUNC_RECOMPOSER(argsWithParentheses) \
4750
LDBG_FUNC_CHOOSER argsWithParentheses
4851
#define LDBG_CHOOSE_FROM_ARG_COUNT(...) \
49-
LDBG_FUNC_RECOMPOSER((__VA_ARGS__, LDBG_LOG_LEVEL, ))
50-
#define LDBG_NO_ARG_EXPANDER() , LDBG_LOG_LEVEL_1
52+
LDBG_FUNC_RECOMPOSER( \
53+
(__VA_ARGS__, LDBG_LOG_LEVEL_WITH_TYPE, LDBG_LOG_LEVEL, ))
54+
#define LDBG_NO_ARG_EXPANDER() , , LDBG_LOG_LEVEL_1
5155
#define _GET_LDBG_MACRO(...) \
5256
LDBG_CHOOSE_FROM_ARG_COUNT(LDBG_NO_ARG_EXPANDER __VA_ARGS__())
5357

5458
// Dispatch macros to support the `level` argument or none (default to 1)
5559
#define LDBG_LOG_LEVEL(LEVEL) \
5660
DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), LEVEL, DEBUG_TYPE)
5761
#define LDBG_LOG_LEVEL_1() LDBG_LOG_LEVEL(1)
62+
// This macro is a helper when LDBG() is called with 2 arguments.
63+
// In this case we want to allow the order of the arguments to be swapped.
64+
// We rely on the fact that the `level` argument is an integer, and the `type`
65+
// is a string and dispatch to a C++ API that is overloaded.
66+
#define LDBG_LOG_LEVEL_WITH_TYPE(LEVEL_OR_TYPE, TYPE_OR_LEVEL) \
67+
DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), (LEVEL_OR_TYPE), (TYPE_OR_LEVEL))
5868

5969
// We want the filename without the full path. We are using the __FILE__ macro
6070
// and a constexpr function to strip the path prefix. We can avoid the frontend
@@ -171,6 +181,12 @@ computePrefix(const char *DebugType, const char *File, int Line, int Level) {
171181
OsPrefix << File << ":" << Line << " ";
172182
return OsPrefix.str();
173183
}
184+
/// Overload allowing to swap the order of the DebugType and Level arguments.
185+
static LLVM_ATTRIBUTE_UNUSED std::string
186+
computePrefix(int Level, const char *File, int Line, const char *DebugType) {
187+
return computePrefix(DebugType, File, Line, Level);
188+
}
189+
174190
} // end namespace impl
175191
#else
176192
// As others in Debug, When compiling without assertions, the -debug-* options

mlir/lib/Dialect/GPU/Transforms/EliminateBarriers.cpp

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
2626
#include "llvm/ADT/TypeSwitch.h"
2727
#include "llvm/Support/Debug.h"
28+
#include "llvm/Support/DebugLog.h"
2829

2930
namespace mlir {
3031
#define GEN_PASS_DEF_GPUELIMINATEBARRIERS
@@ -37,9 +38,6 @@ using namespace mlir::gpu;
3738
#define DEBUG_TYPE "gpu-erase-barriers"
3839
#define DEBUG_TYPE_ALIAS "gpu-erase-barries-alias"
3940

40-
#define DBGS() (llvm::dbgs() << '[' << DEBUG_TYPE << "] ")
41-
#define DBGS_ALIAS() (llvm::dbgs() << '[' << DEBUG_TYPE_ALIAS << "] ")
42-
4341
// The functions below provide interface-like verification, but are too specific
4442
// to barrier elimination to become interfaces.
4543

@@ -424,27 +422,18 @@ static bool maybeCaptured(Value v) {
424422
/// everything. This seems sufficient to achieve barrier removal in structured
425423
/// control flow, more complex cases would require a proper dataflow analysis.
426424
static bool mayAlias(Value first, Value second) {
427-
DEBUG_WITH_TYPE(DEBUG_TYPE_ALIAS, {
428-
DBGS_ALIAS() << "checking aliasing between ";
429-
DBGS_ALIAS() << first << "\n";
430-
DBGS_ALIAS() << " and ";
431-
DBGS_ALIAS() << second << "\n";
432-
});
425+
LDBG(DEBUG_TYPE_ALIAS, 1)
426+
<< "checking aliasing between " << first << " and " << second;
433427

434428
first = getBase(first);
435429
second = getBase(second);
436430

437-
DEBUG_WITH_TYPE(DEBUG_TYPE_ALIAS, {
438-
DBGS_ALIAS() << "base ";
439-
DBGS_ALIAS() << first << "\n";
440-
DBGS_ALIAS() << " and ";
441-
DBGS_ALIAS() << second << "\n";
442-
});
431+
LDBG(DEBUG_TYPE_ALIAS, 1) << "base " << first << " and " << second;
443432

444433
// Values derived from the same base memref do alias (unless we do a more
445434
// advanced analysis to prove non-overlapping accesses).
446435
if (first == second) {
447-
DEBUG_WITH_TYPE(DEBUG_TYPE_ALIAS, DBGS_ALIAS() << "-> do alias!\n");
436+
LDBG(DEBUG_TYPE_ALIAS, 1) << "-> do alias!";
448437
return true;
449438
}
450439

@@ -493,7 +482,7 @@ static bool mayAlias(Value first, Value second) {
493482
return false;
494483

495484
// Otherwise, conservatively assume aliasing.
496-
DEBUG_WITH_TYPE(DEBUG_TYPE_ALIAS, DBGS_ALIAS() << "-> may alias!\n");
485+
LDBG(DEBUG_TYPE_ALIAS, 1) << "-> may alias!";
497486
return true;
498487
}
499488

@@ -567,20 +556,16 @@ haveConflictingEffects(ArrayRef<MemoryEffects::EffectInstance> beforeEffects,
567556
continue;
568557

569558
// Other kinds of effects create a conflict, e.g. read-after-write.
570-
LLVM_DEBUG(
571-
DBGS() << "found a conflict between (before): " << before.getValue()
572-
<< " read:" << isa<MemoryEffects::Read>(before.getEffect())
573-
<< " write:" << isa<MemoryEffects::Write>(before.getEffect())
574-
<< " alloc:"
575-
<< isa<MemoryEffects::Allocate>(before.getEffect()) << " free:"
576-
<< isa<MemoryEffects::Free>(before.getEffect()) << "\n");
577-
LLVM_DEBUG(
578-
DBGS() << "and (after): " << after.getValue()
579-
<< " read:" << isa<MemoryEffects::Read>(after.getEffect())
580-
<< " write:" << isa<MemoryEffects::Write>(after.getEffect())
581-
<< " alloc:" << isa<MemoryEffects::Allocate>(after.getEffect())
582-
<< " free:" << isa<MemoryEffects::Free>(after.getEffect())
583-
<< "\n");
559+
LDBG() << "found a conflict between (before): " << before.getValue()
560+
<< " read:" << isa<MemoryEffects::Read>(before.getEffect())
561+
<< " write:" << isa<MemoryEffects::Write>(before.getEffect())
562+
<< " alloc:" << isa<MemoryEffects::Allocate>(before.getEffect())
563+
<< " free:" << isa<MemoryEffects::Free>(before.getEffect());
564+
LDBG() << "and (after): " << after.getValue()
565+
<< " read:" << isa<MemoryEffects::Read>(after.getEffect())
566+
<< " write:" << isa<MemoryEffects::Write>(after.getEffect())
567+
<< " alloc:" << isa<MemoryEffects::Allocate>(after.getEffect())
568+
<< " free:" << isa<MemoryEffects::Free>(after.getEffect());
584569
return true;
585570
}
586571
}
@@ -595,8 +580,8 @@ class BarrierElimination final : public OpRewritePattern<BarrierOp> {
595580

596581
LogicalResult matchAndRewrite(BarrierOp barrier,
597582
PatternRewriter &rewriter) const override {
598-
LLVM_DEBUG(DBGS() << "checking the necessity of: " << barrier << " "
599-
<< barrier.getLoc() << "\n");
583+
LDBG() << "checking the necessity of: " << barrier << " "
584+
<< barrier.getLoc();
600585

601586
SmallVector<MemoryEffects::EffectInstance> beforeEffects;
602587
getEffectsBefore(barrier, beforeEffects, /*stopAtBarrier=*/true);
@@ -605,14 +590,12 @@ class BarrierElimination final : public OpRewritePattern<BarrierOp> {
605590
getEffectsAfter(barrier, afterEffects, /*stopAtBarrier=*/true);
606591

607592
if (!haveConflictingEffects(beforeEffects, afterEffects)) {
608-
LLVM_DEBUG(DBGS() << "the surrounding barriers are sufficient, removing "
609-
<< barrier << "\n");
593+
LDBG() << "the surrounding barriers are sufficient, removing " << barrier;
610594
rewriter.eraseOp(barrier);
611595
return success();
612596
}
613597

614-
LLVM_DEBUG(DBGS() << "barrier is necessary: " << barrier << " "
615-
<< barrier.getLoc() << "\n");
598+
LDBG() << "barrier is necessary: " << barrier << " " << barrier.getLoc();
616599
return failure();
617600
}
618601
};

0 commit comments

Comments
 (0)