Skip to content

Commit d33f819

Browse files
committed
[region-isolation] Move freeform logging on the specific error we are emitting into a method on the error itself.
I am doing this since I discovered that we are not printing certain errors as early as we used to (due to the refactoring I did here), which makes it harder to see the errors that we are emitting while processing individual instructions and before we run the actual dataflow. A nice side-effect of this is that it will make it easy to dump the error in the debugger rather than having to wait until the point in the code where the normal logging takes place.
1 parent afaf393 commit d33f819

File tree

4 files changed

+136
-71
lines changed

4 files changed

+136
-71
lines changed

include/swift/SILOptimizer/Utils/PartitionUtils.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ namespace swift {
8888

8989
class Partition;
9090
class SendingOperandToStateMap;
91+
class RegionAnalysisValueMap;
9192

9293
/// The representative value of the equivalence class that makes up a tracked
9394
/// value.
@@ -380,6 +381,7 @@ class IsolationHistory::Factory {
380381
IsolationHistory get() { return IsolationHistory(this); }
381382
};
382383

384+
/// A struct that represents a specific "sending" operand of an ApplySite.
383385
struct SendingOperandState {
384386
/// The dynamic isolation info of the region of value when we sent.
385387
///
@@ -954,6 +956,12 @@ class PartitionOpError {
954956
const PartitionOp *op;
955957

956958
UnknownCodePatternError(const PartitionOp &op) : op(&op) {}
959+
960+
void print(llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const;
961+
962+
SWIFT_DEBUG_DUMPER(dump(RegionAnalysisValueMap &valueMap)) {
963+
print(llvm::dbgs(), valueMap);
964+
}
957965
};
958966

959967
struct LocalUseAfterSendError {
@@ -964,6 +972,12 @@ class PartitionOpError {
964972
LocalUseAfterSendError(const PartitionOp &op, Element elt,
965973
Operand *sendingOp)
966974
: op(&op), sentElement(elt), sendingOp(sendingOp) {}
975+
976+
void print(llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const;
977+
978+
SWIFT_DEBUG_DUMPER(dump(RegionAnalysisValueMap &valueMap)) {
979+
print(llvm::dbgs(), valueMap);
980+
}
967981
};
968982

969983
struct SentNeverSendableError {
@@ -975,6 +989,12 @@ class PartitionOpError {
975989
SILDynamicMergedIsolationInfo isolationRegionInfo)
976990
: op(&op), sentElement(sentElement),
977991
isolationRegionInfo(isolationRegionInfo) {}
992+
993+
void print(llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const;
994+
995+
SWIFT_DEBUG_DUMPER(dump(RegionAnalysisValueMap &valueMap)) {
996+
print(llvm::dbgs(), valueMap);
997+
}
978998
};
979999

9801000
struct AssignNeverSendableIntoSendingResultError {
@@ -992,6 +1012,12 @@ class PartitionOpError {
9921012
: op(&op), destElement(destElement), destValue(destValue),
9931013
srcElement(srcElement), srcValue(srcValue),
9941014
srcIsolationRegionInfo(srcIsolationRegionInfo) {}
1015+
1016+
void print(llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const;
1017+
1018+
SWIFT_DEBUG_DUMPER(dump(RegionAnalysisValueMap &valueMap)) {
1019+
print(llvm::dbgs(), valueMap);
1020+
}
9951021
};
9961022

9971023
struct InOutSendingNotInitializedAtExitError {
@@ -1002,6 +1028,12 @@ class PartitionOpError {
10021028
InOutSendingNotInitializedAtExitError(const PartitionOp &op, Element elt,
10031029
Operand *sendingOp)
10041030
: op(&op), sentElement(elt), sendingOp(sendingOp) {}
1031+
1032+
void print(llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const;
1033+
1034+
SWIFT_DEBUG_DUMPER(dump(RegionAnalysisValueMap &valueMap)) {
1035+
print(llvm::dbgs(), valueMap);
1036+
}
10051037
};
10061038

10071039
struct InOutSendingNotDisconnectedAtExitError {
@@ -1013,6 +1045,12 @@ class PartitionOpError {
10131045
const PartitionOp &op, Element elt,
10141046
SILDynamicMergedIsolationInfo isolation)
10151047
: op(&op), inoutSendingElement(elt), isolationInfo(isolation) {}
1048+
1049+
void print(llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const;
1050+
1051+
SWIFT_DEBUG_DUMPER(dump(RegionAnalysisValueMap &valueMap)) {
1052+
print(llvm::dbgs(), valueMap);
1053+
}
10161054
};
10171055

10181056
#define PARTITION_OP_ERROR(NAME) \
@@ -1067,6 +1105,20 @@ class PartitionOpError {
10671105

10681106
Kind getKind() const { return kind; }
10691107

1108+
void print(llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const {
1109+
switch (getKind()) {
1110+
#define PARTITION_OP_ERROR(NAME) \
1111+
case NAME: \
1112+
return get##NAME##Error().print(os, valueMap);
1113+
#include "PartitionOpError.def"
1114+
}
1115+
llvm_unreachable("Covered switch isn't covered?!");
1116+
}
1117+
1118+
SWIFT_DEBUG_DUMPER(dump(RegionAnalysisValueMap &valueMap)) {
1119+
return print(llvm::dbgs(), valueMap);
1120+
}
1121+
10701122
#define PARTITION_OP_ERROR(NAME) \
10711123
NAME##Error get##NAME##Error() const { \
10721124
assert(getKind() == Kind::NAME); \

include/swift/SILOptimizer/Utils/SILIsolationInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ class SILDynamicMergedIsolationInfo {
507507
operator bool() const { return bool(innerInfo); }
508508

509509
SILIsolationInfo *operator->() { return &innerInfo; }
510+
const SILIsolationInfo *operator->() const { return &innerInfo; }
510511

511512
SILIsolationInfo getIsolationInfo() const { return innerInfo; }
512513

lib/SILOptimizer/Mandatory/SendNonSendable.cpp

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,36 +2335,18 @@ struct DiagnosticEvaluator final
23352335
}
23362336
}
23372337

2338-
auto rep = info->getValueMap().getRepresentative(sentElement);
2339-
REGIONBASEDISOLATION_LOG(
2340-
llvm::dbgs() << " Emitting Error. Kind: Use After Send\n"
2341-
<< " Sending Inst: " << *sendingOp->getUser()
2342-
<< " Sending Op Value: " << sendingOp->get()
2343-
<< " Require Inst: " << *partitionOp.getSourceInst()
2344-
<< " ID: %%" << sentElement << "\n"
2345-
<< " Rep: " << *rep << " Sending Op Num: "
2346-
<< sendingOp->getOperandNumber() << '\n');
2338+
REGIONBASEDISOLATION_LOG(error.print(llvm::dbgs(), info->getValueMap()));
23472339
sendingOpToRequireInstMultiMap.insert(
23482340
sendingOp, RequireInst::forUseAfterSend(partitionOp.getSourceInst()));
23492341
}
23502342

23512343
void handleInOutSendingNotInitializedAtExitError(
23522344
InOutSendingNotInitializedAtExitError error) const {
23532345
const PartitionOp &partitionOp = *error.op;
2354-
Element inoutSendingVal = error.sentElement;
23552346
Operand *sendingOp = error.sendingOp;
23562347

2357-
auto rep = info->getValueMap().getRepresentative(inoutSendingVal);
2358-
REGIONBASEDISOLATION_LOG(
2359-
llvm::dbgs()
2360-
<< " Emitting Error. Kind: InOut Not Reinitialized At End Of "
2361-
"Function\n"
2362-
<< " Sending Inst: " << *sendingOp->getUser()
2363-
<< " Sending Op Value: " << sendingOp->get()
2364-
<< " Require Inst: " << *partitionOp.getSourceInst()
2365-
<< " ID: %%" << inoutSendingVal << "\n"
2366-
<< " Rep: " << *rep
2367-
<< " Sending Op Num: " << sendingOp->getOperandNumber() << '\n');
2348+
REGIONBASEDISOLATION_LOG(error.print(llvm::dbgs(), info->getValueMap()));
2349+
23682350
sendingOpToRequireInstMultiMap.insert(
23692351
sendingOp, RequireInst::forInOutReinitializationNeeded(
23702352
partitionOp.getSourceInst()));
@@ -2496,18 +2478,7 @@ void SendNonSendableImpl::emitVerbatimErrors() {
24962478
llvm_unreachable("Handled elsewhere");
24972479
case PartitionOpError::AssignNeverSendableIntoSendingResult: {
24982480
auto error = erasedError.getAssignNeverSendableIntoSendingResultError();
2499-
auto srcRep =
2500-
info->getValueMap().getRepresentativeValue(error.srcElement);
2501-
REGIONBASEDISOLATION_LOG(
2502-
llvm::dbgs()
2503-
<< " Emitting Error. Kind: Assign Isolated Into Sending Result!\n"
2504-
<< " Assign Inst: " << *error.op->getSourceInst()
2505-
<< " Dest Value: " << *error.destValue
2506-
<< " Dest Element: " << error.destElement << '\n'
2507-
<< " Src Value: " << error.srcValue
2508-
<< " Src Element: " << error.srcElement << '\n'
2509-
<< " Src Rep: " << srcRep
2510-
<< " Src Isolation: " << error.srcIsolationRegionInfo << '\n');
2481+
REGIONBASEDISOLATION_LOG(error.print(llvm::dbgs(), info->getValueMap()));
25112482
AssignIsolatedIntoSendingResultDiagnosticEmitter emitter(
25122483
error.op->getSourceOp(), error.destValue, error.srcValue,
25132484
error.srcIsolationRegionInfo);
@@ -2520,16 +2491,7 @@ void SendNonSendableImpl::emitVerbatimErrors() {
25202491
info->getValueMap().getRepresentative(error.inoutSendingElement);
25212492
auto isolationRegionInfo = error.isolationInfo;
25222493

2523-
REGIONBASEDISOLATION_LOG(
2524-
llvm::dbgs()
2525-
<< " Emitting Error. Kind: InOut Sending ActorIsolated "
2526-
"at end of "
2527-
"Function Error!\n"
2528-
<< " ID: %%" << error.inoutSendingElement << "\n"
2529-
<< " Rep: " << *inoutSendingVal
2530-
<< " Dynamic Isolation Region: ";
2531-
isolationRegionInfo.printForOneLineLogging(llvm::dbgs());
2532-
llvm::dbgs() << '\n');
2494+
REGIONBASEDISOLATION_LOG(error.print(llvm::dbgs(), info->getValueMap()));
25332495

25342496
InOutSendingNotDisconnectedDiagnosticEmitter emitter(
25352497
cast<TermInst>(error.op->getSourceInst()), inoutSendingVal,
@@ -2539,26 +2501,7 @@ void SendNonSendableImpl::emitVerbatimErrors() {
25392501
}
25402502
case PartitionOpError::SentNeverSendable: {
25412503
auto e = erasedError.getSentNeverSendableError();
2542-
auto errorLog = [&] {
2543-
llvm::dbgs() << " Emitting Error. Kind: Send Non Sendable\n"
2544-
<< " ID: %%" << e.sentElement << "\n"
2545-
<< " Rep: "
2546-
<< *info->getValueMap().getRepresentative(e.sentElement)
2547-
<< " Dynamic Isolation Region: ";
2548-
e.isolationRegionInfo.printForOneLineLogging(llvm::dbgs());
2549-
llvm::dbgs() << '\n';
2550-
if (auto isolatedValue =
2551-
e.isolationRegionInfo->maybeGetIsolatedValue()) {
2552-
llvm::dbgs() << " Isolated Value: " << isolatedValue;
2553-
auto name = inferNameHelper(isolatedValue);
2554-
llvm::dbgs() << " Isolated Value Name: "
2555-
<< (name.has_value() ? name->get() : "none") << '\n';
2556-
} else {
2557-
llvm::dbgs() << " Isolated Value: none\n";
2558-
};
2559-
};
2560-
REGIONBASEDISOLATION_LOG(errorLog());
2561-
2504+
REGIONBASEDISOLATION_LOG(e.print(llvm::dbgs(), info->getValueMap()));
25622505
SentNeverSendableDiagnosticInferrer diagnosticInferrer(
25632506
info->getValueMap(), e);
25642507
diagnosticInferrer.run();

lib/SILOptimizer/Utils/PartitionUtils.cpp

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,88 @@
1212

1313
#include "swift/SILOptimizer/Utils/PartitionUtils.h"
1414

15-
#include "swift/AST/ASTWalker.h"
16-
#include "swift/AST/Expr.h"
17-
#include "swift/Basic/Assertions.h"
18-
#include "swift/SIL/ApplySite.h"
19-
#include "swift/SIL/InstructionUtils.h"
20-
#include "swift/SIL/PatternMatch.h"
21-
#include "swift/SIL/SILGlobalVariable.h"
15+
// We only use this so we can implement print on our type erased errors.
16+
#include "swift/SILOptimizer/Analysis/RegionAnalysis.h"
2217
#include "swift/SILOptimizer/Utils/VariableNameUtils.h"
2318

2419
using namespace swift;
25-
using namespace swift::PatternMatch;
2620
using namespace swift::PartitionPrimitives;
2721

22+
//===----------------------------------------------------------------------===//
23+
// MARK: PartitionOpError
24+
//===----------------------------------------------------------------------===//
25+
26+
void PartitionOpError::UnknownCodePatternError::print(
27+
llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const {
28+
os << " Emitting Error. Kind: Unknown Code Pattern Error\n"
29+
<< " Inst: " << *op->getSourceInst();
30+
}
31+
32+
void PartitionOpError::LocalUseAfterSendError::print(
33+
llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const {
34+
os << " Emitting Error. Kind: Use After Send\n"
35+
<< " Sending Inst: " << *sendingOp->getUser()
36+
<< " Sending Op Value: " << sendingOp->get()
37+
<< " Require Inst: " << *op->getSourceInst() << " ID: %%"
38+
<< sentElement << "\n"
39+
<< " Rep: " << valueMap.getRepresentativeValue(sentElement)
40+
<< " Sending Op Num: " << sendingOp->getOperandNumber() << '\n';
41+
}
42+
43+
void PartitionOpError::SentNeverSendableError::print(
44+
llvm::raw_ostream &os, RegionAnalysisValueMap &info) const {
45+
os << " Emitting Error. Kind: Sent Non Sendable\n"
46+
<< " ID: %%" << sentElement << "\n"
47+
<< " Rep: " << *info.getRepresentative(sentElement)
48+
<< " Dynamic Isolation Region: ";
49+
isolationRegionInfo.printForOneLineLogging(os);
50+
os << '\n';
51+
if (auto isolatedValue = isolationRegionInfo->maybeGetIsolatedValue()) {
52+
os << " Isolated Value: " << isolatedValue;
53+
auto name = VariableNameInferrer::inferName(isolatedValue);
54+
os << " Isolated Value Name: "
55+
<< (name.has_value() ? name->get() : "none") << '\n';
56+
} else {
57+
os << " Isolated Value: none\n";
58+
};
59+
}
60+
61+
void PartitionOpError::AssignNeverSendableIntoSendingResultError::print(
62+
llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const {
63+
os << " Emitting Error. Kind: Assign Isolated Into Sending Result!\n"
64+
<< " Assign Inst: " << *op->getSourceInst()
65+
<< " Dest Value: " << *destValue
66+
<< " Dest Element: " << destElement << '\n'
67+
<< " Src Value: " << srcValue
68+
<< " Src Element: " << srcElement << '\n'
69+
<< " Src Rep: " << valueMap.getRepresentativeValue(srcElement)
70+
<< " Src Isolation: " << srcIsolationRegionInfo << '\n';
71+
}
72+
73+
void PartitionOpError::InOutSendingNotInitializedAtExitError::print(
74+
llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const {
75+
os << " Emitting Error. Kind: InOut Not Reinitialized At End Of "
76+
"Function\n"
77+
<< " Sending Inst: " << *sendingOp->getUser()
78+
<< " Sending Op Value: " << sendingOp->get()
79+
<< " Require Inst: " << *op->getSourceInst() << " ID: %%"
80+
<< sentElement << "\n"
81+
<< " Rep: " << valueMap.getRepresentativeValue(sentElement)
82+
<< " Sending Op Num: " << sendingOp->getOperandNumber() << '\n';
83+
}
84+
85+
void PartitionOpError::InOutSendingNotDisconnectedAtExitError::print(
86+
llvm::raw_ostream &os, RegionAnalysisValueMap &valueMap) const {
87+
os << " Emitting Error. Kind: InOut Sending ActorIsolated "
88+
"at end of "
89+
"Function Error!\n"
90+
<< " ID: %%" << inoutSendingElement << "\n"
91+
<< " Rep: " << valueMap.getRepresentativeValue(inoutSendingElement)
92+
<< " Dynamic Isolation Region: ";
93+
isolationInfo.printForOneLineLogging(os);
94+
os << '\n';
95+
}
96+
2897
//===----------------------------------------------------------------------===//
2998
// MARK: PartitionOp
3099
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)