|
| 1 | +#include <libasr/asr.h> |
| 2 | +#include <libasr/containers.h> |
| 3 | +#include <libasr/exception.h> |
| 4 | +#include <libasr/asr_utils.h> |
| 5 | +#include <libasr/asr_verify.h> |
| 6 | +#include <libasr/pass/subroutine_from_function.h> |
| 7 | +#include <libasr/pass/pass_utils.h> |
| 8 | + |
| 9 | +#include <vector> |
| 10 | +#include <string> |
| 11 | + |
| 12 | + |
| 13 | +namespace LFortran { |
| 14 | + |
| 15 | +using ASR::down_cast; |
| 16 | +using ASR::is_a; |
| 17 | + |
| 18 | +class CreateFunctionFromSubroutine: public PassUtils::PassVisitor<CreateFunctionFromSubroutine> { |
| 19 | + |
| 20 | + public: |
| 21 | + |
| 22 | + CreateFunctionFromSubroutine(Allocator &al_) : |
| 23 | + PassVisitor(al_, nullptr) |
| 24 | + { |
| 25 | + pass_result.reserve(al, 1); |
| 26 | + } |
| 27 | + |
| 28 | + ASR::symbol_t* create_subroutine_from_function(ASR::Function_t* s) { |
| 29 | + for( auto& s_item: s->m_symtab->get_scope() ) { |
| 30 | + ASR::symbol_t* curr_sym = s_item.second; |
| 31 | + if( curr_sym->type == ASR::symbolType::Variable ) { |
| 32 | + ASR::Variable_t* var = ASR::down_cast<ASR::Variable_t>(curr_sym); |
| 33 | + if( var->m_intent == ASR::intentType::Unspecified ) { |
| 34 | + var->m_intent = ASR::intentType::In; |
| 35 | + } else if( var->m_intent == ASR::intentType::ReturnVar ) { |
| 36 | + var->m_intent = ASR::intentType::Out; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + Vec<ASR::expr_t*> a_args; |
| 41 | + a_args.reserve(al, s->n_args + 1); |
| 42 | + for( size_t i = 0; i < s->n_args; i++ ) { |
| 43 | + a_args.push_back(al, s->m_args[i]); |
| 44 | + } |
| 45 | + LFORTRAN_ASSERT(s->m_return_var) |
| 46 | + a_args.push_back(al, s->m_return_var); |
| 47 | + ASR::asr_t* s_sub_asr = ASR::make_Function_t(al, s->base.base.loc, |
| 48 | + s->m_symtab, |
| 49 | + s->m_name, s->m_dependencies, s->n_dependencies, |
| 50 | + a_args.p, a_args.size(), s->m_body, s->n_body, |
| 51 | + nullptr, |
| 52 | + s->m_abi, s->m_access, s->m_deftype, nullptr, false, false, |
| 53 | + false, s->m_inline, s->m_static, |
| 54 | + s->m_type_params, s->n_type_params, |
| 55 | + s->m_restrictions, s->n_restrictions, |
| 56 | + s->m_is_restriction); |
| 57 | + ASR::symbol_t* s_sub = ASR::down_cast<ASR::symbol_t>(s_sub_asr); |
| 58 | + return s_sub; |
| 59 | + } |
| 60 | + |
| 61 | + void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { |
| 62 | + std::vector<std::pair<std::string, ASR::symbol_t*>> replace_vec; |
| 63 | + // Transform functions returning arrays to subroutines |
| 64 | + for (auto &item : x.m_global_scope->get_scope()) { |
| 65 | + if (is_a<ASR::Function_t>(*item.second)) { |
| 66 | + ASR::Function_t *s = down_cast<ASR::Function_t>(item.second); |
| 67 | + if (s->m_return_var) { |
| 68 | + /* |
| 69 | + * A function which returns a aggregate type like array, struct will be converted |
| 70 | + * to a subroutine with the destination array as the last |
| 71 | + * argument. This helps in avoiding deep copies and the |
| 72 | + * destination memory directly gets filled inside the subroutine. |
| 73 | + */ |
| 74 | + if( PassUtils::is_aggregate_type(s->m_return_var) ) { |
| 75 | + ASR::symbol_t* s_sub = create_subroutine_from_function(s); |
| 76 | + replace_vec.push_back(std::make_pair(item.first, s_sub)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // FIXME: this is a hack, we need to pass in a non-const `x`, |
| 83 | + // which requires to generate a TransformVisitor. |
| 84 | + ASR::TranslationUnit_t &xx = const_cast<ASR::TranslationUnit_t&>(x); |
| 85 | + // Updating the symbol table so that now the name |
| 86 | + // of the function (which returned array) now points |
| 87 | + // to the newly created subroutine. |
| 88 | + for( auto& item: replace_vec ) { |
| 89 | + xx.m_global_scope->add_symbol(item.first, item.second); |
| 90 | + } |
| 91 | + |
| 92 | + // Now visit everything else |
| 93 | + for (auto &item : x.m_global_scope->get_scope()) { |
| 94 | + this->visit_symbol(*item.second); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + void visit_Program(const ASR::Program_t &x) { |
| 99 | + std::vector<std::pair<std::string, ASR::symbol_t*> > replace_vec; |
| 100 | + // FIXME: this is a hack, we need to pass in a non-const `x`, |
| 101 | + // which requires to generate a TransformVisitor. |
| 102 | + ASR::Program_t &xx = const_cast<ASR::Program_t&>(x); |
| 103 | + current_scope = xx.m_symtab; |
| 104 | + |
| 105 | + for (auto &item : x.m_symtab->get_scope()) { |
| 106 | + if (is_a<ASR::Function_t>(*item.second)) { |
| 107 | + ASR::Function_t *s = ASR::down_cast<ASR::Function_t>(item.second); |
| 108 | + if (s->m_return_var) { |
| 109 | + /* |
| 110 | + * A function which returns an array will be converted |
| 111 | + * to a subroutine with the destination array as the last |
| 112 | + * argument. This helps in avoiding deep copies and the |
| 113 | + * destination memory directly gets filled inside the subroutine. |
| 114 | + */ |
| 115 | + if( PassUtils::is_aggregate_type(s->m_return_var) ) { |
| 116 | + ASR::symbol_t* s_sub = create_subroutine_from_function(s); |
| 117 | + replace_vec.push_back(std::make_pair(item.first, s_sub)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Updating the symbol table so that now the name |
| 124 | + // of the function (which returned array) now points |
| 125 | + // to the newly created subroutine. |
| 126 | + for( auto& item: replace_vec ) { |
| 127 | + current_scope->add_symbol(item.first, item.second); |
| 128 | + } |
| 129 | + |
| 130 | + for (auto &item : x.m_symtab->get_scope()) { |
| 131 | + if (is_a<ASR::AssociateBlock_t>(*item.second)) { |
| 132 | + ASR::AssociateBlock_t *s = ASR::down_cast<ASR::AssociateBlock_t>(item.second); |
| 133 | + visit_AssociateBlock(*s); |
| 134 | + } |
| 135 | + if (is_a<ASR::Function_t>(*item.second)) { |
| 136 | + ASR::Function_t *s = ASR::down_cast<ASR::Function_t>(item.second); |
| 137 | + visit_Function(*s); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + current_scope = xx.m_symtab; |
| 142 | + transform_stmts(xx.m_body, xx.n_body); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +}; |
| 147 | + |
| 148 | +class ReplaceFunctionCallWithSubroutineCall: public PassUtils::PassVisitor<ReplaceFunctionCallWithSubroutineCall> { |
| 149 | + |
| 150 | + private: |
| 151 | + |
| 152 | + ASR::expr_t *result_var; |
| 153 | + |
| 154 | + public: |
| 155 | + |
| 156 | + ReplaceFunctionCallWithSubroutineCall(Allocator& al_): |
| 157 | + PassVisitor(al_, nullptr), result_var(nullptr) |
| 158 | + { |
| 159 | + pass_result.reserve(al, 1); |
| 160 | + } |
| 161 | + |
| 162 | + void visit_Assignment(const ASR::Assignment_t& x) { |
| 163 | + if( PassUtils::is_aggregate_type(x.m_target) ) { |
| 164 | + result_var = x.m_target; |
| 165 | + this->visit_expr(*(x.m_value)); |
| 166 | + } |
| 167 | + result_var = nullptr; |
| 168 | + } |
| 169 | + |
| 170 | + void visit_FunctionCall(const ASR::FunctionCall_t& x) { |
| 171 | + std::string x_name; |
| 172 | + if( x.m_name->type == ASR::symbolType::ExternalSymbol ) { |
| 173 | + x_name = down_cast<ASR::ExternalSymbol_t>(x.m_name)->m_name; |
| 174 | + } else if( x.m_name->type == ASR::symbolType::Function ) { |
| 175 | + x_name = down_cast<ASR::Function_t>(x.m_name)->m_name; |
| 176 | + } |
| 177 | + // The following checks if the name of a function actually |
| 178 | + // points to a subroutine. If true this would mean that the |
| 179 | + // original function returned an array and is now a subroutine. |
| 180 | + // So the current function call will be converted to a subroutine |
| 181 | + // call. In short, this check acts as a signal whether to convert |
| 182 | + // a function call to a subroutine call. |
| 183 | + if (current_scope == nullptr) { |
| 184 | + return ; |
| 185 | + } |
| 186 | + |
| 187 | + ASR::symbol_t *sub = current_scope->resolve_symbol(x_name); |
| 188 | + if (sub && ASR::is_a<ASR::Function_t>(*sub) |
| 189 | + && ASR::down_cast<ASR::Function_t>(sub)->m_return_var == nullptr) { |
| 190 | + LFORTRAN_ASSERT(result_var != nullptr); |
| 191 | + Vec<ASR::call_arg_t> s_args; |
| 192 | + s_args.reserve(al, x.n_args + 1); |
| 193 | + for( size_t i = 0; i < x.n_args; i++ ) { |
| 194 | + s_args.push_back(al, x.m_args[i]); |
| 195 | + } |
| 196 | + ASR::call_arg_t result_arg; |
| 197 | + result_arg.loc = result_var->base.loc; |
| 198 | + result_arg.m_value = result_var; |
| 199 | + s_args.push_back(al, result_arg); |
| 200 | + ASR::stmt_t* subrout_call = LFortran::ASRUtils::STMT(ASR::make_SubroutineCall_t(al, x.base.base.loc, |
| 201 | + sub, nullptr, |
| 202 | + s_args.p, s_args.size(), nullptr)); |
| 203 | + pass_result.push_back(al, subrout_call); |
| 204 | + } |
| 205 | + result_var = nullptr; |
| 206 | + } |
| 207 | + |
| 208 | +}; |
| 209 | + |
| 210 | +void pass_create_subroutine_from_function(Allocator &al, ASR::TranslationUnit_t &unit, |
| 211 | + const LCompilers::PassOptions& /*pass_options*/) { |
| 212 | + CreateFunctionFromSubroutine v(al); |
| 213 | + v.visit_TranslationUnit(unit); |
| 214 | + ReplaceFunctionCallWithSubroutineCall u(al); |
| 215 | + u.visit_TranslationUnit(unit); |
| 216 | + LFORTRAN_ASSERT(asr_verify(unit)); |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | +} // namespace LFortran |
0 commit comments