Skip to content

Commit 3a4bb96

Browse files
authored
Merge pull request #41200 from slavapestov/enable-rqm-protocols-for-stdlib
Enable RequirementMachine for the standard library's protocol signatures
2 parents dde3291 + 76571c7 commit 3a4bb96

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8388,7 +8388,7 @@ AbstractGenericSignatureRequest::evaluate(
83888388
auto gsbResult = buildViaGSB();
83898389

83908390
if (!rqmResult.getPointer() && !gsbResult.getPointer())
8391-
return gsbResult;
8391+
return rqmResult;
83928392

83938393
if (!rqmResult.getPointer()->isEqual(gsbResult.getPointer())) {
83948394
llvm::errs() << "RequirementMachine generic signature minimization is broken:\n";
@@ -8398,7 +8398,7 @@ AbstractGenericSignatureRequest::evaluate(
83988398
abort();
83998399
}
84008400

8401-
return gsbResult;
8401+
return rqmResult;
84028402
}
84038403
}
84048404
}
@@ -8558,7 +8558,7 @@ InferredGenericSignatureRequest::evaluate(
85588558
auto gsbResult = buildViaGSB();
85598559

85608560
if (!rqmResult.getPointer() && !gsbResult.getPointer())
8561-
return gsbResult;
8561+
return rqmResult;
85628562

85638563
if (!rqmResult.getPointer()->isEqual(gsbResult.getPointer())) {
85648564
llvm::errs() << "RequirementMachine generic signature minimization is broken:\n";
@@ -8568,7 +8568,7 @@ InferredGenericSignatureRequest::evaluate(
85688568
abort();
85698569
}
85708570

8571-
return gsbResult;
8571+
return rqmResult;
85728572
}
85738573
}
85748574
}
@@ -8630,6 +8630,27 @@ RequirementSignatureRequest::evaluate(Evaluator &evaluator,
86308630
ArrayRef<Requirement>());
86318631
};
86328632

8633+
auto compare = [&](ArrayRef<Requirement> rqmResult,
8634+
ArrayRef<Requirement> gsbResult) {
8635+
if (proto->getParentModule()->isStdlibModule() &&
8636+
(proto->getName().is("Collection") ||
8637+
proto->getName().is("StringProtocol"))) {
8638+
if (rqmResult.size() > gsbResult.size())
8639+
return false;
8640+
} else {
8641+
if (rqmResult.size() != gsbResult.size())
8642+
return false;
8643+
}
8644+
8645+
return std::equal(rqmResult.begin(),
8646+
rqmResult.end(),
8647+
gsbResult.begin(),
8648+
[](const Requirement &lhs,
8649+
const Requirement &rhs) {
8650+
return lhs.getCanonical() == rhs.getCanonical();
8651+
});
8652+
};
8653+
86338654
switch (ctx.LangOpts.RequirementMachineProtocolSignatures) {
86348655
case RequirementMachineMode::Disabled:
86358656
return buildViaGSB();
@@ -8641,23 +8662,7 @@ RequirementSignatureRequest::evaluate(Evaluator &evaluator,
86418662
auto rqmResult = buildViaRQM();
86428663
auto gsbResult = buildViaGSB();
86438664

8644-
// For now, only compare conformance requirements, since those are the
8645-
// important ones from the ABI perspective.
8646-
SmallVector<Requirement, 2> rqmConformances;
8647-
for (auto req : rqmResult) {
8648-
if (req.getKind() == RequirementKind::Conformance)
8649-
rqmConformances.push_back(req);
8650-
}
8651-
SmallVector<Requirement, 2> gsbConformances;
8652-
for (auto req : gsbResult) {
8653-
if (req.getKind() == RequirementKind::Conformance)
8654-
gsbConformances.push_back(req);
8655-
}
8656-
8657-
if (rqmConformances.size() != gsbConformances.size() ||
8658-
!std::equal(rqmConformances.begin(),
8659-
rqmConformances.end(),
8660-
gsbConformances.begin())) {
8665+
if (!compare(rqmResult, gsbResult)) {
86618666
llvm::errs() << "RequirementMachine protocol signature minimization is broken:\n";
86628667
llvm::errs() << "Protocol: " << proto->getName() << "\n";
86638668

@@ -8672,7 +8677,7 @@ RequirementSignatureRequest::evaluate(Evaluator &evaluator,
86728677
abort();
86738678
}
86748679

8675-
return gsbResult;
8680+
return rqmResult;
86768681
}
86778682
}
86788683
}

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ function(_compile_swift_files
457457
list(APPEND swift_flags "-Xfrontend" "-sil-verify-all")
458458
endif()
459459

460+
# The standard library and overlays are built with -requirement-machine-protocol-signatures=verify.
461+
if(SWIFTFILE_IS_STDLIB)
462+
list(APPEND swift_flags "-Xfrontend" "-requirement-machine-protocol-signatures=verify")
463+
endif()
464+
460465
# The standard library and overlays are built resiliently when SWIFT_STDLIB_STABLE_ABI=On.
461466
if(SWIFTFILE_IS_STDLIB AND SWIFT_STDLIB_STABLE_ABI)
462467
list(APPEND swift_flags "-enable-library-evolution")

test/Generics/concrete_conformances_in_protocol.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=verify 2>&1 | %FileCheck %s
22

3+
// rdar://problem/88135912
4+
// XFAIL: *
5+
36
protocol P {
47
associatedtype T
58
}

test/api-digester/Outputs/stability-stdlib-source-x86_64.swift.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ Subscript MutableCollection.subscript(_:) has generic signature change from <Sel
1010
// FixedWidthInteger instead.
1111
Func SignedInteger.&+(_:_:) has been removed
1212
Func SignedInteger.&-(_:_:) has been removed
13+
14+
// The GenericSignatureBuilder would produce redundant same-type requirements in the minimized signature; these are now gone.
15+
16+
Protocol Collection has generic signature change from <Self : Swift.Sequence, Self.Element == Self.SubSequence.Element, Self.Index : Swift.Comparable, Self.Index == Self.Indices.Element, Self.Indices : Swift.Collection, Self.Indices == Self.Indices.SubSequence, Self.SubSequence : Swift.Collection, Self.SubSequence == Self.SubSequence.SubSequence, Self.Indices.Element == Self.Indices.Index, Self.Indices.Index == Self.SubSequence.Index, Self.SubSequence.Index == Self.Indices.Indices.Element, Self.Indices.Indices.Element == Self.Indices.Indices.Index, Self.Indices.Indices.Index == Self.SubSequence.Indices.Element, Self.SubSequence.Indices.Element == Self.SubSequence.Indices.Index, Self.SubSequence.Indices.Index == Self.SubSequence.Indices.Indices.Element, Self.SubSequence.Indices.Indices.Element == Self.SubSequence.Indices.Indices.Index> to <Self : Swift.Sequence, Self.Element == Self.SubSequence.Element, Self.Index : Swift.Comparable, Self.Index == Self.Indices.Element, Self.Indices : Swift.Collection, Self.Indices == Self.Indices.SubSequence, Self.SubSequence : Swift.Collection, Self.SubSequence == Self.SubSequence.SubSequence, Self.Indices.Element == Self.Indices.Index, Self.Indices.Index == Self.SubSequence.Index>
17+
18+
Protocol StringProtocol has generic signature change from <Self : Swift.BidirectionalCollection, Self : Swift.Comparable, Self : Swift.ExpressibleByStringInterpolation, Self : Swift.Hashable, Self : Swift.LosslessStringConvertible, Self : Swift.TextOutputStream, Self : Swift.TextOutputStreamable, Self.Element == Swift.Character, Self.Index == Swift.String.Index, Self.StringInterpolation == Swift.DefaultStringInterpolation, Self.SubSequence : Swift.StringProtocol, Self.UTF16View : Swift.BidirectionalCollection, Self.UTF8View : Swift.Collection, Self.UnicodeScalarView : Swift.BidirectionalCollection, Self.UTF16View.Element == Swift.UInt16, Self.UTF16View.Index == Swift.String.Index, Self.UTF8View.Element == Swift.UInt8, Self.UTF8View.Index == Swift.String.Index, Self.UnicodeScalarView.Element == Swift.Unicode.Scalar, Self.UnicodeScalarView.Index == Swift.String.Index, Self.SubSequence.UTF16View.Index == Swift.String.Index, Self.SubSequence.UTF8View.Index == Swift.String.Index, Self.SubSequence.UnicodeScalarView.Index == Swift.String.Index> to <Self : Swift.BidirectionalCollection, Self : Swift.Comparable, Self : Swift.ExpressibleByStringInterpolation, Self : Swift.Hashable, Self : Swift.LosslessStringConvertible, Self : Swift.TextOutputStream, Self : Swift.TextOutputStreamable, Self.Element == Swift.Character, Self.Index == Swift.String.Index, Self.StringInterpolation == Swift.DefaultStringInterpolation, Self.SubSequence : Swift.StringProtocol, Self.UTF16View : Swift.BidirectionalCollection, Self.UTF8View : Swift.Collection, Self.UnicodeScalarView : Swift.BidirectionalCollection, Self.UTF16View.Element == Swift.UInt16, Self.UTF16View.Index == Swift.String.Index, Self.UTF8View.Element == Swift.UInt8, Self.UTF8View.Index == Swift.String.Index, Self.UnicodeScalarView.Element == Swift.Unicode.Scalar, Self.UnicodeScalarView.Index == Swift.String.Index>

test/api-digester/stability-stdlib-abi-without-asserts.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@ Enum Never has added a conformance to an existing protocol Identifiable
7474
Func SignedInteger.&+(_:_:) has been removed
7575
Func SignedInteger.&-(_:_:) has been removed
7676

77+
// The GenericSignatureBuilder would produce redundant same-type requirements in the minimized signature; these are now gone.
78+
79+
Protocol Collection has generic signature change from <Self : Swift.Sequence, Self.Element == Self.SubSequence.Element, Self.Index : Swift.Comparable, Self.Index == Self.Indices.Element, Self.Indices : Swift.Collection, Self.Indices == Self.Indices.SubSequence, Self.SubSequence : Swift.Collection, Self.SubSequence == Self.SubSequence.SubSequence, Self.Indices.Element == Self.Indices.Index, Self.Indices.Index == Self.SubSequence.Index, Self.SubSequence.Index == Self.Indices.Indices.Element, Self.Indices.Indices.Element == Self.Indices.Indices.Index, Self.Indices.Indices.Index == Self.SubSequence.Indices.Element, Self.SubSequence.Indices.Element == Self.SubSequence.Indices.Index, Self.SubSequence.Indices.Index == Self.SubSequence.Indices.Indices.Element, Self.SubSequence.Indices.Indices.Element == Self.SubSequence.Indices.Indices.Index> to <Self : Swift.Sequence, Self.Element == Self.SubSequence.Element, Self.Index : Swift.Comparable, Self.Index == Self.Indices.Element, Self.Indices : Swift.Collection, Self.Indices == Self.Indices.SubSequence, Self.SubSequence : Swift.Collection, Self.SubSequence == Self.SubSequence.SubSequence, Self.Indices.Element == Self.Indices.Index, Self.Indices.Index == Self.SubSequence.Index>
80+
81+
Protocol StringProtocol has generic signature change from <Self : Swift.BidirectionalCollection, Self : Swift.Comparable, Self : Swift.ExpressibleByStringInterpolation, Self : Swift.Hashable, Self : Swift.LosslessStringConvertible, Self : Swift.TextOutputStream, Self : Swift.TextOutputStreamable, Self.Element == Swift.Character, Self.Index == Swift.String.Index, Self.StringInterpolation == Swift.DefaultStringInterpolation, Self.SubSequence : Swift.StringProtocol, Self.UTF16View : Swift.BidirectionalCollection, Self.UTF8View : Swift.Collection, Self.UnicodeScalarView : Swift.BidirectionalCollection, Self.UTF16View.Element == Swift.UInt16, Self.UTF16View.Index == Swift.String.Index, Self.UTF8View.Element == Swift.UInt8, Self.UTF8View.Index == Swift.String.Index, Self.UnicodeScalarView.Element == Swift.Unicode.Scalar, Self.UnicodeScalarView.Index == Swift.String.Index, Self.SubSequence.UTF16View.Index == Swift.String.Index, Self.SubSequence.UTF8View.Index == Swift.String.Index, Self.SubSequence.UnicodeScalarView.Index == Swift.String.Index> to <Self : Swift.BidirectionalCollection, Self : Swift.Comparable, Self : Swift.ExpressibleByStringInterpolation, Self : Swift.Hashable, Self : Swift.LosslessStringConvertible, Self : Swift.TextOutputStream, Self : Swift.TextOutputStreamable, Self.Element == Swift.Character, Self.Index == Swift.String.Index, Self.StringInterpolation == Swift.DefaultStringInterpolation, Self.SubSequence : Swift.StringProtocol, Self.UTF16View : Swift.BidirectionalCollection, Self.UTF8View : Swift.Collection, Self.UnicodeScalarView : Swift.BidirectionalCollection, Self.UTF16View.Element == Swift.UInt16, Self.UTF16View.Index == Swift.String.Index, Self.UTF8View.Element == Swift.UInt8, Self.UTF8View.Index == Swift.String.Index, Self.UnicodeScalarView.Element == Swift.Unicode.Scalar, Self.UnicodeScalarView.Index == Swift.String.Index>
82+
7783
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)

0 commit comments

Comments
 (0)