Skip to content

Commit 0204dcc

Browse files
authored
Merge pull request #966 from czgdp1807/pass_fix
Simplifying registering passes in ``pass_manager.h``
2 parents 8499cee + c770591 commit 0204dcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+233
-229
lines changed

src/bin/lpython.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ int emit_asr(const std::string &infile,
165165
return 2;
166166
}
167167
LFortran::ASR::TranslationUnit_t* asr = r.result;
168-
pass_manager.apply_passes(al, asr, "f", true);
168+
LCompilers::PassOptions pass_options;
169+
pass_options.run_fun = "f";
170+
pass_options.always_run = true;
171+
pass_manager.apply_passes(al, asr, pass_options);
169172

170173
if (compiler_options.tree) {
171174
std::cout << LFortran::pickle_tree(*asr, compiler_options.use_colors,
@@ -304,33 +307,33 @@ int get_symbols (const std::string &infile,
304307
rapidjson::Document end_detail(rapidjson::kObjectType);
305308
rapidjson::Document location_object(rapidjson::kObjectType);
306309
rapidjson::Document test_capture(rapidjson::kObjectType);
307-
310+
308311
test_output.SetArray();
309-
312+
310313
for (auto symbol : symbol_lists) {
311314
uint32_t start_character = symbol.first_column;
312315
uint32_t start_line = symbol.first_line;
313316
uint32_t end_character = symbol.last_column;
314317
uint32_t end_line = symbol.last_line;
315318
std::string name = symbol.symbol_name;
316-
319+
317320
range_object.SetObject();
318321
rapidjson::Document::AllocatorType &allocator = range_object.GetAllocator();
319-
322+
320323
start_detail.SetObject();
321324
start_detail.AddMember("character", rapidjson::Value().SetInt(start_character), allocator);
322325
start_detail.AddMember("line", rapidjson::Value().SetInt(start_line), allocator);
323326
range_object.AddMember("start", start_detail, allocator);
324-
327+
325328
end_detail.SetObject();
326329
end_detail.AddMember("character", rapidjson::Value().SetInt(end_character), allocator);
327330
end_detail.AddMember("line", rapidjson::Value().SetInt(end_line), allocator);
328331
range_object.AddMember("end", end_detail, allocator);
329-
332+
330333
location_object.SetObject();
331334
location_object.AddMember("range", range_object, allocator);
332335
location_object.AddMember("uri", rapidjson::Value().SetString("uri", allocator), allocator);
333-
336+
334337
test_capture.SetObject();
335338
test_capture.AddMember("kind", rapidjson::Value().SetInt(1), allocator);
336339
test_capture.AddMember("location", location_object, allocator);
@@ -342,16 +345,16 @@ int get_symbols (const std::string &infile,
342345
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
343346
test_output.Accept(writer);
344347
std::string resp_str( buffer.GetString() );
345-
348+
346349
std::cout << resp_str;
347350
}
348351
else {
349352
std::cout << "{}\n";
350353
}
351-
354+
352355
return 0;
353356
}
354-
357+
355358

356359
int get_errors (const std::string &infile,
357360
const std::string &runtime_library_dir,
@@ -363,7 +366,7 @@ int get_errors (const std::string &infile,
363366
std::string input = LFortran::read_file(infile);
364367
lm.init_simple(input);
365368
LFortran::Result<LFortran::LPython::AST::ast_t*>
366-
r1 = LFortran::parse_python_file(al, runtime_library_dir, infile,
369+
r1 = LFortran::parse_python_file(al, runtime_library_dir, infile,
367370
diagnostics, compiler_options.new_parser);
368371
if (r1.ok) {
369372
LFortran::LPython::AST::ast_t* ast = r1.result;
@@ -396,7 +399,7 @@ int get_errors (const std::string &infile,
396399
}
397400
}
398401
rapidjson::Document range_obj(rapidjson::kObjectType);
399-
rapidjson::Document start_detail(rapidjson::kObjectType);
402+
rapidjson::Document start_detail(rapidjson::kObjectType);
400403
rapidjson::Document end_detail(rapidjson::kObjectType);
401404
rapidjson::Document diag_results(rapidjson::kArrayType);
402405
rapidjson::Document diag_capture(rapidjson::kObjectType);
@@ -411,7 +414,7 @@ int get_errors (const std::string &infile,
411414
std::string msg = diag.message;
412415

413416
range_obj.SetObject();
414-
rapidjson::Document::AllocatorType &allocator = range_obj.GetAllocator();
417+
rapidjson::Document::AllocatorType &allocator = range_obj.GetAllocator();
415418

416419
start_detail.SetObject();
417420
start_detail.AddMember("line", rapidjson::Value().SetInt(start_line), allocator);

src/libasr/codegen/asr_to_c.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,11 @@ Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
655655
diag::Diagnostics &diagnostics, Platform &platform,
656656
int64_t default_lower_bound)
657657
{
658-
pass_unused_functions(al, asr, true);
659-
pass_replace_class_constructor(al, asr);
658+
659+
LCompilers::PassOptions pass_options;
660+
pass_options.always_run = true;
661+
pass_unused_functions(al, asr, pass_options);
662+
pass_replace_class_constructor(al, asr, pass_options);
660663
ASRToCVisitor v(diagnostics, platform, default_lower_bound);
661664
try {
662665
v.visit_asr((ASR::asr_t &)asr);

src/libasr/codegen/asr_to_cpp.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,9 @@ Result<std::string> asr_to_cpp(Allocator &al, ASR::TranslationUnit_t &asr,
699699
diag::Diagnostics &diagnostics, Platform &platform,
700700
int64_t default_lower_bound)
701701
{
702-
pass_unused_functions(al, asr, true);
702+
LCompilers::PassOptions pass_options;
703+
pass_options.always_run = true;
704+
pass_unused_functions(al, asr, pass_options);
703705
ASRToCPPVisitor v(diagnostics, platform, default_lower_bound);
704706
try {
705707
v.visit_asr((ASR::asr_t &)asr);

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5430,7 +5430,10 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
54305430
Platform platform, const std::string &run_fn)
54315431
{
54325432
ASRToLLVMVisitor v(al, context, platform, diagnostics);
5433-
pass_manager.apply_passes(al, &asr, run_fn, false);
5433+
LCompilers::PassOptions pass_options;
5434+
pass_options.run_fun = run_fn;
5435+
pass_options.always_run = false;
5436+
pass_manager.apply_passes(al, &asr, pass_options);
54345437

54355438
// Uncomment for debugging the ASR after the transformation
54365439
// std::cout << pickle(asr, true, true, true) << std::endl;

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <libasr/pass/do_loops.h>
1212
#include <libasr/pass/unused_functions.h>
1313
#include <libasr/pass/arr_dims_propagate.h>
14+
#include <libasr/pass/pass_array_by_data.h>
1415
#include <libasr/exception.h>
1516
#include <libasr/asr_utils.h>
1617

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

1535-
pass_unused_functions(al, asr, true);
1536-
pass_replace_do_loops(al, asr);
1537-
pass_propagate_arr_dims(al, asr);
1536+
LCompilers::PassOptions pass_options;
1537+
pass_replace_do_loops(al, asr, pass_options);
1538+
pass_array_by_data(al, asr, pass_options);
1539+
pass_options.always_run = true;
1540+
pass_unused_functions(al, asr, pass_options);
15381541

15391542
// std::cout << pickle(asr, true /* use colors */, true /* indent */,
15401543
// true /* with_intrinsic_modules */) << std::endl;

src/libasr/codegen/asr_to_x86.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,19 @@ Result<int> asr_to_x86(ASR::TranslationUnit_t &asr, Allocator &al,
532532

533533
ASRToX86Visitor v(al);
534534

535+
LCompilers::PassOptions pass_options;
536+
pass_options.run_fun = "f";
537+
535538
{
536539
auto t1 = std::chrono::high_resolution_clock::now();
537-
pass_wrap_global_stmts_into_function(al, asr, "f");
540+
pass_wrap_global_stmts_into_function(al, asr, pass_options);
538541
auto t2 = std::chrono::high_resolution_clock::now();
539542
time_pass_global = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
540543
}
541544

542545
{
543546
auto t1 = std::chrono::high_resolution_clock::now();
544-
pass_replace_do_loops(al, asr);
547+
pass_replace_do_loops(al, asr, pass_options);
545548
auto t2 = std::chrono::high_resolution_clock::now();
546549
time_pass_do_loops = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
547550
}

src/libasr/pass/arr_slice.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ class ArrSliceVisitor : public PassUtils::PassVisitor<ArrSliceVisitor>
274274
};
275275

276276
void pass_replace_arr_slice(Allocator &al, ASR::TranslationUnit_t &unit,
277-
const std::string &rl_path) {
277+
const LCompilers::PassOptions& pass_options) {
278+
std::string rl_path = pass_options.runtime_library_dir;
278279
ArrSliceVisitor v(al, rl_path);
279280
v.visit_TranslationUnit(unit);
280281
LFORTRAN_ASSERT(asr_verify(unit));

src/libasr/pass/arr_slice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
#define LFORTRAN_PASS_ARR_SLICE_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

89
void pass_replace_arr_slice(Allocator &al, ASR::TranslationUnit_t &unit,
9-
const std::string &rl_path);
10+
const LCompilers::PassOptions& pass_options);
1011

1112
} // namespace LFortran
1213

src/libasr/pass/array_op.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,8 @@ class ArrayOpVisitor : public PassUtils::PassVisitor<ArrayOpVisitor>
929929
};
930930

931931
void pass_replace_array_op(Allocator &al, ASR::TranslationUnit_t &unit,
932-
const std::string &rl_path) {
932+
const LCompilers::PassOptions& pass_options) {
933+
std::string rl_path = pass_options.runtime_library_dir;
933934
ArrayOpVisitor v(al, rl_path);
934935
v.visit_TranslationUnit(unit);
935936
LFORTRAN_ASSERT(asr_verify(unit));

src/libasr/pass/array_op.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
#define LFORTRAN_PASS_ARRAY_OP_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

89
void pass_replace_array_op(Allocator &al, ASR::TranslationUnit_t &unit,
9-
const std::string &rl_path);
10+
const LCompilers::PassOptions& pass_options);
1011

1112
} // namespace LFortran
1213

src/libasr/pass/class_constructor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class ClassConstructorVisitor : public PassUtils::PassVisitor<ClassConstructorVi
5454
}
5555
};
5656

57-
void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit) {
57+
void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit,
58+
const LCompilers::PassOptions& /*pass_options*/) {
5859
ClassConstructorVisitor v(al);
5960
do {
6061
v.is_constructor_present = false;

src/libasr/pass/class_constructor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define LFORTRAN_PASS_CLASS_CONSTRUCTOR_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

8-
void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit);
9+
void pass_replace_class_constructor(Allocator &al, ASR::TranslationUnit_t &unit,
10+
const LCompilers::PassOptions& pass_options);
911

1012
} // namespace LFortran
1113

src/libasr/pass/dead_code_removal.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ class DeadCodeRemovalVisitor : public PassUtils::PassVisitor<DeadCodeRemovalVisi
9999
};
100100

101101
void pass_dead_code_removal(Allocator &al, ASR::TranslationUnit_t &unit,
102-
const std::string& rl_path) {
102+
const LCompilers::PassOptions& pass_options) {
103+
std::string rl_path = pass_options.runtime_library_dir;
103104
DeadCodeRemovalVisitor v(al, rl_path);
104105
v.visit_TranslationUnit(unit);
105106
LFORTRAN_ASSERT(asr_verify(unit));

src/libasr/pass/dead_code_removal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define LIBASR_PASS_DEAD_CODE_REMOVAL_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

8-
void pass_dead_code_removal(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
9+
void pass_dead_code_removal(Allocator &al, ASR::TranslationUnit_t &unit,
10+
const LCompilers::PassOptions& pass_options);
911

1012
} // namespace LFortran
1113

src/libasr/pass/div_to_mul.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class DivToMulVisitor : public PassUtils::PassVisitor<DivToMulVisitor>
7878
};
7979

8080
void pass_replace_div_to_mul(Allocator &al, ASR::TranslationUnit_t &unit,
81-
const std::string& rl_path) {
81+
const LCompilers::PassOptions& pass_options) {
82+
std::string rl_path = pass_options.runtime_library_dir;
8283
DivToMulVisitor v(al, rl_path);
8384
v.visit_TranslationUnit(unit);
8485
LFORTRAN_ASSERT(asr_verify(unit));

src/libasr/pass/div_to_mul.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define LIBASR_PASS_DIV_TO_MUL_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

8-
void pass_replace_div_to_mul(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
9+
void pass_replace_div_to_mul(Allocator &al, ASR::TranslationUnit_t &unit,
10+
const LCompilers::PassOptions& pass_options);
911

1012
} // namespace LFortran
1113

src/libasr/pass/do_loops.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class DoLoopVisitor : public ASR::StatementWalkVisitor<DoLoopVisitor>
4444
}
4545
};
4646

47-
void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit) {
47+
void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit,
48+
const LCompilers::PassOptions& /*pass_options*/) {
4849
DoLoopVisitor v(al);
4950
// Each call transforms only one layer of nested loops, so we call it twice
5051
// to transform doubly nested loops:

src/libasr/pass/do_loops.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define LFORTRAN_PASS_DO_LOOPS_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

8-
void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit);
9+
void pass_replace_do_loops(Allocator &al, ASR::TranslationUnit_t &unit,
10+
const LCompilers::PassOptions& pass_options);
911

1012
} // namespace LFortran
1113

src/libasr/pass/flip_sign.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ class FlipSignVisitor : public PassUtils::SkipOptimizationFunctionVisitor<FlipSi
209209
};
210210

211211
void pass_replace_flip_sign(Allocator &al, ASR::TranslationUnit_t &unit,
212-
const std::string& rl_path) {
212+
const LCompilers::PassOptions& pass_options) {
213+
std::string rl_path = pass_options.runtime_library_dir;
213214
FlipSignVisitor v(al, unit, rl_path);
214215
v.visit_TranslationUnit(unit);
215216
LFORTRAN_ASSERT(asr_verify(unit));

src/libasr/pass/flip_sign.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define LIBASR_PASS_FLIP_SIGN_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

8-
void pass_replace_flip_sign(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
9+
void pass_replace_flip_sign(Allocator &al, ASR::TranslationUnit_t &unit,
10+
const LCompilers::PassOptions& pass_options);
911

1012
} // namespace LFortran
1113

src/libasr/pass/fma.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ class FMAVisitor : public PassUtils::SkipOptimizationFunctionVisitor<FMAVisitor>
166166
};
167167

168168
void pass_replace_fma(Allocator &al, ASR::TranslationUnit_t &unit,
169-
const std::string& rl_path) {
169+
const LCompilers::PassOptions& pass_options) {
170+
std::string rl_path = pass_options.runtime_library_dir;
170171
FMAVisitor v(al, unit, rl_path);
171172
v.visit_TranslationUnit(unit);
172173
LFORTRAN_ASSERT(asr_verify(unit));

src/libasr/pass/fma.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define LIBASR_PASS_FMA_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

8-
void pass_replace_fma(Allocator &al, ASR::TranslationUnit_t &unit, const std::string& rl_path);
9+
void pass_replace_fma(Allocator &al, ASR::TranslationUnit_t &unit,
10+
const LCompilers::PassOptions& pass_options);
911

1012
} // namespace LFortran
1113

src/libasr/pass/for_all.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class ForAllVisitor : public ASR::StatementWalkVisitor<ForAllVisitor>
4343
}
4444
};
4545

46-
void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit) {
46+
void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit,
47+
const LCompilers::PassOptions& /*pass_options*/) {
4748
ForAllVisitor v(al);
4849
v.visit_TranslationUnit(unit);
4950
LFORTRAN_ASSERT(asr_verify(unit));

src/libasr/pass/for_all.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define LFORTRAN_PASS_FOR_ALL
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

8-
void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit);
9+
void pass_replace_forall(Allocator &al, ASR::TranslationUnit_t &unit,
10+
const LCompilers::PassOptions& pass_options);
911

1012
} // namespace LFortran
1113

0 commit comments

Comments
 (0)