Unverified Commit cc057574 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: move import_usage computation into ImportMap (#92891)

I'm moving all of the export-related logic into `EvalContext` (and thus
`ImportMap`) so that `VarGraph` is only needed for computing the
references, and not the exports.
parent b91cff03
...@@ -31,9 +31,7 @@ use crate::{ ...@@ -31,9 +31,7 @@ use crate::{
AnalyzeMode, SpecifiedModuleType, AnalyzeMode, SpecifiedModuleType,
analyzer::{WellKnownObjectKind, is_unresolved}, analyzer::{WellKnownObjectKind, is_unresolved},
code_gen::CodeGen, code_gen::CodeGen,
references::{ references::{constant_value::parse_single_expr_lit, esm::EsmModuleItem},
constant_value::parse_single_expr_lit, esm::EsmModuleItem, for_each_ident_in_pat,
},
utils::{AstPathRange, unparen}, utils::{AstPathRange, unparen},
}; };
...@@ -308,30 +306,6 @@ impl AssignmentScopes { ...@@ -308,30 +306,6 @@ impl AssignmentScopes {
} }
} }
#[derive(Clone, Debug)]
pub enum DeclUsage {
SideEffects,
Bindings(FxHashSet<Id>),
}
impl Default for DeclUsage {
fn default() -> Self {
DeclUsage::Bindings(Default::default())
}
}
impl DeclUsage {
fn add_usage(&mut self, user: &Id) {
match self {
Self::Bindings(set) => {
set.insert(user.clone());
}
Self::SideEffects => {}
}
}
fn make_side_effects(&mut self) {
*self = Self::SideEffects;
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct VarGraph { pub struct VarGraph {
pub values: FxHashMap<Id, JsValue>, pub values: FxHashMap<Id, JsValue>,
...@@ -345,13 +319,6 @@ pub struct VarGraph { ...@@ -345,13 +319,6 @@ pub struct VarGraph {
pub effects: Vec<Effect>, pub effects: Vec<Effect>,
// Some unconditional codegens, usually for ESM items. // Some unconditional codegens, usually for ESM items.
pub code_gens: Vec<CodeGen>, pub code_gens: Vec<CodeGen>,
// ident -> immediate usage (top level decl)
pub decl_usages: FxHashMap<Id, DeclUsage>,
// import -> immediate usage (top level decl)
pub import_usages: FxHashMap<usize, DeclUsage>,
// export name -> top level decl
pub exports: FxHashMap<Atom, Id>,
} }
impl VarGraph { impl VarGraph {
...@@ -376,9 +343,6 @@ pub fn create_graph( ...@@ -376,9 +343,6 @@ pub fn create_graph(
free_var_ids: Default::default(), free_var_ids: Default::default(),
effects: Default::default(), effects: Default::default(),
code_gens: Default::default(), code_gens: Default::default(),
decl_usages: Default::default(),
import_usages: Default::default(),
exports: Default::default(),
}; };
m.visit_with_ast_path( m.visit_with_ast_path(
...@@ -429,7 +393,9 @@ impl EvalContext { ...@@ -429,7 +393,9 @@ impl EvalContext {
Self { Self {
unresolved_mark, unresolved_mark,
top_level_mark, top_level_mark,
imports: module.map_or(ImportMap::default(), |m| ImportMap::analyze(m, comments)), imports: module.map_or(ImportMap::default(), |m| {
ImportMap::analyze(unresolved_mark, m, comments)
}),
force_free_values, force_free_values,
} }
} }
...@@ -1014,15 +980,6 @@ mod analyzer_state { ...@@ -1014,15 +980,6 @@ mod analyzer_state {
early_return_stack: Vec<EarlyReturn>, early_return_stack: Vec<EarlyReturn>,
lexical_stack: Vec<LexicalContext>, lexical_stack: Vec<LexicalContext>,
var_decl_kind: Option<VarDeclKind>, var_decl_kind: Option<VarDeclKind>,
cur_top_level_decl_name: Option<Id>,
}
impl AnalyzerState {
/// Returns the identifier of the current top level declaration.
pub(super) fn cur_top_level_decl_name(&self) -> &Option<Id> {
&self.cur_top_level_decl_name
}
} }
impl Analyzer<'_> { impl Analyzer<'_> {
...@@ -1317,23 +1274,6 @@ mod analyzer_state { ...@@ -1317,23 +1274,6 @@ mod analyzer_state {
} }
always_returns always_returns
} }
/// Runs `visitor` with the current top level declaration identifier
pub(super) fn enter_top_level_decl<T>(
&mut self,
name: &Ident,
visitor: impl FnOnce(&mut Self) -> T,
) -> T {
let is_top_level_fn = self.state.cur_top_level_decl_name.is_none();
if is_top_level_fn {
self.state.cur_top_level_decl_name = Some(name.to_id());
}
let result = visitor(self);
if is_top_level_fn {
self.state.cur_top_level_decl_name = None;
}
result
}
} }
} }
...@@ -2128,10 +2068,8 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2128,10 +2068,8 @@ impl VisitAstPath for Analyzer<'_> {
decl: &'ast FnDecl, decl: &'ast FnDecl,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>, ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) { ) {
let fn_value = self.enter_top_level_decl(&decl.ident, |this| { let fn_value = self.enter_fn(&*decl.function, |this| {
this.enter_fn(&*decl.function, |this| { decl.visit_children_with_ast_path(this, ast_path);
decl.visit_children_with_ast_path(this, ast_path);
})
}); });
// Take all effects produced by the function and move them to hoisted effects since // Take all effects produced by the function and move them to hoisted effects since
...@@ -2608,17 +2546,6 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2608,17 +2546,6 @@ impl VisitAstPath for Analyzer<'_> {
if let Some((esm_reference_index, export)) = if let Some((esm_reference_index, export)) =
self.eval_context.imports.get_binding(&ident.to_id()) self.eval_context.imports.get_binding(&ident.to_id())
{ {
let usage = self
.data
.import_usages
.entry(esm_reference_index)
.or_default();
if let Some(top_level) = self.state.cur_top_level_decl_name() {
usage.add_usage(top_level);
} else {
usage.make_side_effects();
}
// Optimization: Look for a MemberExpr to see if we only access a few members from the // Optimization: Look for a MemberExpr to see if we only access a few members from the
// module, add those specific effects instead of depending on the entire module. // module, add those specific effects instead of depending on the entire module.
// //
...@@ -2645,7 +2572,7 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2645,7 +2572,7 @@ impl VisitAstPath for Analyzer<'_> {
} else { } else {
self.add_effect(Effect::ImportedBinding { self.add_effect(Effect::ImportedBinding {
esm_reference_index, esm_reference_index,
export, export: export.map(|e| RcStr::from(e.as_str())),
ast_path: as_parent_path(ast_path), ast_path: as_parent_path(ast_path),
span: ident.span(), span: ident.span(),
}) })
...@@ -2665,24 +2592,6 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2665,24 +2592,6 @@ impl VisitAstPath for Analyzer<'_> {
span: ident.span(), span: ident.span(),
}) })
} }
if !is_unresolved(ident, self.eval_context.unresolved_mark) {
if let Some(top_level) = self.state.cur_top_level_decl_name() {
if !(ident.sym == top_level.0 && ident.ctxt == top_level.1) {
self.data
.decl_usages
.entry(ident.to_id())
.or_default()
.add_usage(top_level);
}
} else {
self.data
.decl_usages
.entry(ident.to_id())
.or_default()
.make_side_effects();
}
}
} }
fn visit_this_expr<'ast: 'r, 'r>( fn visit_this_expr<'ast: 'r, 'r>(
...@@ -2998,32 +2907,6 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2998,32 +2907,6 @@ impl VisitAstPath for Analyzer<'_> {
node: &'ast ExportDecl, node: &'ast ExportDecl,
ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>, ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>,
) { ) {
match &node.decl {
Decl::Class(node) => {
self.data
.exports
.insert(node.ident.sym.clone(), node.ident.to_id());
}
Decl::Fn(node) => {
self.data
.exports
.insert(node.ident.sym.clone(), node.ident.to_id());
}
Decl::Var(node) => {
for VarDeclarator { name, .. } in &node.decls {
for_each_ident_in_pat(name, &mut |name, ctxt| {
self.data.exports.insert(name.clone(), (name.clone(), ctxt));
});
}
}
Decl::Using(_) => {
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export#:~:text=You%20cannot%20use%20export%20on%20a%20using%20or%20await%20using%20declaration
unreachable!("using declarations can not be exported");
}
Decl::TsInterface(_) | Decl::TsTypeAlias(_) | Decl::TsEnum(_) | Decl::TsModule(_) => {
// ignore typescript for code generation
}
};
self.add_esm_module_item(ast_path); self.add_esm_module_item(ast_path);
node.visit_children_with_ast_path(self, ast_path); node.visit_children_with_ast_path(self, ast_path);
} }
...@@ -3036,19 +2919,6 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -3036,19 +2919,6 @@ impl VisitAstPath for Analyzer<'_> {
if node.is_type_only { if node.is_type_only {
return; return;
} }
let export_name = node
.exported
.as_ref()
.unwrap_or(&node.orig)
.atom()
.into_owned();
self.data.exports.insert(
export_name,
match &node.orig {
ModuleExportName::Ident(ident) => ident.to_id(),
ModuleExportName::Str(_) => unreachable!("exporting a string should be impossible"),
},
);
node.visit_children_with_ast_path(self, ast_path); node.visit_children_with_ast_path(self, ast_path);
} }
......
...@@ -42,7 +42,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; ...@@ -42,7 +42,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{ use swc_core::{
atoms::{Atom, Wtf8Atom, atom}, atoms::{Atom, Wtf8Atom, atom},
common::{ common::{
GLOBALS, Globals, Span, Spanned, SyntaxContext, GLOBALS, Globals, Span, Spanned,
comments::{CommentKind, Comments}, comments::{CommentKind, Comments},
errors::{DiagnosticId, HANDLER, Handler, Level}, errors::{DiagnosticId, HANDLER, Handler, Level},
source_map::SmallPos, source_map::SmallPos,
...@@ -101,7 +101,7 @@ use crate::{ ...@@ -101,7 +101,7 @@ use crate::{
ConstantNumber, ConstantString, ConstantValue as JsConstantValue, JsValue, JsValueUrlKind, ConstantNumber, ConstantString, ConstantValue as JsConstantValue, JsValue, JsValueUrlKind,
ObjectPart, RequireContextValue, WellKnownFunctionKind, WellKnownObjectKind, ObjectPart, RequireContextValue, WellKnownFunctionKind, WellKnownObjectKind,
builtin::{early_replace_builtin, replace_builtin}, builtin::{early_replace_builtin, replace_builtin},
graph::{ConditionalKind, DeclUsage, Effect, EffectArg, VarGraph, create_graph}, graph::{ConditionalKind, Effect, EffectArg, VarGraph, create_graph},
imports::{ImportAnnotations, ImportAttributes, ImportMap, ImportedSymbol}, imports::{ImportAnnotations, ImportAttributes, ImportMap, ImportedSymbol},
linker::link, linker::link,
parse_require_context, side_effects, parse_require_context, side_effects,
...@@ -751,58 +751,6 @@ async fn analyze_ecmascript_module_internal( ...@@ -751,58 +751,6 @@ async fn analyze_ecmascript_module_internal(
.supports_block_scoping() .supports_block_scoping()
.await?; .await?;
let mut var_graph = {
let _span = tracing::trace_span!("analyze variable values").entered();
set_handler_and_globals(&handler, globals, || {
create_graph(program, eval_context, analyze_mode, supports_block_scoping)
})
};
let mut import_usage =
FxHashMap::with_capacity_and_hasher(var_graph.import_usages.len(), Default::default());
for (reference, usage) in &var_graph.import_usages {
// TODO make this more efficient, i.e. cache the result?
if let DeclUsage::Bindings(ids) = usage {
// compute transitive closure of `ids` over `top_level_mappings`
let mut visited = ids.clone();
let mut stack = ids.iter().collect::<Vec<_>>();
let mut has_global_usage = false;
while let Some(id) = stack.pop() {
match var_graph.decl_usages.get(id) {
Some(DeclUsage::SideEffects) => {
has_global_usage = true;
break;
}
Some(DeclUsage::Bindings(callers)) => {
for caller in callers {
if visited.insert(caller.clone()) {
stack.push(caller);
}
}
}
_ => {}
}
}
// Collect all `visited` declarations which are exported
import_usage.insert(
*reference,
if has_global_usage {
ImportUsage::TopLevel
} else {
ImportUsage::Exports(
var_graph
.exports
.iter()
.filter(|(_, id)| visited.contains(*id))
.map(|(exported, _)| exported.as_str().into())
.collect(),
)
},
);
}
}
let span = tracing::trace_span!("esm import references"); let span = tracing::trace_span!("esm import references");
let import_references = async { let import_references = async {
let mut import_references = Vec::with_capacity(eval_context.imports.references().len()); let mut import_references = Vec::with_capacity(eval_context.imports.references().len());
...@@ -843,7 +791,12 @@ async fn analyze_ecmascript_module_internal( ...@@ -843,7 +791,12 @@ async fn analyze_ecmascript_module_internal(
) )
.then(ModulePart::exports), .then(ModulePart::exports),
}, },
import_usage.get(&i).cloned().unwrap_or_default(), eval_context
.imports
.import_usage
.get(&i)
.cloned()
.unwrap_or_default(),
import_externals, import_externals,
options.tree_shaking_mode, options.tree_shaking_mode,
) )
...@@ -979,6 +932,13 @@ async fn analyze_ecmascript_module_internal( ...@@ -979,6 +932,13 @@ async fn analyze_ecmascript_module_internal(
.instrument(span) .instrument(span)
.await?; .await?;
let mut var_graph = {
let _span = tracing::trace_span!("analyze variable values").entered();
set_handler_and_globals(&handler, globals, || {
create_graph(program, eval_context, analyze_mode, supports_block_scoping)
})
};
let span = tracing::trace_span!("effects processing"); let span = tracing::trace_span!("effects processing");
async { async {
analysis.code_gens.extend(take(&mut var_graph.code_gens)); analysis.code_gens.extend(take(&mut var_graph.code_gens));
...@@ -3254,6 +3214,8 @@ async fn handle_free_var_reference( ...@@ -3254,6 +3214,8 @@ async fn handle_free_var_reference(
), ),
Default::default(), Default::default(),
export.clone().map(ModulePart::export), export.clone().map(ModulePart::export),
// TODO This could be optimized. E.g. referencing `Buffer` in some top
// level function could set ImportUsage properly here
ImportUsage::TopLevel, ImportUsage::TopLevel,
state.import_externals, state.import_externals,
state.tree_shaking_mode, state.tree_shaking_mode,
...@@ -3814,41 +3776,6 @@ async fn require_context_visitor( ...@@ -3814,41 +3776,6 @@ async fn require_context_visitor(
)) ))
} }
pub(crate) fn for_each_ident_in_pat(pat: &Pat, f: &mut impl FnMut(&Atom, SyntaxContext)) {
match pat {
Pat::Ident(BindingIdent { id, .. }) => {
f(&id.sym, id.ctxt);
}
Pat::Array(ArrayPat { elems, .. }) => elems.iter().for_each(|e| {
if let Some(e) = e {
for_each_ident_in_pat(e, f);
}
}),
Pat::Rest(RestPat { arg, .. }) => {
for_each_ident_in_pat(arg, f);
}
Pat::Object(ObjectPat { props, .. }) => {
props.iter().for_each(|p| match p {
ObjectPatProp::KeyValue(KeyValuePatProp { value, .. }) => {
for_each_ident_in_pat(value, f);
}
ObjectPatProp::Assign(AssignPatProp { key, .. }) => {
f(&key.sym, key.ctxt);
}
ObjectPatProp::Rest(RestPat { arg, .. }) => {
for_each_ident_in_pat(arg, f);
}
});
}
Pat::Assign(AssignPat { left, .. }) => {
for_each_ident_in_pat(left, f);
}
Pat::Invalid(_) | Pat::Expr(_) => {
panic!("Unexpected pattern while enumerating idents");
}
}
}
#[derive(Hash, Debug, Clone, Eq, PartialEq, TraceRawVcs, Encode, Decode)] #[derive(Hash, Debug, Clone, Eq, PartialEq, TraceRawVcs, Encode, Decode)]
pub struct AstPath( pub struct AstPath(
#[bincode(with_serde)] #[bincode(with_serde)]
......
...@@ -564,13 +564,16 @@ pub(super) async fn split_module(asset: Vc<EcmascriptModuleAsset>) -> Result<Vc< ...@@ -564,13 +564,16 @@ pub(super) async fn split_module(asset: Vc<EcmascriptModuleAsset>) -> Result<Vc<
.into_iter() .into_iter()
.map(|module| { .map(|module| {
let program = Program::Module(module); let program = Program::Module(module);
let eval_context = EvalContext::new(
Some(&program), let eval_context = GLOBALS.set(globals, || {
eval_context.unresolved_mark, EvalContext::new(
eval_context.top_level_mark, Some(&program),
eval_context.force_free_values.clone(), eval_context.unresolved_mark,
None, eval_context.top_level_mark,
); eval_context.force_free_values.clone(),
None,
)
});
ParseResult::resolved_cell(ParseResult::Ok { ParseResult::resolved_cell(ParseResult::Ok {
program, program,
...@@ -691,13 +694,15 @@ pub(crate) async fn part_of_module( ...@@ -691,13 +694,15 @@ pub(crate) async fn part_of_module(
})); }));
let program = Program::Module(module); let program = Program::Module(module);
let eval_context = EvalContext::new( let eval_context = GLOBALS.set(globals, || {
Some(&program), EvalContext::new(
eval_context.unresolved_mark, Some(&program),
eval_context.top_level_mark, eval_context.unresolved_mark,
eval_context.force_free_values.clone(), eval_context.top_level_mark,
None, eval_context.force_free_values.clone(),
); None,
)
});
return Ok(ParseResult::Ok { return Ok(ParseResult::Ok {
program, program,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment