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()));

0 commit comments

Comments
 (0)