Remove flag scope data from BuildOptions - #31007
Draft
fmeum wants to merge 3 commits into
Draft
Conversation
2 tasks
fmeum
force-pushed
the
flag-scope-details
branch
from
September 6, 2026 15:00
11e9608 to
e27f4a9
Compare
2 tasks
The scope of a Starlark flag (its `scope` and `on_leave_scope` attributes and, for project-scoped flags, the project directories from its PROJECT.scl) is a property of the flag's definition, not of the configuration. Carrying it in `BuildOptions` meant that the same flag values could come with different states of scope fill-in, which made otherwise identical `BuildOptions` unequal, produced duplicate configurations and made every operation on `BuildOptions` care about metadata it should not know about (bazelbuild#29319). Scope data is now resolved through Skyframe from the flag definitions and never stored in `BuildOptions`, so `BuildOptions` and every key derived from it contain only flag values. There is exactly one node that materializes per-flag-definition metadata for transitions already, `StarlarkBuildSettingsDetailsValue`, and it now also carries each build setting's scope type, its on_leave_scope value and, for project-scoped settings, its `Scope` including the project definition. `BuildOptionsScopeFunction` and `BuildOptionsScopeValue` are gone. `BuildConfigurationFunction` resolves that value once per configuration for the configuration's own Starlark flags and stores it on `BuildConfigurationValue`. Both consumers read it off the configuration: the exec transition gets it through the exec transition provider, and project scoping in `BuildConfigurationKeyProducer` gets the source configuration's value through the dependency producers and only asks Skyframe when a transition introduces flags the source configuration did not set. No per-target or per-edge Skyframe lookup is added, the exec transition provider neither hashes nor compares the details, and `BuildConfigurationValue.equals` includes them so that a changed flag definition is not change-pruned away. The baseline configuration's options are marked as such on `BuildConfigurationKeyValue.Key` instead of having all their scopes overwritten with a placeholder, which is what previously kept scoping the baseline from depending on itself. Fixes bazelbuild#29319 Claude-Session: https://claude.ai/code/session_01AGXYj2nBsaAk4s4inx5JoR
fmeum
force-pushed
the
flag-scope-details
branch
from
September 6, 2026 15:15
e27f4a9 to
0b4a161
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Preparatory changes are split into #31030 (exec transition cache equality) and #31031 (record conversions).
The scope of a Starlark flag (its
scopeandon_leave_scopeattributes and, for project-scoped flags, the project directories from its PROJECT.scl) is a property of the flag's definition, not of a configuration. This PR stops storing it inBuildOptions. Scope data is resolved through Skyframe from the flag definitions, once per configuration, and read off theBuildConfigurationValueby its only two consumers, the exec transition and project scoping.BuildOptionsand every key derived from it now contain only flag values.Concretely:
StarlarkBuildSettingsDetailsValue, the node that already materializes per-flag-definition metadata for transitions, now also carries each build setting's scope type, itson_leave_scopevalue and, for project-scoped settings, itsScopeincluding the project definition.BuildOptionsScopeFunctionandBuildOptionsScopeValueare deleted.BuildConfigurationFunctionrequests that value for the configuration's own Starlark flags (plus thehost_flag aliases that the customexec:--host_fooscope needs even when neither flag is set) and stores it in a single field onBuildConfigurationValue. The field participates inequalsso that a changed flag definition is not change-pruned away.BuildConfigurationKeyProducerreceives the source configuration's details through the dependency producers and only asks Skyframe when a transition introduces a flag the source configuration does not set. That fallback uses the same key asBuildConfigurationFunction, so the node is shared.BuildConfigurationKeyValue.Keyinstead of having placeholder scopes written into them, which is what kept scoping the baseline from depending on itself.This supersedes #28574, which had the same goal but kept a separate per-flag-set Skyframe function and threaded two values through the configuration. The internal benchmark of that PR still showed a CPU regression after several rounds of fixes. The costs identified there do not exist in this version, see below.
Motivation
Fixes #29319.
Carrying scope data in
BuildOptionsmeans that the same flag values can come with different states of scope fill-in, depending on whether they arrived through the options parser, a platform mapping, a transition or the scope function. Since the scope maps are part ofBuildOptionsequality and its checksum, otherwise identical options become unequal, which produces duplicate configurations and makes every operation onBuildOptionscare about metadata it should not know about.Why this should not regress performance
Accounting of every hot path against master:
loadStarlarkExecTransitiononce per target and once per Skyframe restart of it. This PR passes one extra reference into that call. There is no lookup and no hashing. Remove flag scope data from BuildOptions #28574 computed the hash of the details record, seven maps deep, in the provider's constructor on every one of those calls, which is the most likely source of its residual CPU cost.containsAllover cached label hashes, and iterates the flags only if the details contain a project-scoped flag at all. Master performed a Skyframe lookup keyed by the entireBuildOptionson every edge where a flag was project-scoped, custom-exec-scoped or introduced by a transition. This PR only looks up when a transition introduces a flag the source configuration does not set, and that key is a small interned label set.CoreOptionsinstead of being recomputed from the alias map on every edge.--trim_test_configurationevery rule goes through the rule transition path. For rules whose transition is the identity, this PR requests the pre-rule-transition configuration one step earlier than master did, which Skyframe deduplicates within the evaluation. For rules whose transition changes the configuration, it is one additional dep edge. The request is skipped entirely when the configuration sets no Starlark flags.BuildOptionsloses two maps and their entries. TheBuildOptionsScopeValue.Keys, each of which embedded a fullBuildOptions, and their values disappear. The details value of a configuration is one instance shared by all targets in it.Package.Two behavior notes, both shared with #28574: a flag scoped
exec:--host_foono longer causes--host_foo's default value to be injected as an explicit value into the target configuration, since the exec transition now takes the value from the flag definition instead. And every configuration now depends on the packages defining its Starlark flags, so editing such a package dirties the configurations that set the flag, with change pruning limiting the fallout to configurations whose scope data actually changed.flags_scoping_integration_testsgained a case that edits a PROJECT.scl between builds to exercise this.Tested with the unit tests under
analysis,skyframe,runtime,buildtool,packagesandrules/configthat cover transitions, configurations, aspects, feature flags, producers and serialization, a newStarlarkBuildSettingsDetailsFunctionTest, and theflags_scoping_integration_testsandstarlark_configurations_testshell suites.Build API Changes
No
Checklist
Release Notes
RELNOTES: None