Skip to content

Commit f4b0cd9

Browse files
authored
Merge pull request #1397 from swiftwasm/release/5.3
[pull] swiftwasm-release/5.3 from release/5.3
2 parents b8d6c85 + 4d144e6 commit f4b0cd9

File tree

17 files changed

+404
-159
lines changed

17 files changed

+404
-159
lines changed

include/swift/AST/Builtins.def

+29
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,35 @@ BUILTIN_SIL_OPERATION(IsUnique, "isUnique", Special)
429429
/// BridgeObject to be treated as a native object by the runtime.
430430
BUILTIN_SIL_OPERATION(IsUnique_native, "isUnique_native", Special)
431431

432+
/// beginCOWMutation<T : AnyObject>(inout T) -> Int1
433+
///
434+
/// Begins a copy-on-write mutation for a buffer reference which is passed as
435+
/// inout argument. It returns a true if the buffer is uniquely referenced.
436+
/// In this case the buffer may be mutated after calling this builtin.
437+
///
438+
/// The beginCOWMutation builtin is very similar to isUnique. It just translates
439+
/// to a different SIL instruction (begin_cow_mutation), which is the preferred
440+
/// representation of COW in SIL.
441+
BUILTIN_SIL_OPERATION(BeginCOWMutation, "beginCOWMutation", Special)
442+
443+
/// beginCOWMutation_native<T : AnyObject>(inout T) -> Int1
444+
///
445+
/// Like beginCOWMutation, but it's assumed that T has native Swift reference
446+
/// counting.
447+
BUILTIN_SIL_OPERATION(BeginCOWMutation_native, "beginCOWMutation_native", Special)
448+
449+
/// endCOWMutation<T : AnyObject>(inout T)
450+
///
451+
/// Ends a copy-on-write mutation for a buffer reference which is passed as
452+
/// inout argument. After calling this builtin, the buffer must not be mutated.
453+
BUILTIN_SIL_OPERATION(EndCOWMutation, "endCOWMutation", Special)
454+
455+
/// COWBufferForReading has type <T: AnyObject> T -> T
456+
///
457+
/// Returns the buffer reference which is passed as argument.
458+
/// This builtin indicates to the optimizer that the buffer is not mutable.
459+
BUILTIN_SIL_OPERATION(COWBufferForReading, "COWBufferForReading", Special)
460+
432461
/// bindMemory : <T> (Builtin.RawPointer, Builtin.Word, T.Type) -> ()
433462
BUILTIN_SIL_OPERATION(BindMemory, "bindMemory", Special)
434463

include/swift/Demangling/Demangler.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ class Demangler : public NodeFactory {
564564
NodePointer demangleValueWitness();
565565

566566
NodePointer demangleTypeMangling();
567-
NodePointer demangleSymbolicReference(unsigned char rawKind,
568-
const void *at);
567+
NodePointer demangleSymbolicReference(unsigned char rawKind);
569568

570569
bool demangleBoundGenerics(Vector<NodePointer> &TypeListList,
571570
NodePointer &RetroactiveConformances);

lib/AST/Builtins.cpp

+46-3
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,26 @@ createGenericParam(ASTContext &ctx, const char *name, unsigned index) {
431431

432432
/// Create a generic parameter list with multiple generic parameters.
433433
static GenericParamList *getGenericParams(ASTContext &ctx,
434-
unsigned numParameters) {
434+
unsigned numParameters,
435+
bool isAnyObject) {
435436
assert(numParameters <= llvm::array_lengthof(GenericParamNames));
436437

437438
SmallVector<GenericTypeParamDecl*, 2> genericParams;
438439
for (unsigned i = 0; i != numParameters; ++i)
439440
genericParams.push_back(createGenericParam(ctx, GenericParamNames[i], i));
440441

442+
443+
if (isAnyObject) {
444+
CanType ao = ctx.getAnyObjectType();
445+
SmallVector<RequirementRepr, 1> req;
446+
req.push_back(RequirementRepr::getTypeConstraint(TypeLoc::withoutLoc(genericParams[0]->getInterfaceType()), SourceLoc(),
447+
TypeLoc::withoutLoc(ao)));
448+
449+
auto paramList = GenericParamList::create(ctx, SourceLoc(), genericParams,
450+
SourceLoc(), req, SourceLoc());
451+
return paramList;
452+
}
453+
441454
auto paramList = GenericParamList::create(ctx, SourceLoc(), genericParams,
442455
SourceLoc());
443456
return paramList;
@@ -460,9 +473,10 @@ namespace {
460473
SmallVector<Requirement, 2> addedRequirements;
461474

462475
public:
463-
BuiltinFunctionBuilder(ASTContext &ctx, unsigned numGenericParams = 1)
476+
BuiltinFunctionBuilder(ASTContext &ctx, unsigned numGenericParams = 1,
477+
bool isAnyObject = false)
464478
: Context(ctx) {
465-
TheGenericParamList = getGenericParams(ctx, numGenericParams);
479+
TheGenericParamList = getGenericParams(ctx, numGenericParams, isAnyObject);
466480
for (auto gp : TheGenericParamList->getParams()) {
467481
genericParamTypes.push_back(
468482
gp->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());
@@ -645,6 +659,14 @@ static ValueDecl *getIsUniqueOperation(ASTContext &Context, Identifier Id) {
645659
return builder.build(Id);
646660
}
647661

662+
static ValueDecl *getEndCOWMutation(ASTContext &Context, Identifier Id) {
663+
// <T> (@inout T) -> ()
664+
BuiltinFunctionBuilder builder(Context);
665+
builder.addParameter(makeGenericParam(), ValueOwnership::InOut);
666+
builder.setResult(makeConcrete(TupleType::getEmpty(Context)));
667+
return builder.build(Id);
668+
}
669+
648670
static ValueDecl *getBindMemoryOperation(ASTContext &Context, Identifier Id) {
649671
BuiltinFunctionBuilder builder(Context);
650672
builder.addParameter(makeConcrete(Context.TheRawPointerType));
@@ -908,6 +930,16 @@ static ValueDecl *getValueToBridgeObject(ASTContext &C, Identifier Id) {
908930
return builder.build(Id);
909931
}
910932

933+
static ValueDecl *getCOWBufferForReading(ASTContext &C, Identifier Id) {
934+
// <T : AnyObject> T -> T
935+
//
936+
BuiltinFunctionBuilder builder(C, 1, true);
937+
auto T = makeGenericParam();
938+
builder.addParameter(T);
939+
builder.setResult(T);
940+
return builder.build(Id);
941+
}
942+
911943
static ValueDecl *getUnsafeGuaranteed(ASTContext &C, Identifier Id) {
912944
// <T : AnyObject> T -> (T, Int8Ty)
913945
//
@@ -2249,9 +2281,16 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
22492281

22502282
case BuiltinValueKind::IsUnique:
22512283
case BuiltinValueKind::IsUnique_native:
2284+
case BuiltinValueKind::BeginCOWMutation:
2285+
case BuiltinValueKind::BeginCOWMutation_native:
22522286
if (!Types.empty()) return nullptr;
2287+
// BeginCOWMutation has the same signature as IsUnique.
22532288
return getIsUniqueOperation(Context, Id);
22542289

2290+
case BuiltinValueKind::EndCOWMutation:
2291+
if (!Types.empty()) return nullptr;
2292+
return getEndCOWMutation(Context, Id);
2293+
22552294
case BuiltinValueKind::BindMemory:
22562295
if (!Types.empty()) return nullptr;
22572296
return getBindMemoryOperation(Context, Id);
@@ -2380,6 +2419,10 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
23802419
if (!Types.empty())
23812420
return nullptr;
23822421
return getValueToBridgeObject(Context, Id);
2422+
2423+
case BuiltinValueKind::COWBufferForReading:
2424+
return getCOWBufferForReading(Context, Id);
2425+
23832426
case BuiltinValueKind::UnsafeGuaranteed:
23842427
return getUnsafeGuaranteed(Context, Id);
23852428

lib/ClangImporter/ImportDecl.cpp

-21
Original file line numberDiff line numberDiff line change
@@ -4993,27 +4993,6 @@ namespace {
49934993
}
49944994
result->setSuperclass(superclassType);
49954995

4996-
// Mark the class as runtime-only if it is named 'OS_object', even
4997-
// if it doesn't have the runtime-only Clang attribute. This is a
4998-
// targeted fix allowing IRGen to emit convenience initializers
4999-
// correctly.
5000-
//
5001-
// FIXME: Remove this once SILGen gets proper support for factory
5002-
// initializers.
5003-
if (decl->getName() == "OS_object" ||
5004-
decl->getName() == "OS_os_log") {
5005-
result->setForeignClassKind(ClassDecl::ForeignKind::RuntimeOnly);
5006-
}
5007-
5008-
// If the superclass is runtime-only, our class is also. This only
5009-
// matters in the case above.
5010-
if (superclassType) {
5011-
auto superclassDecl = cast<ClassDecl>(superclassType->getAnyNominal());
5012-
auto kind = superclassDecl->getForeignClassKind();
5013-
if (kind != ClassDecl::ForeignKind::Normal)
5014-
result->setForeignClassKind(kind);
5015-
}
5016-
50174996
// Import protocols this class conforms to.
50184997
importObjCProtocols(result, decl->getReferencedProtocols(),
50194998
inheritedTypes);

lib/Demangling/Demangler.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -677,12 +677,14 @@ NodePointer Demangler::demangleTypeMangling() {
677677
return TypeMangling;
678678
}
679679

680-
NodePointer Demangler::demangleSymbolicReference(unsigned char rawKind,
681-
const void *at) {
680+
NodePointer Demangler::demangleSymbolicReference(unsigned char rawKind) {
682681
// The symbolic reference is a 4-byte machine integer encoded in the following
683682
// four bytes.
683+
if (Pos + 4 > Text.size())
684+
return nullptr;
685+
const void *at = Text.data() + Pos;
684686
int32_t value;
685-
memcpy(&value, Text.data() + Pos, 4);
687+
memcpy(&value, at, 4);
686688
Pos += 4;
687689

688690
// Map the encoded kind to a specific kind and directness.
@@ -734,7 +736,7 @@ NodePointer Demangler::demangleOperator() {
734736
goto recur;
735737
case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
736738
case 9: case 0xA: case 0xB: case 0xC:
737-
return demangleSymbolicReference((unsigned char)c, Text.data() + Pos);
739+
return demangleSymbolicReference((unsigned char)c);
738740
case 'A': return demangleMultiSubstitutions();
739741
case 'B': return demangleBuiltinType();
740742
case 'C': return demangleAnyGenericType(Node::Kind::Class);

lib/SILGen/SILGenBuiltin.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,45 @@ emitBuiltinIsUnique_native(SILGenFunction &SGF,
901901
return ManagedValue::forUnmanaged(result);
902902
}
903903

904+
static ManagedValue
905+
emitBuiltinBeginCOWMutation(SILGenFunction &SGF,
906+
SILLocation loc,
907+
SubstitutionMap subs,
908+
ArrayRef<ManagedValue> args,
909+
SGFContext C) {
910+
return emitBuiltinIsUnique(SGF, loc, subs, args, C);
911+
}
912+
913+
static ManagedValue
914+
emitBuiltinBeginCOWMutation_native(SILGenFunction &SGF,
915+
SILLocation loc,
916+
SubstitutionMap subs,
917+
ArrayRef<ManagedValue> args,
918+
SGFContext C) {
919+
return emitBuiltinIsUnique_native(SGF, loc, subs, args, C);
920+
}
921+
922+
static ManagedValue
923+
emitBuiltinEndCOWMutation(SILGenFunction &SGF,
924+
SILLocation loc,
925+
SubstitutionMap subs,
926+
ArrayRef<ManagedValue> args,
927+
SGFContext C) {
928+
return ManagedValue::forUnmanaged(SGF.emitEmptyTuple(loc));
929+
}
930+
931+
static ManagedValue
932+
emitBuiltinCOWBufferForReading(SILGenFunction &SGF,
933+
SILLocation loc,
934+
SubstitutionMap subs,
935+
ArrayRef<ManagedValue> args,
936+
SGFContext C) {
937+
938+
939+
assert(args.size() == 1 && "isUnique_native should have one arg.");
940+
return args[0];
941+
}
942+
904943
static ManagedValue emitBuiltinBindMemory(SILGenFunction &SGF,
905944
SILLocation loc,
906945
SubstitutionMap subs,

lib/SILOptimizer/Analysis/ArraySemantic.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ void ArraySemanticsCall::initialize(ApplyInst *AI, StringRef semanticName,
183183

184184
// Need a 'self' argument otherwise this is not a semantic call that
185185
// we recognize.
186-
if (getKind() < ArrayCallKind::kArrayInit && !hasSelf())
186+
ArrayCallKind kind = getKind();
187+
if (kind == ArrayCallKind::kNone) {
188+
SemanticsCall = nullptr;
189+
return;
190+
}
191+
192+
if (kind < ArrayCallKind::kArrayInit && !hasSelf())
187193
SemanticsCall = nullptr;
188194

189195
// A arguments must be passed reference count neutral except for self.

lib/Sema/CSBindings.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,9 @@ bool TypeVarBindingProducer::computeNext() {
10381038

10391039
auto srcLocator = binding.getLocator();
10401040
if (srcLocator &&
1041-
srcLocator->isLastElement<LocatorPathElt::ApplyArgToParam>() &&
1042-
!type->hasTypeVariable() && CS.isCollectionType(type)) {
1041+
(srcLocator->isLastElement<LocatorPathElt::ApplyArgToParam>() ||
1042+
srcLocator->isLastElement<LocatorPathElt::AutoclosureResult>()) &&
1043+
!type->hasTypeVariable() && CS.isCollectionType(type)) {
10431044
// If the type binding comes from the argument conversion, let's
10441045
// instead of binding collection types directly, try to bind
10451046
// using temporary type variables substituted for element

lib/Sema/TypeCheckConstraints.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ namespace {
11611161
ExprStack.pop_back();
11621162

11631163
// Mark the direct callee as being a callee.
1164-
if (auto *call = dyn_cast<CallExpr>(expr))
1164+
if (auto *call = dyn_cast<ApplyExpr>(expr))
11651165
markDirectCallee(call->getFn());
11661166

11671167
// Fold sequence expressions.

stdlib/public/runtime/Casting.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -1189,13 +1189,6 @@ swift_dynamicCastMetatypeImpl(const Metadata *sourceType,
11891189
}
11901190
break;
11911191

1192-
case MetadataKind::Existential: {
1193-
auto targetTypeAsExistential = static_cast<const ExistentialTypeMetadata *>(targetType);
1194-
if (_conformsToProtocols(nullptr, sourceType, targetTypeAsExistential, nullptr))
1195-
return origSourceType;
1196-
return nullptr;
1197-
}
1198-
11991192
default:
12001193
return nullptr;
12011194
}

test/Constraints/keyword_arguments.swift

+16
Original file line numberDiff line numberDiff line change
@@ -874,3 +874,19 @@ func generic_and_missing_label<T>(x: T) {}
874874

875875
generic_and_missing_label(42)
876876
// expected-error@-1 {{missing argument label 'x:' in call}} {{27-27=x: }}
877+
878+
// SR-13135: Type inference regression in Swift 5.3 - can't infer a type of @autoclosure result.
879+
func sr13135() {
880+
struct Foo {
881+
var bar: [Int] = []
882+
}
883+
884+
let baz: Int? = nil
885+
886+
func foo<T: Equatable>(
887+
_ a: @autoclosure () throws -> T,
888+
_ b: @autoclosure () throws -> T
889+
) {}
890+
891+
foo(Foo().bar, [baz])
892+
}

test/Constraints/operator.swift

+6
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,9 @@ func rdar_62054241() {
290290
return arr.sorted(by: <) // expected-error {{no exact matches in reference to operator function '<'}}
291291
}
292292
}
293+
294+
// SR-11399 - Operator returning IUO doesn't implicitly unwrap
295+
postfix operator ^^^
296+
postfix func ^^^ (lhs: Int) -> Int! { 0 }
297+
298+
let x: Int = 1^^^

test/IRGen/builtins.swift

+6
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,12 @@ func isUniqueIUO(_ ref: inout Builtin.NativeObject?) -> Bool {
683683
return Builtin.isUnique(&iuo)
684684
}
685685

686+
// CHECK-LABEL: define hidden {{.*}} @"$s8builtins19COWBufferForReadingyAA1CCADnF"
687+
// CHECK: ret %T8builtins1CC* %0
688+
func COWBufferForReading(_ ref: __owned C) -> C {
689+
return Builtin.COWBufferForReading(ref)
690+
}
691+
686692
// CHECK-LABEL: define {{.*}} @{{.*}}generic_ispod_test
687693
func generic_ispod_test<T>(_: T) {
688694
// CHECK: [[T0:%.*]] = getelementptr inbounds %swift.vwtable, %swift.vwtable* [[T:%.*]], i32 10

0 commit comments

Comments
 (0)