-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
WASM: Supporting Multi-Dimensional Arrays #70
Merged
Shaikh-Ubaid
merged 9 commits into
lfortran:main
from
Shaikh-Ubaid:wasm_multi_dim_arrays
Aug 2, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
67ed729
Refactor: Move ArrayConstant to nearby other ArrayOpeartions
Shaikh-Ubaid 3c6da58
Refactor: WASM: Define and use get_array_dims()
Shaikh-Ubaid 1ba610c
WASM: Implement visit_ArraySize()
Shaikh-Ubaid c4d7fd2
WASM: Try to support mult-dimensional arrays
Shaikh-Ubaid cea9c7b
WASM: Support kind parameter in ArraySize
Shaikh-Ubaid 4b7398a
Visitor: Define StatementsFirstWalkVisitorVisitor()
Shaikh-Ubaid 53bffcd
Pass: Define pass_propagate_arr_dims()
Shaikh-Ubaid a755cff
WASM: Use pass_propagate_arr_dims()
Shaikh-Ubaid c2b280c
WASM: Enable supporting tests for Arrays
Shaikh-Ubaid File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <libasr/asr.h> | ||
#include <libasr/containers.h> | ||
#include <libasr/exception.h> | ||
#include <libasr/asr_utils.h> | ||
#include <libasr/asr_verify.h> | ||
#include <libasr/pass/for_all.h> | ||
#include <libasr/pass/stmt_walk_visitor.h> | ||
|
||
namespace LFortran { | ||
|
||
/* | ||
* This ASR pass replaces ttype for all arrays passed. | ||
* | ||
* Converts: | ||
* integer :: a(:, :) | ||
* | ||
* to: | ||
* integer :: a(2, 3) | ||
*/ | ||
|
||
class ArrDimsPropagate : public ASR::StatementsFirstBaseWalkVisitor<ArrDimsPropagate> | ||
{ | ||
private: | ||
Allocator &m_al; | ||
public: | ||
ArrDimsPropagate(Allocator &al) : m_al(al) { } | ||
|
||
void visit_FunctionCall(const ASR::FunctionCall_t &x) { | ||
ASR::Function_t *fn = ASR::down_cast<ASR::Function_t>(ASRUtils::symbol_get_past_external(x.m_name)); | ||
|
||
for (size_t i = 0; i < x.n_args; i++) { | ||
if (ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value) && ASRUtils::is_array(ASRUtils::expr_type(x.m_args[i].m_value))) { | ||
ASR::Variable_t* v = ASRUtils::EXPR2VAR(x.m_args[i].m_value); | ||
ASR::Variable_t *fn_param = ASRUtils::EXPR2VAR(fn->m_args[i]); | ||
ASR::dimension_t* m_dims; | ||
int n_dims = ASRUtils::extract_dimensions_from_ttype(fn_param->m_type, m_dims); | ||
if (n_dims > 0 && !m_dims[0].m_length && ASRUtils::check_equal_type(v->m_type, fn_param->m_type)) { | ||
fn_param->m_type = v->m_type; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void visit_SubroutineCall(const ASR::SubroutineCall_t &x) { | ||
ASR::Subroutine_t *sb = ASR::down_cast<ASR::Subroutine_t>(ASRUtils::symbol_get_past_external(x.m_name)); | ||
for (size_t i = 0; i < x.n_args; i++) { | ||
if (ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value) && ASRUtils::is_array(ASRUtils::expr_type(x.m_args[i].m_value))) { | ||
ASR::Variable_t* v = ASRUtils::EXPR2VAR(x.m_args[i].m_value); | ||
ASR::Variable_t *sb_param = ASRUtils::EXPR2VAR(sb->m_args[i]); | ||
ASR::dimension_t* m_dims; | ||
int n_dims = ASRUtils::extract_dimensions_from_ttype(sb_param->m_type, m_dims); | ||
if (n_dims > 0 && !m_dims[0].m_length && ASRUtils::check_equal_type(v->m_type, sb_param->m_type)) { | ||
sb_param->m_type = v->m_type; | ||
} | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, every time I see identical code like this, I get more and more convinced we should just merge Subroutine/Function: lcompilers/lpython#866. |
||
}; | ||
|
||
void pass_propagate_arr_dims(Allocator &al, ASR::TranslationUnit_t &unit) { | ||
ArrDimsPropagate v(al); | ||
v.visit_TranslationUnit(unit); | ||
LFORTRAN_ASSERT(asr_verify(unit)); | ||
} | ||
|
||
} // namespace LFortran |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef LFORTRAN_PASS_ARR_DIMS_PROPAGATE | ||
#define LFORTRAN_PASS_ARR_DIMS_PROPAGATE | ||
|
||
#include <libasr/asr.h> | ||
|
||
namespace LFortran { | ||
|
||
void pass_propagate_arr_dims(Allocator &al, ASR::TranslationUnit_t &unit); | ||
|
||
} // namespace LFortran | ||
|
||
#endif // LFORTRAN_PASS_ARR_DIMS_PROPAGATE |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you changing this based on the function call? I don't think you can do that in general, consider, e.g.:
Then you can indeed "specialize"
f
for this specific case. But what if you have:?
I think the way to do it is to just inline the function with our inline pass, then that should take care of it.
Alternatively, modify this pass to do the following:
Change this:
to this
And you have to change all places that call this function to change from:
to
This would be beautiful and extremely useful, and for cases when the array is contiguous, this would be equivalent (I think).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Effectively this would pass the array descriptor "by value" already at the ASR level. It would be an optional pass (for now you can use it in the WASM backend), but very useful down the road. These are the kinds of code transformations that we need to have, and later on we will combine them in various ways to get very good optimization pipeline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. I will update the pass soon as suggested above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, yes, I think, the current approach would/will fail on the shared example.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the suggested approach looks
beautiful
to me as well. Thank you for sharing it.