Skip to content

Sync libasr from LFortran #1314

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 4 commits into from
Nov 18, 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
3 changes: 2 additions & 1 deletion src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ int emit_asr(const std::string &infile,
LCompilers::PassOptions pass_options;
pass_options.run_fun = "f";
pass_options.always_run = true;
pass_manager.apply_passes(al, asr, pass_options);
pass_manager.apply_passes(al, asr, pass_options, diagnostics);

if (compiler_options.tree) {
std::cout << LFortran::pickle_tree(*asr, compiler_options.use_colors,
Expand Down Expand Up @@ -1345,6 +1345,7 @@ int main(int argc, char *argv[])
case (LFortran::Platform::macOS_ARM) : std::cout << "macOS ARM"; break;
case (LFortran::Platform::Windows) : std::cout << "Windows"; break;
case (LFortran::Platform::FreeBSD) : std::cout << "FreeBSD"; break;
case (LFortran::Platform::OpenBSD) : std::cout << "OpenBSD"; break;
}
std::cout << std::endl;
#ifdef HAVE_LFORTRAN_LLVM
Expand Down
1 change: 1 addition & 0 deletions src/libasr/asdl_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,7 @@ def make_visitor(self, name, fields):
ASR::symbol_t *s = ((ASR::%s_t*)f)->m_v;
if (s->type == ASR::symbolType::ExternalSymbol) {
ASR::ExternalSymbol_t *e = ASR::down_cast<ASR::ExternalSymbol_t>(s);
LFORTRAN_ASSERT(e->m_external);
LFORTRAN_ASSERT(!ASR::is_a<ASR::ExternalSymbol_t>(*e->m_external));
s = e->m_external;
}
Expand Down
54 changes: 36 additions & 18 deletions src/libasr/asr_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <libasr/asr_verify.h>
#include <libasr/utils.h>
#include <libasr/modfile.h>
#include <libasr/pass/pass_utils.h>

namespace LFortran {

Expand Down Expand Up @@ -114,7 +115,7 @@ ASR::Module_t* extract_module(const ASR::TranslationUnit_t &m) {
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
const std::string &module_name,
const Location &loc, bool intrinsic,
const std::string &rl_path,
LCompilers::PassOptions& pass_options,
bool run_verify,
const std::function<void (const std::string &, const Location &)> err) {
LFORTRAN_ASSERT(symtab);
Expand All @@ -128,14 +129,14 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
}
LFORTRAN_ASSERT(symtab->parent == nullptr);
ASR::TranslationUnit_t *mod1 = find_and_load_module(al, module_name,
*symtab, intrinsic, rl_path);
*symtab, intrinsic, pass_options);
if (mod1 == nullptr && !intrinsic) {
// Module not found as a regular module. Try intrinsic module
if (module_name == "iso_c_binding"
||module_name == "iso_fortran_env"
||module_name == "ieee_arithmetic") {
mod1 = find_and_load_module(al, "lfortran_intrinsic_" + module_name,
*symtab, true, rl_path);
*symtab, true, pass_options);
}
}
if (mod1 == nullptr) {
Expand Down Expand Up @@ -172,13 +173,13 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
bool is_intrinsic = startswith(item, "lfortran_intrinsic");
ASR::TranslationUnit_t *mod1 = find_and_load_module(al,
item,
*symtab, is_intrinsic, rl_path);
*symtab, is_intrinsic, pass_options);
if (mod1 == nullptr && !is_intrinsic) {
// Module not found as a regular module. Try intrinsic module
if (item == "iso_c_binding"
||item == "iso_fortran_env") {
mod1 = find_and_load_module(al, "lfortran_intrinsic_" + item,
*symtab, true, rl_path);
*symtab, true, pass_options);
}
}

Expand All @@ -205,8 +206,16 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,

// Fix all external symbols
fix_external_symbols(*tu, *symtab);
PassUtils::UpdateDependenciesVisitor v(al);
v.visit_TranslationUnit(*tu);
if (run_verify) {
LFORTRAN_ASSERT(asr_verify(*tu));
#if defined(WITH_LFORTRAN_ASSERT)
diag::Diagnostics diagnostics;
if (!asr_verify(*tu, true, diagnostics)) {
std::cerr << diagnostics.render2();
throw LCompilersException("Verify failed");
};
#endif
}
symtab->asr_owner = orig_asr_owner;

Expand Down Expand Up @@ -252,20 +261,29 @@ void set_intrinsic(ASR::TranslationUnit_t* trans_unit) {

ASR::TranslationUnit_t* find_and_load_module(Allocator &al, const std::string &msym,
SymbolTable &symtab, bool intrinsic,
const std::string &rl_path) {
std::string modfilename = msym + ".mod";
if (intrinsic) {
modfilename = rl_path + "/" + modfilename;
}
LCompilers::PassOptions& pass_options) {
std::filesystem::path runtime_library_dir { pass_options.runtime_library_dir };
std::filesystem::path filename {msym + ".mod"};
std::vector<std::filesystem::path> mod_files_dirs;

mod_files_dirs.push_back( runtime_library_dir );
mod_files_dirs.push_back( pass_options.mod_files_dir );
mod_files_dirs.insert(mod_files_dirs.end(),
pass_options.include_dirs.begin(),
pass_options.include_dirs.end());

std::string modfile;
if (!read_file(modfilename, modfile)) return nullptr;
ASR::TranslationUnit_t *asr = load_modfile(al, modfile, false,
symtab);
if (intrinsic) {
set_intrinsic(asr);
for (auto path : mod_files_dirs) {
std::string modfile;
std::filesystem::path full_path = path / filename;
if (read_file(full_path.string(), modfile)) {
ASR::TranslationUnit_t *asr = load_modfile(al, modfile, false, symtab);
if (intrinsic) {
set_intrinsic(asr);
}
return asr;
}
}
return asr;
return nullptr;
}

ASR::asr_t* getStructInstanceMember_t(Allocator& al, const Location& loc,
Expand Down
49 changes: 32 additions & 17 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
#include <libasr/assert.h>
#include <libasr/asr.h>
#include <libasr/string_utils.h>
#include <libasr/utils.h>

namespace LFortran {

namespace ASRUtils {

static inline double extract_real(const char *s) {
return std::atof(s);
}
// TODO: this is inefficient. We should
// convert this in the tokenizer where we know most information
std::string x = s;
x = replace(x, "d", "e");
x = replace(x, "D", "E");
return std::atof(x.c_str());
}

static inline ASR::expr_t* EXPR(const ASR::asr_t *f)
{
Expand Down Expand Up @@ -664,16 +670,16 @@ static inline bool extract_value(ASR::expr_t* value_expr, T& value) {
value = (T) const_int->m_n;
break;
}
case ASR::exprType::LogicalConstant: {
ASR::LogicalConstant_t* const_logical = ASR::down_cast<ASR::LogicalConstant_t>(value_expr);
value = (T) const_logical->m_value;
break;
}
case ASR::exprType::RealConstant: {
ASR::RealConstant_t* const_real = ASR::down_cast<ASR::RealConstant_t>(value_expr);
value = (T) const_real->m_r;
break;
}
case ASR::exprType::LogicalConstant: {
ASR::LogicalConstant_t* const_logical = ASR::down_cast<ASR::LogicalConstant_t>(value_expr);
value = (T) const_logical->m_value;
break;
}
default:
return false;
}
Expand Down Expand Up @@ -768,7 +774,7 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
}
case ASR::ttypeType::Logical: {
ASR::Logical_t* bool_ = ASR::down_cast<ASR::Logical_t>(t);
std::string res = "bool";
res = "bool";
if( encode_dimensions_ ) {
encode_dimensions(bool_->n_dims, res, use_underscore_sep);
return res;
Expand Down Expand Up @@ -862,16 +868,20 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
case ASR::ttypeType::Pointer: {
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(t);
if( use_underscore_sep ) {
return "Pointer_" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "_";
return "Pointer_" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "_";
}
return "Pointer[" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "]";
return "Pointer[" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "]";
}
case ASR::ttypeType::Const: {
ASR::Const_t* p = ASR::down_cast<ASR::Const_t>(t);
if( use_underscore_sep ) {
return "Const_" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "_";
return "Const_" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "_";
}
return "Const[" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "]";
return "Const[" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "]";
}
default: {
throw LCompilersException("Type encoding not implemented for "
Expand Down Expand Up @@ -1115,7 +1125,7 @@ std::vector<std::string> determine_module_dependencies(
const ASR::TranslationUnit_t &unit);

std::vector<std::string> determine_function_definition_order(
SymbolTable* symtab);
SymbolTable* symtab);

void extract_module_python(const ASR::TranslationUnit_t &m,
std::vector<std::pair<std::string, ASR::Module_t*>>& children_modules,
Expand All @@ -1126,13 +1136,13 @@ ASR::Module_t* extract_module(const ASR::TranslationUnit_t &m);
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
const std::string &module_name,
const Location &loc, bool intrinsic,
const std::string &rl_path,
LCompilers::PassOptions& pass_options,
bool run_verify,
const std::function<void (const std::string &, const Location &)> err);

ASR::TranslationUnit_t* find_and_load_module(Allocator &al, const std::string &msym,
SymbolTable &symtab, bool intrinsic,
const std::string &rl_path);
LCompilers::PassOptions& pass_options);

void set_intrinsic(ASR::TranslationUnit_t* trans_unit);

Expand Down Expand Up @@ -1189,8 +1199,8 @@ static inline int extract_kind_from_ttype_t(const ASR::ttype_t* type) {
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Pointer_t>(type)->m_type);
}
case ASR::ttypeType::Const: {
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type);
}
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type);
}
default : {
return -1;
}
Expand Down Expand Up @@ -1350,6 +1360,11 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
return n_dims;
}

inline int extract_n_dims_from_ttype(ASR::ttype_t *x) {
ASR::dimension_t* m_dims_temp = nullptr;
return extract_dimensions_from_ttype(x, m_dims_temp);
}

// Sets the dimension member of `ttype_t`. Returns `true` if dimensions set.
// Returns `false` if the `ttype_t` does not have a dimension member.
inline bool ttype_set_dimensions(ASR::ttype_t *x,
Expand Down
Loading