Skip to content

Commit 4c9658a

Browse files
committed
Merge branch 'master' of github.com:apple/swift into tensorflow-stage
* 'master' of github.com:apple/swift: [NFC] Use SILPrintContext for configuring printSIL. [NFC] Clean up construction of ExtInfo(Builder). [NFC] Add some TODO comments for cleanup. Clean up AutoClosureExpr::getUnwrappedCurryThunkExpr(). [Concurrency] Handle non-escaping partial application. [Concurrency] Prohibit partial application of actor-isolated methods.
2 parents ce79526 + 67af4ce commit 4c9658a

File tree

16 files changed

+234
-126
lines changed

16 files changed

+234
-126
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4205,6 +4205,9 @@ ERROR(actor_isolated_self_independent_context,none,
42054205
"actor-isolated %0 %1 can not be referenced from an "
42064206
"'@actorIndependent' context",
42074207
(DescriptiveDeclKind, DeclName))
4208+
ERROR(actor_isolated_partial_apply,none,
4209+
"actor-isolated %0 %1 can not be partially applied",
4210+
(DescriptiveDeclKind, DeclName))
42084211
WARNING(concurrent_access_local,none,
42094212
"local %0 %1 is unsafe to reference in code that may execute "
42104213
"concurrently",

include/swift/AST/ExtInfo.h

Lines changed: 20 additions & 4 deletions
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

Lines changed: 2 additions & 17 deletions
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

Lines changed: 2 additions & 0 deletions
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/AST/Expr.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,17 +2007,37 @@ Expr *AutoClosureExpr::getSingleExpressionBody() const {
20072007
}
20082008

20092009
Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
2010+
auto maybeUnwrapOpenExistential = [](Expr *expr) {
2011+
if (auto *openExistential = dyn_cast<OpenExistentialExpr>(expr)) {
2012+
expr = openExistential->getSubExpr()->getSemanticsProvidingExpr();
2013+
if (auto *ICE = dyn_cast<ImplicitConversionExpr>(expr))
2014+
expr = ICE->getSyntacticSubExpr();
2015+
}
2016+
2017+
return expr;
2018+
};
2019+
2020+
auto maybeUnwrapOptionalEval = [](Expr *expr) {
2021+
if (auto optEval = dyn_cast<OptionalEvaluationExpr>(expr))
2022+
expr = optEval->getSubExpr();
2023+
if (auto inject = dyn_cast<InjectIntoOptionalExpr>(expr))
2024+
expr = inject->getSubExpr();
2025+
if (auto erasure = dyn_cast<ErasureExpr>(expr))
2026+
expr = erasure->getSubExpr();
2027+
if (auto bind = dyn_cast<BindOptionalExpr>(expr))
2028+
expr = bind->getSubExpr();
2029+
return expr;
2030+
};
2031+
20102032
switch (getThunkKind()) {
20112033
case AutoClosureExpr::Kind::None:
20122034
break;
20132035

20142036
case AutoClosureExpr::Kind::SingleCurryThunk: {
20152037
auto *body = getSingleExpressionBody();
20162038
body = body->getSemanticsProvidingExpr();
2017-
2018-
if (auto *openExistential = dyn_cast<OpenExistentialExpr>(body)) {
2019-
body = openExistential->getSubExpr()->getSemanticsProvidingExpr();
2020-
}
2039+
body = maybeUnwrapOpenExistential(body);
2040+
body = maybeUnwrapOptionalEval(body);
20212041

20222042
if (auto *outerCall = dyn_cast<ApplyExpr>(body)) {
20232043
return outerCall->getFn();
@@ -2034,18 +2054,12 @@ Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
20342054
AutoClosureExpr::Kind::SingleCurryThunk);
20352055
auto *innerBody = innerClosure->getSingleExpressionBody();
20362056
innerBody = innerBody->getSemanticsProvidingExpr();
2037-
2038-
if (auto *openExistential = dyn_cast<OpenExistentialExpr>(innerBody)) {
2039-
innerBody = openExistential->getSubExpr()->getSemanticsProvidingExpr();
2040-
if (auto *ICE = dyn_cast<ImplicitConversionExpr>(innerBody))
2041-
innerBody = ICE->getSyntacticSubExpr();
2042-
}
2057+
innerBody = maybeUnwrapOpenExistential(innerBody);
2058+
innerBody = maybeUnwrapOptionalEval(innerBody);
20432059

20442060
if (auto *outerCall = dyn_cast<ApplyExpr>(innerBody)) {
20452061
if (auto *innerCall = dyn_cast<ApplyExpr>(outerCall->getFn())) {
2046-
if (auto *declRef = dyn_cast<DeclRefExpr>(innerCall->getFn())) {
2047-
return declRef;
2048-
}
2062+
return innerCall->getFn();
20492063
}
20502064
}
20512065
}

lib/SIL/IR/AbstractionPattern.cpp

Lines changed: 1 addition & 0 deletions
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

Lines changed: 22 additions & 7 deletions
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

Lines changed: 1 addition & 8 deletions
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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 2 additions & 14 deletions
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

Lines changed: 4 additions & 11 deletions
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/CSApply.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,6 @@ namespace {
925925

926926
case ValueOwnership::Owned:
927927
case ValueOwnership::Shared:
928-
// Ensure that the argument type matches up exactly.
929928
auto selfArgTy = ParenType::get(context,
930929
selfParam.getPlainType(),
931930
selfParam.getParameterFlags());

0 commit comments

Comments
 (0)