Skip to content

[pull] swiftwasm-release/5.3 from release/5.3 #1397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,35 @@ BUILTIN_SIL_OPERATION(IsUnique, "isUnique", Special)
/// BridgeObject to be treated as a native object by the runtime.
BUILTIN_SIL_OPERATION(IsUnique_native, "isUnique_native", Special)

/// beginCOWMutation<T : AnyObject>(inout T) -> Int1
///
/// Begins a copy-on-write mutation for a buffer reference which is passed as
/// inout argument. It returns a true if the buffer is uniquely referenced.
/// In this case the buffer may be mutated after calling this builtin.
///
/// The beginCOWMutation builtin is very similar to isUnique. It just translates
/// to a different SIL instruction (begin_cow_mutation), which is the preferred
/// representation of COW in SIL.
BUILTIN_SIL_OPERATION(BeginCOWMutation, "beginCOWMutation", Special)

/// beginCOWMutation_native<T : AnyObject>(inout T) -> Int1
///
/// Like beginCOWMutation, but it's assumed that T has native Swift reference
/// counting.
BUILTIN_SIL_OPERATION(BeginCOWMutation_native, "beginCOWMutation_native", Special)

/// endCOWMutation<T : AnyObject>(inout T)
///
/// Ends a copy-on-write mutation for a buffer reference which is passed as
/// inout argument. After calling this builtin, the buffer must not be mutated.
BUILTIN_SIL_OPERATION(EndCOWMutation, "endCOWMutation", Special)

/// COWBufferForReading has type <T: AnyObject> T -> T
///
/// Returns the buffer reference which is passed as argument.
/// This builtin indicates to the optimizer that the buffer is not mutable.
BUILTIN_SIL_OPERATION(COWBufferForReading, "COWBufferForReading", Special)

/// bindMemory : <T> (Builtin.RawPointer, Builtin.Word, T.Type) -> ()
BUILTIN_SIL_OPERATION(BindMemory, "bindMemory", Special)

Expand Down
3 changes: 1 addition & 2 deletions include/swift/Demangling/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,7 @@ class Demangler : public NodeFactory {
NodePointer demangleValueWitness();

NodePointer demangleTypeMangling();
NodePointer demangleSymbolicReference(unsigned char rawKind,
const void *at);
NodePointer demangleSymbolicReference(unsigned char rawKind);

bool demangleBoundGenerics(Vector<NodePointer> &TypeListList,
NodePointer &RetroactiveConformances);
Expand Down
49 changes: 46 additions & 3 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,26 @@ createGenericParam(ASTContext &ctx, const char *name, unsigned index) {

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

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


if (isAnyObject) {
CanType ao = ctx.getAnyObjectType();
SmallVector<RequirementRepr, 1> req;
req.push_back(RequirementRepr::getTypeConstraint(TypeLoc::withoutLoc(genericParams[0]->getInterfaceType()), SourceLoc(),
TypeLoc::withoutLoc(ao)));

auto paramList = GenericParamList::create(ctx, SourceLoc(), genericParams,
SourceLoc(), req, SourceLoc());
return paramList;
}

auto paramList = GenericParamList::create(ctx, SourceLoc(), genericParams,
SourceLoc());
return paramList;
Expand All @@ -460,9 +473,10 @@ namespace {
SmallVector<Requirement, 2> addedRequirements;

public:
BuiltinFunctionBuilder(ASTContext &ctx, unsigned numGenericParams = 1)
BuiltinFunctionBuilder(ASTContext &ctx, unsigned numGenericParams = 1,
bool isAnyObject = false)
: Context(ctx) {
TheGenericParamList = getGenericParams(ctx, numGenericParams);
TheGenericParamList = getGenericParams(ctx, numGenericParams, isAnyObject);
for (auto gp : TheGenericParamList->getParams()) {
genericParamTypes.push_back(
gp->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());
Expand Down Expand Up @@ -645,6 +659,14 @@ static ValueDecl *getIsUniqueOperation(ASTContext &Context, Identifier Id) {
return builder.build(Id);
}

static ValueDecl *getEndCOWMutation(ASTContext &Context, Identifier Id) {
// <T> (@inout T) -> ()
BuiltinFunctionBuilder builder(Context);
builder.addParameter(makeGenericParam(), ValueOwnership::InOut);
builder.setResult(makeConcrete(TupleType::getEmpty(Context)));
return builder.build(Id);
}

static ValueDecl *getBindMemoryOperation(ASTContext &Context, Identifier Id) {
BuiltinFunctionBuilder builder(Context);
builder.addParameter(makeConcrete(Context.TheRawPointerType));
Expand Down Expand Up @@ -908,6 +930,16 @@ static ValueDecl *getValueToBridgeObject(ASTContext &C, Identifier Id) {
return builder.build(Id);
}

static ValueDecl *getCOWBufferForReading(ASTContext &C, Identifier Id) {
// <T : AnyObject> T -> T
//
BuiltinFunctionBuilder builder(C, 1, true);
auto T = makeGenericParam();
builder.addParameter(T);
builder.setResult(T);
return builder.build(Id);
}

static ValueDecl *getUnsafeGuaranteed(ASTContext &C, Identifier Id) {
// <T : AnyObject> T -> (T, Int8Ty)
//
Expand Down Expand Up @@ -2249,9 +2281,16 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {

case BuiltinValueKind::IsUnique:
case BuiltinValueKind::IsUnique_native:
case BuiltinValueKind::BeginCOWMutation:
case BuiltinValueKind::BeginCOWMutation_native:
if (!Types.empty()) return nullptr;
// BeginCOWMutation has the same signature as IsUnique.
return getIsUniqueOperation(Context, Id);

case BuiltinValueKind::EndCOWMutation:
if (!Types.empty()) return nullptr;
return getEndCOWMutation(Context, Id);

case BuiltinValueKind::BindMemory:
if (!Types.empty()) return nullptr;
return getBindMemoryOperation(Context, Id);
Expand Down Expand Up @@ -2380,6 +2419,10 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
if (!Types.empty())
return nullptr;
return getValueToBridgeObject(Context, Id);

case BuiltinValueKind::COWBufferForReading:
return getCOWBufferForReading(Context, Id);

case BuiltinValueKind::UnsafeGuaranteed:
return getUnsafeGuaranteed(Context, Id);

Expand Down
21 changes: 0 additions & 21 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4993,27 +4993,6 @@ namespace {
}
result->setSuperclass(superclassType);

// Mark the class as runtime-only if it is named 'OS_object', even
// if it doesn't have the runtime-only Clang attribute. This is a
// targeted fix allowing IRGen to emit convenience initializers
// correctly.
//
// FIXME: Remove this once SILGen gets proper support for factory
// initializers.
if (decl->getName() == "OS_object" ||
decl->getName() == "OS_os_log") {
result->setForeignClassKind(ClassDecl::ForeignKind::RuntimeOnly);
}

// If the superclass is runtime-only, our class is also. This only
// matters in the case above.
if (superclassType) {
auto superclassDecl = cast<ClassDecl>(superclassType->getAnyNominal());
auto kind = superclassDecl->getForeignClassKind();
if (kind != ClassDecl::ForeignKind::Normal)
result->setForeignClassKind(kind);
}

// Import protocols this class conforms to.
importObjCProtocols(result, decl->getReferencedProtocols(),
inheritedTypes);
Expand Down
10 changes: 6 additions & 4 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,14 @@ NodePointer Demangler::demangleTypeMangling() {
return TypeMangling;
}

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

// Map the encoded kind to a specific kind and directness.
Expand Down Expand Up @@ -734,7 +736,7 @@ NodePointer Demangler::demangleOperator() {
goto recur;
case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
case 9: case 0xA: case 0xB: case 0xC:
return demangleSymbolicReference((unsigned char)c, Text.data() + Pos);
return demangleSymbolicReference((unsigned char)c);
case 'A': return demangleMultiSubstitutions();
case 'B': return demangleBuiltinType();
case 'C': return demangleAnyGenericType(Node::Kind::Class);
Expand Down
39 changes: 39 additions & 0 deletions lib/SILGen/SILGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,45 @@ emitBuiltinIsUnique_native(SILGenFunction &SGF,
return ManagedValue::forUnmanaged(result);
}

static ManagedValue
emitBuiltinBeginCOWMutation(SILGenFunction &SGF,
SILLocation loc,
SubstitutionMap subs,
ArrayRef<ManagedValue> args,
SGFContext C) {
return emitBuiltinIsUnique(SGF, loc, subs, args, C);
}

static ManagedValue
emitBuiltinBeginCOWMutation_native(SILGenFunction &SGF,
SILLocation loc,
SubstitutionMap subs,
ArrayRef<ManagedValue> args,
SGFContext C) {
return emitBuiltinIsUnique_native(SGF, loc, subs, args, C);
}

static ManagedValue
emitBuiltinEndCOWMutation(SILGenFunction &SGF,
SILLocation loc,
SubstitutionMap subs,
ArrayRef<ManagedValue> args,
SGFContext C) {
return ManagedValue::forUnmanaged(SGF.emitEmptyTuple(loc));
}

static ManagedValue
emitBuiltinCOWBufferForReading(SILGenFunction &SGF,
SILLocation loc,
SubstitutionMap subs,
ArrayRef<ManagedValue> args,
SGFContext C) {


assert(args.size() == 1 && "isUnique_native should have one arg.");
return args[0];
}

static ManagedValue emitBuiltinBindMemory(SILGenFunction &SGF,
SILLocation loc,
SubstitutionMap subs,
Expand Down
8 changes: 7 additions & 1 deletion lib/SILOptimizer/Analysis/ArraySemantic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,13 @@ void ArraySemanticsCall::initialize(ApplyInst *AI, StringRef semanticName,

// Need a 'self' argument otherwise this is not a semantic call that
// we recognize.
if (getKind() < ArrayCallKind::kArrayInit && !hasSelf())
ArrayCallKind kind = getKind();
if (kind == ArrayCallKind::kNone) {
SemanticsCall = nullptr;
return;
}

if (kind < ArrayCallKind::kArrayInit && !hasSelf())
SemanticsCall = nullptr;

// A arguments must be passed reference count neutral except for self.
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,9 @@ bool TypeVarBindingProducer::computeNext() {

auto srcLocator = binding.getLocator();
if (srcLocator &&
srcLocator->isLastElement<LocatorPathElt::ApplyArgToParam>() &&
!type->hasTypeVariable() && CS.isCollectionType(type)) {
(srcLocator->isLastElement<LocatorPathElt::ApplyArgToParam>() ||
srcLocator->isLastElement<LocatorPathElt::AutoclosureResult>()) &&
!type->hasTypeVariable() && CS.isCollectionType(type)) {
// If the type binding comes from the argument conversion, let's
// instead of binding collection types directly, try to bind
// using temporary type variables substituted for element
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ namespace {
ExprStack.pop_back();

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

// Fold sequence expressions.
Expand Down
7 changes: 0 additions & 7 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,13 +1189,6 @@ swift_dynamicCastMetatypeImpl(const Metadata *sourceType,
}
break;

case MetadataKind::Existential: {
auto targetTypeAsExistential = static_cast<const ExistentialTypeMetadata *>(targetType);
if (_conformsToProtocols(nullptr, sourceType, targetTypeAsExistential, nullptr))
return origSourceType;
return nullptr;
}

default:
return nullptr;
}
Expand Down
16 changes: 16 additions & 0 deletions test/Constraints/keyword_arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,19 @@ func generic_and_missing_label<T>(x: T) {}

generic_and_missing_label(42)
// expected-error@-1 {{missing argument label 'x:' in call}} {{27-27=x: }}

// SR-13135: Type inference regression in Swift 5.3 - can't infer a type of @autoclosure result.
func sr13135() {
struct Foo {
var bar: [Int] = []
}

let baz: Int? = nil

func foo<T: Equatable>(
_ a: @autoclosure () throws -> T,
_ b: @autoclosure () throws -> T
) {}

foo(Foo().bar, [baz])
}
6 changes: 6 additions & 0 deletions test/Constraints/operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,9 @@ func rdar_62054241() {
return arr.sorted(by: <) // expected-error {{no exact matches in reference to operator function '<'}}
}
}

// SR-11399 - Operator returning IUO doesn't implicitly unwrap
postfix operator ^^^
postfix func ^^^ (lhs: Int) -> Int! { 0 }

let x: Int = 1^^^
6 changes: 6 additions & 0 deletions test/IRGen/builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,12 @@ func isUniqueIUO(_ ref: inout Builtin.NativeObject?) -> Bool {
return Builtin.isUnique(&iuo)
}

// CHECK-LABEL: define hidden {{.*}} @"$s8builtins19COWBufferForReadingyAA1CCADnF"
// CHECK: ret %T8builtins1CC* %0
func COWBufferForReading(_ ref: __owned C) -> C {
return Builtin.COWBufferForReading(ref)
}

// CHECK-LABEL: define {{.*}} @{{.*}}generic_ispod_test
func generic_ispod_test<T>(_: T) {
// CHECK: [[T0:%.*]] = getelementptr inbounds %swift.vwtable, %swift.vwtable* [[T:%.*]], i32 10
Expand Down
Loading