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
WASM: Supporting Multi-Dimensional Arrays #70
Conversation
Also, enable it and TreeVisitorVisitor()
Example Code for program expr2
implicit none
integer :: x (4, 10)
integer :: i, j
do i = 1, size(x, dim=1)
do j = 1, size(x, dim=2)
x(i, j) = 5
end do
end do
x(3, 10) = 2
print *, mysum(x)
contains
integer function mysum(a) result(r)
integer, intent(in) :: a(:, :)
integer :: i, j
print *, a(3, 10), a(10, 3)
r = 0
do i = 1, size(a, dim=1)
do j = 1, size(a, dim=2)
r = r + a(i, j)
end do
end do
end function
end program
2 5
197
2 5
197 |
* integer :: a(:, :) | ||
* | ||
* to: | ||
* integer :: a(2, 3) |
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.:
subroutine f(a)
integer :: a(:,:)
end subroutine
integer :: x(2,3)
call f(x)
Then you can indeed "specialize" f
for this specific case. But what if you have:
integer :: x(2,3), y(10,11), z(20,21)
call f(x)
call f(y)
call f(z)
?
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:
subroutine f(a, b)
integer :: a(:,:)
real :: b(:,:,:)
end subroutine
to this
subroutine f(na1, na2, a, nb1, nb2, nb3, b)
integer, intent(in) :: na1, na2, nb1, nb2, nb3
integer :: a(na1, na2)
real :: b(nb1, nb2, nb3)
end subroutine
And you have to change all places that call this function to change from:
call f(x, y)
to
call f(size(x,1), size(x, 2), x, size(y, 1), size(y, 2), size(y, 3), y)
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.
Then you can indeed "specialize" f for this specific case. But what if you have:
integer :: x(2,3), y(10,11), z(20,21)
call f(x)
call f(y)
call f(z)
?
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.
} | ||
} | ||
} | ||
} |
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.
Btw, every time I see identical code like this, I get more and more convinced we should just merge Subroutine/Function: lcompilers/lpython#866.
Ondřej, as currently the |
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.
Yes, this is fine to merge as is.
This
PR
brings support formulti-dimensional
arrays in thewasm
Backend.