-
Notifications
You must be signed in to change notification settings - Fork 14
feat(DeadCodeElimination - While loop and Function wrapper): Add WhileStatementElimination and UnusedFunctionElimination in DCE pass #39
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
base: dce
Are you sure you want to change the base?
Changes from all commits
d57851b
869e3df
b399cab
017c8f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,20 @@ | |
|
|
||
| #include "maldoca/js/ir/transforms/dead_code_elimination/pass.h" | ||
|
|
||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "absl/container/flat_hash_map.h" | ||
| #include "llvm/ADT/STLExtras.h" | ||
| #include "maldoca/js/ast/ast.generated.h" | ||
| #include "maldoca/js/ir/ir.h" | ||
| #include "maldoca/js/ir/jsir_utils.h" | ||
| #include "mlir/IR/Block.h" | ||
| #include "mlir/IR/Operation.h" | ||
| #include "mlir/IR/Value.h" | ||
| #include "maldoca/js/ir/ir.h" | ||
|
|
||
| namespace maldoca { | ||
|
|
||
| void IfStatementElimination(mlir::Operation* root_op) { | ||
| root_op->walk([&](JshirIfStatementOp op) { | ||
| auto condition = op.getTest().getDefiningOp<JsirBooleanLiteralOp>(); | ||
|
|
@@ -56,7 +63,165 @@ void IfStatementElimination(mlir::Operation* root_op) { | |
| }); | ||
| } | ||
|
|
||
| void WhileStatementElimination(mlir::Operation* root_op) { | ||
| root_op->walk([&](JshirWhileStatementOp op) { | ||
| auto condition_op = GetExprRegionOp<JsirBooleanLiteralOp>(op.getTest()); | ||
| if (!condition_op.ok()) { | ||
| return; | ||
| } | ||
|
|
||
| if (!condition_op->getValue()) { | ||
| // Condition is constantly false, eliminate the entire loop. | ||
| op.erase(); | ||
| } | ||
|
|
||
| // If condition is true, it's an infinite loop. For now, we leave it. | ||
| }); | ||
| } | ||
|
|
||
| struct SymbolInfo { | ||
| std::vector<mlir::Operation*> definitions; | ||
| std::vector<mlir::Operation*> references; | ||
| // Symbols defined within the scope of this symbol's definition (e.g., if | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this vector computed recursively? Or does it only include the variables one level deeper? For example, the current symbol |
||
| // this symbol is a function, these are variables defined inside it). | ||
| std::vector<JsSymbolId> inner_definitions; | ||
| // Symbols defined in the immediate parent scope of this symbol. | ||
| std::vector<JsSymbolId> outer_definitions; | ||
| }; | ||
|
|
||
| bool operator==(const JsSymbolId& lhs, const JsSymbolId& rhs) { | ||
| return std::forward_as_tuple(lhs.name(), lhs.def_scope_uid()) == | ||
| std::forward_as_tuple(rhs.name(), rhs.def_scope_uid()); | ||
| } | ||
|
|
||
| template <typename H> | ||
| H AbslHashValue(H h, const JsSymbolId& m) { | ||
| return H::combine(std::move(h), m.name(), m.def_scope_uid()); | ||
| } | ||
|
|
||
| JsSymbolId GetSymbolIdFromAttr(JsirSymbolIdAttr symbol_attr) { | ||
| std::string name = symbol_attr.getName().str(); | ||
| std::optional<int64_t> scope_uid = symbol_attr.getDefScopeId(); | ||
| return JsSymbolId{name, scope_uid}; | ||
| } | ||
|
|
||
| void UnusedFunctionElimination(mlir::Operation* root_op) { | ||
| absl::flat_hash_map<JsSymbolId, SymbolInfo> symbol_infos; | ||
|
|
||
| root_op->walk([&](mlir::Operation* op) { | ||
| // TODO: We can remove this special handling once the trivia is fixed. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you mention the issue number in the comment? // TODO(#xx) |
||
| if (llvm::isa<JsirExprsRegionEndOp>(op) || | ||
| llvm::isa<JsirExprRegionEndOp>(op)) { | ||
| return; | ||
| } | ||
| auto trivia = llvm::dyn_cast<JsirTriviaAttr>(op->getLoc()); | ||
| if (trivia == nullptr) { | ||
| return; | ||
| } | ||
| JsirSymbolIdAttr symbol = trivia.getReferencedSymbol(); | ||
| if (symbol != nullptr) { | ||
| symbol_infos[GetSymbolIdFromAttr(symbol)].references.push_back(op); | ||
| } | ||
|
|
||
| llvm::ArrayRef<JsirSymbolIdAttr> mlir_defined_symbols = | ||
| trivia.getDefinedSymbols(); | ||
| for (JsirSymbolIdAttr defined_symbol : mlir_defined_symbols) { | ||
| symbol_infos[GetSymbolIdFromAttr(defined_symbol)].definitions.push_back( | ||
| op); | ||
| } | ||
| }); | ||
|
|
||
| for (auto& [symbol, info] : symbol_infos) { | ||
| if (info.definitions.empty()) { | ||
| continue; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check this scenario: let a = 1;
b = a;
let c = b;I remember that JavaScript spec says |
||
| } | ||
| if (info.definitions.size() > 1) { | ||
| continue; | ||
| } | ||
|
|
||
| mlir::Operation* def_op = info.definitions[0]; | ||
| // Recursively traverse the parent chain of def_op by using getParentOp() | ||
| // and find the first op in the chain that defines symbols. In order to | ||
| // check if an op defines symbols, we can use the trivia attribute like | ||
| // above in the same file. | ||
| for (mlir::Operation* current_op = def_op->getParentOp(); | ||
| current_op != nullptr; current_op = current_op->getParentOp()) { | ||
| auto current_trivia = | ||
| llvm::dyn_cast<JsirTriviaAttr>(current_op->getLoc()); | ||
| if (current_trivia != nullptr) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (current_trivia == nullptr) {
continue;
} |
||
| llvm::ArrayRef<JsirSymbolIdAttr> outer_defined_symbols = | ||
| current_trivia.getDefinedSymbols(); | ||
| if (!outer_defined_symbols.empty()) { | ||
| for (JsirSymbolIdAttr outer_defined_symbol_attr : | ||
| outer_defined_symbols) { | ||
| JsSymbolId outer_symbol = | ||
| GetSymbolIdFromAttr(outer_defined_symbol_attr); | ||
| auto outer_symbol_info_it = symbol_infos.find(outer_symbol); | ||
| if (outer_symbol_info_it != symbol_infos.end()) { | ||
| info.outer_definitions.push_back(outer_symbol); | ||
| outer_symbol_info_it->second.inner_definitions.push_back(symbol); | ||
| } | ||
| } | ||
| break; // Found the first op in the chain that defines symbols | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check if info.definitions contains duplicates across symbol_infos? If so, | ||
| // print warning | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to make this comment consistent with the code. If you don't want spamming the log, you can use CHECK(...). |
||
| absl::flat_hash_map<mlir::Operation*, size_t> defined_ops; | ||
| for (const auto& [symbol, info] : symbol_infos) { | ||
| for (mlir::Operation* def_op : info.definitions) { | ||
| defined_ops[def_op]++; | ||
| } | ||
| } | ||
|
|
||
| for (auto& [symbol, info] : symbol_infos) { | ||
| if (info.references.empty()) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // TODO: Also consider if the symbol is written to. |
||
| for (mlir::Operation* def_op : info.definitions) { | ||
| if (llvm::isa<JsirFunctionDeclarationOp>(def_op)) { | ||
| // Recursively remove SymbolInfo records for all inner definitions. | ||
| // When we remove a function declaration, we are also removing all ops | ||
| // in the function body, including definitions of local variables. | ||
| // This means we need to remove those SymbolInfo records. | ||
| std::vector<JsSymbolId> worklist = info.inner_definitions; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a stack instead of a queue - does this matter? |
||
| while (!worklist.empty()) { | ||
| JsSymbolId current_symbol_id = worklist.back(); | ||
| worklist.pop_back(); | ||
|
|
||
| auto it = symbol_infos.find(current_symbol_id); | ||
| if (it != symbol_infos.end()) { | ||
| it->second.definitions.clear(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this unnecessary? We are erasing |
||
| worklist.insert(worklist.end(), | ||
| it->second.inner_definitions.begin(), | ||
| it->second.inner_definitions.end()); | ||
| symbol_infos.erase(it); | ||
| } | ||
| } | ||
| def_op->erase(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that we should also remove the symbol info entry for You might need to change the for (auto it = symbol_infos.begin(); it != symbol_infos.end(); ??) {
...
if `it` should be erased:
it = symbol_infos.erase(it);
if `it` should not be erased:
it++;
} |
||
| for (const auto& outer_symbol : info.outer_definitions) { | ||
| auto outer_it = symbol_infos.find(outer_symbol); | ||
| if (outer_it != symbol_infos.end()) { | ||
| auto& outer_info = outer_it->second; | ||
| outer_info.inner_definitions.erase( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: You can check if If you want to keep insertion order, use |
||
| std::remove(outer_info.inner_definitions.begin(), | ||
| outer_info.inner_definitions.end(), symbol), | ||
| outer_info.inner_definitions.end()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void DeadCodeElimination(mlir::Operation* root_op) { | ||
| IfStatementElimination(root_op); | ||
| WhileStatementElimination(root_op); | ||
| // TODO: Iteratively eliminate unused functions until no more can be | ||
| // eliminated. | ||
| // TODO: Update the AST after dynamic constant progropagation (before dce). | ||
| UnusedFunctionElimination(root_op); | ||
| } | ||
| } // namespace maldoca | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| load("//bazel:lit.bzl", "glob_lit_tests") | ||
|
|
||
| package(default_applicable_licenses = ["//:license"]) | ||
|
|
||
| licenses(["notice"]) | ||
|
|
||
| glob_lit_tests( | ||
| name = "all_tests", | ||
| data = [ | ||
| "input.js", | ||
| "output.generated.txt", | ||
| "//maldoca/js/ir:lit_test_files", | ||
| ], | ||
| test_file_exts = [ | ||
| "lit", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| To run manually: | ||
|
|
||
| ```shell | ||
| bazel run //maldoca/js/ir:jsir_gen -- \ | ||
| --input_file $(pwd)/maldoca/js/ir/transforms/dead_code_elimination/tests/high/input.js \ | ||
| --passes "source2ast,ast2hir,dead_code_elimination,hir2ast,ast2source" | ||
| ``` |
Uh oh!
There was an error while loading. Please reload this page.