Skip to content

Commit da67a99

Browse files
Merge pull request #5537 from swiftwasm/katei/merge-main-2023-06-20
Merge main 2023-06-20
2 parents 6aa2733 + d6a4123 commit da67a99

21 files changed

+222
-64
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,14 @@ elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "ANDROID")
10481048
set(SWIFT_PRIMARY_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
10491049
set(SWIFT_PRIMARY_VARIANT_ARCH_default "${SWIFT_HOST_VARIANT_ARCH}")
10501050

1051+
elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASI")
1052+
set(SWIFT_HOST_VARIANT "wasi" CACHE STRING
1053+
"Deployment OS for Swift host tools (the compiler) [wasi]")
1054+
1055+
configure_sdk_unix("WASI" "wasm32")
1056+
set(SWIFT_PRIMARY_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
1057+
set(SWIFT_PRIMARY_VARIANT_ARCH_default "${SWIFT_HOST_VARIANT_ARCH}")
1058+
10511059
elseif("${SWIFT_HOST_VARIANT_SDK}" MATCHES "(OSX|IOS*|TVOS*|WATCHOS*)")
10521060

10531061
set(SWIFT_HOST_VARIANT "macosx" CACHE STRING

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/Basic/OptimizationMode.h"
2626
#include "swift/Config.h"
2727
#include "clang/Basic/PointerAuthOptions.h"
28+
#include "llvm/IR/CallingConv.h"
2829
// FIXME: This include is just for llvm::SanitizerCoverageOptions. We should
2930
// split the header upstream so we don't include so much.
3031
#include "llvm/Transforms/Instrumentation.h"
@@ -477,6 +478,9 @@ class IRGenOptions {
477478
/// function instead of to trap instructions.
478479
std::string TrapFuncName = "";
479480

481+
/// The calling convention used to perform non-swift calls.
482+
llvm::CallingConv::ID PlatformCCallingConvention;
483+
480484
IRGenOptions()
481485
: DWARFVersion(2),
482486
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
@@ -517,7 +521,8 @@ class IRGenOptions {
517521
ColocateTypeDescriptors(true),
518522
UseRelativeProtocolWitnessTables(false), CmdArgs(),
519523
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
520-
TypeInfoFilter(TypeInfoDumpFilter::All) {
524+
TypeInfoFilter(TypeInfoDumpFilter::All),
525+
PlatformCCallingConvention(llvm::CallingConv::C) {
521526
#ifndef NDEBUG
522527
DisableRoundTripDebugTypes = false;
523528
#else

include/swift/Option/FrontendOptions.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,4 +1224,12 @@ def experimental_spi_only_imports :
12241224
def enable_ossa_complete_lifetimes :
12251225
Flag<["-"], "enable-ossa-complete-lifetimes">,
12261226
HelpText<"Require linear OSSA lifetimes after SILGen">;
1227+
1228+
def platform_c_calling_convention :
1229+
Separate<["-"], "experimental-platform-c-calling-convention">,
1230+
HelpText<"Which calling convention is used to perform non-swift calls. "
1231+
"Defaults to llvm's standard C calling convention.">;
1232+
def platform_c_calling_convention_EQ :
1233+
Joined<["-"], "experimental-platform-c-calling-convention=">,
1234+
Alias<platform_c_calling_convention>;
12271235
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Sema/Constraint.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,12 @@ enum class ConstraintKind : char {
186186
/// constraint.
187187
OneWayBindParam,
188188
/// If there is no contextual info e.g. `_ = { 42 }` default first type
189-
/// to a second type (inferred closure type). This is effectively a
190-
/// `Defaultable` constraint which a couple of differences:
189+
/// to a second type. This is effectively a `Defaultable` constraint
190+
/// which one significant difference:
191191
///
192-
/// - References inferred closure type and all of the outer parameters
193-
/// referenced by closure body.
194192
/// - Handled specially by binding inference, specifically contributes
195193
/// to the bindings only if there are no contextual types available.
196-
DefaultClosureType,
194+
FallbackType,
197195
/// The first type represents a result of an unresolved member chain,
198196
/// and the second type is its base type. This constraint acts almost
199197
/// like `Equal` but also enforces following semantics:
@@ -701,7 +699,7 @@ class Constraint final : public llvm::ilist_node<Constraint>,
701699
case ConstraintKind::OptionalObject:
702700
case ConstraintKind::OneWayEqual:
703701
case ConstraintKind::OneWayBindParam:
704-
case ConstraintKind::DefaultClosureType:
702+
case ConstraintKind::FallbackType:
705703
case ConstraintKind::UnresolvedMemberChainBase:
706704
case ConstraintKind::PackElementOf:
707705
case ConstraintKind::SameShape:

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4852,11 +4852,12 @@ class ConstraintSystem {
48524852
TypeMatchOptions flags,
48534853
ConstraintLocatorBuilder locator);
48544854

4855-
/// Attempt to simplify the given defaultable closure type constraint.
4856-
SolutionKind simplifyDefaultClosureTypeConstraint(
4857-
Type closureType, Type inferredType,
4858-
ArrayRef<TypeVariableType *> referencedOuterParameters,
4859-
TypeMatchOptions flags, ConstraintLocatorBuilder locator);
4855+
/// Attempt to simplify the given fallback type constraint.
4856+
SolutionKind
4857+
simplifyFallbackTypeConstraint(Type defaultableType, Type fallbackType,
4858+
ArrayRef<TypeVariableType *> referencedVars,
4859+
TypeMatchOptions flags,
4860+
ConstraintLocatorBuilder locator);
48604861

48614862
/// Attempt to simplify a property wrapper constraint.
48624863
SolutionKind simplifyPropertyWrapperConstraint(Type wrapperType, Type wrappedValueType,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,16 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
28072807
return true;
28082808
}
28092809

2810+
if (const Arg *A = Args.getLastArg(options::OPT_platform_c_calling_convention)) {
2811+
Opts.PlatformCCallingConvention =
2812+
llvm::StringSwitch<llvm::CallingConv::ID>(A->getValue())
2813+
.Case("c", llvm::CallingConv::C)
2814+
.Case("arm_apcs", llvm::CallingConv::ARM_APCS)
2815+
.Case("arm_aapcs", llvm::CallingConv::ARM_AAPCS)
2816+
.Case("arm_aapcs_vfp", llvm::CallingConv::ARM_AAPCS_VFP)
2817+
.Default(llvm::CallingConv::C);
2818+
}
2819+
28102820
return false;
28112821
}
28122822

lib/IRGen/GenCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
322322
case SILFunctionTypeRepresentation::ObjCMethod:
323323
case SILFunctionTypeRepresentation::CXXMethod:
324324
case SILFunctionTypeRepresentation::Block:
325-
return llvm::CallingConv::C;
325+
return IGM.getOptions().PlatformCCallingConvention;
326326

327327
case SILFunctionTypeRepresentation::Method:
328328
case SILFunctionTypeRepresentation::WitnessMethod:

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3297,7 +3297,7 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
32973297
llvm::Function *thunk = llvm::Function::Create(
32983298
assumedFnType, llvm::Function::PrivateLinkage, name, &IGM.Module);
32993299

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

33023302
llvm::AttrBuilder attrBuilder(IGM.getLLVMContext());
33033303
IGM.constructInitialFnAttributes(attrBuilder);

lib/IRGen/GenHeap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ emitHeapMetadataRefForUnknownHeapObject(IRGenFunction &IGF,
19781978
auto metadata = IGF.Builder.CreateCall(
19791979
IGF.IGM.getGetObjectClassFunctionPointer(), object);
19801980
metadata->setName(object->getName() + ".Type");
1981-
metadata->setCallingConv(llvm::CallingConv::C);
1981+
metadata->setCallingConv(IGF.IGM.getOptions().PlatformCCallingConvention);
19821982
metadata->setDoesNotThrow();
19831983
metadata->addFnAttr(llvm::Attribute::ReadOnly);
19841984
return metadata;

lib/IRGen/IRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
570570
InvariantNode = llvm::MDNode::get(getLLVMContext(), {});
571571
DereferenceableID = getLLVMContext().getMDKindID("dereferenceable");
572572

573-
C_CC = llvm::CallingConv::C;
573+
C_CC = getOptions().PlatformCCallingConvention;
574574
// TODO: use "tinycc" on platforms that support it
575575
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
576576
SwiftCC = llvm::CallingConv::Swift;

lib/Parse/ParseExpr.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,19 +1664,35 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
16641664
}
16651665

16661666
case tok::identifier: // foo
1667-
case tok::kw_self: // self
1668-
1667+
case tok::kw_self: { // self
1668+
auto canParseBindingInPattern = [&]() {
1669+
if (InBindingPattern != PatternBindingState::ImplicitlyImmutable &&
1670+
!InBindingPattern.getIntroducer().hasValue()) {
1671+
return false;
1672+
}
1673+
// If we have "case let x.", "case let x(", or "case let x[", we parse 'x'
1674+
// as a normal name, not a binding, because it is the start of an enum
1675+
// pattern, call, or subscript.
1676+
if (peekToken().isAny(tok::period, tok::period_prefix, tok::l_paren,
1677+
tok::l_square)) {
1678+
return false;
1679+
}
1680+
// If we have a generic argument list, this is something like
1681+
// "case let E<Int>.e(y)", and 'E' should be parsed as a normal name, not
1682+
// a binding.
1683+
if (peekToken().isAnyOperator() && peekToken().getText().equals("<")) {
1684+
BacktrackingScope S(*this);
1685+
consumeToken();
1686+
return !canParseAsGenericArgumentList();
1687+
}
1688+
return true;
1689+
}();
16691690
// If we are parsing a refutable pattern and are inside a let/var pattern,
16701691
// the identifiers change to be value bindings instead of decl references.
16711692
// Parse and return this as an UnresolvedPatternExpr around a binding. This
16721693
// will be resolved (or rejected) by sema when the overall refutable pattern
16731694
// it transformed from an expression into a pattern.
1674-
if ((InBindingPattern == PatternBindingState::ImplicitlyImmutable ||
1675-
InBindingPattern.getIntroducer().hasValue()) &&
1676-
// If we have "case let x." or "case let x(", we parse x as a normal
1677-
// name, not a binding, because it is the start of an enum pattern or
1678-
// call pattern.
1679-
peekToken().isNot(tok::period, tok::period_prefix, tok::l_paren)) {
1695+
if (canParseBindingInPattern) {
16801696
Identifier name;
16811697
SourceLoc loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);
16821698
// If we have an inout/let/var, set that as our introducer. otherwise
@@ -1710,6 +1726,7 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
17101726
}
17111727

17121728
LLVM_FALLTHROUGH;
1729+
}
17131730
case tok::kw_Self: // Self
17141731
return parseExprIdentifier();
17151732

lib/Sema/CSBindings.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ void BindingSet::inferTransitiveBindings(
452452

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

458458
addDefault(def.second);
@@ -1510,7 +1510,7 @@ void PotentialBindings::infer(Constraint *constraint) {
15101510
}
15111511

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

16361636
case ConstraintKind::Defaultable:
1637-
case ConstraintKind::DefaultClosureType: {
1637+
case ConstraintKind::FallbackType: {
16381638
Defaults.erase(constraint);
16391639
break;
16401640
}
@@ -2075,11 +2075,10 @@ bool TypeVarBindingProducer::computeNext() {
20752075
if (NumTries == 0) {
20762076
// Add defaultable constraints (if any).
20772077
for (auto *constraint : DelayedDefaults) {
2078-
if (constraint->getKind() == ConstraintKind::DefaultClosureType) {
2079-
// If there are no other possible bindings for this closure
2080-
// let's default it to the type inferred from its parameters/body,
2081-
// otherwise we should only attempt contextual types as a
2082-
// top-level closure type.
2078+
if (constraint->getKind() == ConstraintKind::FallbackType) {
2079+
// If there are no other possible bindings for this variable
2080+
// let's default it to the fallback type, otherwise we should
2081+
// only attempt contextual types.
20832082
if (!ExploredTypes.empty())
20842083
continue;
20852084
}

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,9 +2921,9 @@ namespace {
29212921
SmallVector<TypeVariableType *, 4> referencedVars{
29222922
collectVarRefs.varRefs.begin(), collectVarRefs.varRefs.end()};
29232923

2924-
CS.addUnsolvedConstraint(Constraint::create(
2925-
CS, ConstraintKind::DefaultClosureType, closureType, inferredType,
2926-
locator, referencedVars));
2924+
CS.addUnsolvedConstraint(
2925+
Constraint::create(CS, ConstraintKind::FallbackType, closureType,
2926+
inferredType, locator, referencedVars));
29272927

29282928
CS.setClosureType(closure, inferredType);
29292929
return closureType;

lib/Sema/CSSimplify.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
22842284
case ConstraintKind::BridgingConversion:
22852285
case ConstraintKind::OneWayEqual:
22862286
case ConstraintKind::OneWayBindParam:
2287-
case ConstraintKind::DefaultClosureType:
2287+
case ConstraintKind::FallbackType:
22882288
case ConstraintKind::UnresolvedMemberChainBase:
22892289
case ConstraintKind::PropertyWrapper:
22902290
case ConstraintKind::SyntacticElement:
@@ -2643,7 +2643,7 @@ static bool matchFunctionRepresentations(FunctionType::ExtInfo einfo1,
26432643
case ConstraintKind::ValueWitness:
26442644
case ConstraintKind::OneWayEqual:
26452645
case ConstraintKind::OneWayBindParam:
2646-
case ConstraintKind::DefaultClosureType:
2646+
case ConstraintKind::FallbackType:
26472647
case ConstraintKind::UnresolvedMemberChainBase:
26482648
case ConstraintKind::PropertyWrapper:
26492649
case ConstraintKind::SyntacticElement:
@@ -3161,7 +3161,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
31613161
case ConstraintKind::BridgingConversion:
31623162
case ConstraintKind::OneWayEqual:
31633163
case ConstraintKind::OneWayBindParam:
3164-
case ConstraintKind::DefaultClosureType:
3164+
case ConstraintKind::FallbackType:
31653165
case ConstraintKind::UnresolvedMemberChainBase:
31663166
case ConstraintKind::PropertyWrapper:
31673167
case ConstraintKind::SyntacticElement:
@@ -6811,7 +6811,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
68116811
case ConstraintKind::ValueWitness:
68126812
case ConstraintKind::OneWayEqual:
68136813
case ConstraintKind::OneWayBindParam:
6814-
case ConstraintKind::DefaultClosureType:
6814+
case ConstraintKind::FallbackType:
68156815
case ConstraintKind::UnresolvedMemberChainBase:
68166816
case ConstraintKind::PropertyWrapper:
68176817
case ConstraintKind::SyntacticElement:
@@ -10989,18 +10989,18 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyDefaultableConstraint(
1098910989
return SolutionKind::Solved;
1099010990
}
1099110991

10992-
ConstraintSystem::SolutionKind
10993-
ConstraintSystem::simplifyDefaultClosureTypeConstraint(
10994-
Type closureType, Type inferredType,
10995-
ArrayRef<TypeVariableType *> referencedOuterParameters,
10996-
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
10997-
closureType = getFixedTypeRecursive(closureType, flags, /*wantRValue=*/true);
10992+
ConstraintSystem::SolutionKind ConstraintSystem::simplifyFallbackTypeConstraint(
10993+
Type defaultableType, Type fallbackType,
10994+
ArrayRef<TypeVariableType *> referencedVars, TypeMatchOptions flags,
10995+
ConstraintLocatorBuilder locator) {
10996+
defaultableType =
10997+
getFixedTypeRecursive(defaultableType, flags, /*wantRValue=*/true);
1099810998

10999-
if (closureType->isTypeVariableOrMember()) {
10999+
if (defaultableType->isTypeVariableOrMember()) {
1100011000
if (flags.contains(TMF_GenerateConstraints)) {
1100111001
addUnsolvedConstraint(Constraint::create(
11002-
*this, ConstraintKind::DefaultClosureType, closureType, inferredType,
11003-
getConstraintLocator(locator), referencedOuterParameters));
11002+
*this, ConstraintKind::FallbackType, defaultableType, fallbackType,
11003+
getConstraintLocator(locator), referencedVars));
1100411004
return SolutionKind::Solved;
1100511005
}
1100611006

@@ -15065,7 +15065,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
1506515065
case ConstraintKind::Conjunction:
1506615066
case ConstraintKind::KeyPath:
1506715067
case ConstraintKind::KeyPathApplication:
15068-
case ConstraintKind::DefaultClosureType:
15068+
case ConstraintKind::FallbackType:
1506915069
case ConstraintKind::SyntacticElement:
1507015070
llvm_unreachable("Use the correct addConstraint()");
1507115071
}
@@ -15597,12 +15597,12 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1559715597
/*flags*/ None,
1559815598
constraint.getLocator());
1559915599

15600-
case ConstraintKind::DefaultClosureType:
15601-
return simplifyDefaultClosureTypeConstraint(constraint.getFirstType(),
15602-
constraint.getSecondType(),
15603-
constraint.getTypeVariables(),
15604-
/*flags*/ None,
15605-
constraint.getLocator());
15600+
case ConstraintKind::FallbackType:
15601+
return simplifyFallbackTypeConstraint(constraint.getFirstType(),
15602+
constraint.getSecondType(),
15603+
constraint.getTypeVariables(),
15604+
/*flags*/ None,
15605+
constraint.getLocator());
1560615606

1560715607
case ConstraintKind::PropertyWrapper:
1560815608
return simplifyPropertyWrapperConstraint(constraint.getFirstType(),

0 commit comments

Comments
 (0)