Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' of github.com:apple/swift into tensorflow-stage
Browse files Browse the repository at this point in the history
* 'main' of github.com:apple/swift:
  [test] Mark two SILOptimizer tests requiring asserts
  [test] Disable IRGen/unmanaged_objc_throw_func.swift with non-optimized stdlib
  [NFC] Move Migrated SDK Target List into StdlibDeploymentTarget
  [stdlib] Unbreak unified builds after swiftlang#34859
  Update a comment in EscapeAnalysis.
  [concurrency] IRGen: update task/executor/context on every suspend point
  [auto-diff] Fix a bunch of places in the *Cloners where we were not closing borrow scopes.
  [test] Adjusting stdlib ocurrences where class syntax is used for protocol inheritance
  [test] Adding specific tests for the warning for protocol inheritance class keyword syntax deprecation
  [test] Adjusting test files where class syntax is used for protocol inheritance
  [Sema] Adding deprecation warning for protocol inheritance class keyword syntax
  [stdlib] Simplify buffer pointer initialization.
  [cxx-interop] Bail on functions that use unimportable types.
  Sema: Remove a dead param on TypeChecker::conformsToProtocol
  [CSBinding] Attempt to join any existing and viable bindings with new binding
  [cmake] Semi-parametrize manpage location.
  • Loading branch information
ainu-bot committed Dec 1, 2020
2 parents f8d4829 + 820b2c6 commit b669583
Show file tree
Hide file tree
Showing 46 changed files with 265 additions and 136 deletions.
6 changes: 5 additions & 1 deletion cmake/modules/SwiftManpage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ function(manpage)
ALL)

add_dependencies(${MP_INSTALL_IN_COMPONENT} ${manpage_target})
set(MANPAGE_DEST "share/")
if("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "OPENBSD")
set(MANPAGE_DEST "")
endif()
swift_install_in_component(FILES "${output_file_name}"
DESTINATION "share/man/man${MP_MAN_SECTION}"
DESTINATION "${MANPAGE_DEST}man/man${MP_MAN_SECTION}"
COMPONENT "${MP_INSTALL_IN_COMPONENT}")
endfunction()

3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2616,6 +2616,9 @@ WARNING(duplicate_anyobject_class_inheritance,none,
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
ERROR(inheritance_from_protocol_with_superclass,none,
"inheritance from class-constrained protocol composition type %0", (Type))
WARNING(anyobject_class_inheritance_deprecated,none,
"using 'class' keyword for protocol inheritance is deprecated; "
"use 'AnyObject' instead", ())
ERROR(multiple_inheritance,none,
"multiple inheritance from classes %0 and %1", (Type, Type))
ERROR(inheritance_from_non_protocol_or_class,none,
Expand Down
8 changes: 5 additions & 3 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4706,6 +4706,11 @@ class ConstraintSystem {
return {type, kind, BindingSource};
}

/// Determine whether this binding could be a viable candidate
/// to be "joined" with some other binding. It has to be at least
/// a non-default r-value supertype binding with no type variables.
bool isViableForJoin() const;

static PotentialBinding forHole(TypeVariableType *typeVar,
ConstraintLocator *locator) {
return {HoleType::get(typeVar->getASTContext(), typeVar),
Expand Down Expand Up @@ -4745,9 +4750,6 @@ class ConstraintSystem {
/// Whether this type variable has literal bindings.
LiteralBindingKind LiteralBinding = LiteralBindingKind::None;

/// Tracks the position of the last known supertype in the group.
Optional<unsigned> lastSupertypeIndex;

/// A set of all not-yet-resolved type variables this type variable
/// is a subtype of, supertype of or is equivalent to. This is used
/// to determine ordering inside of a chain of subtypes to help infer
Expand Down
4 changes: 4 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3896,6 +3896,10 @@ namespace {
bodyParams =
getNonSelfParamList(dc, decl, selfIdx, name.getArgumentNames(),
allowNSUIntegerAsInt, !name, templateParams);
// If we can't import a param for some reason (ex. it's a dependent
// type), bail.
if (!bodyParams)
return nullptr;

importedType =
Impl.importFunctionReturnType(dc, decl, allowNSUIntegerAsInt);
Expand Down
57 changes: 45 additions & 12 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,25 +331,60 @@ static Alignment getAsyncContextAlignment(IRGenModule &IGM) {
return IGM.getPointerAlignment();
}

void IRGenFunction::setupAsync() {
llvm::Value *t = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Task);
asyncTaskLocation = createAlloca(t->getType(), IGM.getPointerAlignment());
Builder.CreateStore(t, asyncTaskLocation);

llvm::Value *e = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Executor);
asyncExecutorLocation = createAlloca(e->getType(), IGM.getPointerAlignment());
Builder.CreateStore(e, asyncExecutorLocation);

llvm::Value *c = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Context);
asyncContextLocation = createAlloca(c->getType(), IGM.getPointerAlignment());
Builder.CreateStore(c, asyncContextLocation);
}

llvm::Value *IRGenFunction::getAsyncTask() {
assert(isAsync());
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Task);
assert(value->getType() == IGM.SwiftTaskPtrTy);
return value;
return Builder.CreateLoad(asyncTaskLocation);
}

llvm::Value *IRGenFunction::getAsyncExecutor() {
assert(isAsync());
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Executor);
assert(value->getType() == IGM.SwiftExecutorPtrTy);
return value;
return Builder.CreateLoad(asyncExecutorLocation);
}

llvm::Value *IRGenFunction::getAsyncContext() {
assert(isAsync());
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Context);
assert(value->getType() == IGM.SwiftContextPtrTy);
return value;
return Builder.CreateLoad(asyncContextLocation);
}

llvm::CallInst *IRGenFunction::emitSuspendAsyncCall(ArrayRef<llvm::Value *> args) {
auto *id =
Builder.CreateIntrinsicCall(llvm::Intrinsic::coro_suspend_async, args);

// Update the current values of task, executor and context.

auto *rawTask = Builder.CreateExtractValue(id,
(unsigned)AsyncFunctionArgumentIndex::Task);
auto *task = Builder.CreateBitCast(rawTask, IGM.SwiftTaskPtrTy);
Builder.CreateStore(task, asyncTaskLocation);

auto *rawExecutor = Builder.CreateExtractValue(id,
(unsigned)AsyncFunctionArgumentIndex::Executor);
auto *executor = Builder.CreateBitCast(rawExecutor, IGM.SwiftExecutorPtrTy);
Builder.CreateStore(executor, asyncExecutorLocation);

auto *calleeContext = Builder.CreateExtractValue(id,
(unsigned)AsyncFunctionArgumentIndex::Context);
llvm::Constant *projectFn = cast<llvm::Constant>(args[1])->stripPointerCasts();
// Get the caller context from the calle context.
llvm::Value *context = Builder.CreateCall(projectFn, {calleeContext});
context = Builder.CreateBitCast(context, IGM.SwiftContextPtrTy);
Builder.CreateStore(context, asyncContextLocation);

return id;
}

llvm::Type *ExplosionSchema::getScalarResultType(IRGenModule &IGM) const {
Expand Down Expand Up @@ -2421,9 +2456,7 @@ class AsyncCallEmission final : public CallEmission {
Builder.CreateBitOrPointerCast(fn.getRawPointer(), IGM.Int8PtrTy));
for (auto arg: args)
arguments.push_back(arg);
auto *id = Builder.CreateIntrinsicCall(llvm::Intrinsic::coro_suspend_async,
arguments);
return id;
return IGF.emitSuspendAsyncCall(arguments);
}
};

Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,8 @@ static llvm::Function *emitPartialApplicationForwarder(IRGenModule &IGM,
fwd->addAttributes(llvm::AttributeList::FunctionIndex, b);

IRGenFunction subIGF(IGM, fwd);
subIGF.setAsync(origType->isAsync());
if (origType->isAsync())
subIGF.setupAsync();
if (IGM.DebugInfo)
IGM.DebugInfo->emitArtificialFunction(subIGF, fwd);

Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ void IRGenModule::emitDispatchThunk(SILDeclRef declRef) {
}

IRGenFunction IGF(*this, f);
IGF.setAsync(declRef.getAbstractFunctionDecl()->hasAsync());
if (declRef.getAbstractFunctionDecl()->hasAsync())
IGF.setupAsync();

// Look up the method.
auto fn = lookupMethod(IGF, declRef);
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ void IRGenFunction::emitAwaitAsyncContinuation(
Builder.CreateBitOrPointerCast(getAsyncExecutor(), IGM.Int8PtrTy));
arguments.push_back(Builder.CreateBitOrPointerCast(
AsyncCoroutineCurrentContinuationContext, IGM.Int8PtrTy));
Builder.CreateIntrinsicCall(llvm::Intrinsic::coro_suspend_async, arguments);
emitSuspendAsyncCall(arguments);

auto results = Builder.CreateAtomicCmpXchg(
contAwaitSyncAddr, null, one,
Expand Down
11 changes: 8 additions & 3 deletions lib/IRGen/IRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class IRGenFunction {
llvm::Value *getAsyncExecutor();
llvm::Value *getAsyncContext();

llvm::CallInst *emitSuspendAsyncCall(ArrayRef<llvm::Value *> args);

llvm::Function *getOrCreateResumePrjFn();
llvm::Function *createAsyncDispatchFn(const FunctionPointer &fnPtr,
ArrayRef<llvm::Value *> args);
Expand Down Expand Up @@ -162,7 +164,10 @@ class IRGenFunction {
llvm::Value *CoroutineHandle = nullptr;
llvm::Value *AsyncCoroutineCurrentResume = nullptr;
llvm::Value *AsyncCoroutineCurrentContinuationContext = nullptr;
bool IsAsync = false;

Address asyncTaskLocation;
Address asyncExecutorLocation;
Address asyncContextLocation;

/// The unique block that calls @llvm.coro.end.
llvm::BasicBlock *CoroutineExitBlock = nullptr;
Expand All @@ -182,8 +187,8 @@ class IRGenFunction {
return getEffectiveOptimizationMode() == OptimizationMode::ForSize;
}

bool isAsync() const { return IsAsync; }
void setAsync(bool async = true) { IsAsync = async; }
void setupAsync();
bool isAsync() const { return asyncTaskLocation.isValid(); }

Address createAlloca(llvm::Type *ty, Alignment align,
const llvm::Twine &name = "");
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,8 @@ IRGenSILFunction::IRGenSILFunction(IRGenModule &IGM, SILFunction *f)
IGM.createReplaceableProlog(*this, f);
}

setAsync(f->getLoweredFunctionType()->isAsync());
if (f->getLoweredFunctionType()->isAsync())
setupAsync();
}

IRGenSILFunction::~IRGenSILFunction() {
Expand Down
13 changes: 8 additions & 5 deletions lib/SIL/IR/SILBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,15 +637,18 @@ DebugValueAddrInst *SILBuilder::createDebugValueAddr(SILLocation Loc,

void SILBuilder::emitScopedBorrowOperation(SILLocation loc, SILValue original,
function_ref<void(SILValue)> &&fun) {
if (original->getType().isAddress()) {
original = createLoadBorrow(loc, original);
SILValue value = original;
if (value->getType().isAddress()) {
value = createLoadBorrow(loc, value);
} else {
original = createBeginBorrow(loc, original);
value = emitBeginBorrowOperation(loc, value);
}

fun(original);
fun(value);

createEndBorrow(loc, original);
// If we actually inserted a borrowing operation... insert the end_borrow.
if (value != original)
createEndBorrow(loc, value);
}

CheckedCastBranchInst *SILBuilder::createCheckedCastBranch(
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Analysis/EscapeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,8 @@ void EscapeAnalysis::ConnectionGraph::mergeAllScheduledNodes() {
if (From->mappedValue) {
// values previously mapped to 'From' but not transferred to 'To's
// mappedValue must remain mapped to 'From'. Lookups on those values will
// find 'To' via the mergeTarget. Dropping a value's mapping is illegal
// find 'To' via the mergeTarget and will remap those values to 'To'
// on-the-fly for efficiency. Dropping a value's mapping is illegal
// because it could cause a node to be recreated without the edges that
// have already been discovered.
if (!To->mappedValue) {
Expand Down
24 changes: 14 additions & 10 deletions lib/SILOptimizer/Differentiation/JVPCloner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,13 @@ class JVPCloner::Implementation final
return;
}
}
auto borrowedDiffFunc = builder.emitBeginBorrowOperation(loc, origCallee);
jvpValue = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::JVP,
borrowedDiffFunc);
jvpValue = builder.emitCopyValueOperation(loc, jvpValue);
builder.emitScopedBorrowOperation(
loc, origCallee, [&](SILValue borrowedDiffFunc) {
jvpValue = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::JVP,
borrowedDiffFunc);
jvpValue = builder.emitCopyValueOperation(loc, jvpValue);
});
}

// If JVP has not yet been found, emit an `differentiable_function`
Expand Down Expand Up @@ -614,11 +616,13 @@ class JVPCloner::Implementation final
// Record the `differentiable_function` instruction.
context.getDifferentiableFunctionInstWorklist().push_back(diffFuncInst);

auto borrowedADFunc = builder.emitBeginBorrowOperation(loc, diffFuncInst);
auto extractedJVP = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::JVP, borrowedADFunc);
jvpValue = builder.emitCopyValueOperation(loc, extractedJVP);
builder.emitEndBorrowOperation(loc, borrowedADFunc);
builder.emitScopedBorrowOperation(
loc, diffFuncInst, [&](SILValue borrowedADFunc) {
auto extractedJVP = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::JVP,
borrowedADFunc);
jvpValue = builder.emitCopyValueOperation(loc, extractedJVP);
});
builder.emitDestroyValueOperation(loc, diffFuncInst);
}

Expand Down
25 changes: 15 additions & 10 deletions lib/SILOptimizer/Differentiation/VJPCloner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,13 @@ class VJPCloner::Implementation final
loc, origCallee, SILType::getPrimitiveObjectType(origFnUnsubstType),
/*withoutActuallyEscaping*/ false);
}
auto borrowedDiffFunc = builder.emitBeginBorrowOperation(loc, origCallee);
vjpValue = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::VJP,
borrowedDiffFunc);
vjpValue = builder.emitCopyValueOperation(loc, vjpValue);
builder.emitScopedBorrowOperation(
loc, origCallee, [&](SILValue borrowedDiffFunc) {
vjpValue = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::VJP,
borrowedDiffFunc);
vjpValue = builder.emitCopyValueOperation(loc, vjpValue);
});
auto vjpFnType = vjpValue->getType().castTo<SILFunctionType>();
auto vjpFnUnsubstType = vjpFnType->getUnsubstitutedType(getModule());
if (vjpFnType != vjpFnUnsubstType) {
Expand Down Expand Up @@ -601,11 +603,14 @@ class VJPCloner::Implementation final
// Record the `differentiable_function` instruction.
context.getDifferentiableFunctionInstWorklist().push_back(diffFuncInst);

auto borrowedADFunc = builder.emitBeginBorrowOperation(loc, diffFuncInst);
auto extractedVJP = getBuilder().createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::VJP, borrowedADFunc);
vjpValue = builder.emitCopyValueOperation(loc, extractedVJP);
builder.emitEndBorrowOperation(loc, borrowedADFunc);
builder.emitScopedBorrowOperation(
loc, diffFuncInst, [&](SILValue borrowedADFunc) {
auto extractedVJP =
getBuilder().createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::VJP,
borrowedADFunc);
vjpValue = builder.emitCopyValueOperation(loc, extractedVJP);
});
builder.emitDestroyValueOperation(loc, diffFuncInst);
}

Expand Down
53 changes: 32 additions & 21 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
using namespace swift;
using namespace constraints;

bool ConstraintSystem::PotentialBinding::isViableForJoin() const {
return Kind == AllowedBindingKind::Supertypes &&
!BindingType->hasLValueType() &&
!BindingType->hasUnresolvedType() &&
!BindingType->hasTypeVariable() &&
!BindingType->hasHole() &&
!BindingType->hasUnboundGenericType() &&
!hasDefaultedLiteralProtocol() &&
!isDefaultableBinding();
}

bool ConstraintSystem::PotentialBindings::isPotentiallyIncomplete() const {
// Generic parameters are always potentially incomplete.
if (isGenericParameter())
Expand Down Expand Up @@ -679,30 +690,30 @@ void ConstraintSystem::PotentialBindings::addPotentialBinding(
// If this is a non-defaulted supertype binding,
// check whether we can combine it with another
// supertype binding by computing the 'join' of the types.
if (binding.Kind == AllowedBindingKind::Supertypes &&
!binding.BindingType->hasUnresolvedType() &&
!binding.BindingType->hasTypeVariable() &&
!binding.BindingType->hasHole() &&
!binding.BindingType->hasUnboundGenericType() &&
!binding.hasDefaultedLiteralProtocol() &&
!binding.isDefaultableBinding() && allowJoinMeet) {
if (lastSupertypeIndex) {
auto &lastBinding = Bindings[*lastSupertypeIndex];
auto lastType = lastBinding.BindingType->getWithoutSpecifierType();
auto bindingType = binding.BindingType->getWithoutSpecifierType();

auto join = Type::join(lastType, bindingType);
if (join && !(*join)->isAny() &&
(!(*join)->getOptionalObjectType()
|| !(*join)->getOptionalObjectType()->isAny())) {
// Replace the last supertype binding with the join. We're done.
lastBinding.BindingType = *join;
return;
if (binding.isViableForJoin() && allowJoinMeet) {
bool joined = false;

auto isAcceptableJoin = [](Type type) {
return !type->isAny() && (!type->getOptionalObjectType() ||
!type->getOptionalObjectType()->isAny());
};

for (auto &existingBinding : Bindings) {
if (!existingBinding.isViableForJoin())
continue;

auto join = Type::join(existingBinding.BindingType, binding.BindingType);

if (join && isAcceptableJoin(*join)) {
existingBinding.BindingType = *join;
joined = true;
}
}

// Record this as the most recent supertype index.
lastSupertypeIndex = Bindings.size();
// If new binding has been joined with at least one of existing
// bindings, there is no reason to include it into the set.
if (joined)
return;
}

if (auto *literalProtocol = binding.getDefaultedLiteralProtocol())
Expand Down
Loading

0 comments on commit b669583

Please sign in to comment.