-
Notifications
You must be signed in to change notification settings - Fork 176
Fix ASR Slice frontend #1051
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
base: main
Are you sure you want to change the base?
Fix ASR Slice frontend #1051
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||||||||||||||||||||
| from ltypes import i32 | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_issue_332(): | ||||||||||||||||||||||||
| arr: i32[4] | ||||||||||||||||||||||||
| arr[0] = 1 | ||||||||||||||||||||||||
| arr[1] = 0 | ||||||||||||||||||||||||
| arr[2] = -4 | ||||||||||||||||||||||||
| print(arr[:2]) | ||||||||||||||||||||||||
| print(arr[1]) | ||||||||||||||||||||||||
| print(arr[2]) | ||||||||||||||||||||||||
|
Comment on lines
+8
to
+10
Collaborator
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. Usage of
Suggested change
Collaborator
Author
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. Not yet:
Collaborator
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. Makes sense. Can you try fixing the bug?
Collaborator
Author
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. I think the LLVM backend currently only supports string-slicing assignments and not arrays because they can be n-dimensional. Is that intentional? |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| test_issue_332() | ||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -801,9 +801,11 @@ LFORTRAN_API char* _lfortran_str_copy(char* s, int32_t idx1, int32_t idx2) { | |
| return dest_char; | ||
| } | ||
|
|
||
| // This functions considers idx1 and idx2 both inclusive. | ||
| LFORTRAN_API char* _lfortran_str_slice(char* s, int32_t idx1, int32_t idx2, int32_t step, | ||
| bool idx1_present, bool idx2_present) { | ||
| int s_len = strlen(s); | ||
| idx2++; | ||
|
Collaborator
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. I am surprised why tests didn't fail after this change. Can you try applying this specific change to LFortran to make sure this works there as well.
Collaborator
Author
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. Just because the frontend subtracts one and in backend (internally) we add one again. So, logic remains the same. And it's expected to pass all the tests.
Collaborator
Author
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.
Yep, tried. All tests passed for me. On the latest main branch of LFortran: diff --git a/src/libasr/runtime/lfortran_intrinsics.c b/src/libasr/runtime/lfortran_intrinsics.c
index 17399c784..b71bfdd95 100644
--- a/src/libasr/runtime/lfortran_intrinsics.c
+++ b/src/libasr/runtime/lfortran_intrinsics.c
@@ -784,6 +784,7 @@ LFORTRAN_API char* _lfortran_str_copy(char* s, int32_t idx1, int32_t idx2) {
LFORTRAN_API char* _lfortran_str_slice(char* s, int32_t idx1, int32_t idx2, int32_t step,
bool idx1_present, bool idx2_present) {
int s_len = strlen(s);
+ idx2++;
if (step == 0) {
Collaborator
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. Just make a PR there (in LFortran) as well with a similar test. We will merge both together. |
||
| if (step == 0) { | ||
| printf("slice step cannot be zero\n"); | ||
| exit(1); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,4 +10,4 @@ | |
| "stderr": null, | ||
| "stderr_hash": null, | ||
| "returncode": 0 | ||
| } | ||
| } | ||
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.
Use
assertfor verifying that the outputs are actually correct?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.
I am not sure if we can assign Array Section at present (I think only string is supported).
Throws this: