Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inlining nested block statements in branch simplifier #1536

Merged
merged 2 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions ecmascript/transforms/optimization/src/simplify/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,15 @@ impl Fold for Remover {
Stmt::Block(BlockStmt { span, stmts }) => {
if stmts.is_empty() {
Stmt::Empty(EmptyStmt { span })
} else if stmts.len() == 1 && !is_block_scoped_stuff(&stmts[0]) {
} else if stmts.len() == 1
&& !is_block_scoped_stuff(&stmts[0])
&& stmt_depth(&stmts[0]) <= 1
{
stmts.into_iter().next().unwrap().fold_with(self)
} else {
Stmt::Block(BlockStmt { span, stmts })
}
}

Stmt::Try(TryStmt {
span,
block,
Expand Down Expand Up @@ -1532,3 +1534,36 @@ fn check_for_stopper(s: &[Stmt], only_conditional: bool) -> bool {
v.visit_stmts(s, &Invalid { span: DUMMY_SP } as _);
v.found
}

/// Finds the depth of statements without hitting a block
fn stmt_depth(s: &Stmt) -> u32 {
let mut depth = 0;

match s {
// Stop when hitting a statement we know can't increase statement depth.
Stmt::Block(_) | Stmt::Labeled(_) | Stmt::Switch(_) | Stmt::Decl(_) | Stmt::Expr(_) => {}
// Take the max depth of if statements
Stmt::If(i) => {
depth += 1;
if let Some(alt) = &i.alt {
depth += std::cmp::max(stmt_depth(&i.cons), stmt_depth(&alt));
} else {
depth += stmt_depth(&i.cons);
}
}
// These statements can have bodies without a wrapping block
Stmt::With(WithStmt { body, .. })
| Stmt::While(WhileStmt { body, .. })
| Stmt::DoWhile(DoWhileStmt { body, .. })
| Stmt::For(ForStmt { body, .. })
| Stmt::ForIn(ForInStmt { body, .. })
| Stmt::ForOf(ForOfStmt { body, .. }) => {
depth += 1;
depth += stmt_depth(&body);
}
// All other statements increase the depth by 1
_ => depth += 1,
}

depth
}
18 changes: 18 additions & 0 deletions ecmascript/transforms/optimization/src/simplify/branch/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1722,3 +1722,21 @@ c = 3;
console.log(c);",
);
}

#[test]
fn nested_block_stmt() {
test(
"if (Date.now() < 0) {
for(let i = 0; i < 10; i++){
if (Date.now() < 0) {
console.log(1);
}
}
} else {
console.log(2);
}",
"if (Date.now() < 0) {
for(let i = 0; i < 10; i++)if (Date.now() < 0) console.log(1);
} else console.log(2);",
);
}
6 changes: 4 additions & 2 deletions spack/tests/pass/issue-1328/case1/output/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,10 @@ var load = __spack_require__.bind(void 0, function(module, exports) {
this.method = "next";
this.arg = undefined;
this.tryEntries.forEach(resetTryEntry);
if (!skipTempReset) for(var name in this)// Not sure about the optimal order of these conditions:
if (name.charAt(0) === "t" && hasOwn.call(this, name) && !isNaN(+name.slice(1))) this[name] = undefined;
if (!skipTempReset) {
for(var name in this)// Not sure about the optimal order of these conditions:
if (name.charAt(0) === "t" && hasOwn.call(this, name) && !isNaN(+name.slice(1))) this[name] = undefined;
}
},
stop: function() {
this.done = true;
Expand Down