Skip to content

Commit

Permalink
Merge pull request #75760 from gottesmm/pr-d43ef71ddf97306ef50962d71b…
Browse files Browse the repository at this point in the history
…306ca6b7ce0b3d

[region-isolation] Make logging and debug tooling appear in non-asserts builds.
  • Loading branch information
gottesmm authored Aug 7, 2024
2 parents 5482b47 + 1fbc930 commit d263bf0
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 143 deletions.
6 changes: 0 additions & 6 deletions include/swift/SILOptimizer/Analysis/RegionAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,12 @@ class RegionAnalysisValueMap;

namespace regionanalysisimpl {

#ifndef NDEBUG
/// Global bool set only when asserts are enabled to ease debugging by causing
/// unknown pattern errors to cause an assert so we drop into the debugger.
extern bool AbortOnUnknownPatternMatchError;
#endif

static inline bool shouldAbortOnUnknownPatternMatchError() {
#ifndef NDEBUG
return AbortOnUnknownPatternMatchError;
#else
return false;
#endif
}

using TransferringOperandSetFactory = Partition::TransferringOperandSetFactory;
Expand Down
24 changes: 19 additions & 5 deletions include/swift/SILOptimizer/Utils/PartitionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,31 @@ namespace swift {

namespace PartitionPrimitives {

#ifndef NDEBUG
extern bool REGIONBASEDISOLATION_ENABLE_LOGGING;

#ifdef REGIONBASEDISOLATION_LOG
#error "REGIONBASEDISOLATION_LOG already defined?!"
#endif

#define REGIONBASEDISOLATION_LOG(...) \
do { \
if (PartitionPrimitives::REGIONBASEDISOLATION_ENABLE_LOGGING) { \
__VA_ARGS__; \
} \
} while (0);

extern bool REGIONBASEDISOLATION_ENABLE_VERBOSE_LOGGING;

#ifdef REGIONBASEDISOLATION_VERBOSE_LOG
#error "REGIONBASEDISOLATION_VERBOSE_LOG already defined?!"
#endif

#define REGIONBASEDISOLATION_VERBOSE_LOG(...) \
do { \
if (PartitionPrimitives::REGIONBASEDISOLATION_ENABLE_VERBOSE_LOGGING) { \
LLVM_DEBUG(__VA_ARGS__); \
__VA_ARGS__; \
} \
} while (0);
#else
#define REGIONBASEDISOLATION_VERBOSE_LOG(...)
#endif

struct Element {
unsigned num;
Expand Down
111 changes: 60 additions & 51 deletions lib/SILOptimizer/Analysis/RegionAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ using namespace swift::PartitionPrimitives;
using namespace swift::PatternMatch;
using namespace swift::regionanalysisimpl;

#ifndef NDEBUG

bool swift::regionanalysisimpl::AbortOnUnknownPatternMatchError = false;

static llvm::cl::opt<bool, true> AbortOnUnknownPatternMatchErrorCmdLine(
Expand All @@ -60,8 +58,6 @@ static llvm::cl::opt<bool, true> AbortOnUnknownPatternMatchErrorCmdLine(
llvm::cl::location(
swift::regionanalysisimpl::AbortOnUnknownPatternMatchError));

#endif

//===----------------------------------------------------------------------===//
// MARK: Utilities
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -778,8 +774,9 @@ void PartialApplyReachabilityDataflow::add(Operand *op) {
assert(!propagatedReachability &&
"Cannot add more operands once reachability is computed");
SILValue underlyingValue = getRootValue(op->get());
LLVM_DEBUG(llvm::dbgs() << "PartialApplyReachability::add.\nValue: "
<< underlyingValue << "User: " << *op->getUser());
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< "PartialApplyReachability::add.\nValue: "
<< underlyingValue << "User: " << *op->getUser());

unsigned bit = getBitForValue(underlyingValue);
auto &state = blockData[op->getParentBlock()];
Expand Down Expand Up @@ -899,8 +896,8 @@ void PartialApplyReachabilityDataflow::propagateReachability() {
}
}

LLVM_DEBUG(llvm::dbgs() << "Propagating Captures Result!\n";
print(llvm::dbgs()));
REGIONBASEDISOLATION_LOG(llvm::dbgs() << "Propagating Captures Result!\n";
print(llvm::dbgs()));
}

void PartialApplyReachabilityDataflow::print(llvm::raw_ostream &os) const {
Expand Down Expand Up @@ -1177,9 +1174,11 @@ struct PartitionOpBuilder {

Element srcID = lookupValueID(srcOperand->get());
if (lookupValueID(destValue) == srcID) {
LLVM_DEBUG(llvm::dbgs() << " Skipping assign since tgt and src have "
"the same representative.\n");
LLVM_DEBUG(llvm::dbgs() << " Rep ID: %%" << srcID.num << ".\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< " Skipping assign since tgt and src have "
"the same representative.\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< " Rep ID: %%" << srcID.num << ".\n");
return;
}

Expand Down Expand Up @@ -1434,9 +1433,10 @@ class PartitionOpTranslator {
RegionAnalysisValueMap &valueMap;

void gatherFlowInsensitiveInformationBeforeDataflow() {
LLVM_DEBUG(llvm::dbgs() << ">>> Performing pre-dataflow scan to gather "
"flow insensitive information "
<< function->getName() << ":\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< ">>> Performing pre-dataflow scan to gather "
"flow insensitive information "
<< function->getName() << ":\n");

for (auto &block : *function) {
for (auto &inst : block) {
Expand All @@ -1462,7 +1462,7 @@ class PartitionOpTranslator {
isNonSendableType(val->getType())) {
auto trackVal = getTrackableValue(val, true);
(void)trackVal;
LLVM_DEBUG(trackVal.print(llvm::dbgs()));
REGIONBASEDISOLATION_LOG(trackVal.print(llvm::dbgs()));
continue;
}
if (auto *pbi = dyn_cast<ProjectBoxInst>(val)) {
Expand Down Expand Up @@ -1490,10 +1490,10 @@ class PartitionOpTranslator {
builder.translator = this;
gatherFlowInsensitiveInformationBeforeDataflow();

LLVM_DEBUG(llvm::dbgs() << "Initializing Function Args:\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs() << "Initializing Function Args:\n");
auto functionArguments = function->getArguments();
if (functionArguments.empty()) {
LLVM_DEBUG(llvm::dbgs() << " None.\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " None.\n");
functionArgPartition = Partition::singleRegion(SILLocation::invalid(), {},
historyFactory.get());
return;
Expand All @@ -1510,19 +1510,20 @@ class PartitionOpTranslator {
// NOTE: We do not support today the ability to have multiple parameters
// transfer together as part of the same region.
if (isTransferrableFunctionArgument(cast<SILFunctionArgument>(arg))) {
LLVM_DEBUG(llvm::dbgs() << " %%" << state->getID()
<< " (transferring): " << *arg);
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " %%" << state->getID()
<< " (transferring): " << *arg);
nonSendableSeparateIndices.push_back(state->getID());
continue;
}

// Otherwise, it is one of our merged parameters. Add it to the never
// transfer list and to the region join list.
LLVM_DEBUG(llvm::dbgs() << " %%" << state->getID() << ": ";
state->print(llvm::dbgs()); llvm::dbgs() << *arg);
REGIONBASEDISOLATION_LOG(
llvm::dbgs() << " %%" << state->getID() << ": ";
state->print(llvm::dbgs()); llvm::dbgs() << *arg);
nonSendableJoinedIndices.push_back(state->getID());
} else {
LLVM_DEBUG(llvm::dbgs() << " Sendable: " << *arg);
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " Sendable: " << *arg);
}
}

Expand Down Expand Up @@ -1665,7 +1666,7 @@ class PartitionOpTranslator {
// of our arguments (consider isolated to different actors) or with the
// isolationInfo we specified. Emit an unknown patten error.
if (!mergedInfo) {
LLVM_DEBUG(
REGIONBASEDISOLATION_LOG(
llvm::dbgs() << "Merge Failure!\n"
<< "Original Info: ";
if (originalMergedInfo)
Expand Down Expand Up @@ -1829,7 +1830,8 @@ class PartitionOpTranslator {
}

void translateSILPartialApplyAsyncLetBegin(PartialApplyInst *pai) {
LLVM_DEBUG(llvm::dbgs() << "Translating Async Let Begin Partial Apply!\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< "Translating Async Let Begin Partial Apply!\n");
// Grab our partial apply and transfer all of its non-sendable
// parameters. We do not merge the parameters since each individual capture
// of the async let at the program level is viewed as still being in
Expand Down Expand Up @@ -1858,7 +1860,8 @@ class PartitionOpTranslator {
void translateIsolatedPartialApply(PartialApplyInst *pai,
SILIsolationInfo actorIsolation) {
ApplySite applySite(pai);
LLVM_DEBUG(llvm::dbgs() << "Translating Isolated Partial Apply!\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< "Translating Isolated Partial Apply!\n");

// For each argument operand.
for (auto &op : applySite.getArgumentOperands()) {
Expand Down Expand Up @@ -2346,15 +2349,16 @@ class PartitionOpTranslator {
/// that define the block's dataflow.
void translateSILBasicBlock(SILBasicBlock *basicBlock,
std::vector<PartitionOp> &foundPartitionOps) {
LLVM_DEBUG(llvm::dbgs() << SEP_STR << "Compiling basic block for function "
<< basicBlock->getFunction()->getName() << ": ";
basicBlock->dumpID(); llvm::dbgs() << SEP_STR;
basicBlock->print(llvm::dbgs());
llvm::dbgs() << SEP_STR << "Results:\n";);
REGIONBASEDISOLATION_LOG(
llvm::dbgs() << SEP_STR << "Compiling basic block for function "
<< basicBlock->getFunction()->getName() << ": ";
basicBlock->dumpID(); llvm::dbgs() << SEP_STR;
basicBlock->print(llvm::dbgs());
llvm::dbgs() << SEP_STR << "Results:\n";);
// Translate each SIL instruction to the PartitionOps that it represents if
// any.
for (auto &instruction : *basicBlock) {
LLVM_DEBUG(llvm::dbgs() << "Visiting: " << instruction);
REGIONBASEDISOLATION_LOG(llvm::dbgs() << "Visiting: " << instruction);
translateSILInstruction(&instruction);
copy(builder.currentInstPartitionOps,
std::back_inserter(foundPartitionOps));
Expand Down Expand Up @@ -2383,7 +2387,7 @@ class PartitionOpTranslator {
/// Top level switch that translates SIL instructions.
void translateSILInstruction(SILInstruction *inst) {
builder.reset(inst);
SWIFT_DEFER { LLVM_DEBUG(builder.print(llvm::dbgs())); };
SWIFT_DEFER { REGIONBASEDISOLATION_LOG(builder.print(llvm::dbgs())); };

auto computeOpKind = [&]() -> TranslationSemantics {
switch (inst->getKind()) {
Expand All @@ -2395,7 +2399,7 @@ class PartitionOpTranslator {
};

auto kind = computeOpKind();
LLVM_DEBUG(llvm::dbgs() << " Semantics: " << kind << '\n');
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " Semantics: " << kind << '\n');
switch (kind) {
case TranslationSemantics::Ignored:
return;
Expand Down Expand Up @@ -3170,7 +3174,8 @@ PartitionOpTranslator::visitRefElementAddrInst(RefElementAddrInst *reai) {
// And the field is a let... then ignore it. We know that we cannot race on
// any writes to the field.
if (reai->getField()->isLet()) {
LLVM_DEBUG(llvm::dbgs() << " Found a let! Not tracking!\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< " Found a let! Not tracking!\n");
return TranslationSemantics::Ignored;
}

Expand All @@ -3189,7 +3194,7 @@ PartitionOpTranslator::visitRefTailAddrInst(RefTailAddrInst *reai) {
// And our ref_tail_addr is immutable... we can ignore the access since we
// cannot race against a write to any of these fields.
if (reai->isImmutable()) {
LLVM_DEBUG(
REGIONBASEDISOLATION_LOG(
llvm::dbgs()
<< " Found an immutable Sendable ref_tail_addr! Not tracking!\n");
return TranslationSemantics::Ignored;
Expand Down Expand Up @@ -3326,14 +3331,14 @@ bool BlockPartitionState::recomputeExitFromEntry(
// will be surpressed. will be suppressed
eval.apply(partitionOp);
}
LLVM_DEBUG(llvm::dbgs() << " Working Partition: ";
workingPartition.print(llvm::dbgs()));
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " Working Partition: ";
workingPartition.print(llvm::dbgs()));
bool exitUpdated = !Partition::equals(exitPartition, workingPartition);
exitPartition = workingPartition;
LLVM_DEBUG(llvm::dbgs() << " Exit Partition: ";
exitPartition.print(llvm::dbgs()));
LLVM_DEBUG(llvm::dbgs() << " Updated Partition: "
<< (exitUpdated ? "yes\n" : "no\n"));
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " Exit Partition: ";
exitPartition.print(llvm::dbgs()));
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " Updated Partition: "
<< (exitUpdated ? "yes\n" : "no\n"));
return exitUpdated;
}

Expand Down Expand Up @@ -3389,7 +3394,8 @@ static bool canComputeRegionsForFunction(SILFunction *fn) {
}

if (!fn->hasOwnership()) {
LLVM_DEBUG(llvm::dbgs() << "Only runs on Ownership SSA, skipping!\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< "Only runs on Ownership SSA, skipping!\n");
return false;
}

Expand Down Expand Up @@ -3453,9 +3459,10 @@ void RegionAnalysisFunctionInfo::runDataflow() {
assert(!solved && "solve should only be called once");
solved = true;

LLVM_DEBUG(llvm::dbgs() << SEP_STR << "Performing Dataflow!\n" << SEP_STR);
LLVM_DEBUG(llvm::dbgs() << "Values!\n";
translator->getValueMap().print(llvm::dbgs()));
REGIONBASEDISOLATION_LOG(llvm::dbgs() << SEP_STR << "Performing Dataflow!\n"
<< SEP_STR);
REGIONBASEDISOLATION_LOG(llvm::dbgs() << "Values!\n";
translator->getValueMap().print(llvm::dbgs()));

bool anyNeedUpdate = true;
while (anyNeedUpdate) {
Expand All @@ -3465,9 +3472,11 @@ void RegionAnalysisFunctionInfo::runDataflow() {
auto &blockState = (*blockStates)[block];
blockState.isLive = true;

LLVM_DEBUG(llvm::dbgs() << "Block: bb" << block->getDebugID() << "\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< "Block: bb" << block->getDebugID() << "\n");
if (!blockState.needsUpdate) {
LLVM_DEBUG(llvm::dbgs() << " Doesn't need update! Skipping!\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs()
<< " Doesn't need update! Skipping!\n");
continue;
}

Expand All @@ -3477,7 +3486,7 @@ void RegionAnalysisFunctionInfo::runDataflow() {
// Compute the new entry partition to this block.
Partition newEntryPartition = blockState.entryPartition;

LLVM_DEBUG(llvm::dbgs() << " Visiting Preds!\n");
REGIONBASEDISOLATION_LOG(llvm::dbgs() << " Visiting Preds!\n");

// This loop computes the union of the exit partitions of all
// predecessors of this block
Expand All @@ -3486,9 +3495,9 @@ void RegionAnalysisFunctionInfo::runDataflow() {

// Predecessors that have not been reached yet will have an empty pred
// state... so just merge them all. Also our initial value of
LLVM_DEBUG(llvm::dbgs()
<< " Pred. bb" << predBlock->getDebugID() << ": ";
predState.exitPartition.print(llvm::dbgs()));
REGIONBASEDISOLATION_LOG(
llvm::dbgs() << " Pred. bb" << predBlock->getDebugID() << ": ";
predState.exitPartition.print(llvm::dbgs()));
newEntryPartition =
Partition::join(newEntryPartition, predState.exitPartition);
}
Expand Down
Loading

0 comments on commit d263bf0

Please sign in to comment.