From 078c14cb13d84c6ce1f56dcb3dba3e975c3a2f52 Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 2 Mar 2023 08:33:54 -0800 Subject: [PATCH] Memoize IncompatibleTargetChecker.createDirectlyIncompatibleTarget. This uses StateMachine to avoid recomputation on restart. PiperOrigin-RevId: 513546261 Change-Id: Ice4245a72fe29ccd8dda040910460626d5041512 --- .../google/devtools/build/lib/analysis/BUILD | 3 +- .../IncompatibleTargetChecker.java | 151 ++++++++++-------- .../lib/skyframe/PrerequisiteProducer.java | 61 ++++++- 3 files changed, 142 insertions(+), 73 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/BUILD b/src/main/java/com/google/devtools/build/lib/analysis/BUILD index def0888963071f..a6c1ab6b77d1bc 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/BUILD +++ b/src/main/java/com/google/devtools/build/lib/analysis/BUILD @@ -2256,13 +2256,13 @@ java_library( "//src/main/java/com/google/devtools/build/lib/analysis:config/config_conditions", "//src/main/java/com/google/devtools/build/lib/analysis:configured_target", "//src/main/java/com/google/devtools/build/lib/analysis:configured_target_value", - "//src/main/java/com/google/devtools/build/lib/analysis:dependency", "//src/main/java/com/google/devtools/build/lib/analysis:dependency_kind", "//src/main/java/com/google/devtools/build/lib/analysis:target_and_configuration", "//src/main/java/com/google/devtools/build/lib/analysis/platform", "//src/main/java/com/google/devtools/build/lib/analysis/platform:utils", "//src/main/java/com/google/devtools/build/lib/cmdline", "//src/main/java/com/google/devtools/build/lib/collect/nestedset", + "//src/main/java/com/google/devtools/build/lib/events", "//src/main/java/com/google/devtools/build/lib/packages", "//src/main/java/com/google/devtools/build/lib/packages:configured_attribute_mapper", "//src/main/java/com/google/devtools/build/lib/skyframe:configured_target_and_data", @@ -2270,6 +2270,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/skyframe:rule_configured_target_value", "//src/main/java/com/google/devtools/build/lib/util", "//src/main/java/com/google/devtools/build/skyframe", + "//src/main/java/com/google/devtools/build/skyframe:skyframe-objects", "//third_party:guava", "//third_party:jsr305", ], diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java index 2f4bb040daf303..b22578cbfecc27 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java @@ -14,7 +14,6 @@ package com.google.devtools.build.lib.analysis.constraints; -import static com.google.common.base.Predicates.notNull; import static com.google.common.collect.ImmutableList.toImmutableList; import com.google.common.collect.ImmutableList; @@ -22,7 +21,6 @@ import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.ConfiguredTarget; import com.google.devtools.build.lib.analysis.ConfiguredTargetValue; -import com.google.devtools.build.lib.analysis.Dependency; import com.google.devtools.build.lib.analysis.DependencyKind; import com.google.devtools.build.lib.analysis.FileProvider; import com.google.devtools.build.lib.analysis.FilesToRunProvider; @@ -44,6 +42,7 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.collect.nestedset.Order; +import com.google.devtools.build.lib.events.ExtendedEventHandler; import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.ConfiguredAttributeMapper; import com.google.devtools.build.lib.packages.Package; @@ -55,10 +54,10 @@ import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; import com.google.devtools.build.lib.skyframe.RuleConfiguredTargetValue; import com.google.devtools.build.lib.util.OrderedSetMultimap; -import com.google.devtools.build.skyframe.SkyFunction.Environment; -import com.google.devtools.build.skyframe.SkyframeLookupResult; -import java.util.List; +import com.google.devtools.build.skyframe.SkyValue; +import com.google.devtools.build.skyframe.state.StateMachine; import java.util.Optional; +import java.util.function.Consumer; import javax.annotation.Nullable; /** @@ -87,81 +86,105 @@ public class IncompatibleTargetChecker { /** * Creates an incompatible configured target if it is "directly incompatible". * - *

In other words, this function checks if a target is incompatible because of its + *

In other words, this state machine checks if a target is incompatible because of its * "target_compatible_with" attribute. * - *

This function returns a nullable {@code Optional} of a {@link RuleConfiguredTargetValue}. - * This provides three states of return values: + *

Outputs an {@code Optional} {@link RuleConfiguredTargetValue} as follows. * *

*/ - @Nullable - public static Optional createDirectlyIncompatibleTarget( - TargetAndConfiguration targetAndConfiguration, - ConfigConditions configConditions, - Environment env, - @Nullable PlatformInfo platformInfo, - NestedSetBuilder transitivePackages) - throws InterruptedException { - Target target = targetAndConfiguration.getTarget(); - Rule rule = target.getAssociatedRule(); + public static class IncompatibleTargetProducer implements StateMachine, Consumer { + private final Target target; + @Nullable // Non-null when the target has an associated rule. + private final BuildConfigurationValue configuration; + private final ConfigConditions configConditions; + @Nullable private final PlatformInfo platformInfo; + @Nullable private final NestedSetBuilder transitivePackages; - if (rule == null || rule.getRuleClass().equals("toolchain") || platformInfo == null) { - return Optional.empty(); + private final ResultSink sink; + + private final ImmutableList.Builder invalidConstraintValuesBuilder = + new ImmutableList.Builder<>(); + + /** Sink for the output of this state machine. */ + public interface ResultSink { + void accept(Optional incompatibleTarget); } - // Retrieve the label list for the target_compatible_with attribute. - BuildConfigurationValue configuration = targetAndConfiguration.getConfiguration(); - ConfiguredAttributeMapper attrs = - ConfiguredAttributeMapper.of(rule, configConditions.asProviders(), configuration); - if (!attrs.has("target_compatible_with", BuildType.LABEL_LIST)) { - return Optional.empty(); + public IncompatibleTargetProducer( + Target target, + @Nullable BuildConfigurationValue configuration, + ConfigConditions configConditions, + @Nullable PlatformInfo platformInfo, + @Nullable NestedSetBuilder transitivePackages, + ResultSink sink) { + this.target = target; + this.configuration = configuration; + this.configConditions = configConditions; + this.platformInfo = platformInfo; + this.transitivePackages = transitivePackages; + this.sink = sink; } - // Resolve the constraint labels. - List