Skip to content

Commit

Permalink
Merge pull request #5537 from swiftwasm/katei/merge-main-2023-06-20
Browse files Browse the repository at this point in the history
Merge main 2023-06-20
  • Loading branch information
kateinoigakukun authored Jun 20, 2023
2 parents 6aa2733 + d6a4123 commit da67a99
Show file tree
Hide file tree
Showing 21 changed files with 222 additions and 64 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,14 @@ elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "ANDROID")
set(SWIFT_PRIMARY_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
set(SWIFT_PRIMARY_VARIANT_ARCH_default "${SWIFT_HOST_VARIANT_ARCH}")

elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASI")
set(SWIFT_HOST_VARIANT "wasi" CACHE STRING
"Deployment OS for Swift host tools (the compiler) [wasi]")

configure_sdk_unix("WASI" "wasm32")
set(SWIFT_PRIMARY_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
set(SWIFT_PRIMARY_VARIANT_ARCH_default "${SWIFT_HOST_VARIANT_ARCH}")

elseif("${SWIFT_HOST_VARIANT_SDK}" MATCHES "(OSX|IOS*|TVOS*|WATCHOS*)")

set(SWIFT_HOST_VARIANT "macosx" CACHE STRING
Expand Down
7 changes: 6 additions & 1 deletion include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "swift/Basic/OptimizationMode.h"
#include "swift/Config.h"
#include "clang/Basic/PointerAuthOptions.h"
#include "llvm/IR/CallingConv.h"
// FIXME: This include is just for llvm::SanitizerCoverageOptions. We should
// split the header upstream so we don't include so much.
#include "llvm/Transforms/Instrumentation.h"
Expand Down Expand Up @@ -477,6 +478,9 @@ class IRGenOptions {
/// function instead of to trap instructions.
std::string TrapFuncName = "";

/// The calling convention used to perform non-swift calls.
llvm::CallingConv::ID PlatformCCallingConvention;

IRGenOptions()
: DWARFVersion(2),
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
Expand Down Expand Up @@ -517,7 +521,8 @@ class IRGenOptions {
ColocateTypeDescriptors(true),
UseRelativeProtocolWitnessTables(false), CmdArgs(),
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
TypeInfoFilter(TypeInfoDumpFilter::All) {
TypeInfoFilter(TypeInfoDumpFilter::All),
PlatformCCallingConvention(llvm::CallingConv::C) {
#ifndef NDEBUG
DisableRoundTripDebugTypes = false;
#else
Expand Down
8 changes: 8 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1224,4 +1224,12 @@ def experimental_spi_only_imports :
def enable_ossa_complete_lifetimes :
Flag<["-"], "enable-ossa-complete-lifetimes">,
HelpText<"Require linear OSSA lifetimes after SILGen">;

def platform_c_calling_convention :
Separate<["-"], "experimental-platform-c-calling-convention">,
HelpText<"Which calling convention is used to perform non-swift calls. "
"Defaults to llvm's standard C calling convention.">;
def platform_c_calling_convention_EQ :
Joined<["-"], "experimental-platform-c-calling-convention=">,
Alias<platform_c_calling_convention>;
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]
10 changes: 4 additions & 6 deletions include/swift/Sema/Constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,12 @@ enum class ConstraintKind : char {
/// constraint.
OneWayBindParam,
/// If there is no contextual info e.g. `_ = { 42 }` default first type
/// to a second type (inferred closure type). This is effectively a
/// `Defaultable` constraint which a couple of differences:
/// to a second type. This is effectively a `Defaultable` constraint
/// which one significant difference:
///
/// - References inferred closure type and all of the outer parameters
/// referenced by closure body.
/// - Handled specially by binding inference, specifically contributes
/// to the bindings only if there are no contextual types available.
DefaultClosureType,
FallbackType,
/// The first type represents a result of an unresolved member chain,
/// and the second type is its base type. This constraint acts almost
/// like `Equal` but also enforces following semantics:
Expand Down Expand Up @@ -701,7 +699,7 @@ class Constraint final : public llvm::ilist_node<Constraint>,
case ConstraintKind::OptionalObject:
case ConstraintKind::OneWayEqual:
case ConstraintKind::OneWayBindParam:
case ConstraintKind::DefaultClosureType:
case ConstraintKind::FallbackType:
case ConstraintKind::UnresolvedMemberChainBase:
case ConstraintKind::PackElementOf:
case ConstraintKind::SameShape:
Expand Down
11 changes: 6 additions & 5 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4852,11 +4852,12 @@ class ConstraintSystem {
TypeMatchOptions flags,
ConstraintLocatorBuilder locator);

/// Attempt to simplify the given defaultable closure type constraint.
SolutionKind simplifyDefaultClosureTypeConstraint(
Type closureType, Type inferredType,
ArrayRef<TypeVariableType *> referencedOuterParameters,
TypeMatchOptions flags, ConstraintLocatorBuilder locator);
/// Attempt to simplify the given fallback type constraint.
SolutionKind
simplifyFallbackTypeConstraint(Type defaultableType, Type fallbackType,
ArrayRef<TypeVariableType *> referencedVars,
TypeMatchOptions flags,
ConstraintLocatorBuilder locator);

/// Attempt to simplify a property wrapper constraint.
SolutionKind simplifyPropertyWrapperConstraint(Type wrapperType, Type wrappedValueType,
Expand Down
10 changes: 10 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,16 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
return true;
}

if (const Arg *A = Args.getLastArg(options::OPT_platform_c_calling_convention)) {
Opts.PlatformCCallingConvention =
llvm::StringSwitch<llvm::CallingConv::ID>(A->getValue())
.Case("c", llvm::CallingConv::C)
.Case("arm_apcs", llvm::CallingConv::ARM_APCS)
.Case("arm_aapcs", llvm::CallingConv::ARM_AAPCS)
.Case("arm_aapcs_vfp", llvm::CallingConv::ARM_AAPCS_VFP)
.Default(llvm::CallingConv::C);
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::Block:
return llvm::CallingConv::C;
return IGM.getOptions().PlatformCCallingConvention;

case SILFunctionTypeRepresentation::Method:
case SILFunctionTypeRepresentation::WitnessMethod:
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3297,7 +3297,7 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
llvm::Function *thunk = llvm::Function::Create(
assumedFnType, llvm::Function::PrivateLinkage, name, &IGM.Module);

thunk->setCallingConv(llvm::CallingConv::C);
thunk->setCallingConv(IGM.getOptions().PlatformCCallingConvention);

llvm::AttrBuilder attrBuilder(IGM.getLLVMContext());
IGM.constructInitialFnAttributes(attrBuilder);
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ emitHeapMetadataRefForUnknownHeapObject(IRGenFunction &IGF,
auto metadata = IGF.Builder.CreateCall(
IGF.IGM.getGetObjectClassFunctionPointer(), object);
metadata->setName(object->getName() + ".Type");
metadata->setCallingConv(llvm::CallingConv::C);
metadata->setCallingConv(IGF.IGM.getOptions().PlatformCCallingConvention);
metadata->setDoesNotThrow();
metadata->addFnAttr(llvm::Attribute::ReadOnly);
return metadata;
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
InvariantNode = llvm::MDNode::get(getLLVMContext(), {});
DereferenceableID = getLLVMContext().getMDKindID("dereferenceable");

C_CC = llvm::CallingConv::C;
C_CC = getOptions().PlatformCCallingConvention;
// TODO: use "tinycc" on platforms that support it
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
SwiftCC = llvm::CallingConv::Swift;
Expand Down
33 changes: 25 additions & 8 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1664,19 +1664,35 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
}

case tok::identifier: // foo
case tok::kw_self: // self

case tok::kw_self: { // self
auto canParseBindingInPattern = [&]() {
if (InBindingPattern != PatternBindingState::ImplicitlyImmutable &&
!InBindingPattern.getIntroducer().hasValue()) {
return false;
}
// If we have "case let x.", "case let x(", or "case let x[", we parse 'x'
// as a normal name, not a binding, because it is the start of an enum
// pattern, call, or subscript.
if (peekToken().isAny(tok::period, tok::period_prefix, tok::l_paren,
tok::l_square)) {
return false;
}
// If we have a generic argument list, this is something like
// "case let E<Int>.e(y)", and 'E' should be parsed as a normal name, not
// a binding.
if (peekToken().isAnyOperator() && peekToken().getText().equals("<")) {
BacktrackingScope S(*this);
consumeToken();
return !canParseAsGenericArgumentList();
}
return true;
}();
// If we are parsing a refutable pattern and are inside a let/var pattern,
// the identifiers change to be value bindings instead of decl references.
// Parse and return this as an UnresolvedPatternExpr around a binding. This
// will be resolved (or rejected) by sema when the overall refutable pattern
// it transformed from an expression into a pattern.
if ((InBindingPattern == PatternBindingState::ImplicitlyImmutable ||
InBindingPattern.getIntroducer().hasValue()) &&
// If we have "case let x." or "case let x(", we parse x as a normal
// name, not a binding, because it is the start of an enum pattern or
// call pattern.
peekToken().isNot(tok::period, tok::period_prefix, tok::l_paren)) {
if (canParseBindingInPattern) {
Identifier name;
SourceLoc loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);
// If we have an inout/let/var, set that as our introducer. otherwise
Expand Down Expand Up @@ -1710,6 +1726,7 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
}

LLVM_FALLTHROUGH;
}
case tok::kw_Self: // Self
return parseExprIdentifier();

Expand Down
15 changes: 7 additions & 8 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ void BindingSet::inferTransitiveBindings(

// Infer transitive defaults.
for (const auto &def : bindings.Defaults) {
if (def.getSecond()->getKind() == ConstraintKind::DefaultClosureType)
if (def.getSecond()->getKind() == ConstraintKind::FallbackType)
continue;

addDefault(def.second);
Expand Down Expand Up @@ -1510,7 +1510,7 @@ void PotentialBindings::infer(Constraint *constraint) {
}

case ConstraintKind::Defaultable:
case ConstraintKind::DefaultClosureType:
case ConstraintKind::FallbackType:
// Do these in a separate pass.
if (CS.getFixedTypeRecursive(constraint->getFirstType(), true)
->getAs<TypeVariableType>() == TypeVar) {
Expand Down Expand Up @@ -1634,7 +1634,7 @@ void PotentialBindings::retract(Constraint *constraint) {
break;

case ConstraintKind::Defaultable:
case ConstraintKind::DefaultClosureType: {
case ConstraintKind::FallbackType: {
Defaults.erase(constraint);
break;
}
Expand Down Expand Up @@ -2075,11 +2075,10 @@ bool TypeVarBindingProducer::computeNext() {
if (NumTries == 0) {
// Add defaultable constraints (if any).
for (auto *constraint : DelayedDefaults) {
if (constraint->getKind() == ConstraintKind::DefaultClosureType) {
// If there are no other possible bindings for this closure
// let's default it to the type inferred from its parameters/body,
// otherwise we should only attempt contextual types as a
// top-level closure type.
if (constraint->getKind() == ConstraintKind::FallbackType) {
// If there are no other possible bindings for this variable
// let's default it to the fallback type, otherwise we should
// only attempt contextual types.
if (!ExploredTypes.empty())
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2921,9 +2921,9 @@ namespace {
SmallVector<TypeVariableType *, 4> referencedVars{
collectVarRefs.varRefs.begin(), collectVarRefs.varRefs.end()};

CS.addUnsolvedConstraint(Constraint::create(
CS, ConstraintKind::DefaultClosureType, closureType, inferredType,
locator, referencedVars));
CS.addUnsolvedConstraint(
Constraint::create(CS, ConstraintKind::FallbackType, closureType,
inferredType, locator, referencedVars));

CS.setClosureType(closure, inferredType);
return closureType;
Expand Down
40 changes: 20 additions & 20 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
case ConstraintKind::BridgingConversion:
case ConstraintKind::OneWayEqual:
case ConstraintKind::OneWayBindParam:
case ConstraintKind::DefaultClosureType:
case ConstraintKind::FallbackType:
case ConstraintKind::UnresolvedMemberChainBase:
case ConstraintKind::PropertyWrapper:
case ConstraintKind::SyntacticElement:
Expand Down Expand Up @@ -2643,7 +2643,7 @@ static bool matchFunctionRepresentations(FunctionType::ExtInfo einfo1,
case ConstraintKind::ValueWitness:
case ConstraintKind::OneWayEqual:
case ConstraintKind::OneWayBindParam:
case ConstraintKind::DefaultClosureType:
case ConstraintKind::FallbackType:
case ConstraintKind::UnresolvedMemberChainBase:
case ConstraintKind::PropertyWrapper:
case ConstraintKind::SyntacticElement:
Expand Down Expand Up @@ -3161,7 +3161,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
case ConstraintKind::BridgingConversion:
case ConstraintKind::OneWayEqual:
case ConstraintKind::OneWayBindParam:
case ConstraintKind::DefaultClosureType:
case ConstraintKind::FallbackType:
case ConstraintKind::UnresolvedMemberChainBase:
case ConstraintKind::PropertyWrapper:
case ConstraintKind::SyntacticElement:
Expand Down Expand Up @@ -6811,7 +6811,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
case ConstraintKind::ValueWitness:
case ConstraintKind::OneWayEqual:
case ConstraintKind::OneWayBindParam:
case ConstraintKind::DefaultClosureType:
case ConstraintKind::FallbackType:
case ConstraintKind::UnresolvedMemberChainBase:
case ConstraintKind::PropertyWrapper:
case ConstraintKind::SyntacticElement:
Expand Down Expand Up @@ -10989,18 +10989,18 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyDefaultableConstraint(
return SolutionKind::Solved;
}

ConstraintSystem::SolutionKind
ConstraintSystem::simplifyDefaultClosureTypeConstraint(
Type closureType, Type inferredType,
ArrayRef<TypeVariableType *> referencedOuterParameters,
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
closureType = getFixedTypeRecursive(closureType, flags, /*wantRValue=*/true);
ConstraintSystem::SolutionKind ConstraintSystem::simplifyFallbackTypeConstraint(
Type defaultableType, Type fallbackType,
ArrayRef<TypeVariableType *> referencedVars, TypeMatchOptions flags,
ConstraintLocatorBuilder locator) {
defaultableType =
getFixedTypeRecursive(defaultableType, flags, /*wantRValue=*/true);

if (closureType->isTypeVariableOrMember()) {
if (defaultableType->isTypeVariableOrMember()) {
if (flags.contains(TMF_GenerateConstraints)) {
addUnsolvedConstraint(Constraint::create(
*this, ConstraintKind::DefaultClosureType, closureType, inferredType,
getConstraintLocator(locator), referencedOuterParameters));
*this, ConstraintKind::FallbackType, defaultableType, fallbackType,
getConstraintLocator(locator), referencedVars));
return SolutionKind::Solved;
}

Expand Down Expand Up @@ -15065,7 +15065,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
case ConstraintKind::Conjunction:
case ConstraintKind::KeyPath:
case ConstraintKind::KeyPathApplication:
case ConstraintKind::DefaultClosureType:
case ConstraintKind::FallbackType:
case ConstraintKind::SyntacticElement:
llvm_unreachable("Use the correct addConstraint()");
}
Expand Down Expand Up @@ -15597,12 +15597,12 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
/*flags*/ None,
constraint.getLocator());

case ConstraintKind::DefaultClosureType:
return simplifyDefaultClosureTypeConstraint(constraint.getFirstType(),
constraint.getSecondType(),
constraint.getTypeVariables(),
/*flags*/ None,
constraint.getLocator());
case ConstraintKind::FallbackType:
return simplifyFallbackTypeConstraint(constraint.getFirstType(),
constraint.getSecondType(),
constraint.getTypeVariables(),
/*flags*/ None,
constraint.getLocator());

case ConstraintKind::PropertyWrapper:
return simplifyPropertyWrapperConstraint(constraint.getFirstType(),
Expand Down
Loading

0 comments on commit da67a99

Please sign in to comment.