Skip to content

Simplifying registering passes in pass_manager.h #966

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

Merged
merged 6 commits into from
Aug 15, 2022
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
31 changes: 17 additions & 14 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ int emit_asr(const std::string &infile,
return 2;
}
LFortran::ASR::TranslationUnit_t* asr = r.result;
pass_manager.apply_passes(al, asr, "f", true);
LCompilers::PassOptions pass_options;
pass_options.run_fun = "f";
pass_options.always_run = true;
pass_manager.apply_passes(al, asr, pass_options);

if (compiler_options.tree) {
std::cout << LFortran::pickle_tree(*asr, compiler_options.use_colors,
Expand Down Expand Up @@ -304,33 +307,33 @@ int get_symbols (const std::string &infile,
rapidjson::Document end_detail(rapidjson::kObjectType);
rapidjson::Document location_object(rapidjson::kObjectType);
rapidjson::Document test_capture(rapidjson::kObjectType);

test_output.SetArray();

for (auto symbol : symbol_lists) {
uint32_t start_character = symbol.first_column;
uint32_t start_line = symbol.first_line;
uint32_t end_character = symbol.last_column;
uint32_t end_line = symbol.last_line;
std::string name = symbol.symbol_name;

range_object.SetObject();
rapidjson::Document::AllocatorType &allocator = range_object.GetAllocator();

start_detail.SetObject();
start_detail.AddMember("character", rapidjson::Value().SetInt(start_character), allocator);
start_detail.AddMember("line", rapidjson::Value().SetInt(start_line), allocator);
range_object.AddMember("start", start_detail, allocator);

end_detail.SetObject();
end_detail.AddMember("character", rapidjson::Value().SetInt(end_character), allocator);
end_detail.AddMember("line", rapidjson::Value().SetInt(end_line), allocator);
range_object.AddMember("end", end_detail, allocator);

location_object.SetObject();
location_object.AddMember("range", range_object, allocator);
location_object.AddMember("uri", rapidjson::Value().SetString("uri", allocator), allocator);

test_capture.SetObject();
test_capture.AddMember("kind", rapidjson::Value().SetInt(1), allocator);
test_capture.AddMember("location", location_object, allocator);
Expand All @@ -342,16 +345,16 @@ int get_symbols (const std::string &infile,
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
test_output.Accept(writer);
std::string resp_str( buffer.GetString() );

std::cout << resp_str;
}
else {
std::cout << "{}\n";
}

return 0;
}


int get_errors (const std::string &infile,
const std::string &runtime_library_dir,
Expand All @@ -363,7 +366,7 @@ int get_errors (const std::string &infile,
std::string input = LFortran::read_file(infile);
lm.init_simple(input);
LFortran::Result<LFortran::LPython::AST::ast_t*>
r1 = LFortran::parse_python_file(al, runtime_library_dir, infile,
r1 = LFortran::parse_python_file(al, runtime_library_dir, infile,
diagnostics, compiler_options.new_parser);
if (r1.ok) {
LFortran::LPython::AST::ast_t* ast = r1.result;
Expand Down Expand Up @@ -396,7 +399,7 @@ int get_errors (const std::string &infile,
}
}
rapidjson::Document range_obj(rapidjson::kObjectType);
rapidjson::Document start_detail(rapidjson::kObjectType);
rapidjson::Document start_detail(rapidjson::kObjectType);
rapidjson::Document end_detail(rapidjson::kObjectType);
rapidjson::Document diag_results(rapidjson::kArrayType);
rapidjson::Document diag_capture(rapidjson::kObjectType);
Expand All @@ -411,7 +414,7 @@ int get_errors (const std::string &infile,
std::string msg = diag.message;

range_obj.SetObject();
rapidjson::Document::AllocatorType &allocator = range_obj.GetAllocator();
rapidjson::Document::AllocatorType &allocator = range_obj.GetAllocator();

start_detail.SetObject();
start_detail.AddMember("line", rapidjson::Value().SetInt(start_line), allocator);
Expand Down
7 changes: 5 additions & 2 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,11 @@ Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
diag::Diagnostics &diagnostics, Platform &platform,
int64_t default_lower_bound)
{
pass_unused_functions(al, asr, true);
pass_replace_class_constructor(al, asr);

LCompilers::PassOptions pass_options;
pass_options.always_run = true;
pass_unused_functions(al, asr, pass_options);
pass_replace_class_constructor(al, asr, pass_options);
ASRToCVisitor v(diagnostics, platform, default_lower_bound);
try {
v.visit_asr((ASR::asr_t &)asr);
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/codegen/asr_to_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ Result<std::string> asr_to_cpp(Allocator &al, ASR::TranslationUnit_t &asr,
diag::Diagnostics &diagnostics, Platform &platform,
int64_t default_lower_bound)
{
pass_unused_functions(al, asr, true);
LCompilers::PassOptions pass_options;
pass_options.always_run = true;
pass_unused_functions(al, asr, pass_options);
ASRToCPPVisitor v(diagnostics, platform, default_lower_bound);
try {
v.visit_asr((ASR::asr_t &)asr);
Expand Down
5 changes: 4 additions & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5430,7 +5430,10 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
Platform platform, const std::string &run_fn)
{
ASRToLLVMVisitor v(al, context, platform, diagnostics);
pass_manager.apply_passes(al, &asr, run_fn, false);
LCompilers::PassOptions pass_options;
pass_options.run_fun = run_fn;
pass_options.always_run = false;
pass_manager.apply_passes(al, &asr, pass_options);

// Uncomment for debugging the ASR after the transformation
// std::cout << pickle(asr, true, true, true) << std::endl;
Expand Down
9 changes: 6 additions & 3 deletions src/libasr/codegen/asr_to_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <libasr/pass/do_loops.h>
#include <libasr/pass/unused_functions.h>
#include <libasr/pass/arr_dims_propagate.h>
#include <libasr/pass/pass_array_by_data.h>
#include <libasr/exception.h>
#include <libasr/asr_utils.h>

Expand Down Expand Up @@ -1532,9 +1533,11 @@ Result<Vec<uint8_t>> asr_to_wasm_bytes_stream(ASR::TranslationUnit_t &asr, Alloc
ASRToWASMVisitor v(al, diagnostics);
Vec<uint8_t> wasm_bytes;

pass_unused_functions(al, asr, true);
pass_replace_do_loops(al, asr);
pass_propagate_arr_dims(al, asr);
LCompilers::PassOptions pass_options;
pass_replace_do_loops(al, asr, pass_options);
pass_array_by_data(al, asr, pass_options);
pass_options.always_run = true;
pass_unused_functions(al, asr, pass_options);

// std::cout << pickle(asr, true /* use colors */, true /* indent */,
// true /* with_intrinsic_modules */) << std::endl;
Expand Down
7 changes: 5 additions & 2 deletions src/libasr/codegen/asr_to_x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,16 +532,19 @@ Result<int> asr_to_x86(ASR::TranslationUnit_t &asr, Allocator &al,

ASRToX86Visitor v(al);

LCompilers::PassOptions pass_options;
pass_options.run_fun = "f";

{
auto t1 = std::chrono::high_resolution_clock::now();
pass_wrap_global_stmts_into_function(al, asr, "f");
pass_wrap_global_stmts_into_function(al, asr, pass_options);
auto t2 = std::chrono::high_resolution_clock::now();
time_pass_global = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
}

{
auto t1 = std::chrono::high_resolution_clock::now();
pass_replace_do_loops(al, asr);
pass_replace_do_loops(al, asr, pass_options);
auto t2 = std::chrono::high_resolution_clock::now();
time_pass_do_loops = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
}
Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/arr_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ class ArrSliceVisitor : public PassUtils::PassVisitor<ArrSliceVisitor>
};

void pass_replace_arr_slice(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string &rl_path) {
const LCompilers::PassOptions& pass_options) {
std::string rl_path = pass_options.runtime_library_dir;
ArrSliceVisitor v(al, rl_path);
v.visit_TranslationUnit(unit);
LFORTRAN_ASSERT(asr_verify(unit));
Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/arr_slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define LFORTRAN_PASS_ARR_SLICE_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_arr_slice(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string &rl_path);
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/array_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,8 @@ class ArrayOpVisitor : public PassUtils::PassVisitor<ArrayOpVisitor>
};

void pass_replace_array_op(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string &rl_path) {
const LCompilers::PassOptions& pass_options) {
std::string rl_path = pass_options.runtime_library_dir;
ArrayOpVisitor v(al, rl_path);
v.visit_TranslationUnit(unit);
LFORTRAN_ASSERT(asr_verify(unit));
Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/array_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define LFORTRAN_PASS_ARRAY_OP_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_array_op(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string &rl_path);
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/class_constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class ClassConstructorVisitor : public PassUtils::PassVisitor<ClassConstructorVi
}
};

void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit) {
void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& /*pass_options*/) {
ClassConstructorVisitor v(al);
do {
v.is_constructor_present = false;
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/pass/class_constructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define LFORTRAN_PASS_CLASS_CONSTRUCTOR_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit);
void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/dead_code_removal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class DeadCodeRemovalVisitor : public PassUtils::PassVisitor<DeadCodeRemovalVisi
};

void pass_dead_code_removal(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string& rl_path) {
const LCompilers::PassOptions& pass_options) {
std::string rl_path = pass_options.runtime_library_dir;
DeadCodeRemovalVisitor v(al, rl_path);
v.visit_TranslationUnit(unit);
LFORTRAN_ASSERT(asr_verify(unit));
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/pass/dead_code_removal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define LIBASR_PASS_DEAD_CODE_REMOVAL_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_dead_code_removal(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
void pass_dead_code_removal(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/div_to_mul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class DivToMulVisitor : public PassUtils::PassVisitor<DivToMulVisitor>
};

void pass_replace_div_to_mul(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string& rl_path) {
const LCompilers::PassOptions& pass_options) {
std::string rl_path = pass_options.runtime_library_dir;
DivToMulVisitor v(al, rl_path);
v.visit_TranslationUnit(unit);
LFORTRAN_ASSERT(asr_verify(unit));
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/pass/div_to_mul.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define LIBASR_PASS_DIV_TO_MUL_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_div_to_mul(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
void pass_replace_div_to_mul(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/do_loops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class DoLoopVisitor : public ASR::StatementWalkVisitor<DoLoopVisitor>
}
};

void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit) {
void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& /*pass_options*/) {
DoLoopVisitor v(al);
// Each call transforms only one layer of nested loops, so we call it twice
// to transform doubly nested loops:
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/pass/do_loops.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define LFORTRAN_PASS_DO_LOOPS_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit);
void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/flip_sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class FlipSignVisitor : public PassUtils::SkipOptimizationFunctionVisitor<FlipSi
};

void pass_replace_flip_sign(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string& rl_path) {
const LCompilers::PassOptions& pass_options) {
std::string rl_path = pass_options.runtime_library_dir;
FlipSignVisitor v(al, unit, rl_path);
v.visit_TranslationUnit(unit);
LFORTRAN_ASSERT(asr_verify(unit));
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/pass/flip_sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define LIBASR_PASS_FLIP_SIGN_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_flip_sign(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
void pass_replace_flip_sign(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/fma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ class FMAVisitor : public PassUtils::SkipOptimizationFunctionVisitor<FMAVisitor>
};

void pass_replace_fma(Allocator &al, ASR::TranslationUnit_t &unit,
const std::string& rl_path) {
const LCompilers::PassOptions& pass_options) {
std::string rl_path = pass_options.runtime_library_dir;
FMAVisitor v(al, unit, rl_path);
v.visit_TranslationUnit(unit);
LFORTRAN_ASSERT(asr_verify(unit));
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/pass/fma.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define LIBASR_PASS_FMA_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_fma(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
void pass_replace_fma(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/for_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class ForAllVisitor : public ASR::StatementWalkVisitor<ForAllVisitor>
}
};

void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit) {
void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& /*pass_options*/) {
ForAllVisitor v(al);
v.visit_TranslationUnit(unit);
LFORTRAN_ASSERT(asr_verify(unit));
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/pass/for_all.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define LFORTRAN_PASS_FOR_ALL

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LFortran {

void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit);
void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& pass_options);

} // namespace LFortran

Expand Down
Loading