From abce3de41ca14423ea044288c34dbc42c8222708 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Fri, 21 Aug 2020 11:08:42 -0700 Subject: [PATCH 1/3] [NFC] Add some TODO comments for cleanup. --- lib/AST/ASTDumper.cpp | 2 ++ lib/SIL/IR/AbstractionPattern.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/lib/AST/ASTDumper.cpp b/lib/AST/ASTDumper.cpp index 9dec0089db721..91b809e881ed5 100644 --- a/lib/AST/ASTDumper.cpp +++ b/lib/AST/ASTDumper.cpp @@ -3792,6 +3792,7 @@ namespace { OS << "\n"; Indent += 2; + // [TODO: Improve-Clang-type-printing] if (!T->getClangTypeInfo().empty()) { std::string s; llvm::raw_string_ostream os(s); @@ -3842,6 +3843,7 @@ namespace { OS << '\n'; T->getInvocationSubstitutions().dump(OS, SubstitutionMap::DumpStyle::Full, Indent+2); + // [TODO: Improve-Clang-type-printing] if (!T->getClangTypeInfo().empty()) { std::string s; llvm::raw_string_ostream os(s); diff --git a/lib/SIL/IR/AbstractionPattern.cpp b/lib/SIL/IR/AbstractionPattern.cpp index 5592dd1d3bef4..3a87c9029189c 100644 --- a/lib/SIL/IR/AbstractionPattern.cpp +++ b/lib/SIL/IR/AbstractionPattern.cpp @@ -827,6 +827,7 @@ void AbstractionPattern::print(raw_ostream &out) const { } getType().dump(out); out << ", "; + // [TODO: Improve-Clang-type-printing] // It would be better to use print, but we need a PrintingPolicy // for that, for which we need a clang LangOptions, and... ugh. clang::QualType(getClangType(), 0).dump(); From 6dfdb7b5482fac56c277e8d518b27042283446a2 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Mon, 31 Aug 2020 17:29:07 -0700 Subject: [PATCH 2/3] [NFC] Clean up construction of ExtInfo(Builder). --- include/swift/AST/ExtInfo.h | 24 +++++++-- lib/SILGen/SILGen.cpp | 9 +--- lib/SILGen/SILGenFunction.cpp | 10 ++-- lib/SILOptimizer/Transforms/Outliner.cpp | 16 +----- .../UtilityPasses/BugReducerTester.cpp | 15 ++---- lib/Sema/ConstraintSystem.cpp | 52 +++++++------------ 6 files changed, 51 insertions(+), 75 deletions(-) diff --git a/include/swift/AST/ExtInfo.h b/include/swift/AST/ExtInfo.h index 4216002504520..ffea1db284b01 100644 --- a/include/swift/AST/ExtInfo.h +++ b/include/swift/AST/ExtInfo.h @@ -275,7 +275,8 @@ class ASTExtInfoBuilder { : bits(bits), clangTypeInfo(clangTypeInfo) {} public: - // Constructor with all defaults. + /// An ExtInfoBuilder for a typical Swift function: @convention(swift), + /// @escaping, non-throwing, non-differentiable. ASTExtInfoBuilder() : ASTExtInfoBuilder(Representation::Swift, false, false, DifferentiabilityKind::NonDifferentiable, nullptr) {} @@ -447,6 +448,8 @@ class ASTExtInfo { }; public: + /// An ExtInfo for a typical Swift function: @convention(swift), @escaping, + /// non-throwing, non-differentiable. ASTExtInfo() : builder() { builder.checkInvariants(); }; /// Create a builder with the same state as \c this. @@ -598,10 +601,14 @@ class SILExtInfoBuilder { } public: - // Constructor with all defaults. - SILExtInfoBuilder() : SILExtInfoBuilder(0, ClangTypeInfo(nullptr)) {} + /// An ExtInfoBuilder for a typical Swift function: thick, @escaping, + /// non-pseudogeneric, non-differentiable. + SILExtInfoBuilder() + : SILExtInfoBuilder(makeBits(SILFunctionTypeRepresentation::Thick, false, + false, false, + DifferentiabilityKind::NonDifferentiable), + ClangTypeInfo(nullptr)) {} - // Constructor for polymorphic type. SILExtInfoBuilder(Representation rep, bool isPseudogeneric, bool isNoEscape, bool isAsync, DifferentiabilityKind diffKind, const clang::Type *type) @@ -609,6 +616,7 @@ class SILExtInfoBuilder { diffKind), ClangTypeInfo(type)) {} + // Constructor for polymorphic type. SILExtInfoBuilder(ASTExtInfoBuilder info, bool isPseudogeneric) : SILExtInfoBuilder(makeBits(info.getSILRepresentation(), isPseudogeneric, info.isNoEscape(), info.isAsync(), @@ -686,25 +694,30 @@ class SILExtInfoBuilder { // Note that we don't have setters. That is by design, use // the following with methods instead of mutating these objects. + LLVM_NODISCARD SILExtInfoBuilder withRepresentation(Representation rep) const { return SILExtInfoBuilder((bits & ~RepresentationMask) | (unsigned)rep, shouldStoreClangType(rep) ? clangTypeInfo : ClangTypeInfo()); } + LLVM_NODISCARD SILExtInfoBuilder withIsPseudogeneric(bool isPseudogeneric = true) const { return SILExtInfoBuilder(isPseudogeneric ? (bits | PseudogenericMask) : (bits & ~PseudogenericMask), clangTypeInfo); } + LLVM_NODISCARD SILExtInfoBuilder withNoEscape(bool noEscape = true) const { return SILExtInfoBuilder(noEscape ? (bits | NoEscapeMask) : (bits & ~NoEscapeMask), clangTypeInfo); } + LLVM_NODISCARD SILExtInfoBuilder withAsync(bool isAsync = true) const { return SILExtInfoBuilder(isAsync ? (bits | AsyncMask) : (bits & ~AsyncMask), clangTypeInfo); } + LLVM_NODISCARD SILExtInfoBuilder withDifferentiabilityKind(DifferentiabilityKind differentiability) const { return SILExtInfoBuilder( @@ -750,6 +763,8 @@ class SILExtInfo { }; public: + /// An ExtInfo for a typical Swift function: thick, @escaping, + /// non-pseudogeneric, non-differentiable. SILExtInfo() : builder() { builder.checkInvariants(); }; SILExtInfo(ASTExtInfo info, bool isPseudogeneric) @@ -757,6 +772,7 @@ class SILExtInfo { builder.checkInvariants(); } + /// A default ExtInfo but with a Thin convention. static SILExtInfo getThin() { return SILExtInfoBuilder(SILExtInfoBuilder::Representation::Thin, false, false, false, diff --git a/lib/SILGen/SILGen.cpp b/lib/SILGen/SILGen.cpp index 83639dc8bfa0f..df9f0e13b97fd 100644 --- a/lib/SILGen/SILGen.cpp +++ b/lib/SILGen/SILGen.cpp @@ -411,14 +411,7 @@ SILGenModule::getKeyPathProjectionCoroutine(bool isReadAccess, : ParameterConvention::Indirect_In_Guaranteed }, }; - auto extInfo = - SILFunctionType::ExtInfoBuilder(SILFunctionTypeRepresentation::Thin, - /*pseudogeneric*/ false, - /*non-escaping*/ false, - /*async*/ false, - DifferentiabilityKind::NonDifferentiable, - /*clangFunctionType*/ nullptr) - .build(); + auto extInfo = SILFunctionType::ExtInfo::getThin(); auto functionTy = SILFunctionType::get(sig, extInfo, SILCoroutineKind::YieldOnce, diff --git a/lib/SILGen/SILGenFunction.cpp b/lib/SILGen/SILGenFunction.cpp index b9672b021bb89..006a46461f3b4 100644 --- a/lib/SILGen/SILGenFunction.cpp +++ b/lib/SILGen/SILGenFunction.cpp @@ -696,12 +696,10 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) { }; auto NSApplicationMainType = SILFunctionType::get( nullptr, - SILFunctionType::ExtInfoBuilder() - // Should be C calling convention, but NSApplicationMain - // has an overlay to fix the type of argv. - .withRepresentation(SILFunctionType::Representation::Thin) - .build(), - SILCoroutineKind::None, ParameterConvention::Direct_Unowned, argTypes, + // Should be C calling convention, but NSApplicationMain + // has an overlay to fix the type of argv. + SILFunctionType::ExtInfo::getThin(), SILCoroutineKind::None, + ParameterConvention::Direct_Unowned, argTypes, /*yields*/ {}, SILResultInfo(argc->getType().getASTType(), ResultConvention::Unowned), /*error result*/ None, SubstitutionMap(), SubstitutionMap(), diff --git a/lib/SILOptimizer/Transforms/Outliner.cpp b/lib/SILOptimizer/Transforms/Outliner.cpp index b5a89537fbe0e..8b519c32e2203 100644 --- a/lib/SILOptimizer/Transforms/Outliner.cpp +++ b/lib/SILOptimizer/Transforms/Outliner.cpp @@ -295,12 +295,7 @@ CanSILFunctionType BridgedProperty::getOutlinedFunctionType(SILModule &M) { Results.push_back(SILResultInfo( switchInfo.Br->getArg(0)->getType().getASTType(), ResultConvention::Owned)); - auto ExtInfo = SILFunctionType::ExtInfoBuilder( - SILFunctionType::Representation::Thin, - /*pseudogeneric*/ false, /*noescape*/ false, - /*async*/ false, DifferentiabilityKind::NonDifferentiable, - /*clangFunctionType*/ nullptr) - .build(); + auto ExtInfo = SILFunctionType::ExtInfo::getThin(); auto FunctionType = SILFunctionType::get( nullptr, ExtInfo, SILCoroutineKind::None, ParameterConvention::Direct_Unowned, Parameters, /*yields*/ {}, @@ -1177,14 +1172,7 @@ CanSILFunctionType ObjCMethodCall::getOutlinedFunctionType(SILModule &M) { ++OrigSigIdx; } - auto ExtInfo = - SILFunctionType::ExtInfoBuilder(SILFunctionType::Representation::Thin, - /*pseudogeneric*/ false, - /*noescape*/ false, - /*async*/ false, - DifferentiabilityKind::NonDifferentiable, - /*clangFunctionType*/ nullptr) - .build(); + auto ExtInfo = SILFunctionType::ExtInfo::getThin(); SmallVector Results; // If we don't have a bridged return we changed from @autoreleased to @owned diff --git a/lib/SILOptimizer/UtilityPasses/BugReducerTester.cpp b/lib/SILOptimizer/UtilityPasses/BugReducerTester.cpp index 9ef7e2d70befd..adae7464eb0f6 100644 --- a/lib/SILOptimizer/UtilityPasses/BugReducerTester.cpp +++ b/lib/SILOptimizer/UtilityPasses/BugReducerTester.cpp @@ -83,17 +83,10 @@ class BugReducerTester : public SILFunctionTransform { ResultInfoArray.push_back( SILResultInfo(EmptyTupleCanType, ResultConvention::Unowned)); auto FuncType = SILFunctionType::get( - nullptr, - SILFunctionType::ExtInfoBuilder( - SILFunctionType::Representation::Thin, false /*isPseudoGeneric*/, - false /*noescape*/, false /*async*/, - DifferentiabilityKind::NonDifferentiable, - nullptr /*clangFunctionType*/) - .build(), - SILCoroutineKind::None, ParameterConvention::Direct_Unowned, - ArrayRef(), ArrayRef(), ResultInfoArray, - None, SubstitutionMap(), SubstitutionMap(), - getFunction()->getModule().getASTContext()); + nullptr, SILFunctionType::ExtInfo::getThin(), SILCoroutineKind::None, + ParameterConvention::Direct_Unowned, ArrayRef(), + ArrayRef(), ResultInfoArray, None, SubstitutionMap(), + SubstitutionMap(), getFunction()->getModule().getASTContext()); SILOptFunctionBuilder FunctionBuilder(*this); SILFunction *F = FunctionBuilder.getOrCreateSharedFunction( diff --git a/lib/Sema/ConstraintSystem.cpp b/lib/Sema/ConstraintSystem.cpp index bad871b49e8af..730de34b54935 100644 --- a/lib/Sema/ConstraintSystem.cpp +++ b/lib/Sema/ConstraintSystem.cpp @@ -1889,27 +1889,21 @@ static std::pair getTypeOfReferenceWithSpecialTypeCheckingSemantics( CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult), TVO_CanBindToNoEscape); FunctionType::Param arg(escapeClosure); - auto bodyClosure = FunctionType::get( - arg, result, - FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift, - /*noescape*/ true, - /*throws*/ true, - DifferentiabilityKind::NonDifferentiable, - /*clangFunctionType*/ nullptr) - .build()); + auto bodyClosure = FunctionType::get(arg, result, + FunctionType::ExtInfoBuilder() + .withNoEscape(true) + .withThrows(true) + .build()); FunctionType::Param args[] = { FunctionType::Param(noescapeClosure), FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")), }; - auto refType = FunctionType::get( - args, result, - FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift, - /*noescape*/ false, - /*throws*/ true, - DifferentiabilityKind::NonDifferentiable, - /*clangFunctionType*/ nullptr) - .build()); + auto refType = FunctionType::get(args, result, + FunctionType::ExtInfoBuilder() + .withNoEscape(false) + .withThrows(true) + .build()); return {refType, refType}; } case DeclTypeCheckingSemantics::OpenExistential: { @@ -1928,26 +1922,20 @@ static std::pair getTypeOfReferenceWithSpecialTypeCheckingSemantics( CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult), TVO_CanBindToNoEscape); FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)}; - auto bodyClosure = FunctionType::get( - bodyArgs, result, - FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift, - /*noescape*/ true, - /*throws*/ true, - DifferentiabilityKind::NonDifferentiable, - /*clangFunctionType*/ nullptr) - .build()); + auto bodyClosure = FunctionType::get(bodyArgs, result, + FunctionType::ExtInfoBuilder() + .withNoEscape(true) + .withThrows(true) + .build()); FunctionType::Param args[] = { FunctionType::Param(existentialTy), FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")), }; - auto refType = FunctionType::get( - args, result, - FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift, - /*noescape*/ false, - /*throws*/ true, - DifferentiabilityKind::NonDifferentiable, - /*clangFunctionType*/ nullptr) - .build()); + auto refType = FunctionType::get(args, result, + FunctionType::ExtInfoBuilder() + .withNoEscape(false) + .withThrows(true) + .build()); return {refType, refType}; } } From e67c178eb1a97cedf977a224eca73c68cc0f86fd Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Thu, 24 Sep 2020 00:50:34 -0700 Subject: [PATCH 3/3] [NFC] Use SILPrintContext for configuring printSIL. --- include/swift/AST/PrintOptions.h | 19 ++----------------- lib/SIL/IR/SILPrinter.cpp | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/include/swift/AST/PrintOptions.h b/include/swift/AST/PrintOptions.h index 1e968ac989c1b..ebdf2db947a5e 100644 --- a/include/swift/AST/PrintOptions.h +++ b/include/swift/AST/PrintOptions.h @@ -38,7 +38,7 @@ class ModuleDecl; enum DeclAttrKind : unsigned; class SynthesizedExtensionAnalyzer; struct PrintOptions; - +class SILPrintContext; /// Necessary information for archetype transformation during printing. struct TypeTransformContext { @@ -594,22 +594,7 @@ struct PrintOptions { static PrintOptions printDocInterface(); /// Retrieve the set of options suitable for printing SIL functions. - static PrintOptions printSIL(bool printFullConvention = false) { - PrintOptions result; - result.PrintLongAttrsOnSeparateLines = true; - result.PrintStorageRepresentationAttrs = true; - result.AbstractAccessors = false; - result.PrintForSIL = true; - result.PrintInSILBody = true; - result.PreferTypeRepr = false; - result.PrintIfConfig = false; - result.OpaqueReturnTypePrinting = - OpaqueReturnTypePrintingMode::StableReference; - if (printFullConvention) - result.PrintFunctionRepresentationAttrs = - PrintOptions::FunctionRepresentationMode::Full; - return result; - } + static PrintOptions printSIL(const SILPrintContext *silPrintCtx = nullptr); static PrintOptions printQualifiedSILType() { PrintOptions result = PrintOptions::printSIL(); diff --git a/lib/SIL/IR/SILPrinter.cpp b/lib/SIL/IR/SILPrinter.cpp index 3fe377a57792e..81207e5fd5306 100644 --- a/lib/SIL/IR/SILPrinter.cpp +++ b/lib/SIL/IR/SILPrinter.cpp @@ -459,7 +459,7 @@ void SILType::dump() const { static void printSILFunctionNameAndType( llvm::raw_ostream &OS, const SILFunction *function, llvm::DenseMap &sugaredTypeNames, - bool printFullConvention = false) { + const SILPrintContext *silPrintContext = nullptr) { function->printName(OS); OS << " : $"; auto *genEnv = function->getGenericEnvironment(); @@ -496,7 +496,7 @@ static void printSILFunctionNameAndType( sugaredTypeNames[archetypeTy->getCanonicalType()] = name; } } - auto printOptions = PrintOptions::printSIL(printFullConvention); + auto printOptions = PrintOptions::printSIL(silPrintContext); printOptions.GenericSig = genSig; printOptions.AlternativeTypeNames = sugaredTypeNames.empty() ? nullptr : &sugaredTypeNames; @@ -571,8 +571,7 @@ class SILPrinter : public SILInstructionVisitor { SILPrintContext &PrintCtx, llvm::DenseMap *AlternativeTypeNames = nullptr) : Ctx(PrintCtx), PrintState{{PrintCtx.OS()}, - PrintOptions::printSIL( - PrintCtx.printFullConvention())}, + PrintOptions::printSIL(&PrintCtx)}, LastBufferID(0) { PrintState.ASTOptions.AlternativeTypeNames = AlternativeTypeNames; PrintState.ASTOptions.PrintForSIL = true; @@ -2685,8 +2684,7 @@ void SILFunction::print(SILPrintContext &PrintCtx) const { OS << "[ossa] "; llvm::DenseMap sugaredTypeNames; - printSILFunctionNameAndType(OS, this, sugaredTypeNames, - PrintCtx.printFullConvention()); + printSILFunctionNameAndType(OS, this, sugaredTypeNames, &PrintCtx); if (!isExternalDeclaration()) { if (auto eCount = getEntryCount()) { @@ -2971,7 +2969,7 @@ static void printFileIDMap(SILPrintContext &Ctx, const FileIDMap map) { } void SILProperty::print(SILPrintContext &Ctx) const { - PrintOptions Options = PrintOptions::printSIL(Ctx.printFullConvention()); + PrintOptions Options = PrintOptions::printSIL(&Ctx); auto &OS = Ctx.OS(); OS << "sil_property "; @@ -3612,3 +3610,20 @@ ID SILPrintContext::getID(const SILNode *node) { ID R = {ID::SSAValue, ValueToIDMap[node]}; return R; } + +PrintOptions PrintOptions::printSIL(const SILPrintContext *ctx) { + PrintOptions result; + result.PrintLongAttrsOnSeparateLines = true; + result.PrintStorageRepresentationAttrs = true; + result.AbstractAccessors = false; + result.PrintForSIL = true; + result.PrintInSILBody = true; + result.PreferTypeRepr = false; + result.PrintIfConfig = false; + result.OpaqueReturnTypePrinting = + OpaqueReturnTypePrintingMode::StableReference; + if (ctx && ctx->printFullConvention()) + result.PrintFunctionRepresentationAttrs = + PrintOptions::FunctionRepresentationMode::Full; + return result; +}