Skip to content

Commit 132334c

Browse files
committed
RequirementMachine: Add debugging flag to disable re-using requirement machines
1 parent 21c5b88 commit 132334c

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ namespace swift {
549549
/// enabled. It can be disabled for debugging and testing.
550550
bool EnableRequirementMachineLoopNormalization = true;
551551

552+
/// Enable reuse of requirement machines for minimization. Usually you want
553+
/// this enabled. It can be disabled for debugging and testing.
554+
bool EnableRequirementMachineReuse = true;
555+
552556
/// Enable experimental, more correct support for opaque result types as
553557
/// concrete types. This will sometimes fail to produce a convergent
554558
/// rewrite system.

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ def disable_requirement_machine_concrete_contraction : Flag<["-"], "disable-requ
368368
def disable_requirement_machine_loop_normalization : Flag<["-"], "disable-requirement-machine-loop-normalization">,
369369
HelpText<"Disable stronger minimization algorithm, for debugging only">;
370370

371+
def disable_requirement_machine_reuse : Flag<["-"], "disable-requirement-machine-reuse">,
372+
HelpText<"Disable re-use of requirement machines for minimization, for debugging only">;
373+
371374
def enable_requirement_machine_opaque_archetypes : Flag<["-"], "enable-requirement-machine-opaque-archetypes">,
372375
HelpText<"Enable more correct opaque archetype support, which is off by default because it might fail to produce a convergent rewrite system">;
373376

lib/AST/RequirementMachine/RewriteContext.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,15 @@ bool RewriteContext::isRecursivelyConstructingRequirementMachine(
201201
void RewriteContext::installRequirementMachine(
202202
CanGenericSignature sig,
203203
std::unique_ptr<RequirementMachine> machine) {
204-
auto *machinePtr = machine.release();
204+
if (!Context.LangOpts.EnableRequirementMachineReuse)
205+
return;
205206

206-
machinePtr->freeze();
207+
auto &entry = Machines[sig];
208+
if (entry != nullptr)
209+
return;
207210

208-
auto inserted = Machines.insert(std::make_pair(sig, machinePtr)).second;
209-
if (!inserted)
210-
delete machinePtr;
211+
machine->freeze();
212+
entry = machine.release();
211213
}
212214

213215
/// Implement Tarjan's algorithm to compute strongly-connected components in
@@ -455,22 +457,22 @@ bool RewriteContext::isRecursivelyConstructingRequirementMachine(
455457
!component->second.ComputedRequirementSignatures);
456458
}
457459

458-
/// Given a reuirement machine that built the requirement signatures for a
460+
/// Given a requirement machine that built the requirement signatures for a
459461
/// protocol connected component, attempt to re-use it for subsequent
460462
/// queries against the connected component, instead of building a new one
461463
/// later.
462464
void RewriteContext::installRequirementMachine(
463465
const ProtocolDecl *proto,
464466
std::unique_ptr<RequirementMachine> machine) {
465-
auto *machinePtr = machine.release();
466-
467-
machinePtr->freeze();
467+
if (!Context.LangOpts.EnableRequirementMachineReuse)
468+
return;
468469

469470
auto &component = getProtocolComponentImpl(proto);
470-
if (component.Machine == nullptr)
471-
component.Machine = machinePtr;
472-
else
473-
delete machinePtr;
471+
if (component.Machine != nullptr)
472+
return;
473+
474+
machine->freeze();
475+
component.Machine = machine.release();
474476
}
475477

476478
/// We print stats in the destructor, which should get executed at the end of

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
10011001
if (Args.hasArg(OPT_disable_requirement_machine_loop_normalization))
10021002
Opts.EnableRequirementMachineLoopNormalization = false;
10031003

1004+
if (Args.hasArg(OPT_disable_requirement_machine_reuse))
1005+
Opts.EnableRequirementMachineReuse = false;
1006+
10041007
if (Args.hasArg(OPT_enable_requirement_machine_opaque_archetypes))
10051008
Opts.EnableRequirementMachineOpaqueArchetypes = true;
10061009

0 commit comments

Comments
 (0)