Skip to content

Commit 67af4ce

Browse files
Merge pull request swiftlang#34056 from varungandhi-apple/vg-extinfo-cleanup
[NFC] Minor cleanup related to Clang types in SIL
2 parents 14906ba + e67c178 commit 67af4ce

File tree

10 files changed

+78
-99
lines changed

10 files changed

+78
-99
lines changed

include/swift/AST/ExtInfo.h

+20-4
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ class ASTExtInfoBuilder {
275275
: bits(bits), clangTypeInfo(clangTypeInfo) {}
276276

277277
public:
278-
// Constructor with all defaults.
278+
/// An ExtInfoBuilder for a typical Swift function: @convention(swift),
279+
/// @escaping, non-throwing, non-differentiable.
279280
ASTExtInfoBuilder()
280281
: ASTExtInfoBuilder(Representation::Swift, false, false,
281282
DifferentiabilityKind::NonDifferentiable, nullptr) {}
@@ -447,6 +448,8 @@ class ASTExtInfo {
447448
};
448449

449450
public:
451+
/// An ExtInfo for a typical Swift function: @convention(swift), @escaping,
452+
/// non-throwing, non-differentiable.
450453
ASTExtInfo() : builder() { builder.checkInvariants(); };
451454

452455
/// Create a builder with the same state as \c this.
@@ -598,17 +601,22 @@ class SILExtInfoBuilder {
598601
}
599602

600603
public:
601-
// Constructor with all defaults.
602-
SILExtInfoBuilder() : SILExtInfoBuilder(0, ClangTypeInfo(nullptr)) {}
604+
/// An ExtInfoBuilder for a typical Swift function: thick, @escaping,
605+
/// non-pseudogeneric, non-differentiable.
606+
SILExtInfoBuilder()
607+
: SILExtInfoBuilder(makeBits(SILFunctionTypeRepresentation::Thick, false,
608+
false, false,
609+
DifferentiabilityKind::NonDifferentiable),
610+
ClangTypeInfo(nullptr)) {}
603611

604-
// Constructor for polymorphic type.
605612
SILExtInfoBuilder(Representation rep, bool isPseudogeneric, bool isNoEscape,
606613
bool isAsync, DifferentiabilityKind diffKind,
607614
const clang::Type *type)
608615
: SILExtInfoBuilder(makeBits(rep, isPseudogeneric, isNoEscape, isAsync,
609616
diffKind),
610617
ClangTypeInfo(type)) {}
611618

619+
// Constructor for polymorphic type.
612620
SILExtInfoBuilder(ASTExtInfoBuilder info, bool isPseudogeneric)
613621
: SILExtInfoBuilder(makeBits(info.getSILRepresentation(), isPseudogeneric,
614622
info.isNoEscape(), info.isAsync(),
@@ -686,25 +694,30 @@ class SILExtInfoBuilder {
686694

687695
// Note that we don't have setters. That is by design, use
688696
// the following with methods instead of mutating these objects.
697+
LLVM_NODISCARD
689698
SILExtInfoBuilder withRepresentation(Representation rep) const {
690699
return SILExtInfoBuilder((bits & ~RepresentationMask) | (unsigned)rep,
691700
shouldStoreClangType(rep) ? clangTypeInfo
692701
: ClangTypeInfo());
693702
}
703+
LLVM_NODISCARD
694704
SILExtInfoBuilder withIsPseudogeneric(bool isPseudogeneric = true) const {
695705
return SILExtInfoBuilder(isPseudogeneric ? (bits | PseudogenericMask)
696706
: (bits & ~PseudogenericMask),
697707
clangTypeInfo);
698708
}
709+
LLVM_NODISCARD
699710
SILExtInfoBuilder withNoEscape(bool noEscape = true) const {
700711
return SILExtInfoBuilder(noEscape ? (bits | NoEscapeMask)
701712
: (bits & ~NoEscapeMask),
702713
clangTypeInfo);
703714
}
715+
LLVM_NODISCARD
704716
SILExtInfoBuilder withAsync(bool isAsync = true) const {
705717
return SILExtInfoBuilder(isAsync ? (bits | AsyncMask) : (bits & ~AsyncMask),
706718
clangTypeInfo);
707719
}
720+
LLVM_NODISCARD
708721
SILExtInfoBuilder
709722
withDifferentiabilityKind(DifferentiabilityKind differentiability) const {
710723
return SILExtInfoBuilder(
@@ -750,13 +763,16 @@ class SILExtInfo {
750763
};
751764

752765
public:
766+
/// An ExtInfo for a typical Swift function: thick, @escaping,
767+
/// non-pseudogeneric, non-differentiable.
753768
SILExtInfo() : builder() { builder.checkInvariants(); };
754769

755770
SILExtInfo(ASTExtInfo info, bool isPseudogeneric)
756771
: builder(info.intoBuilder(), isPseudogeneric) {
757772
builder.checkInvariants();
758773
}
759774

775+
/// A default ExtInfo but with a Thin convention.
760776
static SILExtInfo getThin() {
761777
return SILExtInfoBuilder(SILExtInfoBuilder::Representation::Thin, false,
762778
false, false,

include/swift/AST/PrintOptions.h

+2-17
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ModuleDecl;
3838
enum DeclAttrKind : unsigned;
3939
class SynthesizedExtensionAnalyzer;
4040
struct PrintOptions;
41-
41+
class SILPrintContext;
4242

4343
/// Necessary information for archetype transformation during printing.
4444
struct TypeTransformContext {
@@ -594,22 +594,7 @@ struct PrintOptions {
594594
static PrintOptions printDocInterface();
595595

596596
/// Retrieve the set of options suitable for printing SIL functions.
597-
static PrintOptions printSIL(bool printFullConvention = false) {
598-
PrintOptions result;
599-
result.PrintLongAttrsOnSeparateLines = true;
600-
result.PrintStorageRepresentationAttrs = true;
601-
result.AbstractAccessors = false;
602-
result.PrintForSIL = true;
603-
result.PrintInSILBody = true;
604-
result.PreferTypeRepr = false;
605-
result.PrintIfConfig = false;
606-
result.OpaqueReturnTypePrinting =
607-
OpaqueReturnTypePrintingMode::StableReference;
608-
if (printFullConvention)
609-
result.PrintFunctionRepresentationAttrs =
610-
PrintOptions::FunctionRepresentationMode::Full;
611-
return result;
612-
}
597+
static PrintOptions printSIL(const SILPrintContext *silPrintCtx = nullptr);
613598

614599
static PrintOptions printQualifiedSILType() {
615600
PrintOptions result = PrintOptions::printSIL();

lib/AST/ASTDumper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -3792,6 +3792,7 @@ namespace {
37923792

37933793
OS << "\n";
37943794
Indent += 2;
3795+
// [TODO: Improve-Clang-type-printing]
37953796
if (!T->getClangTypeInfo().empty()) {
37963797
std::string s;
37973798
llvm::raw_string_ostream os(s);
@@ -3842,6 +3843,7 @@ namespace {
38423843
OS << '\n';
38433844
T->getInvocationSubstitutions().dump(OS, SubstitutionMap::DumpStyle::Full,
38443845
Indent+2);
3846+
// [TODO: Improve-Clang-type-printing]
38453847
if (!T->getClangTypeInfo().empty()) {
38463848
std::string s;
38473849
llvm::raw_string_ostream os(s);

lib/SIL/IR/AbstractionPattern.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ void AbstractionPattern::print(raw_ostream &out) const {
827827
}
828828
getType().dump(out);
829829
out << ", ";
830+
// [TODO: Improve-Clang-type-printing]
830831
// It would be better to use print, but we need a PrintingPolicy
831832
// for that, for which we need a clang LangOptions, and... ugh.
832833
clang::QualType(getClangType(), 0).dump();

lib/SIL/IR/SILPrinter.cpp

+22-7
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ void SILType::dump() const {
459459
static void printSILFunctionNameAndType(
460460
llvm::raw_ostream &OS, const SILFunction *function,
461461
llvm::DenseMap<CanType, Identifier> &sugaredTypeNames,
462-
bool printFullConvention = false) {
462+
const SILPrintContext *silPrintContext = nullptr) {
463463
function->printName(OS);
464464
OS << " : $";
465465
auto *genEnv = function->getGenericEnvironment();
@@ -496,7 +496,7 @@ static void printSILFunctionNameAndType(
496496
sugaredTypeNames[archetypeTy->getCanonicalType()] = name;
497497
}
498498
}
499-
auto printOptions = PrintOptions::printSIL(printFullConvention);
499+
auto printOptions = PrintOptions::printSIL(silPrintContext);
500500
printOptions.GenericSig = genSig;
501501
printOptions.AlternativeTypeNames =
502502
sugaredTypeNames.empty() ? nullptr : &sugaredTypeNames;
@@ -571,8 +571,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
571571
SILPrintContext &PrintCtx,
572572
llvm::DenseMap<CanType, Identifier> *AlternativeTypeNames = nullptr)
573573
: Ctx(PrintCtx), PrintState{{PrintCtx.OS()},
574-
PrintOptions::printSIL(
575-
PrintCtx.printFullConvention())},
574+
PrintOptions::printSIL(&PrintCtx)},
576575
LastBufferID(0) {
577576
PrintState.ASTOptions.AlternativeTypeNames = AlternativeTypeNames;
578577
PrintState.ASTOptions.PrintForSIL = true;
@@ -2685,8 +2684,7 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
26852684
OS << "[ossa] ";
26862685

26872686
llvm::DenseMap<CanType, Identifier> sugaredTypeNames;
2688-
printSILFunctionNameAndType(OS, this, sugaredTypeNames,
2689-
PrintCtx.printFullConvention());
2687+
printSILFunctionNameAndType(OS, this, sugaredTypeNames, &PrintCtx);
26902688

26912689
if (!isExternalDeclaration()) {
26922690
if (auto eCount = getEntryCount()) {
@@ -2971,7 +2969,7 @@ static void printFileIDMap(SILPrintContext &Ctx, const FileIDMap map) {
29712969
}
29722970

29732971
void SILProperty::print(SILPrintContext &Ctx) const {
2974-
PrintOptions Options = PrintOptions::printSIL(Ctx.printFullConvention());
2972+
PrintOptions Options = PrintOptions::printSIL(&Ctx);
29752973

29762974
auto &OS = Ctx.OS();
29772975
OS << "sil_property ";
@@ -3612,3 +3610,20 @@ ID SILPrintContext::getID(const SILNode *node) {
36123610
ID R = {ID::SSAValue, ValueToIDMap[node]};
36133611
return R;
36143612
}
3613+
3614+
PrintOptions PrintOptions::printSIL(const SILPrintContext *ctx) {
3615+
PrintOptions result;
3616+
result.PrintLongAttrsOnSeparateLines = true;
3617+
result.PrintStorageRepresentationAttrs = true;
3618+
result.AbstractAccessors = false;
3619+
result.PrintForSIL = true;
3620+
result.PrintInSILBody = true;
3621+
result.PreferTypeRepr = false;
3622+
result.PrintIfConfig = false;
3623+
result.OpaqueReturnTypePrinting =
3624+
OpaqueReturnTypePrintingMode::StableReference;
3625+
if (ctx && ctx->printFullConvention())
3626+
result.PrintFunctionRepresentationAttrs =
3627+
PrintOptions::FunctionRepresentationMode::Full;
3628+
return result;
3629+
}

lib/SILGen/SILGen.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,7 @@ SILGenModule::getKeyPathProjectionCoroutine(bool isReadAccess,
411411
: ParameterConvention::Indirect_In_Guaranteed },
412412
};
413413

414-
auto extInfo =
415-
SILFunctionType::ExtInfoBuilder(SILFunctionTypeRepresentation::Thin,
416-
/*pseudogeneric*/ false,
417-
/*non-escaping*/ false,
418-
/*async*/ false,
419-
DifferentiabilityKind::NonDifferentiable,
420-
/*clangFunctionType*/ nullptr)
421-
.build();
414+
auto extInfo = SILFunctionType::ExtInfo::getThin();
422415

423416
auto functionTy = SILFunctionType::get(sig, extInfo,
424417
SILCoroutineKind::YieldOnce,

lib/SILGen/SILGenFunction.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,10 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
696696
};
697697
auto NSApplicationMainType = SILFunctionType::get(
698698
nullptr,
699-
SILFunctionType::ExtInfoBuilder()
700-
// Should be C calling convention, but NSApplicationMain
701-
// has an overlay to fix the type of argv.
702-
.withRepresentation(SILFunctionType::Representation::Thin)
703-
.build(),
704-
SILCoroutineKind::None, ParameterConvention::Direct_Unowned, argTypes,
699+
// Should be C calling convention, but NSApplicationMain
700+
// has an overlay to fix the type of argv.
701+
SILFunctionType::ExtInfo::getThin(), SILCoroutineKind::None,
702+
ParameterConvention::Direct_Unowned, argTypes,
705703
/*yields*/ {},
706704
SILResultInfo(argc->getType().getASTType(), ResultConvention::Unowned),
707705
/*error result*/ None, SubstitutionMap(), SubstitutionMap(),

lib/SILOptimizer/Transforms/Outliner.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,7 @@ CanSILFunctionType BridgedProperty::getOutlinedFunctionType(SILModule &M) {
295295
Results.push_back(SILResultInfo(
296296
switchInfo.Br->getArg(0)->getType().getASTType(),
297297
ResultConvention::Owned));
298-
auto ExtInfo = SILFunctionType::ExtInfoBuilder(
299-
SILFunctionType::Representation::Thin,
300-
/*pseudogeneric*/ false, /*noescape*/ false,
301-
/*async*/ false, DifferentiabilityKind::NonDifferentiable,
302-
/*clangFunctionType*/ nullptr)
303-
.build();
298+
auto ExtInfo = SILFunctionType::ExtInfo::getThin();
304299
auto FunctionType = SILFunctionType::get(
305300
nullptr, ExtInfo, SILCoroutineKind::None,
306301
ParameterConvention::Direct_Unowned, Parameters, /*yields*/ {},
@@ -1177,14 +1172,7 @@ CanSILFunctionType ObjCMethodCall::getOutlinedFunctionType(SILModule &M) {
11771172
++OrigSigIdx;
11781173
}
11791174

1180-
auto ExtInfo =
1181-
SILFunctionType::ExtInfoBuilder(SILFunctionType::Representation::Thin,
1182-
/*pseudogeneric*/ false,
1183-
/*noescape*/ false,
1184-
/*async*/ false,
1185-
DifferentiabilityKind::NonDifferentiable,
1186-
/*clangFunctionType*/ nullptr)
1187-
.build();
1175+
auto ExtInfo = SILFunctionType::ExtInfo::getThin();
11881176

11891177
SmallVector<SILResultInfo, 4> Results;
11901178
// If we don't have a bridged return we changed from @autoreleased to @owned

lib/SILOptimizer/UtilityPasses/BugReducerTester.cpp

+4-11
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,10 @@ class BugReducerTester : public SILFunctionTransform {
8383
ResultInfoArray.push_back(
8484
SILResultInfo(EmptyTupleCanType, ResultConvention::Unowned));
8585
auto FuncType = SILFunctionType::get(
86-
nullptr,
87-
SILFunctionType::ExtInfoBuilder(
88-
SILFunctionType::Representation::Thin, false /*isPseudoGeneric*/,
89-
false /*noescape*/, false /*async*/,
90-
DifferentiabilityKind::NonDifferentiable,
91-
nullptr /*clangFunctionType*/)
92-
.build(),
93-
SILCoroutineKind::None, ParameterConvention::Direct_Unowned,
94-
ArrayRef<SILParameterInfo>(), ArrayRef<SILYieldInfo>(), ResultInfoArray,
95-
None, SubstitutionMap(), SubstitutionMap(),
96-
getFunction()->getModule().getASTContext());
86+
nullptr, SILFunctionType::ExtInfo::getThin(), SILCoroutineKind::None,
87+
ParameterConvention::Direct_Unowned, ArrayRef<SILParameterInfo>(),
88+
ArrayRef<SILYieldInfo>(), ResultInfoArray, None, SubstitutionMap(),
89+
SubstitutionMap(), getFunction()->getModule().getASTContext());
9790

9891
SILOptFunctionBuilder FunctionBuilder(*this);
9992
SILFunction *F = FunctionBuilder.getOrCreateSharedFunction(

lib/Sema/ConstraintSystem.cpp

+20-32
Original file line numberDiff line numberDiff line change
@@ -1889,27 +1889,21 @@ static std::pair<Type, Type> getTypeOfReferenceWithSpecialTypeCheckingSemantics(
18891889
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
18901890
TVO_CanBindToNoEscape);
18911891
FunctionType::Param arg(escapeClosure);
1892-
auto bodyClosure = FunctionType::get(
1893-
arg, result,
1894-
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
1895-
/*noescape*/ true,
1896-
/*throws*/ true,
1897-
DifferentiabilityKind::NonDifferentiable,
1898-
/*clangFunctionType*/ nullptr)
1899-
.build());
1892+
auto bodyClosure = FunctionType::get(arg, result,
1893+
FunctionType::ExtInfoBuilder()
1894+
.withNoEscape(true)
1895+
.withThrows(true)
1896+
.build());
19001897
FunctionType::Param args[] = {
19011898
FunctionType::Param(noescapeClosure),
19021899
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
19031900
};
19041901

1905-
auto refType = FunctionType::get(
1906-
args, result,
1907-
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
1908-
/*noescape*/ false,
1909-
/*throws*/ true,
1910-
DifferentiabilityKind::NonDifferentiable,
1911-
/*clangFunctionType*/ nullptr)
1912-
.build());
1902+
auto refType = FunctionType::get(args, result,
1903+
FunctionType::ExtInfoBuilder()
1904+
.withNoEscape(false)
1905+
.withThrows(true)
1906+
.build());
19131907
return {refType, refType};
19141908
}
19151909
case DeclTypeCheckingSemantics::OpenExistential: {
@@ -1928,26 +1922,20 @@ static std::pair<Type, Type> getTypeOfReferenceWithSpecialTypeCheckingSemantics(
19281922
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
19291923
TVO_CanBindToNoEscape);
19301924
FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)};
1931-
auto bodyClosure = FunctionType::get(
1932-
bodyArgs, result,
1933-
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
1934-
/*noescape*/ true,
1935-
/*throws*/ true,
1936-
DifferentiabilityKind::NonDifferentiable,
1937-
/*clangFunctionType*/ nullptr)
1938-
.build());
1925+
auto bodyClosure = FunctionType::get(bodyArgs, result,
1926+
FunctionType::ExtInfoBuilder()
1927+
.withNoEscape(true)
1928+
.withThrows(true)
1929+
.build());
19391930
FunctionType::Param args[] = {
19401931
FunctionType::Param(existentialTy),
19411932
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
19421933
};
1943-
auto refType = FunctionType::get(
1944-
args, result,
1945-
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
1946-
/*noescape*/ false,
1947-
/*throws*/ true,
1948-
DifferentiabilityKind::NonDifferentiable,
1949-
/*clangFunctionType*/ nullptr)
1950-
.build());
1934+
auto refType = FunctionType::get(args, result,
1935+
FunctionType::ExtInfoBuilder()
1936+
.withNoEscape(false)
1937+
.withThrows(true)
1938+
.build());
19511939
return {refType, refType};
19521940
}
19531941
}

0 commit comments

Comments
 (0)