From 9e6d00a6f3e18d8e87ff07e22c1e620f8d94947d Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 12 Nov 2023 11:05:31 +0530 Subject: [PATCH 1/3] Remove redundant pickle code --- src/bin/lpython.cpp | 13 +-- src/lpython/pickle.cpp | 232 ----------------------------------------- src/lpython/pickle.h | 10 +- 3 files changed, 8 insertions(+), 247 deletions(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 3d75cf7190..1cb8d2556e 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -6,8 +6,9 @@ #define CLI11_HAS_FILESYSTEM 0 #include -#include #include +#include +#include #include #include #include @@ -221,15 +222,15 @@ int emit_asr(const std::string &infile, pass_manager.apply_passes(al, asr, compiler_options.po, diagnostics); if (compiler_options.po.tree) { - std::cout << LCompilers::LPython::pickle_tree(*asr, + std::cout << LCompilers::pickle_tree(*asr, compiler_options.use_colors, with_intrinsic_modules) << std::endl; } else if (compiler_options.po.json) { - std::cout << LCompilers::LPython::pickle_json(*asr, lm, with_intrinsic_modules) << std::endl; + std::cout << LCompilers::pickle_json(*asr, lm, false, with_intrinsic_modules) << std::endl; } else if (compiler_options.po.visualize) { - std::string astr_data_json = LCompilers::LPython::pickle_json(*asr, lm, with_intrinsic_modules); + std::string astr_data_json = LCompilers::pickle_json(*asr, lm, false, with_intrinsic_modules); return visualize_json(astr_data_json, compiler_options.platform); } else { - std::cout << LCompilers::LPython::pickle(*asr, compiler_options.use_colors, + std::cout << LCompilers::pickle(*asr, compiler_options.use_colors, compiler_options.indent, with_intrinsic_modules) << std::endl; } return 0; @@ -1361,7 +1362,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) { asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input"); out = diagnostics.render(lm, compiler_options); if (asr.ok) { - out += LCompilers::LPython::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent, + out += LCompilers::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent, false /* with_intrinsic_modules */); } } diff --git a/src/lpython/pickle.cpp b/src/lpython/pickle.cpp index b2b64244c0..ef52293d4a 100644 --- a/src/lpython/pickle.cpp +++ b/src/lpython/pickle.cpp @@ -28,129 +28,6 @@ std::string pickle_python(AST::ast_t &ast, bool colors, bool indent) { return v.get_str(); } -/********************** ASR Pickle *******************/ -class ASRPickleVisitor : - public ASR::PickleBaseVisitor -{ -public: - bool show_intrinsic_modules; - - std::string get_str() { - return s; - } - void visit_symbol(const ASR::symbol_t &x) { - s.append(ASRUtils::symbol_parent_symtab(&x)->get_counter()); - s.append(" "); - if (use_colors) { - s.append(color(fg::yellow)); - } - s.append(ASRUtils::symbol_name(&x)); - if (use_colors) { - s.append(color(fg::reset)); - } - } - void visit_IntegerConstant(const ASR::IntegerConstant_t &x) { - s.append("("); - if (use_colors) { - s.append(color(style::bold)); - s.append(color(fg::magenta)); - } - s.append("IntegerConstant"); - if (use_colors) { - s.append(color(fg::reset)); - s.append(color(style::reset)); - } - s.append(" "); - if (use_colors) { - s.append(color(fg::cyan)); - } - s.append(std::to_string(x.m_n)); - if (use_colors) { - s.append(color(fg::reset)); - } - s.append(" "); - this->visit_ttype(*x.m_type); - s.append(")"); - } - void visit_Module(const ASR::Module_t &x) { - // hide intrinsic modules and numpy module by default - if (!show_intrinsic_modules && - (x.m_intrinsic || startswith(x.m_name, "numpy"))) { - s.append("("); - if (use_colors) { - s.append(color(style::bold)); - s.append(color(fg::magenta)); - } - s.append(x.m_intrinsic ? "IntrinsicModule" : "Module"); - if (use_colors) { - s.append(color(fg::reset)); - s.append(color(style::reset)); - } - s.append(" "); - s.append(x.m_name); - s.append(")"); - } else { - ASR::PickleBaseVisitor::visit_Module(x); - }; - } - - std::string convert_intrinsic_id(int x) { - std::string s; - if (use_colors) { - s.append(color(style::bold)); - s.append(color(fg::green)); - } - s.append(ASRUtils::get_intrinsic_name(x)); - if (use_colors) { - s.append(color(fg::reset)); - s.append(color(style::reset)); - } - return s; - } - - std::string convert_impure_intrinsic_id(int x) { - std::string s; - if (use_colors) { - s.append(color(style::bold)); - s.append(color(fg::green)); - } - s.append(ASRUtils::get_impure_intrinsic_name(x)); - if (use_colors) { - s.append(color(fg::reset)); - s.append(color(style::reset)); - } - return s; - } - - std::string convert_array_intrinsic_id(int x) { - std::string s; - if (use_colors) { - s.append(color(style::bold)); - s.append(color(fg::green)); - } - s.append(ASRUtils::get_array_intrinsic_name(x)); - if (use_colors) { - s.append(color(fg::reset)); - s.append(color(style::reset)); - } - return s; - } -}; - -std::string pickle(ASR::asr_t &asr, bool colors, bool indent, - bool show_intrinsic_modules) { - ASRPickleVisitor v; - v.use_colors = colors; - v.indent = indent; - v.show_intrinsic_modules = show_intrinsic_modules; - v.visit_asr(asr); - return v.get_str(); -} - -std::string pickle(ASR::TranslationUnit_t &asr, bool colors, bool indent, bool show_intrinsic_modules) { - return pickle((ASR::asr_t &)asr, colors, indent, show_intrinsic_modules); -} - /********************** AST Pickle Tree *******************/ class ASTTreeVisitor : public AST::TreeBaseVisitor { @@ -167,31 +44,6 @@ std::string pickle_tree_python(AST::ast_t &ast, bool colors) { return v.get_str(); } -/********************** ASR Pickle Tree *******************/ -class ASRTreeVisitor : - public ASR::TreeBaseVisitor -{ -public: - bool show_intrinsic_modules; - - std::string get_str() { - return s; - } - -}; - -std::string pickle_tree(ASR::asr_t &asr, bool colors, bool show_intrinsic_modules) { - ASRTreeVisitor v; - v.use_colors = colors; - v.show_intrinsic_modules = show_intrinsic_modules; - v.visit_asr(asr); - return v.get_str(); -} - -std::string pickle_tree(ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules) { - return pickle_tree((ASR::asr_t &)asr, colors, show_intrinsic_modules); -} - /********************** AST Pickle Json *******************/ class ASTJsonVisitor : public LPython::AST::JsonBaseVisitor @@ -210,88 +62,4 @@ std::string pickle_json(LPython::AST::ast_t &ast, LocationManager &lm) { return v.get_str(); } -/********************** ASR Pickle Json *******************/ -class ASRJsonVisitor : - public ASR::JsonBaseVisitor -{ -public: - bool show_intrinsic_modules; - - using ASR::JsonBaseVisitor::JsonBaseVisitor; - - std::string get_str() { - return s; - } - - void visit_symbol(const ASR::symbol_t &x) { - s.append("\""); - s.append(ASRUtils::symbol_name(&x)); - s.append(" (SymbolTable"); - s.append(ASRUtils::symbol_parent_symtab(&x)->get_counter()); - s.append(")\""); - } - - void visit_Module(const ASR::Module_t &x) { - // hide intrinsic modules and numpy module by default - if (!show_intrinsic_modules && - (x.m_intrinsic || startswith(x.m_name, "numpy"))) { - s.append("{"); - inc_indent(); s.append("\n" + indtd); - s.append("\"node\": \"Module\""); - s.append(",\n" + indtd); - s.append("\"fields\": {"); - inc_indent(); s.append("\n" + indtd); - s.append("\"name\": "); - s.append("\"" + std::string(x.m_name) + "\""); - s.append(",\n" + indtd); - s.append("\"dependencies\": "); - s.append("["); - if (x.n_dependencies > 0) { - inc_indent(); s.append("\n" + indtd); - for (size_t i=0; i::visit_Module(x); - } - } -}; - -std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool show_intrinsic_modules) { - ASRJsonVisitor v(lm); - v.show_intrinsic_modules = show_intrinsic_modules; - v.visit_asr(asr); - return v.get_str(); -} - -std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool show_intrinsic_modules) { - return pickle_json((ASR::asr_t &)asr, lm, show_intrinsic_modules); -} - } diff --git a/src/lpython/pickle.h b/src/lpython/pickle.h index 64e7f47850..ea193f9513 100644 --- a/src/lpython/pickle.h +++ b/src/lpython/pickle.h @@ -9,20 +9,12 @@ namespace LCompilers::LPython { // Pickle an ASR node std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false); - std::string pickle(ASR::asr_t &asr, bool colors=false, bool indent=false, - bool show_intrinsic_modules=false); - std::string pickle(ASR::TranslationUnit_t &asr, bool colors=false, - bool indent=false, bool show_intrinsic_modules=false); // Print the tree structure std::string pickle_tree_python(AST::ast_t &ast, bool colors=true); - std::string pickle_tree(ASR::asr_t &asr, bool colors, bool show_intrinsic_modules); - std::string pickle_tree(ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules); + // Print the ASR in json format std::string pickle_json(AST::ast_t &ast, LocationManager &lm); - std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool show_intrinsic_modules); - std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool show_intrinsic_modules); - } #endif // LFORTRAN_PICKLE_H From 945f4aa39db523ad0ff9a2460c3ac776bea3c621 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 12 Nov 2023 11:20:37 +0530 Subject: [PATCH 2/3] Remove unused generate_visualize_html() --- src/lpython/utils.cpp | 154 ------------------------------------------ src/lpython/utils.h | 3 - 2 files changed, 157 deletions(-) diff --git a/src/lpython/utils.cpp b/src/lpython/utils.cpp index 6484a88448..849cf540ed 100644 --- a/src/lpython/utils.cpp +++ b/src/lpython/utils.cpp @@ -133,158 +133,4 @@ bool path_exists(std::string path) { int32_t get_exit_status(int32_t err) { return (((err) >> 8) & 0x000000ff); } - -std::string generate_visualize_html(std::string &astr_data_json) { - std::hash hasher; - std::ofstream out; - std::string file_name = "visualize" + std::to_string(hasher(astr_data_json)) + ".html"; - out.open(file_name); - out << R"( - - - LCompilers AST/R Visualization - - - - - - - \n"; - out << R"( - - - - - -)"; - return file_name; -} - } diff --git a/src/lpython/utils.h b/src/lpython/utils.h index f6b9f88f9b..daa3a71e0c 100644 --- a/src/lpython/utils.h +++ b/src/lpython/utils.h @@ -14,9 +14,6 @@ bool path_exists(std::string path); // Decodes the exit status code of the process (in Unix) int32_t get_exit_status(int32_t err); - -std::string generate_visualize_html(std::string &astr_data_json); - } // LFortran #endif // LFORTRAN_UTILS_H From 2591cb8746e255c8fe84c9d95600eea5dacbb63e Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 12 Nov 2023 11:30:30 +0530 Subject: [PATCH 3/3] Add LPython changes so refs stay the same It seem LPython and LFortran diverge slightly here. If we update it as per LFortran, then the reference tests change. --- src/libasr/pickle.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libasr/pickle.cpp b/src/libasr/pickle.cpp index 5c54d6909b..79e71713a2 100644 --- a/src/libasr/pickle.cpp +++ b/src/libasr/pickle.cpp @@ -52,13 +52,13 @@ class ASRPickleVisitor : } void visit_Module(const ASR::Module_t &x) { if (!show_intrinsic_modules && - startswith(x.m_name, "lfortran_intrinsic_")) { + (x.m_intrinsic || startswith(x.m_name, "lfortran_intrinsic_") || startswith(x.m_name, "numpy"))) { s.append("("); if (use_colors) { s.append(color(style::bold)); s.append(color(fg::magenta)); } - s.append("IntrinsicModule"); + s.append(x.m_intrinsic ? "IntrinsicModule" : "Module"); if (use_colors) { s.append(color(fg::reset)); s.append(color(style::reset));