Skip to content

Commit 29f6f01

Browse files
authored
Merge pull request #77763 from eeckstein/sil-printing
SIL: don't print operand types in textual SIL
2 parents 657adcb + d59d357 commit 29f6f01

File tree

979 files changed

+1397
-1287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

979 files changed

+1397
-1287
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ ERROR(sil_value_def_type_mismatch,none,
552552
"value '%0' used with mismatching type %1 (expected %2)", (StringRef, Type, Type))
553553
ERROR(sil_use_of_undefined_value,none,
554554
"use of undefined value '%0'", (StringRef))
555+
ERROR(sil_forward_ref_value_needs_type,none,
556+
"forward-referenced value '%0' needs a type annotation", (StringRef))
555557
NOTE(sil_prior_reference,none,
556558
"prior reference was here", ())
557559

lib/SIL/IR/SILPrinter.cpp

Lines changed: 85 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ llvm::cl::opt<bool>
7777
SILPrintSourceInfo("sil-print-sourceinfo", llvm::cl::init(false),
7878
llvm::cl::desc("Include source annotation in SIL output"));
7979

80+
llvm::cl::opt<bool>
81+
SILPrintTypes("sil-print-types", llvm::cl::init(false),
82+
llvm::cl::desc("always print type annotations for instruction operands in SIL output"));
83+
84+
llvm::cl::opt<bool>
85+
SILPrintNoUses("sil-print-no-uses", llvm::cl::init(false),
86+
llvm::cl::desc("omit use comments in SIL output"));
87+
8088
llvm::cl::opt<bool> SILPrintGenericSpecializationInfo(
8189
"sil-print-generic-specialization-info", llvm::cl::init(false),
8290
llvm::cl::desc("Include generic specialization"
@@ -165,31 +173,34 @@ struct SILValuePrinterInfo {
165173
bool IsCapture = false;
166174
bool IsReborrow = false;
167175
bool IsEscaping = false;
176+
bool needPrintType = false;
168177

169178
SILValuePrinterInfo(ID ValueID) : ValueID(ValueID), Type(), OwnershipKind() {}
170-
SILValuePrinterInfo(ID ValueID, SILType Type)
171-
: ValueID(ValueID), Type(Type), OwnershipKind() {}
179+
SILValuePrinterInfo(ID ValueID, SILType Type, bool needPrintType)
180+
: ValueID(ValueID), Type(Type), OwnershipKind(), needPrintType(needPrintType) {}
172181
SILValuePrinterInfo(ID ValueID, SILType Type,
173182
ValueOwnershipKind OwnershipKind)
174183
: ValueID(ValueID), Type(Type), OwnershipKind(OwnershipKind) {}
175184
SILValuePrinterInfo(ID ValueID, SILType Type,
176185
ValueOwnershipKind OwnershipKind, bool IsNoImplicitCopy,
177186
LifetimeAnnotation Lifetime, bool IsCapture,
178-
bool IsReborrow, bool IsEscaping)
187+
bool IsReborrow, bool IsEscaping, bool needPrintType)
179188
: ValueID(ValueID), Type(Type), OwnershipKind(OwnershipKind),
180189
IsNoImplicitCopy(IsNoImplicitCopy), Lifetime(Lifetime),
181-
IsCapture(IsCapture), IsReborrow(IsReborrow), IsEscaping(IsEscaping) {}
190+
IsCapture(IsCapture), IsReborrow(IsReborrow), IsEscaping(IsEscaping),
191+
needPrintType(needPrintType){}
182192
SILValuePrinterInfo(ID ValueID, SILType Type, bool IsNoImplicitCopy,
183193
LifetimeAnnotation Lifetime, bool IsCapture,
184-
bool IsReborrow, bool IsEscaping)
194+
bool IsReborrow, bool IsEscaping, bool needPrintType)
185195
: ValueID(ValueID), Type(Type), OwnershipKind(),
186196
IsNoImplicitCopy(IsNoImplicitCopy), Lifetime(Lifetime),
187-
IsCapture(IsCapture), IsReborrow(IsReborrow), IsEscaping(IsEscaping) {}
197+
IsCapture(IsCapture), IsReborrow(IsReborrow), IsEscaping(IsEscaping),
198+
needPrintType(needPrintType) {}
188199
SILValuePrinterInfo(ID ValueID, SILType Type,
189200
ValueOwnershipKind OwnershipKind, bool IsReborrow,
190-
bool IsEscaping)
201+
bool IsEscaping, bool needPrintType)
191202
: ValueID(ValueID), Type(Type), OwnershipKind(OwnershipKind),
192-
IsReborrow(IsReborrow), IsEscaping(IsEscaping) {}
203+
IsReborrow(IsReborrow), IsEscaping(IsEscaping), needPrintType(needPrintType) {}
193204
};
194205

195206
/// Return the fully qualified dotted path for DeclContext.
@@ -656,6 +667,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
656667
} PrintState;
657668
LineComments lineComments;
658669
unsigned LastBufferID;
670+
llvm::DenseSet<const SILBasicBlock *> printedBlocks;
659671

660672
// Printers for the underlying stream.
661673
#define SIMPLE_PRINTER(TYPE) \
@@ -684,29 +696,57 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
684696
*this << i.ValueID;
685697
if (!i.Type)
686698
return *this;
687-
*this << " : ";
688-
if (i.IsNoImplicitCopy)
689-
*this << "@noImplicitCopy ";
699+
const char *separator = " : ";
700+
if (i.IsNoImplicitCopy) {
701+
*this << separator << "@noImplicitCopy";
702+
separator = " ";
703+
}
690704
switch (i.Lifetime) {
691705
case LifetimeAnnotation::EagerMove:
692-
*this << "@_eagerMove ";
706+
*this << separator << "@_eagerMove";
707+
separator = " ";
693708
break;
694709
case LifetimeAnnotation::None:
695710
break;
696711
case LifetimeAnnotation::Lexical:
697-
*this << "@_lexical ";
712+
*this << separator << "@_lexical";
713+
separator = " ";
698714
break;
699715
}
700-
if (i.IsCapture)
701-
*this << "@closureCapture ";
702-
if (i.IsReborrow)
703-
*this << "@reborrow ";
704-
if (i.IsEscaping)
705-
*this << "@pointer_escape ";
716+
if (i.IsCapture) {
717+
*this << separator << "@closureCapture";
718+
separator = " ";
719+
}
720+
if (i.IsReborrow) {
721+
*this << separator << "@reborrow";
722+
separator = " ";
723+
}
724+
if (i.IsEscaping) {
725+
*this << separator << "@pointer_escape";
726+
separator = " ";
727+
}
706728
if (!i.IsReborrow && i.OwnershipKind && *i.OwnershipKind != OwnershipKind::None) {
707-
*this << "@" << i.OwnershipKind.value() << " ";
729+
*this << separator << "@" << i.OwnershipKind.value() << " ";
730+
separator = " ";
731+
}
732+
if (i.needPrintType) {
733+
*this << separator << i.Type;
708734
}
709-
return *this << i.Type;
735+
return *this;
736+
}
737+
738+
bool needPrintTypeFor(SILValue V) {
739+
if (SILPrintTypes)
740+
return true;
741+
742+
if (!V)
743+
return false;
744+
745+
if (isa<SILUndef>(V))
746+
return true;
747+
748+
// Make sure to print the type if the operand's definition was not printed so far
749+
return printedBlocks.count(V->getParentBlock()) == 0;
710750
}
711751

712752
SILPrinter &operator<<(Type t) {
@@ -733,13 +773,20 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
733773
}
734774

735775
SILValuePrinterInfo getIDAndType(SILValue V) {
736-
return {Ctx.getID(V), V ? V->getType() : SILType()};
776+
return {Ctx.getID(V), V ? V->getType() : SILType(), needPrintTypeFor(V)};
737777
}
778+
SILValuePrinterInfo getIDAndForcedPrintedType(SILValue V) {
779+
return {Ctx.getID(V), V ? V->getType() : SILType(), /*needPrintType=*/true};
780+
}
781+
738782
SILValuePrinterInfo getIDAndType(SILFunctionArgument *arg) {
739783
return {Ctx.getID(arg), arg->getType(),
740784
arg->isNoImplicitCopy(), arg->getLifetimeAnnotation(),
741785
arg->isClosureCapture(), arg->isReborrow(),
742-
arg->hasPointerEscape()};
786+
arg->hasPointerEscape(), /*needPrintType=*/true};
787+
}
788+
SILValuePrinterInfo getIDAndType(SILArgument *arg) {
789+
return {Ctx.getID(arg), arg->getType(), /*needPrintType=*/true};
743790
}
744791

745792
SILValuePrinterInfo getIDAndTypeAndOwnership(SILValue V) {
@@ -753,11 +800,13 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
753800
arg->getLifetimeAnnotation(),
754801
arg->isClosureCapture(),
755802
arg->isReborrow(),
756-
arg->hasPointerEscape()};
803+
arg->hasPointerEscape(),
804+
/*needPrintType=*/true};
757805
}
758806
SILValuePrinterInfo getIDAndTypeAndOwnership(SILArgument *arg) {
759807
return {Ctx.getID(arg), arg->getType(), arg->getOwnershipKind(),
760-
arg->isReborrow(), arg->hasPointerEscape()};
808+
arg->isReborrow(), arg->hasPointerEscape(),
809+
/*needPrintType=*/true};
761810
}
762811

763812
//===--------------------------------------------------------------------===//
@@ -783,6 +832,9 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
783832
}
784833

785834
void printBlockArgumentUses(const SILBasicBlock *BB) {
835+
if (SILPrintNoUses)
836+
return;
837+
786838
if (BB->args_empty())
787839
return;
788840

@@ -880,6 +932,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
880932
#endif
881933

882934
void print(const SILBasicBlock *BB) {
935+
printedBlocks.insert(BB);
936+
883937
// Output uses for BB arguments. These are put into place as comments before
884938
// the block header.
885939
printBlockArgumentUses(BB);
@@ -895,7 +949,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
895949
printBlockArguments(BB);
896950
*this << ":";
897951

898-
if (!BB->pred_empty()) {
952+
if (!BB->pred_empty() && !SILPrintNoUses) {
899953
PrintState.OS.PadToColumn(50);
900954

901955
*this << "// Preds:";
@@ -985,6 +1039,9 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
9851039
}
9861040

9871041
void printUserList(ArrayRef<SILValue> values, SILNodePointer node) {
1042+
if (SILPrintNoUses)
1043+
return;
1044+
9881045
// If the set of values is empty, we need to print the ID of
9891046
// the instruction. Otherwise, if none of the values has a use,
9901047
// we don't need to do anything.
@@ -2464,7 +2521,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24642521
PrintState.OS, QualifiedSILTypeOptions);
24652522
if (!WMI->getTypeDependentOperands().empty()) {
24662523
*this << ", ";
2467-
*this << getIDAndType(WMI->getTypeDependentOperands()[0].get());
2524+
*this << getIDAndForcedPrintedType(WMI->getTypeDependentOperands()[0].get());
24682525
}
24692526
*this << " : " << WMI->getType();
24702527
printConformances({WMI->getConformance()});
@@ -2505,7 +2562,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
25052562
printConformances(AEI->getConformances());
25062563
}
25072564
void visitInitExistentialRefInst(InitExistentialRefInst *AEI) {
2508-
*this << getIDAndType(AEI->getOperand()) << " : $"
2565+
*this << getIDAndForcedPrintedType(AEI->getOperand()) << " : $"
25092566
<< AEI->getFormalConcreteType() << ", " << AEI->getType();
25102567
printConformances(AEI->getConformances());
25112568
printForwardingOwnershipKind(AEI, AEI->getOperand());

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,14 +1504,23 @@ bool SILParser::parseTypedValueRef(SILValue &Result, SourceLoc &Loc,
15041504
Loc = P.Tok.getLoc();
15051505

15061506
UnresolvedValueName Name;
1507-
SILType Ty;
1508-
if (parseValueName(Name) ||
1509-
P.parseToken(tok::colon, diag::expected_sil_colon_value_ref) ||
1510-
parseSILType(Ty))
1507+
if (parseValueName(Name))
15111508
return true;
1512-
1513-
Result = getLocalValue(Name, Ty, RegularLocation(Loc), B);
1514-
return false;
1509+
1510+
if (P.consumeIf(tok::colon)) {
1511+
SILType Ty;
1512+
parseSILType(Ty);
1513+
Result = getLocalValue(Name, Ty, RegularLocation(Loc), B);
1514+
return false;
1515+
} else {
1516+
ValueBase *&Entry = LocalValues[Name.Name];
1517+
if (!Entry) {
1518+
P.diagnose(Name.NameLoc, diag::sil_forward_ref_value_needs_type, Name.Name);
1519+
return true;
1520+
}
1521+
Result = SILValue(Entry);
1522+
return false;
1523+
}
15151524
}
15161525

15171526
/// Look up whether the given string corresponds to a SIL opcode.
@@ -7432,6 +7441,15 @@ bool SILParserState::parseDeclSIL(Parser &P) {
74327441
}
74337442

74347443
FunctionState.F->setLinkage(resolveSILLinkage(FnLinkage, isDefinition));
7444+
switch (FunctionState.F->getLinkage()) {
7445+
case SILLinkage::PublicExternal:
7446+
case SILLinkage::PackageExternal:
7447+
if (!FunctionState.F->isExternalDeclaration() && !FunctionState.F->isAnySerialized())
7448+
FunctionState.F->getModule().setParsedAsSerializedSIL();
7449+
break;
7450+
default:
7451+
break;
7452+
}
74357453
}
74367454

74377455
if (FunctionState.diagnoseProblems())

test/AutoDiff/SIL/Parse/sildeclref.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt %s -module-name=sildeclref_parse | %target-sil-opt -module-name=sildeclref_parse | %FileCheck %s
1+
// RUN: %target-sil-opt -sil-print-types %s -module-name=sildeclref_parse | %target-sil-opt -sil-print-types -module-name=sildeclref_parse | %FileCheck %s
22
// Parse AutoDiff derivative SILDeclRefs via `witness_method` and `class_method` instructions.
33

44
import Swift

test/AutoDiff/SIL/Serialization/differentiable_function_type.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-sil-opt %s -emit-sib -o %t/tmp.sib -module-name main
3-
// RUN: %target-sil-opt %t/tmp.sib -o %t/tmp.sil -module-name main
2+
// RUN: %target-sil-opt -sil-print-types %s -emit-sib -o %t/tmp.sib -module-name main
3+
// RUN: %target-sil-opt -sil-print-types %t/tmp.sib -o %t/tmp.sil -module-name main
44

55
// https://github.com/apple/swift/issues/54526
66
// Workaround because import declarations are not preserved in .sib files.
77
// RUN: sed -e 's/import Swift$/import Swift; import _Differentiation/' %t/tmp.sil > %t/tmp_fixed.sil
8-
// RUN: %target-sil-opt %t/tmp_fixed.sil -module-name main -emit-sorted-sil | %FileCheck %s
8+
// RUN: %target-sil-opt -sil-print-types %t/tmp_fixed.sil -module-name main -emit-sorted-sil | %FileCheck %s
99

1010
// `shell` is required only to run `sed` as a
1111
// https://github.com/apple/swift/issues/54526 workaround.

test/AutoDiff/SIL/differentiable_function_inst.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Round-trip parsing/printing test.
22

3-
// RUN: %target-sil-opt %s -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
3+
// RUN: %target-sil-opt -sil-print-types %s -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
44

55
// Round-trip serialization-deserialization test.
66

77
// RUN: %empty-directory(%t)
8-
// RUN: %target-sil-opt %s -emit-sib -o %t/tmp.sib -module-name main
9-
// RUN: %target-sil-opt %t/tmp.sib -o %t/tmp.sil -module-name main
8+
// RUN: %target-sil-opt -sil-print-types %s -emit-sib -o %t/tmp.sib -module-name main
9+
// RUN: %target-sil-opt -sil-print-types %t/tmp.sib -o %t/tmp.sil -module-name main
1010

1111
// https://github.com/apple/swift/issues/54526
1212
// Workaround because import declarations are not preserved in .sib files.
1313
// RUN: sed -e 's/import Swift$/import Swift; import _Differentiation/' %t/tmp.sil > %t/tmp_fixed.sil
14-
// RUN: %target-sil-opt %t/tmp_fixed.sil -module-name main -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
14+
// RUN: %target-sil-opt -sil-print-types %t/tmp_fixed.sil -module-name main -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
1515

1616
// IRGen test.
1717

test/AutoDiff/SIL/linear_function_inst.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Round-trip parsing/printing test.
22

3-
// RUN: %target-sil-opt %s -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
3+
// RUN: %target-sil-opt -sil-print-types %s -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
44

55
// Round-trip serialization-deserialization test.
66

77
// RUN: %empty-directory(%t)
8-
// RUN: %target-sil-opt %s -emit-sib -o %t/tmp.sib -module-name main
9-
// RUN: %target-sil-opt %t/tmp.sib -o %t/tmp.sil -module-name main
8+
// RUN: %target-sil-opt -sil-print-types %s -emit-sib -o %t/tmp.sib -module-name main
9+
// RUN: %target-sil-opt -sil-print-types %t/tmp.sib -o %t/tmp.sil -module-name main
1010

1111
// https://github.com/apple/swift/issues/54526
1212
// Workaround because import declarations are not preserved in .sib files.
1313
// RUN: sed -e 's/import Swift$/import Swift; import _Differentiation/' %t/tmp.sil > %t/tmp_fixed.sil
14-
// RUN: %target-sil-opt %t/tmp_fixed.sil -module-name main -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
14+
// RUN: %target-sil-opt -sil-print-types %t/tmp_fixed.sil -module-name main -emit-sorted-sil | %FileCheck %s --check-prefix=CHECK-SIL
1515

1616

1717
sil_stage raw

test/AutoDiff/SILGen/autodiff_builtins.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -parse-stdlib -emit-silgen %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -parse-stdlib -Xllvm -sil-print-types -emit-silgen %s | %FileCheck %s
22

33
import _Differentiation
44
import Swift

test/AutoDiff/SILGen/differentiable_function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -Xllvm -sil-print-types -emit-silgen %s | %FileCheck %s
22

33
// Test SILGen for `@differentiable` function typed values.
44

test/AutoDiff/SILGen/nil_coalescing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -Xllvm -sil-print-types -emit-sil -verify %s | %FileCheck %s
22

33
import _Differentiation
44

test/AutoDiff/SILGen/reabstraction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -Xllvm -sil-print-types -emit-silgen %s | %FileCheck %s
22

33
import _Differentiation
44

test/AutoDiff/SILGen/vtable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -Xllvm -sil-print-types -emit-silgen %s | %FileCheck %s
22

33
// Test derivative function vtable entries for `@differentiable` class members:
44
// - Methods.

test/AutoDiff/SILOptimizer/closure_specialization.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt -test-runner %s -o /dev/null 2>&1 | %FileCheck %s
1+
// RUN: %target-sil-opt -sil-print-types -test-runner %s -o /dev/null 2>&1 | %FileCheck %s
22

33
// REQUIRES: swift_in_compiler
44

test/AutoDiff/SILOptimizer/derivative_sil.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-sil -enable-experimental-forward-mode-differentiation -verify -Xllvm -sil-print-after=differentiation -o /dev/null 2>&1 %s | %FileCheck %s -check-prefix=CHECK-SIL
1+
// RUN: %target-swift-frontend -Xllvm -sil-print-types -emit-sil -enable-experimental-forward-mode-differentiation -verify -Xllvm -sil-print-after=differentiation -o /dev/null 2>&1 %s | %FileCheck %s -check-prefix=CHECK-SIL
22
// REQUIRES: asserts
33

44
// Simple generated derivative code FileCheck tests.

0 commit comments

Comments
 (0)