Skip to content

Commit b54c7a2

Browse files
committed
Support print asr after pass.
Now we will generate a log folder to store each asr pass log. And add a helper function to select file to store the asr.
1 parent 447f2da commit b54c7a2

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/bin/lpython.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ int main(int argc, char *argv[])
789789
bool static_link = false;
790790
std::string arg_backend = "llvm";
791791
std::string arg_kernel_f;
792+
bool print_after_asr_pass = false;
792793
bool print_targets = false;
793794
bool print_rtlib_header_dir = false;
794795

@@ -855,6 +856,7 @@ int main(int argc, char *argv[])
855856
app.add_option("--target", compiler_options.target, "Generate code for the given target")->capture_default_str();
856857
app.add_flag("--print-targets", print_targets, "Print the registered targets");
857858
app.add_flag("--get-rtlib-header-dir", print_rtlib_header_dir, "Print the path to the runtime library header file");
859+
app.add_flag("--print-after-asr-pass", print_after_asr_pass, "Print ASR output after every pass");
858860

859861
// LSP specific options
860862
app.add_flag("--show-errors", show_errors, "Show errors when LSP is running in the background");
@@ -864,6 +866,7 @@ int main(int argc, char *argv[])
864866
lpython_pass_manager.use_optimization_passes();
865867
}
866868

869+
867870
/*
868871
* Subcommands:
869872
*/
@@ -957,6 +960,10 @@ int main(int argc, char *argv[])
957960
return 1;
958961
}
959962

963+
if( print_after_asr_pass ) {
964+
lpython_pass_manager.print_after_asr_pass();
965+
}
966+
960967
if (arg_backend == "llvm") {
961968
backend = Backend::llvm;
962969
} else if (arg_backend == "cpp") {

src/libasr/pass/pass_manager.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#include <lpython/utils.h>
1515
#endif
1616

17+
#if __has_include(<lpython/utils.h>)
18+
#include <lpython/pickle.h>
19+
#endif
20+
1721
#include <libasr/pass/do_loops.h>
1822
#include <libasr/pass/for_all.h>
1923
#include <libasr/pass/implied_do_loops.h>
@@ -37,7 +41,6 @@
3741
#include <libasr/pass/loop_vectorise.h>
3842
#include <libasr/pass/update_array_dim_intrinsic_calls.h>
3943
#include <libasr/pass/pass_array_by_data.h>
40-
4144
#include <map>
4245
#include <vector>
4346

@@ -77,18 +80,32 @@ namespace LCompilers {
7780

7881
bool is_fast;
7982
bool apply_default_passes;
83+
bool do_print_after_asr_pass;
8084

8185
void _apply_passes(Allocator& al, LFortran::ASR::TranslationUnit_t* asr,
8286
std::vector<std::string>& passes, PassOptions pass_options) {
8387
pass_options.runtime_library_dir = LFortran::get_runtime_library_dir();
88+
if (do_print_after_asr_pass) {
89+
std::string cmd = "mkdir asr_pass_log";
90+
int err = system(cmd.c_str());
91+
if (err) {
92+
std::cout << "The command '" + cmd + "' failed." << std::endl;
93+
exit(1);
94+
}
95+
LFortran::write_asr_to_file("./asr_pass_log/0_raw.log", *asr, false, false);
96+
}
8497
for (size_t i = 0; i < passes.size(); i++) {
8598
_passes_db[passes[i]](al, *asr, pass_options);
99+
if (do_print_after_asr_pass) {
100+
std::string file = "./asr_pass_log/" + std::to_string(i+1)+"_"+passes[i]+".log";
101+
LFortran::write_asr_to_file(file,*asr, false, false);
102+
}
86103
}
87104
}
88105

89106
public:
90107

91-
PassManager(): is_fast{false}, apply_default_passes{false} {
108+
PassManager(): is_fast{false}, apply_default_passes{false}, do_print_after_asr_pass(false) {
92109
_passes = {
93110
"global_stmts",
94111
"class_constructor",
@@ -171,6 +188,12 @@ namespace LCompilers {
171188
}
172189
}
173190

191+
192+
193+
void print_after_asr_pass() {
194+
do_print_after_asr_pass = true;
195+
}
196+
174197
void use_optimization_passes() {
175198
is_fast = true;
176199
}

src/lpython/pickle.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string>
2+
#include <fstream>
23

34
#include <lpython/pickle.h>
45
#include <lpython/pickle.h>
@@ -95,7 +96,7 @@ class ASRTreeVisitor :
9596
{
9697
public:
9798
bool show_intrinsic_modules;
98-
99+
99100
std::string get_str() {
100101
return s;
101102
}
@@ -114,4 +115,11 @@ std::string pickle_tree(LFortran::ASR::TranslationUnit_t &asr, bool colors, bool
114115
return pickle_tree((ASR::asr_t &)asr, colors, show_intrinsic_modules);
115116
}
116117

118+
void write_asr_to_file(std::string file, LFortran::ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules) {
119+
std::ofstream f;
120+
f.open(file);
121+
f << pickle_tree(asr, colors, show_intrinsic_modules);
122+
f.close();
123+
}
124+
117125
}

src/lpython/pickle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace LFortran {
1414
// Print the tree structure
1515
std::string pickle_tree(LFortran::ASR::asr_t &asr, bool colors, bool show_intrinsic_modules);
1616
std::string pickle_tree(LFortran::ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules);
17+
void write_asr_to_file(std::string file, LFortran::ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules);
1718

1819
}
1920

0 commit comments

Comments
 (0)