@@ -98,8 +98,9 @@ extern llvm::cl::opt<bool> SILPrintDebugInfo;
9898void swift::verificationFailure(const Twine &complaint,
9999 const SILInstruction *atInstruction,
100100 const SILArgument *atArgument,
101- llvm::function_ref<void(llvm::raw_ostream &out )> extraContext) {
101+ llvm::function_ref<void(SILPrintContext &ctx )> extraContext) {
102102 llvm::raw_ostream &out = llvm::dbgs();
103+ SILPrintContext ctx(out);
103104
104105 const SILFunction *f = nullptr;
105106 StringRef funcName = "?";
@@ -116,14 +117,14 @@ void swift::verificationFailure(const Twine &complaint,
116117
117118 out << "SIL verification failed: " << complaint << "\n";
118119 if (extraContext)
119- extraContext(out );
120+ extraContext(ctx );
120121
121122 if (atInstruction) {
122123 out << "Verifying instruction:\n";
123- atInstruction->printInContext(out );
124+ atInstruction->printInContext(ctx );
124125 } else if (atArgument) {
125126 out << "Verifying argument:\n";
126- atArgument->printInContext(out );
127+ atArgument->printInContext(ctx );
127128 }
128129 if (ContinueOnFailure) {
129130 out << "End Error in function " << funcName << "\n";
@@ -978,7 +979,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
978979 }
979980
980981 void _require(bool condition, const Twine &complaint,
981- llvm::function_ref<void(llvm::raw_ostream &)> extraContext
982+ llvm::function_ref<void(SILPrintContext &)> extraContext
982983 = nullptr) {
983984 if (condition) return;
984985
@@ -1111,16 +1112,16 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
11111112 /// Assert that two types are equal.
11121113 void requireSameType(Type type1, Type type2, const Twine &complaint) {
11131114 _require(type1->isEqual(type2), complaint,
1114- [&](llvm::raw_ostream &out ) {
1115- out << " " << type1 << "\n " << type2 << '\n';
1115+ [&](SILPrintContext &ctx ) {
1116+ ctx.OS() << " " << type1 << "\n " << type2 << '\n';
11161117 });
11171118 }
11181119
11191120 /// Assert that two types are equal.
11201121 void requireSameType(SILType type1, SILType type2, const Twine &complaint) {
11211122 _require(type1 == type2, complaint,
1122- [&](llvm::raw_ostream &out ) {
1123- out << " " << type1 << "\n " << type2 << '\n';
1123+ [&](SILPrintContext &ctx ) {
1124+ ctx.OS() << " " << type1 << "\n " << type2 << '\n';
11241125 });
11251126 }
11261127
@@ -1158,15 +1159,15 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
11581159 return;
11591160
11601161 if (!Result.hasPayload()) {
1161- _require(false, what, [&](llvm::raw_ostream &out ) {
1162- out << " " << Result.getMessage().data() << '\n'
1163- << " " << type1 << "\n " << type2 << '\n';
1162+ _require(false, what, [&](SILPrintContext &ctx ) {
1163+ ctx.OS() << " " << Result.getMessage().data() << '\n'
1164+ << " " << type1 << "\n " << type2 << '\n';
11641165 });
11651166 } else {
1166- _require(false, what, [&](llvm::raw_ostream &out ) {
1167- out << " " << Result.getMessage().data()
1168- << ".\nParameter: " << Result.getPayload()
1169- << "\n " << type1 << "\n " << type2 << '\n';
1167+ _require(false, what, [&](SILPrintContext &ctx ) {
1168+ ctx.OS() << " " << Result.getMessage().data()
1169+ << ".\nParameter: " << Result.getPayload()
1170+ << "\n " << type1 << "\n " << type2 << '\n';
11701171 });
11711172 }
11721173 }
@@ -1193,7 +1194,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
11931194 template <class T>
11941195 T *requireValueKind(SILValue value, const Twine &what) {
11951196 auto match = dyn_cast<T>(value);
1196- _require(match != nullptr, what, [=](llvm::raw_ostream &out) { out << value; });
1197+ _require(match != nullptr, what, [=](SILPrintContext &ctx) {
1198+ value->print(ctx);
1199+ });
11971200 return match;
11981201 }
11991202
@@ -1891,7 +1894,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
18911894 if (subs.getGenericSignature().getCanonicalSignature() !=
18921895 fnTy->getInvocationGenericSignature().getCanonicalSignature()) {
18931896 _require(false, "Substitution map does not match callee in apply instruction",
1894- [&](llvm::raw_ostream &out) {
1897+ [&](SILPrintContext &ctx) {
1898+ auto &out = ctx.OS();
18951899 out << "substitution map's generic signature: ";
18961900 subs.getGenericSignature()->print(out);
18971901 out << "\n";
@@ -6803,22 +6807,22 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
68036807 BBState(maybe_movable_ref<BBState> other)
68046808 : BBState(std::move(other).construct()) {}
68056809
6806- void printStack(llvm::raw_ostream &out , StringRef label) const {
6807- out << label << ": [";
6808- if (!Stack.empty()) out << "\n";
6810+ void printStack(SILPrintContext &ctx , StringRef label) const {
6811+ ctx.OS() << label << ": [";
6812+ if (!Stack.empty()) ctx.OS() << "\n";
68096813 for (auto allocation: Stack) {
6810- allocation->print(out );
6814+ allocation->print(ctx );
68116815 }
6812- out << "]\n";
6816+ ctx.OS() << "]\n";
68136817 }
68146818
6815- void printActiveOps(llvm::raw_ostream &out , StringRef label) const {
6816- out << label << ": [";
6817- if (!ActiveOps.empty()) out << "\n";
6819+ void printActiveOps(SILPrintContext &ctx , StringRef label) const {
6820+ ctx.OS() << label << ": [";
6821+ if (!ActiveOps.empty()) ctx.OS() << "\n";
68186822 for (auto op: ActiveOps) {
6819- op->print(out );
6823+ op->print(ctx );
68206824 }
6821- out << "]\n";
6825+ ctx.OS() << "]\n";
68226826 }
68236827
68246828 /// Given that we have two edges to the same block or dead-end region,
@@ -6877,14 +6881,13 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
68776881 // conservative merge regardless of failure so that we're in a
68786882 // coherent state in the successor block.
68796883 auto fail = [&](StringRef complaint,
6880- llvm::function_ref<void(llvm::raw_ostream &out)> extra
6884+ llvm::function_ref<void(SILPrintContext &out)> extra
68816885 = nullptr) {
68826886 verificationFailure(complaint, term, nullptr,
6883- [&](llvm::raw_ostream &out) {
6884- out << "Entering basic block ";
6885- succBB->printID(out, /*newline*/ true);
6887+ [&](SILPrintContext &ctx) {
6888+ ctx.OS() << "Entering basic block " << ctx.getID(succBB) << "\n";
68866889
6887- if (extra) extra(out );
6890+ if (extra) extra(ctx );
68886891 });
68896892 };
68906893
@@ -6908,9 +6911,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
69086911 if (Stack != otherState.Stack) {
69096912 if (!isDeadEndEdge) {
69106913 fail("inconsistent stack states entering basic block",
6911- [&](llvm::raw_ostream &out ) {
6912- otherState.printStack(out , "Current stack state");
6913- printStack(out , "Recorded stack state");
6914+ [&](SILPrintContext &ctx ) {
6915+ otherState.printStack(ctx , "Current stack state");
6916+ printStack(ctx , "Recorded stack state");
69146917 });
69156918 }
69166919
@@ -6923,9 +6926,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
69236926 if (ActiveOps != otherState.ActiveOps) {
69246927 if (!isDeadEndEdge) {
69256928 fail("inconsistent active-operations sets entering basic block",
6926- [&](llvm::raw_ostream &out ) {
6927- otherState.printActiveOps(out , "Current active operations");
6928- printActiveOps(out , "Recorded active operations");
6929+ [&](SILPrintContext &ctx ) {
6930+ otherState.printActiveOps(ctx , "Current active operations");
6931+ printActiveOps(ctx , "Recorded active operations");
69296932 });
69306933 }
69316934
@@ -7021,9 +7024,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
70217024 } else {
70227025 verificationFailure("deallocating allocation that is not the top of the stack",
70237026 &i, nullptr,
7024- [&](llvm::raw_ostream &out ) {
7025- state.printStack(out , "Current stack state");
7026- out << "Stack allocation:\n" << *op;
7027+ [&](SILPrintContext &ctx ) {
7028+ state.printStack(ctx , "Current stack state");
7029+ ctx.OS() << "Stack allocation:\n" << *op;
70277030 // The deallocation is printed out as the focus of the failure.
70287031 });
70297032 }
0 commit comments