Skip to content

Commit 99c1b86

Browse files
authored
Merge pull request #3714 from swiftwasm/main
[pull] swiftwasm from main
2 parents 0de2697 + 92ff0c1 commit 99c1b86

File tree

28 files changed

+305
-64
lines changed

28 files changed

+305
-64
lines changed

include/swift-c/SyntaxParser/SwiftSyntaxCDataTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define SWIFT_C_SYNTAX_C_DATA_TYPES_H
2828

2929
#include <stdbool.h>
30+
#include <stdint.h>
3031

3132
/// Offset+length in UTF8 bytes.
3233
typedef struct {

include/swift/AST/Types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4653,7 +4653,9 @@ class SILFunctionType final
46534653
AutoDiffDerivativeFunctionKind kind, Lowering::TypeConverter &TC,
46544654
LookupConformanceFn lookupConformance,
46554655
CanGenericSignature derivativeFunctionGenericSignature = nullptr,
4656-
bool isReabstractionThunk = false);
4656+
bool isReabstractionThunk = false,
4657+
CanType origTypeOfAbstraction = CanType());
4658+
46574659

46584660
/// Returns the type of the transpose function for the given parameter
46594661
/// indices, transpose function generic signature (optional), and other

include/swift/SILOptimizer/Utils/OwnershipOptUtils.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ struct OwnershipFixupContext {
7171
/// and use this to seed that new lifetime.
7272
SmallVector<Operand *, 8> allAddressUsesFromOldValue;
7373

74-
/// This is the interior pointer operand that the new value we want to RAUW
75-
/// is transitively derived from and enables us to know the underlying
76-
/// borrowed base value that we need to lifetime extend.
74+
/// This is the interior pointer (e.g. ref_element_addr)
75+
/// that the new value we want to RAUW is transitively derived from and
76+
/// enables us to know the underlying borrowed base value that we need to
77+
/// lifetime extend.
7778
///
7879
/// This is only initialized when the interior pointer has uses that must be
7980
/// replaced.
@@ -149,9 +150,15 @@ class OwnershipRAUWHelper {
149150
operator bool() const { return isValid(); }
150151
bool isValid() const { return bool(ctx) && bool(oldValue) && bool(newValue); }
151152

152-
/// Perform the actual RAUW. We require that \p newValue and \p oldValue have
153-
/// the same type at this point (in contrast to when calling
154-
/// OwnershipRAUWFixupHelper::get()).
153+
/// True if replacement requires copying the original instruction's source
154+
/// operand, creating a new borrow scope for that copy, then cloning the
155+
/// original.
156+
bool requiresCopyBorrowAndClone() const {
157+
return ctx->extraAddressFixupInfo.base;
158+
}
159+
160+
/// Perform OSSA fixup on newValue and return a fixed-up value based that can
161+
/// be used to replace all uses of oldValue.
155162
///
156163
/// This is so that we can avoid creating "forwarding" transformation
157164
/// instructions before we know if we can perform the RAUW. Any such

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ getSemanticResults(SILFunctionType *functionType, IndexSubset *parameterIndices,
362362
}
363363

364364
static CanGenericSignature buildDifferentiableGenericSignature(CanGenericSignature sig,
365-
CanType tanType) {
365+
CanType tanType,
366+
CanType origTypeOfAbstraction) {
366367
if (!sig)
367368
return sig;
368369

@@ -390,6 +391,20 @@ static CanGenericSignature buildDifferentiableGenericSignature(CanGenericSignatu
390391
}
391392
}
392393

394+
if (origTypeOfAbstraction) {
395+
(void) origTypeOfAbstraction.findIf([&](Type t) -> bool {
396+
if (auto *at = t->getAs<ArchetypeType>()) {
397+
types.insert(at->getInterfaceType()->getCanonicalType());
398+
for (auto *proto : at->getConformsTo()) {
399+
reqs.push_back(Requirement(RequirementKind::Conformance,
400+
at->getInterfaceType(),
401+
proto->getDeclaredInterfaceType()));
402+
}
403+
}
404+
return false;
405+
});
406+
}
407+
393408
return evaluateOrDefault(
394409
ctx.evaluator,
395410
AbstractGenericSignatureRequest{sig.getPointer(), {}, reqs},
@@ -427,14 +442,15 @@ static CanType getAutoDiffTangentTypeForLinearMap(
427442
static CanSILFunctionType getAutoDiffDifferentialType(
428443
SILFunctionType *originalFnTy, IndexSubset *parameterIndices,
429444
IndexSubset *resultIndices, LookupConformanceFn lookupConformance,
445+
CanType origTypeOfAbstraction,
430446
TypeConverter &TC) {
431447
// Given the tangent type and the corresponding original parameter's
432448
// convention, returns the tangent parameter's convention.
433449
auto getTangentParameterConvention =
434450
[&](CanType tanType,
435451
ParameterConvention origParamConv) -> ParameterConvention {
436452
auto sig = buildDifferentiableGenericSignature(
437-
originalFnTy->getSubstGenericSignature(), tanType);
453+
originalFnTy->getSubstGenericSignature(), tanType, origTypeOfAbstraction);
438454

439455
tanType = tanType->getCanonicalType(sig);
440456
AbstractionPattern pattern(sig, tanType);
@@ -462,7 +478,7 @@ static CanSILFunctionType getAutoDiffDifferentialType(
462478
[&](CanType tanType,
463479
ResultConvention origResConv) -> ResultConvention {
464480
auto sig = buildDifferentiableGenericSignature(
465-
originalFnTy->getSubstGenericSignature(), tanType);
481+
originalFnTy->getSubstGenericSignature(), tanType, origTypeOfAbstraction);
466482

467483
tanType = tanType->getCanonicalType(sig);
468484
AbstractionPattern pattern(sig, tanType);
@@ -565,7 +581,7 @@ static CanSILFunctionType getAutoDiffDifferentialType(
565581
static CanSILFunctionType getAutoDiffPullbackType(
566582
SILFunctionType *originalFnTy, IndexSubset *parameterIndices,
567583
IndexSubset *resultIndices, LookupConformanceFn lookupConformance,
568-
TypeConverter &TC) {
584+
CanType origTypeOfAbstraction, TypeConverter &TC) {
569585
auto &ctx = originalFnTy->getASTContext();
570586
SmallVector<GenericTypeParamType *, 4> substGenericParams;
571587
SmallVector<Requirement, 4> substRequirements;
@@ -582,7 +598,7 @@ static CanSILFunctionType getAutoDiffPullbackType(
582598
[&](CanType tanType,
583599
ResultConvention origResConv) -> ParameterConvention {
584600
auto sig = buildDifferentiableGenericSignature(
585-
originalFnTy->getSubstGenericSignature(), tanType);
601+
originalFnTy->getSubstGenericSignature(), tanType, origTypeOfAbstraction);
586602

587603
tanType = tanType->getCanonicalType(sig);
588604
AbstractionPattern pattern(sig, tanType);
@@ -613,7 +629,7 @@ static CanSILFunctionType getAutoDiffPullbackType(
613629
[&](CanType tanType,
614630
ParameterConvention origParamConv) -> ResultConvention {
615631
auto sig = buildDifferentiableGenericSignature(
616-
originalFnTy->getSubstGenericSignature(), tanType);
632+
originalFnTy->getSubstGenericSignature(), tanType, origTypeOfAbstraction);
617633

618634
tanType = tanType->getCanonicalType(sig);
619635
AbstractionPattern pattern(sig, tanType);
@@ -780,7 +796,8 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
780796
AutoDiffDerivativeFunctionKind kind, TypeConverter &TC,
781797
LookupConformanceFn lookupConformance,
782798
CanGenericSignature derivativeFnInvocationGenSig,
783-
bool isReabstractionThunk) {
799+
bool isReabstractionThunk,
800+
CanType origTypeOfAbstraction) {
784801
assert(parameterIndices);
785802
assert(!parameterIndices->isEmpty() && "Parameter indices must not be empty");
786803
assert(resultIndices);
@@ -810,12 +827,14 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
810827
case AutoDiffDerivativeFunctionKind::JVP:
811828
closureType =
812829
getAutoDiffDifferentialType(constrainedOriginalFnTy, parameterIndices,
813-
resultIndices, lookupConformance, TC);
830+
resultIndices, lookupConformance,
831+
origTypeOfAbstraction, TC);
814832
break;
815833
case AutoDiffDerivativeFunctionKind::VJP:
816834
closureType =
817835
getAutoDiffPullbackType(constrainedOriginalFnTy, parameterIndices,
818-
resultIndices, lookupConformance, TC);
836+
resultIndices, lookupConformance,
837+
origTypeOfAbstraction, TC);
819838
break;
820839
}
821840
// Compute the derivative function parameters.

lib/SIL/IR/TypeLowering.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,23 @@ namespace {
331331
CanSILFunctionType type, AbstractionPattern origType) {
332332
auto &M = TC.M;
333333
auto origTy = type->getWithoutDifferentiability();
334-
// Pass the `AbstractionPattern` generic signature to
335-
// `SILFunctionType:getAutoDiffDerivativeFunctionType` for correct type
336-
// lowering.
334+
// Pass the original type of abstraction pattern to
335+
// `SILFunctionType:getAutoDiffDerivativeFunctionType` to get the
336+
// necessary generic requirements.
337+
auto origTypeOfAbstraction =
338+
origType.hasGenericSignature() ? origType.getType() : CanType();
337339
auto jvpTy = origTy->getAutoDiffDerivativeFunctionType(
338340
type->getDifferentiabilityParameterIndices(),
339341
type->getDifferentiabilityResultIndices(),
340342
AutoDiffDerivativeFunctionKind::JVP, TC,
341-
LookUpConformanceInModule(&M), CanGenericSignature());
343+
LookUpConformanceInModule(&M), CanGenericSignature(),
344+
false, origTypeOfAbstraction);
342345
auto vjpTy = origTy->getAutoDiffDerivativeFunctionType(
343346
type->getDifferentiabilityParameterIndices(),
344347
type->getDifferentiabilityResultIndices(),
345348
AutoDiffDerivativeFunctionKind::VJP, TC,
346-
LookUpConformanceInModule(&M), CanGenericSignature());
349+
LookUpConformanceInModule(&M), CanGenericSignature(),
350+
false, origTypeOfAbstraction);
347351
RecursiveProperties props;
348352
props.addSubobject(classifyType(origType, origTy, TC, Expansion));
349353
props.addSubobject(classifyType(origType, jvpTy, TC, Expansion));

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,10 @@ bool CSE::processNode(DominanceInfoNode *Node) {
10331033
OwnershipRAUWHelper helper(RAUWFixupContext,
10341034
cast<SingleValueInstruction>(Inst),
10351035
cast<SingleValueInstruction>(AvailInst));
1036-
if (!helper.isValid())
1036+
// If RAUW requires cloning the original, then there's no point. If it
1037+
// also requires introducing a copy and new borrow scope, then it's a
1038+
// very bad idea.
1039+
if (!helper.isValid() || helper.requiresCopyBorrowAndClone())
10371040
continue;
10381041
// Replace SingleValueInstruction using OSSA RAUW here
10391042
nextI = helper.perform();

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,9 @@ OwnershipRAUWHelper::replaceAddressUses(SingleValueInstruction *oldValue,
10411041
SILValue newValue) {
10421042
assert(oldValue->getType().isAddress() && newValue->getType().isAddress());
10431043

1044-
// If we are replacing addresses, see if we need to handle interior pointer
1045-
// fixups. If we don't have any extra info, then we know that we can just RAUW
1046-
// without any further work.
1047-
if (!ctx->extraAddressFixupInfo.base)
1044+
// If newValue was not generated by an interior pointer, then it cannot
1045+
// be within a borrow scope, so direct replacement works.
1046+
if (!requiresCopyBorrowAndClone())
10481047
return replaceAllUsesAndErase(oldValue, newValue, ctx->callbacks);
10491048

10501049
// We are RAUWing two addresses and we found that:

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ option(SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
138138
"Build stdlib without a custom implementation of MetadataAllocator, relying on malloc+free instead."
139139
FALSE)
140140

141+
option(SWIFT_STDLIB_SHORT_MANGLING_LOOKUPS
142+
"Build stdlib with fast-path context descriptor lookups based on well-known short manglings."
143+
TRUE)
144+
141145
option(SWIFT_STDLIB_HAS_COMMANDLINE
142146
"Build stdlib with the CommandLine enum and support for argv/argc."
143147
TRUE)

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ function(_add_target_variant_c_compile_flags)
363363
list(APPEND result "-DSWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR")
364364
endif()
365365

366+
if(SWIFT_STDLIB_SHORT_MANGLING_LOOKUPS)
367+
list(APPEND result "-DSWIFT_STDLIB_SHORT_MANGLING_LOOKUPS")
368+
endif()
369+
366370
if(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING)
367371
list(APPEND result "-DSWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING")
368372
endif()

stdlib/public/Concurrency/GlobalExecutor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static constexpr size_t globalQueueCacheCount =
275275
static_cast<size_t>(JobPriority::UserInteractive) + 1;
276276
static std::atomic<dispatch_queue_t> globalQueueCache[globalQueueCacheCount];
277277

278-
#ifdef SWIFT_CONCURRENCY_BACK_DEPLOYMENT
278+
#if defined(SWIFT_CONCURRENCY_BACK_DEPLOYMENT) || defined(__linux__)
279279
extern "C" void dispatch_queue_set_width(dispatch_queue_t dq, long width);
280280
#endif
281281

@@ -295,7 +295,7 @@ static dispatch_queue_t getGlobalQueue(JobPriority priority) {
295295
if (SWIFT_LIKELY(queue))
296296
return queue;
297297

298-
#ifdef SWIFT_CONCURRENCY_BACK_DEPLOYMENT
298+
#if defined(SWIFT_CONCURRENCY_BACK_DEPLOYMENT) || defined(__linux__)
299299
const int DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS = -3;
300300

301301
// Create a new cooperative concurrent queue and swap it in.

0 commit comments

Comments
 (0)