Skip to content

Commit 441ed8c

Browse files
authored
Merge pull request #3813 from swiftwasm/main
2 parents c4b1010 + 78de7d1 commit 441ed8c

File tree

65 files changed

+1147
-351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1147
-351
lines changed

docs/SIL.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5205,6 +5205,36 @@ independent of the operand. In terms of specific types:
52055205
In ownership qualified functions, a ``copy_value`` produces a +1 value that must
52065206
be consumed at most once along any path through the program.
52075207

5208+
explicit_copy_value
5209+
```````````````````
5210+
5211+
::
5212+
5213+
sil-instruction ::= 'explicit_copy_value' sil-operand
5214+
5215+
%1 = explicit_copy_value %0 : $A
5216+
5217+
Performs a copy of a loadable value as if by the value's type lowering and
5218+
returns the copy. The returned copy semantically is a value that is completely
5219+
independent of the operand. In terms of specific types:
5220+
5221+
1. For trivial types, this is equivalent to just propagating through the trivial
5222+
value.
5223+
2. For reference types, this is equivalent to performing a ``strong_retain``
5224+
operation and returning the reference.
5225+
3. For ``@unowned`` types, this is equivalent to performing an
5226+
``unowned_retain`` and returning the operand.
5227+
4. For aggregate types, this is equivalent to recursively performing a
5228+
``copy_value`` on its components, forming a new aggregate from the copied
5229+
components, and then returning the new aggregate.
5230+
5231+
In ownership qualified functions, a ``explicit_copy_value`` produces a +1 value
5232+
that must be consumed at most once along any path through the program.
5233+
5234+
When move only variable checking is performed, ``explicit_copy_value`` is
5235+
treated as an explicit copy asked for by the user that should not be rewritten
5236+
and should be treated as a non-consuming use.
5237+
52085238
move_value
52095239
``````````
52105240

include/swift/AST/Builtins.def

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,24 @@ BUILTIN_MISC_OPERATION(DestroyTaskGroup,
790790
/// the SILVerifier.
791791
BUILTIN_MISC_OPERATION(Move, "move", "", Special)
792792

793+
/// A builtin that can only be called from a transparent generic function. Takes
794+
/// two operands, the first operand the result address, the second operand the
795+
/// input address. Transforms into
796+
///
797+
/// %input = load [take] %inputAddr
798+
/// %result = explicit_copy_value %input
799+
/// store %result to [init] %resultAddr
800+
/// store %input to [init] %inputAddr
801+
///
802+
/// transparently inlined into a caller that has the generic of the callee
803+
/// specialized into a loadable type. If the transparent inlining does not
804+
/// specialize the type (due to being inlined into a non-generic context, the
805+
/// SILVerifier will abort).
806+
///
807+
/// Illegal to call except for in Swift._copy in the stdlib. This is enforced by
808+
/// the SILVerifier.
809+
BUILTIN_MISC_OPERATION(Copy, "copy", "", Special)
810+
793811
// BUILTIN_MISC_OPERATION_WITH_SILGEN - Miscellaneous operations that are
794812
// specially emitted during SIL generation.
795813
//

include/swift/AST/DiagnosticsSIL.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,10 @@ NOTE(capturepromotion_variable_defined_here,none,
718718
ERROR(move_operator_used_on_generic_or_existential_value, none,
719719
"move() used on a generic or existential value", ())
720720

721+
// copy operator used on generic or evalue
722+
ERROR(copy_operator_used_on_generic_or_existential_value, none,
723+
"copy() used on a generic or existential value", ())
724+
721725
// noimplicitcopy on generic or existential binding
722726
ERROR(noimplicitcopy_used_on_generic_or_existential, none,
723727
"@_noImplicitCopy can not be used on a generic or existential typed "

include/swift/AST/GenericSignature.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ class GenericSignature {
114114

115115
public:
116116
/// Create a new generic signature with the given type parameters and
117-
/// requirements.
117+
/// requirements. The requirements must already be minimal and canonical;
118+
/// to build a signature from an arbitrary set of requirements, use
119+
/// swift::buildGenericSignature() instead.
118120
static GenericSignature get(ArrayRef<GenericTypeParamType *> params,
119121
ArrayRef<Requirement> requirements,
120122
bool isKnownCanonical = false);
@@ -494,6 +496,31 @@ int compareAssociatedTypes(AssociatedTypeDecl *assocType1,
494496

495497
int compareDependentTypes(Type type1, Type type2);
496498

499+
/// Verify the correctness of the given generic signature.
500+
///
501+
/// This routine will test that the given generic signature is both minimal
502+
/// and canonical, emitting errors if it is not.
503+
void validateGenericSignature(ASTContext &context,
504+
GenericSignature sig);
505+
506+
/// Verify all of the generic signatures in the given module.
507+
void validateGenericSignaturesInModule(ModuleDecl *module);
508+
509+
/// Build a generic signature from the given requirements, which are not
510+
/// required to be minimal or canonical, and may contain unresolved
511+
/// DependentMemberTypes.
512+
///
513+
/// If \p baseSignature is non-null, the new parameters and requirements
514+
/// are added on; existing requirements of the base signature might become
515+
/// redundant.
516+
///
517+
/// If \p baseSignature is null, build a new signature from scratch.
518+
GenericSignature buildGenericSignature(
519+
ASTContext &ctx,
520+
GenericSignature baseSignature,
521+
SmallVector<GenericTypeParamType *, 2> addedParameters,
522+
SmallVector<Requirement, 2> addedRequirements);
523+
497524
} // end namespace swift
498525

499526
namespace llvm {

include/swift/AST/SemanticAttrs.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ SEMANTICS_ATTR(FORCE_EMIT_OPT_REMARK_PREFIX, "optremark")
114114
SEMANTICS_ATTR(OBJC_FORBID_ASSOCIATED_OBJECTS, "objc.forbidAssociatedObjects")
115115

116116
SEMANTICS_ATTR(LIFETIMEMANAGEMENT_MOVE, "lifetimemanagement.move")
117+
SEMANTICS_ATTR(LIFETIMEMANAGEMENT_COPY, "lifetimemanagement.copy")
117118

118119
SEMANTICS_ATTR(NO_PERFORMANCE_ANALYSIS, "no_performance_analysis")
119120

include/swift/AST/TypeCheckRequests.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,11 +1411,17 @@ class ClassAncestryFlagsRequest :
14111411

14121412
void simple_display(llvm::raw_ostream &out, AncestryFlags value);
14131413

1414+
/// AbstractGenericSignatureRequest and InferredGenericSignatureRequest
1415+
/// return this type, which stores a GenericSignature together with a bit
1416+
/// indicating if there were any errors detected in the original
1417+
/// requirements.
1418+
using GenericSignatureWithError = llvm::PointerIntPair<GenericSignature, 1>;
1419+
14141420
class AbstractGenericSignatureRequest :
14151421
public SimpleRequest<AbstractGenericSignatureRequest,
1416-
GenericSignature (const GenericSignatureImpl *,
1417-
SmallVector<GenericTypeParamType *, 2>,
1418-
SmallVector<Requirement, 2>),
1422+
GenericSignatureWithError (const GenericSignatureImpl *,
1423+
SmallVector<GenericTypeParamType *, 2>,
1424+
SmallVector<Requirement, 2>),
14191425
RequestFlags::Cached> {
14201426
public:
14211427
using SimpleRequest::SimpleRequest;
@@ -1424,7 +1430,7 @@ class AbstractGenericSignatureRequest :
14241430
friend SimpleRequest;
14251431

14261432
// Evaluation.
1427-
GenericSignature
1433+
GenericSignatureWithError
14281434
evaluate(Evaluator &evaluator,
14291435
const GenericSignatureImpl *baseSignature,
14301436
SmallVector<GenericTypeParamType *, 2> addedParameters,
@@ -1442,13 +1448,13 @@ class AbstractGenericSignatureRequest :
14421448

14431449
class InferredGenericSignatureRequest :
14441450
public SimpleRequest<InferredGenericSignatureRequest,
1445-
GenericSignature (ModuleDecl *,
1446-
const GenericSignatureImpl *,
1447-
GenericParamList *,
1448-
WhereClauseOwner,
1449-
SmallVector<Requirement, 2>,
1450-
SmallVector<TypeLoc, 2>,
1451-
bool),
1451+
GenericSignatureWithError (ModuleDecl *,
1452+
const GenericSignatureImpl *,
1453+
GenericParamList *,
1454+
WhereClauseOwner,
1455+
SmallVector<Requirement, 2>,
1456+
SmallVector<TypeLoc, 2>,
1457+
bool),
14521458
RequestFlags::Cached> {
14531459
public:
14541460
using SimpleRequest::SimpleRequest;
@@ -1457,7 +1463,7 @@ class InferredGenericSignatureRequest :
14571463
friend SimpleRequest;
14581464

14591465
// Evaluation.
1460-
GenericSignature
1466+
GenericSignatureWithError
14611467
evaluate(Evaluator &evaluator,
14621468
ModuleDecl *parentModule,
14631469
const GenericSignatureImpl *baseSignature,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
SWIFT_REQUEST(TypeChecker, AbstractGenericSignatureRequest,
19-
GenericSignature (const GenericSignatureImpl *,
20-
SmallVector<GenericTypeParamType *, 2>,
21-
SmallVector<Requirement, 2>),
19+
GenericSignatureWithError (const GenericSignatureImpl *,
20+
SmallVector<GenericTypeParamType *, 2>,
21+
SmallVector<Requirement, 2>),
2222
Cached, NoLocationInfo)
2323
SWIFT_REQUEST(TypeChecker, ApplyAccessNoteRequest,
2424
evaluator::SideEffect(ValueDecl *), Cached, NoLocationInfo)
@@ -131,12 +131,12 @@ SWIFT_REQUEST(TypeChecker, HasImplementationOnlyImportsRequest,
131131
SWIFT_REQUEST(TypeChecker, ModuleLibraryLevelRequest,
132132
LibraryLevel(ModuleDecl *), Cached, NoLocationInfo)
133133
SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
134-
GenericSignature (ModuleDecl *,
135-
const GenericSignatureImpl *,
136-
GenericParamList *,
137-
WhereClauseOwner,
138-
SmallVector<Requirement, 2>,
139-
SmallVector<TypeLoc, 2>, bool),
134+
GenericSignatureWithError (ModuleDecl *,
135+
const GenericSignatureImpl *,
136+
GenericParamList *,
137+
WhereClauseOwner,
138+
SmallVector<Requirement, 2>,
139+
SmallVector<TypeLoc, 2>, bool),
140140
Cached, NoLocationInfo)
141141
SWIFT_REQUEST(TypeChecker, DistributedModuleIsAvailableRequest,
142142
bool(ModuleDecl *), Cached, NoLocationInfo)

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ LANGUAGE_FEATURE(BuiltinBuildExecutor, 0, "Executor-building builtins", true)
5555
LANGUAGE_FEATURE(BuiltinBuildMainExecutor, 0, "MainActor executor building builtin", true)
5656
LANGUAGE_FEATURE(BuiltinCreateAsyncTaskInGroup, 0, "MainActor executor building builtin", true)
5757
LANGUAGE_FEATURE(BuiltinMove, 0, "Builtin.move()", true)
58+
LANGUAGE_FEATURE(BuiltinCopy, 0, "Builtin.copy()", true)
5859

5960
#undef LANGUAGE_FEATURE

include/swift/SIL/SILBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,15 @@ class SILBuilder {
12371237
CopyValueInst(getSILDebugLocation(Loc), operand));
12381238
}
12391239

1240+
ExplicitCopyValueInst *createExplicitCopyValue(SILLocation Loc,
1241+
SILValue operand) {
1242+
assert(!operand->getType().isTrivial(getFunction()) &&
1243+
"Should not be passing trivial values to this api. Use instead "
1244+
"emitCopyValueOperation");
1245+
return insert(new (getModule())
1246+
ExplicitCopyValueInst(getSILDebugLocation(Loc), operand));
1247+
}
1248+
12401249
DestroyValueInst *createDestroyValue(SILLocation Loc, SILValue operand,
12411250
bool poisonRefs = false) {
12421251
assert(isLoadableOrOpaque(operand->getType()));

include/swift/SIL/SILCloner.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,21 @@ void SILCloner<ImplClass>::visitCopyValueInst(CopyValueInst *Inst) {
17131713
getOpValue(Inst->getOperand())));
17141714
}
17151715

1716+
template <typename ImplClass>
1717+
void SILCloner<ImplClass>::visitExplicitCopyValueInst(
1718+
ExplicitCopyValueInst *Inst) {
1719+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1720+
if (!getBuilder().hasOwnership()) {
1721+
SILValue newValue = getBuilder().emitCopyValueOperation(
1722+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()));
1723+
return recordFoldedValue(Inst, newValue);
1724+
}
1725+
1726+
recordClonedInstruction(
1727+
Inst, getBuilder().createExplicitCopyValue(
1728+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
1729+
}
1730+
17161731
template <typename ImplClass>
17171732
void SILCloner<ImplClass>::visitMoveValueInst(MoveValueInst *Inst) {
17181733
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

include/swift/SIL/SILInstruction.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7267,6 +7267,15 @@ class CopyValueInst
72677267
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {}
72687268
};
72697269

7270+
class ExplicitCopyValueInst
7271+
: public UnaryInstructionBase<SILInstructionKind::ExplicitCopyValueInst,
7272+
SingleValueInstruction> {
7273+
friend class SILBuilder;
7274+
7275+
ExplicitCopyValueInst(SILDebugLocation DebugLoc, SILValue operand)
7276+
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {}
7277+
};
7278+
72707279
#define UNCHECKED_REF_STORAGE(Name, ...) \
72717280
class StrongCopy##Name##ValueInst \
72727281
: public UnaryInstructionBase< \

include/swift/SIL/SILNodes.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
575575
// A copy_value's retain semantics are fully encapsulated in OSSA
576576
// invariants. It has no side effects relative to other OSSA values.
577577
BRIDGED_SINGLE_VALUE_INST(CopyValueInst, copy_value,
578+
SingleValueInstruction, None, DoesNotRelease)
579+
// A copy_value instruction that was explicitly asked for by the user. Left
580+
// alone by OSSA optimizations.
581+
SINGLE_VALUE_INST(ExplicitCopyValueInst, explicit_copy_value,
578582
SingleValueInstruction, None, DoesNotRelease)
579583
#define UNCHECKED_REF_STORAGE(Name, name, ...) \
580584
SINGLE_VALUE_INST(StrongCopy##Name##ValueInst, strong_copy_##name##_value, \

lib/AST/ASTContext.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/ASTContext.h"
1818
#include "ClangTypeConverter.h"
1919
#include "ForeignRepresentationInfo.h"
20+
#include "GenericSignatureBuilder.h"
2021
#include "SubstitutionMapStorage.h"
2122
#include "swift/AST/ClangModuleLoader.h"
2223
#include "swift/AST/ConcreteDeclRef.h"
@@ -28,7 +29,6 @@
2829
#include "swift/AST/ForeignErrorConvention.h"
2930
#include "swift/AST/GenericEnvironment.h"
3031
#include "swift/AST/GenericSignature.h"
31-
#include "swift/AST/GenericSignatureBuilder.h"
3232
#include "swift/AST/ImportCache.h"
3333
#include "swift/AST/IndexSubset.h"
3434
#include "swift/AST/KnownProtocols.h"
@@ -4253,13 +4253,11 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl, unsigned ordinal,
42534253
}
42544254
# endif
42554255
#endif
4256-
auto signature = evaluateOrDefault(
4257-
ctx.evaluator,
4258-
AbstractGenericSignatureRequest{
4259-
Decl->getOpaqueInterfaceGenericSignature().getPointer(),
4256+
auto signature = buildGenericSignature(
4257+
ctx,
4258+
Decl->getOpaqueInterfaceGenericSignature(),
42604259
/*genericParams=*/{ },
4261-
std::move(newRequirements)},
4262-
nullptr);
4260+
std::move(newRequirements));
42634261

42644262
auto reqs = signature->getLocalRequirements(opaqueParamType);
42654263
auto superclass = reqs.superclass;
@@ -5024,10 +5022,10 @@ CanGenericSignature ASTContext::getOpenedArchetypeSignature(Type type) {
50245022
auto genericParam = GenericTypeParamType::get(0, 0, *this);
50255023
Requirement requirement(RequirementKind::Conformance, genericParam,
50265024
existential);
5027-
auto genericSig = evaluateOrDefault(
5028-
evaluator,
5029-
AbstractGenericSignatureRequest{nullptr, {genericParam}, {requirement}},
5030-
GenericSignature());
5025+
auto genericSig = buildGenericSignature(*this,
5026+
GenericSignature(),
5027+
{genericParam},
5028+
{requirement});
50315029

50325030
CanGenericSignature canGenericSig(genericSig);
50335031

@@ -5125,13 +5123,9 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
51255123
}
51265124
}
51275125

5128-
auto genericSig = evaluateOrDefault(
5129-
evaluator,
5130-
AbstractGenericSignatureRequest{
5131-
derivedClassSig.getPointer(),
5132-
std::move(addedGenericParams),
5133-
std::move(addedRequirements)},
5134-
GenericSignature());
5126+
auto genericSig = buildGenericSignature(*this, derivedClassSig,
5127+
std::move(addedGenericParams),
5128+
std::move(addedRequirements));
51355129
getImpl().overrideSigCache.insert(std::make_pair(key, genericSig));
51365130
return genericSig;
51375131
}

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,8 @@ CanGenericSignature ASTBuilder::demangleGenericSignature(
897897

898898
decodeRequirement<BuiltType, BuiltRequirement, BuiltLayoutConstraint,
899899
ASTBuilder>(node, requirements, *this);
900-
return evaluateOrDefault(Ctx.evaluator,
901-
AbstractGenericSignatureRequest{
902-
nominalDecl->getGenericSignature().getPointer(),
903-
{},
904-
std::move(requirements)},
905-
GenericSignature())
900+
return buildGenericSignature(Ctx, nominalDecl->getGenericSignature(),
901+
{}, std::move(requirements))
906902
.getCanonicalSignature();
907903
}
908904

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,8 @@ static bool usesFeatureBuiltinMove(Decl *decl) {
28162816
return false;
28172817
}
28182818

2819+
static bool usesFeatureBuiltinCopy(Decl *decl) { return false; }
2820+
28192821
static bool usesFeatureInheritActorContext(Decl *decl) {
28202822
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
28212823
for (auto param : *func->getParameters()) {

lib/AST/AutoDiff.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,9 @@ GenericSignature autodiff::getConstrainedDerivativeGenericSignature(
273273
requirements.push_back(req);
274274
}
275275
}
276-
return evaluateOrDefault(
277-
ctx.evaluator,
278-
AbstractGenericSignatureRequest{derivativeGenSig.getPointer(),
279-
/*addedGenericParams*/ {},
280-
std::move(requirements)},
281-
nullptr);
276+
return buildGenericSignature(ctx, derivativeGenSig,
277+
/*addedGenericParams*/ {},
278+
std::move(requirements));
282279
}
283280

284281
// Given the rest of a `Builtin.applyDerivative_{jvp|vjp}` or

0 commit comments

Comments
 (0)