Skip to content

Commit

Permalink
skipping python_bind ASR pass if --enable-cpython flag not used
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Aug 12, 2024
1 parent e750a04 commit 9ea5cdc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ int compile_python_using_llvm(
LCompilers::LPython::DynamicLibrary cpython_lib;
LCompilers::LPython::DynamicLibrary symengine_lib;

if (compiler_options.enable_cpython) {
if (compiler_options.po.enable_cpython) {
LCompilers::LPython::open_cpython_library(cpython_lib);
}
if (compiler_options.enable_symengine) {
Expand All @@ -1151,7 +1151,7 @@ int compile_python_using_llvm(
e.execfn<void>("__module___main_____main__global_stmts");
}

if (compiler_options.enable_cpython) {
if (compiler_options.po.enable_cpython) {
LCompilers::LPython::close_cpython_library(cpython_lib);
}
if (compiler_options.enable_symengine) {
Expand Down Expand Up @@ -1921,7 +1921,7 @@ int main(int argc, char *argv[])
app.add_flag("--dump-all-passes", compiler_options.po.dump_all_passes, "Apply all the passes and dump the ASR into a file");
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");
app.add_flag("--cumulative", compiler_options.po.pass_cumulative, "Apply all the passes cumulatively till the given pass");
app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime");
app.add_flag("--enable-cpython", compiler_options.po.enable_cpython, "Enable CPython runtime");
app.add_flag("--enable-symengine", compiler_options.enable_symengine, "Enable Symengine runtime");
app.add_flag("--link-numpy", compiler_options.link_numpy, "Enable NumPy runtime (implies --enable-cpython)");
app.add_flag("--separate-compilation", separate_compilation, "Generates unique names for all the symbols");
Expand Down Expand Up @@ -1983,7 +1983,7 @@ int main(int argc, char *argv[])
}

if (compiler_options.link_numpy) {
compiler_options.enable_cpython = true;
compiler_options.po.enable_cpython = true;
}

if (arg_version) {
Expand Down
4 changes: 2 additions & 2 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ R"(
}

std::string body;
if (compiler_options.enable_cpython) {
if (compiler_options.po.enable_cpython) {
headers.insert("Python.h");
body += R"(
Py_Initialize();
Expand Down Expand Up @@ -851,7 +851,7 @@ R"( // Initialise Numpy
body += src;
}

if (compiler_options.enable_cpython) {
if (compiler_options.po.enable_cpython) {
body += R"(
if (Py_FinalizeEx() < 0) {
fprintf(stderr,"BindPython: Unknown Error\n");
Expand Down
23 changes: 9 additions & 14 deletions src/libasr/pass/python_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
return;
}

if (!pass_options.enable_cpython) {
// python_bind pass is skipped if CPython is not enabled
return;
}

std::vector<ASRUtils::ASRFunc> fns;
fns.push_back({"Py_Initialize", {}, ASRUtils::VOID});
fns.push_back({"Py_IsInitialized", {}, ASRUtils::I32});
Expand All @@ -412,21 +417,17 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
fns.push_back({"PyBool_FromLong", {ASRUtils::I32}, ASRUtils::PTR});
fns.push_back({"PyObject_IsTrue", {ASRUtils::PTR}, ASRUtils::I32});

bool included_cpython_funcs = false;

Location *l = al.make_new<Location>();
l->first = 0;
l->last = 0;

ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");

for (auto &item : unit.m_symtab->get_scope()) {
if (ASR::is_a<ASR::Function_t>(*item.second)) {
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(item.second);
if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) {
if (f->n_body == 0 && f->m_module_file) {
if (!included_cpython_funcs) {
ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");
included_cpython_funcs = true;
}
generate_body(al, *f, *unit.m_symtab);
}
}
Expand All @@ -438,10 +439,6 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(module_item.second);
if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) {
if (f->n_body == 0 && f->m_module_file) {
if (!included_cpython_funcs) {
ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");
included_cpython_funcs = true;
}
generate_body(al, *f, *module->m_symtab);
}
}
Expand All @@ -451,10 +448,8 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt

}

if (included_cpython_funcs) {
PassUtils::UpdateDependenciesVisitor u(al);
u.visit_TranslationUnit(unit);
}
PassUtils::UpdateDependenciesVisitor u(al);
u.visit_TranslationUnit(unit);
}

} // namespace LCompilers
2 changes: 1 addition & 1 deletion src/libasr/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct PassOptions {
bool tree = false;
bool with_intrinsic_mods = false;
bool c_mangling = false;
bool enable_cpython = false;
bool c_skip_bindpy_pass = false;
};

Expand Down Expand Up @@ -99,7 +100,6 @@ struct CompilerOptions {
std::string arg_o = "";
bool emit_debug_info = false;
bool emit_debug_line_column = false;
bool enable_cpython = false;
bool enable_symengine = false;
bool link_numpy = false;
bool run = false;
Expand Down

0 comments on commit 9ea5cdc

Please sign in to comment.