Skip to content

Commit

Permalink
Merge pull request #110 from czgdp1807/fast_opt
Browse files Browse the repository at this point in the history
Added support for ``--fast`` tag
  • Loading branch information
czgdp1807 authored Mar 13, 2024
2 parents 103e557 + da2e235 commit fd8842f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 82 deletions.
90 changes: 45 additions & 45 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,66 +175,66 @@ endmacro(RUN)
# x86 --- compile to x86 binary directly
# wasm --- compile to WASM binary directly

RUN(NAME expr1.c LABELS gcc c wasm llvm NOFAST)
RUN(NAME expr1.c LABELS gcc c wasm llvm)
RUN(NAME expr2.c LABELS gcc c wasm llvm NOFAST)
RUN(NAME expr3.c FAIL LABELS gcc c wasm llvm NOFAST)
RUN(NAME expr3.c FAIL LABELS gcc c wasm llvm)

RUN(NAME switch_case_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME switch_case_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME switch_case_01.cpp LABELS gcc llvm)
RUN(NAME switch_case_02.cpp LABELS gcc llvm)

# arrays
RUN(NAME array_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_03.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_04.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_05.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_06.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_07.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_08.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_09.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_10.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_11.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_12.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_13.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_14.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_15.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_16.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_17.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_18.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_19.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_20.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_21.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_22.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_23.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_24.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_25.cpp LABELS gcc llvm NOFAST)

RUN(NAME struct_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_03.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_04.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_05.cpp LABELS gcc llvm NOFAST)
RUN(NAME struct_06.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_01.cpp LABELS gcc llvm)
RUN(NAME array_02.cpp LABELS gcc llvm)
RUN(NAME array_03.cpp LABELS gcc llvm)
RUN(NAME array_04.cpp LABELS gcc llvm)
RUN(NAME array_05.cpp LABELS gcc llvm)
RUN(NAME array_06.cpp LABELS gcc llvm)
RUN(NAME array_07.cpp LABELS gcc llvm)
RUN(NAME array_08.cpp LABELS gcc llvm)
RUN(NAME array_09.cpp LABELS gcc llvm)
RUN(NAME array_10.cpp LABELS gcc llvm)
RUN(NAME array_11.cpp LABELS gcc llvm)
RUN(NAME array_12.cpp LABELS gcc llvm)
RUN(NAME array_13.cpp LABELS gcc llvm)
RUN(NAME array_14.cpp LABELS gcc llvm)
RUN(NAME array_15.cpp LABELS gcc llvm)
RUN(NAME array_16.cpp LABELS gcc llvm)
RUN(NAME array_17.cpp LABELS gcc llvm)
RUN(NAME array_18.cpp LABELS gcc llvm)
RUN(NAME array_19.cpp LABELS gcc llvm)
RUN(NAME array_20.cpp LABELS gcc llvm)
RUN(NAME array_21.cpp LABELS gcc llvm)
RUN(NAME array_22.cpp LABELS gcc llvm)
RUN(NAME array_23.cpp LABELS gcc llvm)
RUN(NAME array_24.cpp LABELS gcc llvm)
RUN(NAME array_25.cpp LABELS gcc llvm)

RUN(NAME struct_01.cpp LABELS gcc llvm)
RUN(NAME struct_02.cpp LABELS gcc llvm)
RUN(NAME struct_03.cpp LABELS gcc llvm)
RUN(NAME struct_04.cpp LABELS gcc llvm)
RUN(NAME struct_05.cpp LABELS gcc llvm)
RUN(NAME struct_06.cpp LABELS gcc llvm)

RUN(NAME pointer_01.cpp LABELS gcc)
RUN(NAME pointer_02.cpp LABELS gcc)
RUN(NAME pointer_03.cpp LABELS gcc llvm NOFAST)
RUN(NAME pointer_03.cpp LABELS gcc llvm)

RUN(NAME function_01.cpp LABELS gcc llvm NOFAST)

RUN(NAME nbody_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME nbody_02.cpp LABELS gcc llvm NOFAST)

RUN(NAME enum_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_03.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_04.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_01.cpp LABELS gcc llvm)
RUN(NAME enum_02.cpp LABELS gcc llvm)
RUN(NAME enum_03.cpp LABELS gcc llvm)
RUN(NAME enum_04.cpp LABELS gcc llvm)

RUN(NAME union_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME union_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME union_01.cpp LABELS gcc llvm)
RUN(NAME union_02.cpp LABELS gcc llvm)

RUN(NAME vector_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME vector_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME vector_01.cpp LABELS gcc llvm)
RUN(NAME vector_02.cpp LABELS gcc llvm)

RUN(NAME loop_01.cpp LABELS gcc llvm NOFAST)

Expand Down
53 changes: 29 additions & 24 deletions src/bin/lc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ std::string construct_outfile(std::string &arg_file, std::string &ArgO) {
}

#define DeclareLCompilersUtilVars \
LCompilers::CompilerOptions compiler_options; \
LCompilers::diag::Diagnostics diagnostics; \
LCompilers::LocationManager lm; \
{ \
Expand All @@ -74,12 +73,9 @@ std::string construct_outfile(std::string &arg_file, std::string &ArgO) {
lm.init_simple(input); \
lm.file_ends.push_back(input.size()); \
} \
compiler_options.po.always_run = true; \
compiler_options.po.run_fun = "f"; \
compiler_options.po.realloc_lhs = true; \
diagnostics.diagnostics.clear(); \

int emit_wat(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t *asr) {
int emit_wat(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t *asr, LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

LCompilers::Result<LCompilers::Vec<uint8_t>> r2 = LCompilers::asr_to_wasm_bytes_stream(*asr, al, diagnostics, compiler_options);
Expand All @@ -100,7 +96,7 @@ int emit_wat(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUni
return 0;
}

int emit_c(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr) {
int emit_c(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr, LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

// Apply ASR passes
Expand All @@ -118,7 +114,7 @@ int emit_c(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_
return 0;
}

int emit_cpp(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr) {
int emit_cpp(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr, LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

// Apply ASR passes
Expand All @@ -136,7 +132,7 @@ int emit_cpp(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUni
return 0;
}

int emit_fortran(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr) {
int emit_fortran(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr, LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

auto res = LCompilers::asr_to_fortran(*asr, diagnostics, false, 4);
Expand All @@ -149,7 +145,7 @@ int emit_fortran(Allocator &al, std::string &infile, LCompilers::ASR::Translatio
return 0;
}

int emit_llvm(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr) {
int emit_llvm(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUnit_t* asr, LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

LCompilers::PassManager pass_manager;
Expand Down Expand Up @@ -180,7 +176,8 @@ int emit_llvm(Allocator &al, std::string &infile, LCompilers::ASR::TranslationUn

int compile_to_binary_wasm(
Allocator &al, const std::string &infile,
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr) {
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr,
LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;
LCompilers::Result<int> res = LCompilers::asr_to_wasm(*asr, al, outfile, false, diagnostics, compiler_options);
if (!res.ok) {
Expand All @@ -191,7 +188,8 @@ int compile_to_binary_wasm(
}

int compile_to_c(Allocator &al, const std::string &infile,
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr) {
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr,
LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

// Apply ASR passes
Expand All @@ -213,7 +211,8 @@ int compile_to_c(Allocator &al, const std::string &infile,
}

int compile_to_cpp(Allocator &al, const std::string &infile,
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr) {
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr,
LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

// Apply ASR passes
Expand All @@ -235,7 +234,8 @@ int compile_to_cpp(Allocator &al, const std::string &infile,
}

int compile_to_fortran(Allocator &al, const std::string &infile,
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr) {
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr,
LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

auto res = LCompilers::asr_to_fortran(*asr, diagnostics, false, 4);
Expand All @@ -252,7 +252,8 @@ int compile_to_fortran(Allocator &al, const std::string &infile,
}

int compile_to_binary_object(Allocator &al, const std::string &infile,
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr) {
const std::string &outfile, LCompilers::ASR::TranslationUnit_t* asr,
LCompilers::CompilerOptions& compiler_options) {
DeclareLCompilersUtilVars;

LCompilers::PassManager pass_manager;
Expand Down Expand Up @@ -616,6 +617,9 @@ int mainApp(int argc, const char **argv) {
bool parse_all_comments = false;

LCompilers::CompilerOptions co;
co.po.always_run = true;
co.po.run_fun = "f";
co.po.realloc_lhs = true;
co.po.runtime_library_dir = LCompilers::LC::get_runtime_library_dir();

CLI::App app{"LFortran: modern interactive LLVM-based Fortran compiler"};
Expand All @@ -640,6 +644,7 @@ int mainApp(int argc, const char **argv) {
app.add_flag("--get-rtl-header-dir", print_rtl_header_dir, "Print the path to the runtime library header file");
app.add_flag("--get-rtl-dir", print_rtl_dir, "Print the path to the runtime library file");
app.add_flag("--parse-all-comments", parse_all_comments, "Parse all comments in the input code");
app.add_flag("--fast", co.po.fast, "Best performance (disable strict standard compliance)");

app.get_formatter()->column_width(25);
app.require_subcommand(0, 1);
Expand Down Expand Up @@ -678,15 +683,15 @@ int mainApp(int argc, const char **argv) {
std::cout<< LCompilers::pickle(*tu, co.use_colors, co.indent, true) << std::endl;
return 0;
} else if (show_wat) {
return emit_wat(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu);
return emit_wat(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (show_c) {
return emit_c(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu);
return emit_c(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (show_cpp) {
return emit_cpp(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu);
return emit_cpp(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (show_fortran) {
return emit_fortran(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu);
return emit_fortran(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (show_llvm) {
return emit_llvm(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu);
return emit_llvm(al, infile, (LCompilers::ASR::TranslationUnit_t*)tu, co);
}

// compile to binary
Expand All @@ -699,19 +704,19 @@ int mainApp(int argc, const char **argv) {

std::string tmp_file;
if (backend == Backend::wasm) {
status = compile_to_binary_wasm(al, infile, outfile, (LCompilers::ASR::TranslationUnit_t*)tu);
status = compile_to_binary_wasm(al, infile, outfile, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (backend == Backend::c) {
tmp_file = outfile + "__generated__.c";
status = compile_to_c(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu);
status = compile_to_c(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (backend == Backend::cpp) {
tmp_file = outfile + "__generated__.cpp";
status = compile_to_cpp(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu);
status = compile_to_cpp(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (backend == Backend::fortran) {
tmp_file = outfile + "__generated__.f90";
status = compile_to_fortran(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu);
status = compile_to_fortran(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu, co);
} else if (backend == Backend::llvm) {
tmp_file = arg_c ? outfile : (outfile + "__generated__.o");
status = compile_to_binary_object(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu);
status = compile_to_binary_object(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu, co);
}

if (status != 0) {
Expand Down
15 changes: 2 additions & 13 deletions src/libasr/pass/pass_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ namespace LCompilers {
{"insert_deallocate", &pass_insert_deallocate}
};

bool is_fast;
bool apply_default_passes;
bool c_skip_pass; // This will contain the passes that are to be skipped in C

Expand Down Expand Up @@ -197,7 +196,7 @@ namespace LCompilers {
}
}

PassManager(): is_fast{false}, apply_default_passes{false},
PassManager(): apply_default_passes{false},
c_skip_pass{false} {
_passes = {
"nested_vars",
Expand Down Expand Up @@ -288,12 +287,10 @@ namespace LCompilers {
PassOptions& pass_options,
diag::Diagnostics &diagnostics) {
if( !_user_defined_passes.empty() ) {
pass_options.fast = true;
apply_passes(al, asr, _user_defined_passes, pass_options,
diagnostics);
} else if( apply_default_passes ) {
pass_options.fast = is_fast;
if( is_fast ) {
if( pass_options.fast ) {
apply_passes(al, asr, _with_optimization_passes, pass_options,
diagnostics);
} else {
Expand Down Expand Up @@ -372,14 +369,6 @@ namespace LCompilers {
}
}

void use_optimization_passes() {
is_fast = true;
}

void do_not_use_optimization_passes() {
is_fast = false;
}

void use_default_passes(bool _c_skip_pass=false) {
apply_default_passes = true;
c_skip_pass = _c_skip_pass;
Expand Down

0 comments on commit fd8842f

Please sign in to comment.