1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
//===--- RequirementMachine.cpp - Generics with term rewriting ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A requirement machine is constructed from a collection of requirements over
// some set of generic parameters, and consists of a rewrite system together
// with a property map.
//
// The rewrite system and property map are used to answer questions about the
// type parameters expressed by this set of generic requirements. These are
// called "generic signature queries", and are defined as methods on the
// GenericSignature class; for example, two of the more common ones are
// getReducedType() and requiresProtocol().
//
// The terms of the rewrite system describe all possible type parameters that
// can be written -- the generic parameters themselves, together with all nested
// associated types generated by protocol conformances.
//
// The property map describes the requirements imposed on each type parameter,
// either directly by the user or implied by some protocol conformance.
//
// There are two "kinds" of requirement machines:
// - built from canonical, minimal generic signatures, used for queries,
// - built from user-written requirements, used to compute minimal generic
// signatures.
//
// Ultimately, all minimal generic signatures are built by the second kind of
// requirement machine; the first kind consumes a minimal generic signature
// that was previously constructed, for example from a deserialized module.
//
// The second kind of requirement machine records additional information
// during completion.
//
// The second kind can become the first; after a minimal generic signature has
// been computed, the rewrite loops and other information only needed for
// minimization can be discarded.
//
// # Requirement machine initialization
//
// Requirement machines of the first kind are constructed by:
// - initWithProtocolSignatureRequirements()
// - initWithGenericSignature()
//
// The RewriteContext::getRequirementMachine() methods wrap the above with
// a lazy cache.
//
// Requirement machines of the second kind are constructed by:
// - initWithProtocolWrittenRequirements()
// - initWithWrittenRequirements()
//
// These are used from the implementations of RequirementSignatureRequest,
// AbstractGenericSignatureRequest and InferredGenericSignatureRequest
// in RequirementMachineRequests.cpp.
//
// Both kinds of requirement machines undergo a multi-stage construction
// process which is best understood as a series of state transitions:
//
// /--------------------------\
// | Empty RequirementMachine |
// \--------------------------/
// | --------------
// | / Requirement /
// | --------------
// | |
// | v
// | +-------------+
// | | RuleBuilder |
// | +-------------+
// | |
// | v
// | -------
// | / Rule /
// | -------
// | |
// | +-----------------------+
// | |
// v v
// +----------------+
// | Initialization |
// +----------------+
// |
// v
// /-----------------------------\
// | Initial RequirementMachine |
// | /-----------------------\ |
// | | Initial RewriteSystem | |
// | \-----------------------/ |
// \------------------------------/
// |
// v
// +------------+
// | Completion |
// +------------+
// |
// v
// /------------------------------------------------------------------------\
// | Complete RequirementMachine |
// | +------ optional ----+ |
// | /-------------------------\ /-------------\ | /--------------\ | |
// | | Confluent RewriteSystem | | PropertyMap | | | RewriteLoops | | |
// | \-------------------------/ \-------------/ | \--------------/ | |
// | +--------------------+ |
// \------------------------------------------------------------------------/
//
//
// The RuleBuilder converts desugared requirements into rules. See
// RuleBuilder.cpp and RequirementLowering.cpp.
//
// Completion is an iterated process involving the Knuth-Bendix algorithm and
// property map construction, which are implemented in KnuthBendix.cpp and
// PropertyMap.cpp.
//
// # Requirement machine minimization
//
// A complete RequirementMachine of the second kind -- built from user-written
// requirements, with RewriteLoops recorded -- undergoes an additional state
// transition into a minimized state via a minimization process which identifies
// redundant rules. This is implemented in HomotopyReduction.cpp and
// MinimalConformances.cpp.
//
// After minimization, the remaining non-redundant rules are converted into
// the Requirements of a minimal generic signature by the RequirementBuilder.
// Then, the requirement machine undergoes a final state transition into the
// immutable "frozen" state:
//
// /-----------------------------\
// | Complete RequirementMachine |
// \-----------------------------/
// |
// v
// +--------------+ -------------------
// | Minimization | ------> / RequirementError /
// +--------------+ -------------------
// |
// v
// /------------------------------\
// | Minimized RequirementMachine | ---------------+
// \------------------------------/ |
// | v
// | -------
// | / Rule /
// v -------
// | |
// | v
// | +--------------------+
// | | RequirementBuilder |
// | +--------------------+
// | |
// | v
// | --------------
// | / Requirement /
// v --------------
// +----------+
// | Freezing |
// +----------+
// |
// v
// /---------------------------\
// | Frozen RequirementMachine |
// \---------------------------/
//
// # Generic signature queries
//
// Requirement machines of the first kind move into the "frozen" state
// immediately after completion.
//
// /-----------------------------\
// | Complete RequirementMachine |
// \-----------------------------/
// |
// v
// +----------+
// | Freezing |
// +----------+
// |
// v
// /---------------------------\
// | Frozen RequirementMachine |
// \---------------------------/
//
// Once frozen, generic signature queries can be issued against the new
// requirement machine of either kind. These are implemented as methods on
// RequirementMachine in GenericSignatureQueries.cpp.
//
//===----------------------------------------------------------------------===//
#include "RequirementMachine.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/Requirement.h"
#include "RequirementLowering.h"
#include "RuleBuilder.h"
using namespace swift;
using namespace rewriting;
RequirementMachine::RequirementMachine(RewriteContext &ctx)
: Context(ctx), System(ctx), Map(System) {
auto &langOpts = ctx.getASTContext().LangOpts;
Dump = langOpts.DumpRequirementMachine;
MaxRuleCount = langOpts.RequirementMachineMaxRuleCount;
MaxRuleLength = langOpts.RequirementMachineMaxRuleLength;
MaxConcreteNesting = langOpts.RequirementMachineMaxConcreteNesting;
Stats = ctx.getASTContext().Stats;
if (Stats)
++Stats->getFrontendCounters().NumRequirementMachines;
}
RequirementMachine::~RequirementMachine() {}
/// Checks the result of a completion in a context where we can't diagnose
/// failure, either when building a rewrite system from an existing
/// minimal signature (which should have been checked when it was
/// minimized) or from AbstractGenericSignatureRequest (where failure
/// is fatal).
void RequirementMachine::checkCompletionResult(CompletionResult result) const {
switch (result) {
case CompletionResult::Success:
break;
case CompletionResult::MaxRuleCount:
llvm::errs() << "Rewrite system exceeded maximum rule count\n";
dump(llvm::errs());
abort();
case CompletionResult::MaxRuleLength:
llvm::errs() << "Rewrite system exceeded rule length limit\n";
dump(llvm::errs());
abort();
case CompletionResult::MaxConcreteNesting:
llvm::errs() << "Rewrite system exceeded concrete type nesting depth limit\n";
dump(llvm::errs());
abort();
}
}
/// Build a requirement machine for the previously-computed requirement
/// signatures connected component of protocols.
///
/// This must only be called exactly once, before any other operations are
/// performed on this requirement machine.
///
/// Used by RewriteContext::getRequirementMachine(const ProtocolDecl *).
///
/// Returns failure if completion fails within the configured number of steps.
std::pair<CompletionResult, unsigned>
RequirementMachine::initWithProtocolSignatureRequirements(
ArrayRef<const ProtocolDecl *> protos) {
FrontendStatsTracer tracer(Stats, "build-rewrite-system");
if (Dump) {
llvm::dbgs() << "Adding protocols";
for (auto *proto : protos) {
llvm::dbgs() << " " << proto->getName();
}
llvm::dbgs() << " {\n";
}
RuleBuilder builder(Context, System.getReferencedProtocols());
builder.initWithProtocolSignatureRequirements(protos);
// Add the initial set of rewrite rules to the rewrite system.
System.initialize(/*recordLoops=*/false, protos,
std::move(builder.ImportedRules),
std::move(builder.PermanentRules),
std::move(builder.RequirementRules));
auto result = computeCompletion(RewriteSystem::DisallowInvalidRequirements);
freeze();
if (Dump) {
llvm::dbgs() << "}\n";
}
return result;
}
/// Build a requirement machine for the requirements of a generic signature.
///
/// In this mode, minimization is not going to be performed, so rewrite loops
/// are not recorded.
///
/// This must only be called exactly once, before any other operations are
/// performed on this requirement machine.
///
/// Used by ASTContext::getOrCreateRequirementMachine().
///
/// Returns failure if completion fails within the configured number of steps.
std::pair<CompletionResult, unsigned>
RequirementMachine::initWithGenericSignature(GenericSignature sig) {
Sig = sig;
Params.append(sig.getGenericParams().begin(),
sig.getGenericParams().end());
PrettyStackTraceGenericSignature debugStack("building rewrite system for", sig);
FrontendStatsTracer tracer(Stats, "build-rewrite-system");
if (Dump) {
llvm::dbgs() << "Adding generic signature " << sig << " {\n";
}
// Collect the top-level requirements, and all transitively-referenced
// protocol requirement signatures.
RuleBuilder builder(Context, System.getReferencedProtocols());
builder.initWithGenericSignature(sig.getGenericParams(),
sig.getRequirements());
// Add the initial set of rewrite rules to the rewrite system.
System.initialize(/*recordLoops=*/false,
/*protos=*/ArrayRef<const ProtocolDecl *>(),
std::move(builder.ImportedRules),
std::move(builder.PermanentRules),
std::move(builder.RequirementRules));
auto result = computeCompletion(RewriteSystem::DisallowInvalidRequirements);
freeze();
if (Dump) {
llvm::dbgs() << "}\n";
}
return result;
}
/// Build a requirement machine for the user-written requirements of connected
/// component of protocols.
///
/// This is used when actually building the requirement signatures of these
/// protocols. In this mode, minimization will be performed, so rewrite loops
/// are recorded during completion.
///
/// This must only be called exactly once, before any other operations are
/// performed on this requirement machine.
///
/// Used by RequirementSignatureRequest.
///
/// Returns failure if completion fails within the configured number of steps.
std::pair<CompletionResult, unsigned>
RequirementMachine::initWithProtocolWrittenRequirements(
ArrayRef<const ProtocolDecl *> component,
const llvm::DenseMap<const ProtocolDecl *,
SmallVector<StructuralRequirement, 4>> protos) {
FrontendStatsTracer tracer(Stats, "build-rewrite-system");
// For RequirementMachine::verify() when called by generic signature queries;
// We have a single valid generic parameter at depth 0, index 0.
Params.push_back(component[0]->getSelfInterfaceType()->castTo<GenericTypeParamType>());
if (Dump) {
llvm::dbgs() << "Adding protocols";
for (auto *proto : component) {
llvm::dbgs() << " " << proto->getName();
}
llvm::dbgs() << " {\n";
}
RuleBuilder builder(Context, System.getReferencedProtocols());
builder.initWithProtocolWrittenRequirements(component, protos);
// Add the initial set of rewrite rules to the rewrite system.
System.initialize(/*recordLoops=*/true, component,
std::move(builder.ImportedRules),
std::move(builder.PermanentRules),
std::move(builder.RequirementRules));
auto result = computeCompletion(RewriteSystem::AllowInvalidRequirements);
if (Dump) {
llvm::dbgs() << "}\n";
}
return result;
}
/// Build a requirement machine from a set of generic parameters and
/// structural requirements.
///
/// In this mode, minimization will be performed, so rewrite loops are recorded
/// during completion.
///
/// This must only be called exactly once, before any other operations are
/// performed on this requirement machine.
///
/// Used by AbstractGenericSignatureRequest and InferredGenericSignatureRequest.
///
/// Returns failure if completion fails within the configured number of steps.
std::pair<CompletionResult, unsigned>
RequirementMachine::initWithWrittenRequirements(
ArrayRef<GenericTypeParamType *> genericParams,
ArrayRef<StructuralRequirement> requirements) {
Params.append(genericParams.begin(), genericParams.end());
FrontendStatsTracer tracer(Stats, "build-rewrite-system");
if (Dump) {
llvm::dbgs() << "Adding generic parameters:";
for (auto *paramTy : genericParams)
llvm::dbgs() << " " << Type(paramTy);
llvm::dbgs() << "\n";
}
// Collect the top-level requirements, and all transitively-referenced
// protocol requirement signatures.
RuleBuilder builder(Context, System.getReferencedProtocols());
builder.initWithWrittenRequirements(genericParams, requirements);
// Add the initial set of rewrite rules to the rewrite system.
System.initialize(/*recordLoops=*/true,
/*protos=*/ArrayRef<const ProtocolDecl *>(),
std::move(builder.ImportedRules),
std::move(builder.PermanentRules),
std::move(builder.RequirementRules));
auto result = computeCompletion(RewriteSystem::AllowInvalidRequirements);
if (Dump) {
llvm::dbgs() << "}\n";
}
return result;
}
/// Attempt to obtain a confluent rewrite system by iterating the Knuth-Bendix
/// completion procedure together with property map construction until fixed
/// point.
///
/// Returns a pair where the first element is the status. If the status is not
/// CompletionResult::Success, the second element of the pair is the rule ID
/// which triggered failure.
std::pair<CompletionResult, unsigned>
RequirementMachine::computeCompletion(RewriteSystem::ValidityPolicy policy) {
while (true) {
{
unsigned ruleCount = System.getRules().size();
// First, run the Knuth-Bendix algorithm to resolve overlapping rules.
auto result = System.performKnuthBendix(MaxRuleCount, MaxRuleLength);
unsigned rulesAdded = (System.getRules().size() - ruleCount);
if (Stats) {
Stats->getFrontendCounters()
.NumRequirementMachineCompletionSteps += rulesAdded;
}
// Check for failure.
if (result.first != CompletionResult::Success)
return result;
// Check invariants.
System.verifyRewriteRules(policy);
}
{
unsigned ruleCount = System.getRules().size();
// Build the property map, which also performs concrete term
// unification; if this added any new rules, run the completion
// procedure again.
Map.buildPropertyMap();
unsigned rulesAdded = (System.getRules().size() - ruleCount);
// If buildPropertyMap() didn't add any new rules, we are done.
if (rulesAdded == 0)
break;
if (Stats) {
Stats->getFrontendCounters()
.NumRequirementMachineUnifiedConcreteTerms += rulesAdded;
}
// Check new rules added by the property map against configured limits.
for (unsigned i = 0; i < rulesAdded; ++i) {
const auto &newRule = System.getRule(ruleCount + i);
if (newRule.getDepth() > MaxRuleLength + System.getLongestInitialRule()) {
return std::make_pair(CompletionResult::MaxRuleLength,
ruleCount + i);
}
if (newRule.getNesting() > MaxConcreteNesting + System.getDeepestInitialRule()) {
return std::make_pair(CompletionResult::MaxConcreteNesting,
ruleCount + i);
}
}
if (System.getLocalRules().size() > MaxRuleCount) {
return std::make_pair(CompletionResult::MaxRuleCount,
System.getRules().size() - 1);
}
}
}
if (Dump) {
dump(llvm::dbgs());
}
assert(!Complete);
Complete = true;
return std::make_pair(CompletionResult::Success, 0);
}
/// Transitions into a "frozen" state, where the requirement machine is now
/// immutable, and generic signature queries may be performed.
void RequirementMachine::freeze() {
System.freeze();
}
ArrayRef<Rule> RequirementMachine::getLocalRules() const {
return System.getLocalRules();
}
bool RequirementMachine::isComplete() const {
return Complete;
}
GenericSignatureErrors RequirementMachine::getErrors() const {
// FIXME: Assert if we had errors but we didn't emit any diagnostics?
return System.getErrors();
}
void RequirementMachine::dump(llvm::raw_ostream &out) const {
out << "Requirement machine for ";
if (Sig)
out << Sig;
else if (!System.getProtocols().empty()) {
auto protos = System.getProtocols();
out << "protocols [";
for (auto *proto : protos) {
out << " " << proto->getName();
}
out << " ]";
} else {
out << "fresh signature <";
for (auto paramTy : Params) {
out << " " << Type(paramTy);
if (paramTy->isParameterPack())
out << "…";
}
out << " >";
}
out << "\n";
System.dump(out);
Map.dump(out);
out << "Conformance paths: {\n";
for (auto pair : ConformancePaths) {
out << "- " << pair.first.first << " : ";
out << pair.first.second->getName() << " => ";
pair.second.print(out);
out << "\n";
}
out << "}\n";
}
|