Skip to content

Commit 9ea5cdc

Browse files
skipping python_bind ASR pass if --enable-cpython flag not used
1 parent e750a04 commit 9ea5cdc

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

src/bin/lpython.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ int compile_python_using_llvm(
11321132
LCompilers::LPython::DynamicLibrary cpython_lib;
11331133
LCompilers::LPython::DynamicLibrary symengine_lib;
11341134

1135-
if (compiler_options.enable_cpython) {
1135+
if (compiler_options.po.enable_cpython) {
11361136
LCompilers::LPython::open_cpython_library(cpython_lib);
11371137
}
11381138
if (compiler_options.enable_symengine) {
@@ -1151,7 +1151,7 @@ int compile_python_using_llvm(
11511151
e.execfn<void>("__module___main_____main__global_stmts");
11521152
}
11531153

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

19851985
if (compiler_options.link_numpy) {
1986-
compiler_options.enable_cpython = true;
1986+
compiler_options.po.enable_cpython = true;
19871987
}
19881988

19891989
if (arg_version) {

src/libasr/codegen/asr_to_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ R"(
820820
}
821821

822822
std::string body;
823-
if (compiler_options.enable_cpython) {
823+
if (compiler_options.po.enable_cpython) {
824824
headers.insert("Python.h");
825825
body += R"(
826826
Py_Initialize();
@@ -851,7 +851,7 @@ R"( // Initialise Numpy
851851
body += src;
852852
}
853853

854-
if (compiler_options.enable_cpython) {
854+
if (compiler_options.po.enable_cpython) {
855855
body += R"(
856856
if (Py_FinalizeEx() < 0) {
857857
fprintf(stderr,"BindPython: Unknown Error\n");

src/libasr/pass/python_bind.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
387387
return;
388388
}
389389

390+
if (!pass_options.enable_cpython) {
391+
// python_bind pass is skipped if CPython is not enabled
392+
return;
393+
}
394+
390395
std::vector<ASRUtils::ASRFunc> fns;
391396
fns.push_back({"Py_Initialize", {}, ASRUtils::VOID});
392397
fns.push_back({"Py_IsInitialized", {}, ASRUtils::I32});
@@ -412,21 +417,17 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
412417
fns.push_back({"PyBool_FromLong", {ASRUtils::I32}, ASRUtils::PTR});
413418
fns.push_back({"PyObject_IsTrue", {ASRUtils::PTR}, ASRUtils::I32});
414419

415-
bool included_cpython_funcs = false;
416-
417420
Location *l = al.make_new<Location>();
418421
l->first = 0;
419422
l->last = 0;
420423

424+
ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");
425+
421426
for (auto &item : unit.m_symtab->get_scope()) {
422427
if (ASR::is_a<ASR::Function_t>(*item.second)) {
423428
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(item.second);
424429
if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) {
425430
if (f->n_body == 0 && f->m_module_file) {
426-
if (!included_cpython_funcs) {
427-
ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");
428-
included_cpython_funcs = true;
429-
}
430431
generate_body(al, *f, *unit.m_symtab);
431432
}
432433
}
@@ -438,10 +439,6 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
438439
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(module_item.second);
439440
if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) {
440441
if (f->n_body == 0 && f->m_module_file) {
441-
if (!included_cpython_funcs) {
442-
ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");
443-
included_cpython_funcs = true;
444-
}
445442
generate_body(al, *f, *module->m_symtab);
446443
}
447444
}
@@ -451,10 +448,8 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
451448

452449
}
453450

454-
if (included_cpython_funcs) {
455-
PassUtils::UpdateDependenciesVisitor u(al);
456-
u.visit_TranslationUnit(unit);
457-
}
451+
PassUtils::UpdateDependenciesVisitor u(al);
452+
u.visit_TranslationUnit(unit);
458453
}
459454

460455
} // namespace LCompilers

src/libasr/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct PassOptions {
5959
bool tree = false;
6060
bool with_intrinsic_mods = false;
6161
bool c_mangling = false;
62+
bool enable_cpython = false;
6263
bool c_skip_bindpy_pass = false;
6364
};
6465

@@ -99,7 +100,6 @@ struct CompilerOptions {
99100
std::string arg_o = "";
100101
bool emit_debug_info = false;
101102
bool emit_debug_line_column = false;
102-
bool enable_cpython = false;
103103
bool enable_symengine = false;
104104
bool link_numpy = false;
105105
bool run = false;

0 commit comments

Comments
 (0)