Skip to content

Commit

Permalink
Extract out running analysis for anon target dependents
Browse files Browse the repository at this point in the history
Summary: Another attempt to make the code more organized - all the resolve stuff should be grouped together, and we should break up `anon_targets.rs` a bit more if possible

Reviewed By: cjhopman

Differential Revision: D52741556

fbshipit-source-id: c8358ca833670539a16cabad12b0b7016c6d3487
  • Loading branch information
wendy728 authored and facebook-github-bot committed Jan 19, 2024
1 parent 4e5512d commit 59da8c7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
54 changes: 52 additions & 2 deletions app/buck2_anon_target/src/anon_target_attr_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use buck2_analysis::attrs::resolve::attr_type::arg::ConfiguredStringWithMacrosEx
use buck2_analysis::attrs::resolve::attr_type::dep::DepAttrTypeExt;
use buck2_analysis::attrs::resolve::ctx::AttrResolutionContext;
use buck2_artifact::artifact::artifact_type::Artifact;
use buck2_build_api::analysis::calculation::RuleAnalysisCalculation;
use buck2_build_api::interpreter::rule_defs::artifact::StarlarkArtifact;
use buck2_build_api::interpreter::rule_defs::provider::collection::FrozenProviderCollectionValue;
use buck2_build_api::keep_going;
use buck2_core::package::PackageLabel;
use buck2_core::provider::label::ConfiguredProvidersLabel;
use buck2_core::target::configured_target_label::ConfiguredTargetLabel;
Expand All @@ -23,13 +26,16 @@ use buck2_interpreter::types::configured_providers_label::StarlarkProvidersLabel
use buck2_node::attrs::attr_type::dep::DepAttrType;
use buck2_node::attrs::attr_type::query::ResolvedQueryLiterals;
use buck2_node::attrs::configured_traversal::ConfiguredAttrTraversal;
use dice::DiceComputations;
use dupe::Dupe;
use futures::stream::FuturesUnordered;
use starlark::values::dict::Dict;
use starlark::values::tuple::AllocTuple;
use starlark::values::Value;
use starlark_map::small_map::SmallMap;

use crate::anon_target_attr::AnonTargetAttr;
use crate::anon_targets::get_artifact_from_anon_target_analysis;
use crate::anon_targets::AnonTargetKey;
use crate::anon_targets::AnonTargetsError;
use crate::promise_artifacts::PromiseArtifactAttr;
Expand All @@ -38,7 +44,7 @@ use crate::promise_artifacts::PromiseArtifactAttr;
// always be inherited from the anon target.
pub(crate) struct AnonTargetAttrResolutionContext<'v> {
#[allow(unused)] // TODO(@wendyy)
pub(crate) promised_artifacts_map: HashMap<PromiseArtifactAttr, Artifact>,
pub(crate) promised_artifacts_map: HashMap<&'v PromiseArtifactAttr, Artifact>,
pub(crate) rule_analysis_attr_resolution_ctx: RuleAnalysisAttrResolutionContext<'v>,
}

Expand Down Expand Up @@ -130,10 +136,16 @@ impl AnonTargetAttrResolution for AnonTargetAttr {
// Container for things that require looking up analysis results in order to resolve the attribute.
pub(crate) struct AnonTargetDependents {
pub(crate) deps: Vec<ConfiguredTargetLabel>,
#[allow(unused)] // TODO(@wendyy)
pub(crate) promise_artifacts: Vec<PromiseArtifactAttr>,
}

// Container for analysis results of the anon target dependents.
pub(crate) struct AnonTargetDependentAnalysisResults<'v> {
pub(crate) dep_analysis_results:
HashMap<&'v ConfiguredTargetLabel, FrozenProviderCollectionValue>,
pub(crate) promised_artifacts: HashMap<&'v PromiseArtifactAttr, Artifact>,
}

impl AnonTargetDependents {
pub(crate) fn get_dependents(
anon_target: &AnonTargetKey,
Expand Down Expand Up @@ -166,4 +178,42 @@ impl AnonTargetDependents {
promise_artifacts,
})
}

pub(crate) async fn get_analysis_results<'v>(
&'v self,
dice: &'v DiceComputations,
) -> anyhow::Result<AnonTargetDependentAnalysisResults<'v>> {
let dep_analysis_results: HashMap<_, _> = keep_going::try_join_all(
dice,
self.deps
.iter()
.map(async move |dep| {
let res = dice
.get_analysis_result(dep)
.await
.and_then(|v| v.require_compatible());
res.map(|x| (dep, x.providers().dupe()))
})
.collect::<FuturesUnordered<_>>(),
)
.await?;

let promised_artifacts: HashMap<_, _> = keep_going::try_join_all(
dice,
self.promise_artifacts
.iter()
.map(async move |promise_artifact_attr| {
get_artifact_from_anon_target_analysis(&promise_artifact_attr.id, dice)
.await
.map(|artifact| (promise_artifact_attr, artifact))
})
.collect::<FuturesUnordered<_>>(),
)
.await?;

Ok(AnonTargetDependentAnalysisResults {
dep_analysis_results,
promised_artifacts,
})
}
}
25 changes: 4 additions & 21 deletions app/buck2_anon_target/src/anon_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use buck2_artifact::artifact::artifact_type::Artifact;
use buck2_build_api::analysis::anon_promises_dyn::AnonPromisesDyn;
use buck2_build_api::analysis::anon_targets_registry::AnonTargetsRegistryDyn;
use buck2_build_api::analysis::anon_targets_registry::ANON_TARGET_REGISTRY_NEW;
use buck2_build_api::analysis::calculation::RuleAnalysisCalculation;
use buck2_build_api::analysis::registry::AnalysisRegistry;
use buck2_build_api::analysis::AnalysisResult;
use buck2_build_api::artifact_groups::promise::PromiseArtifact;
Expand All @@ -36,7 +35,6 @@ use buck2_build_api::interpreter::rule_defs::context::AnalysisContext;
use buck2_build_api::interpreter::rule_defs::plugins::AnalysisPlugins;
use buck2_build_api::interpreter::rule_defs::provider::collection::FrozenProviderCollectionValue;
use buck2_build_api::interpreter::rule_defs::provider::collection::ProviderCollection;
use buck2_build_api::keep_going;
use buck2_configured::nodes::calculation::find_execution_platform_by_configuration;
use buck2_core::base_deferred_key::BaseDeferredKey;
use buck2_core::base_deferred_key::BaseDeferredKeyDyn;
Expand Down Expand Up @@ -83,7 +81,6 @@ use derive_more::Display;
use dice::DiceComputations;
use dice::Key;
use dupe::Dupe;
use futures::stream::FuturesUnordered;
use futures::Future;
use futures::FutureExt;
use starlark::any::AnyLifetime;
Expand Down Expand Up @@ -300,20 +297,7 @@ impl AnonTargetKey {

async fn run_analysis_impl(&self, dice: &DiceComputations) -> anyhow::Result<AnalysisResult> {
let dependents = AnonTargetDependents::get_dependents(self)?;
let deps = dependents.deps;
let dep_analysis_results: HashMap<_, _> = keep_going::try_join_all(
dice,
deps.iter()
.map(async move |dep| {
let res = dice
.get_analysis_result(dep)
.await
.and_then(|v| v.require_compatible());
res.map(|x| (dep, x.providers().dupe()))
})
.collect::<FuturesUnordered<_>>(),
)
.await?;
let dependents_analyses = dependents.get_analysis_results(dice).await?;

let exec_resolution = ExecutionPlatformResolution::new(
Some(
Expand Down Expand Up @@ -348,14 +332,13 @@ impl AnonTargetKey {
// No attributes are allowed to contain macros or other stuff, so an empty resolution context works
let rule_analysis_attr_resolution_ctx = RuleAnalysisAttrResolutionContext {
module: &env,
dep_analysis_results,
dep_analysis_results: dependents_analyses.dep_analysis_results,
query_results: HashMap::new(),
execution_platform_resolution: exec_resolution.clone(),
};

let resolution_ctx = AnonTargetAttrResolutionContext {
// TODO(@wendyy) - populate
promised_artifacts_map: HashMap::new(),
promised_artifacts_map: dependents_analyses.promised_artifacts,
rule_analysis_attr_resolution_ctx,
};

Expand Down Expand Up @@ -588,7 +571,7 @@ pub(crate) fn init_get_promised_artifact() {
});
}

async fn get_artifact_from_anon_target_analysis<'v>(
pub(crate) async fn get_artifact_from_anon_target_analysis<'v>(
promise_id: &'v PromiseArtifactId,
ctx: &'v DiceComputations,
) -> anyhow::Result<Artifact> {
Expand Down
2 changes: 1 addition & 1 deletion app/buck2_anon_target/src/promise_artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl PromiseArtifactRegistry {
#[allow(unused)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Allocative)]
pub(crate) struct PromiseArtifactAttr {
id: PromiseArtifactId,
pub(crate) id: PromiseArtifactId,
short_path: Option<ForwardRelativePathBuf>,
}

Expand Down

0 comments on commit 59da8c7

Please sign in to comment.