Skip to content

Commit c297cdc

Browse files
committed
Support --dump-all-passes, --dump-all-passes-fortran
1 parent f5948d1 commit c297cdc

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/bin/lpython.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,45 @@ int emit_wat(const std::string &infile,
443443
return 0;
444444
}
445445

446+
int dump_all_passes(const std::string &infile,
447+
const std::string &runtime_library_dir,
448+
CompilerOptions &compiler_options) {
449+
std::string input = LCompilers::read_file(infile);
450+
451+
Allocator al(4*1024);
452+
LCompilers::LocationManager lm;
453+
LCompilers::diag::Diagnostics diagnostics;
454+
{
455+
LCompilers::LocationManager::FileLocations fl;
456+
fl.in_filename = infile;
457+
lm.files.push_back(fl);
458+
lm.file_ends.push_back(input.size());
459+
}
460+
461+
LCompilers::Result<LCompilers::LPython::AST::ast_t*> r = parse_python_file(
462+
al, runtime_library_dir, infile, diagnostics, 0, compiler_options.new_parser);
463+
std::cerr << diagnostics.render(lm, compiler_options);
464+
if (!r.ok) {
465+
return 1;
466+
}
467+
LCompilers::LPython::AST::ast_t* ast = r.result;
468+
diagnostics.diagnostics.clear();
469+
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
470+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
471+
std::cerr << diagnostics.render(lm, compiler_options);
472+
if (r1.ok) {
473+
LCompilers::PassManager pass_manager;
474+
compiler_options.po.always_run = true;
475+
compiler_options.po.run_fun = "f";
476+
pass_manager.dump_all_passes(al, r1.result, compiler_options.po, diagnostics, lm);
477+
std::cerr << diagnostics.render(lm, compiler_options);
478+
} else {
479+
LCOMPILERS_ASSERT(diagnostics.has_error())
480+
return 1;
481+
}
482+
return 0;
483+
}
484+
446485
#ifdef HAVE_LFORTRAN_RAPIDJSON
447486

448487
int get_symbols (const std::string &infile,
@@ -1549,6 +1588,8 @@ int main(int argc, char *argv[])
15491588
app.add_flag("--get-rtl-header-dir", print_rtl_header_dir, "Print the path to the runtime library header file");
15501589
app.add_flag("--get-rtl-dir", print_rtl_dir, "Print the path to the runtime library file");
15511590
app.add_flag("--verbose", compiler_options.po.verbose, "Print debugging statements");
1591+
app.add_flag("--dump-all-passes", compiler_options.po.dump_all_passes, "Apply all the passes and dump the ASR into a file");
1592+
app.add_flag("--dump-all-passes-fortran", compiler_options.po.dump_fortran, "Apply all passes and dump the ASR after each pass into fortran file");
15521593
app.add_flag("--cumulative", compiler_options.po.pass_cumulative, "Apply all the passes cumulatively till the given pass");
15531594
app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime");
15541595
app.add_flag("--enable-symengine", compiler_options.enable_symengine, "Enable Symengine runtime");
@@ -1726,6 +1767,10 @@ int main(int argc, char *argv[])
17261767
outfile = basename + ".out";
17271768
}
17281769

1770+
if (compiler_options.po.dump_fortran || compiler_options.po.dump_all_passes) {
1771+
dump_all_passes(arg_file, compiler_options);
1772+
}
1773+
17291774
// if (arg_E) {
17301775
// return emit_c_preprocessor(arg_file, compiler_options);
17311776
// }

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <libasr/pass/intrinsic_array_function_registry.h>
2424
#include <libasr/modfile.h>
2525
#include <libasr/casting_utils.h>
26+
#include <libasr/codegen/asr_to_fortran.h>
27+
#include <libasr/pickle.h>
2628

2729
#include <lpython/python_ast.h>
2830
#include <lpython/semantics/python_ast_to_asr.h>
@@ -7940,6 +7942,21 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
79407942
return res.error;
79417943
}
79427944
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(unit);
7945+
if (compiler_options.po.dump_all_passes) {
7946+
std::ofstream outfile ("pass_00_initial_asr_01.clj");
7947+
outfile << ";; ASR after SymbolTable Visitor\n" << pickle(*tu, false, true, compiler_options.po.with_intrinsic_mods) << "\n";
7948+
outfile.close();
7949+
}
7950+
if (compiler_options.po.dump_fortran) {
7951+
LCompilers::Result<std::string> fortran_code = LCompilers::asr_to_fortran(*tu, diagnostics, false, 4);
7952+
if (!fortran_code.ok) {
7953+
LCOMPILERS_ASSERT(diagnostics.has_error());
7954+
throw LCompilersException("Fortran code could not be generated after symbol_table_visitor");
7955+
}
7956+
std::ofstream outfile ("pass_fortran_00_initial_code_01.f90");
7957+
outfile << "! Fortran code after SymbolTable Visitor\n" << fortran_code.result << "\n";
7958+
outfile.close();
7959+
}
79437960
#if defined(WITH_LFORTRAN_ASSERT)
79447961
if (!asr_verify(*tu, true, diagnostics)) {
79457962
std::cerr << diagnostics.render2();
@@ -7955,6 +7972,21 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
79557972
} else {
79567973
return res2.error;
79577974
}
7975+
if (compiler_options.po.dump_all_passes) {
7976+
std::ofstream outfile ("pass_00_initial_asr_02.clj");
7977+
outfile << ";; Initial ASR after Body Visitor\n" << pickle(*tu, false, true, compiler_options.po.with_intrinsic_mods) << "\n";
7978+
outfile.close();
7979+
}
7980+
if (compiler_options.po.dump_fortran) {
7981+
LCompilers::Result<std::string> fortran_code = LCompilers::asr_to_fortran(*tu, diagnostics, false, 4);
7982+
if (!fortran_code.ok) {
7983+
LCOMPILERS_ASSERT(diagnostics.has_error());
7984+
throw LCompilersException("Fortran code could not be generated after body_visitor");
7985+
}
7986+
std::ofstream outfile ("pass_fortran_00_initial_code_02.f90");
7987+
outfile << "! Fortran code after Body Visitor\n" << fortran_code.result << "\n";
7988+
outfile.close();
7989+
}
79587990
#if defined(WITH_LFORTRAN_ASSERT)
79597991
diag::Diagnostics diagnostics;
79607992
if (!asr_verify(*tu, true, diagnostics)) {

0 commit comments

Comments
 (0)