Skip to content
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

Bug: Expressions in DIMENSION attribute of arrays inside a SUBROUTINE lead to an ASR verify pass error #4013

Closed
kmr-srbh opened this issue May 16, 2024 · 13 comments · Fixed by #4034
Labels
bug Something isn't working SNAP Issue or pull request related to compiling lanl/snap

Comments

@kmr-srbh
Copy link
Contributor

kmr-srbh commented May 16, 2024

Minimum Reproducible Example (MRE)

m.f90

MODULE m
   IMPLICIT NONE
   INTEGER :: nx = 2
END MODULE m

example.f90

MODULE example
   USE m
   IMPLICIT NONE
CONTAINS
   SUBROUTINE sub (n)

      INTEGER, DIMENSION(nx-1) :: n
      
   END SUBROUTINE sub
END MODULE example

PROGRAM test
   use example
END PROGRAM test

Output

(base) saurabh-kumar@Awadh:~/Projects/System/lfortran$ lfortran ./examples/example.f90
ASR verify pass error: ASR::ttype_t in ASR::FunctionType cannot be tied to a scope.
 --> ./examples/example.f90:5:4 - 9:21
  |
5 |       SUBROUTINE sub (n)
  |       ^^^^^^^^^^^^^^^^^^...
...
  |
9 |       END SUBROUTINE sub
  | ...^^^^^^^^^^^^^^^^^^^^^ failed here


Note: Please report unclear, confusing or incorrect messages as bugs at
https://github.com/lfortran/lfortran/issues.

The bug was caught when trying to compile file expxs.f90 of the SNAP package.

The subroutine expxs_slgg ( scat, map, cs ) causes the above error due to the parameter cs which is declared as REAL(r_knd), DIMENSION(cmom-1,nx), INTENT(OUT) :: cs. On further analysis, we find that the expression cmom-1 is the cause of the issue. Other similar expressions like cmom+1, cmom * 2, etc also lead to the above error. We can simplify the issue to the MRE provided above.

@kmr-srbh kmr-srbh added the SNAP Issue or pull request related to compiling lanl/snap label May 16, 2024
@assem2002
Copy link
Contributor

Do you think it's due to the dimension-shape not being a constant at compile time?

@kmr-srbh
Copy link
Contributor Author

kmr-srbh commented May 16, 2024

Do you think it's due to the dimension-shape not being a constant at compile time?

Thank you @assem2002 , I had missed the module part of the issue. I have updated the code. The previous code had failed when the dimension was not an expression.

@assem2002
Copy link
Contributor

It's still the same problem though. If you figure out any fix inform me, I will look up this issue too and I'll keep updated.

@kmr-srbh
Copy link
Contributor Author

It's still the same problem though. If you figure out any fix inform me, I will look up this issue too and I'll keep updated.

The code compiles and executes properly with GFortran. It compiles with LFortran when we replace nx-1 with nx. Also, the issue does not occur outside of a subroutine. Could you please explain why you think this could be the reason?

@assem2002
Copy link
Contributor

it got confusing when found out it works with nx but not with nx - 1. I'll dig into it and let you know.

@kmr-srbh
Copy link
Contributor Author

it got confusing when found out it works with nx but not with nx - 1. I'll dig into it and let you know.

Yes! The previous code I had provided was incorrect. The issue here has a module in the picture too. The above is the correct code.

@kmr-srbh kmr-srbh added the bug Something isn't working label May 17, 2024
@certik
Copy link
Contributor

certik commented May 19, 2024

I think a variation of this bug was fixed by @Pranavchiku in #3962. Maybe the fix does not work if the module is compiled separately?

@kmr-srbh
Copy link
Contributor Author

I think a variation of this bug was fixed by @Pranavchiku in #3962. Maybe the fix does not work if the module is compiled separately?

I tried the MRE provided in #3962. From what I have observed, setting array dimension works with module variables, but not when we convert it to an expression. So, doing real, dimension(nx), intent(in) :: cs works, but real, dimension(nx-1), intent(in) :: cs fails.

(lf) saurabh-kumar@Awadh:~/Projects/System/lfortran$ lfortran ./examples/example.f90
ASR verify pass error: ASR::ttype_t in ASR::FunctionType cannot be tied to a scope.
 --> ./examples/example.f90:5:1 - 9:14
  |
5 |    subroutine a(cs)
  |    ^^^^^^^^^^^^^^^^...
...
  |
9 |    end subroutine
  | ...^^^^^^^^^^^^^^ failed here


Note: Please report unclear, confusing or incorrect messages as bugs at
https://github.com/lfortran/lfortran/issues.

Given that we already have a fix for it's base case in #3962, I think this should be easy to fix. I am trying to fix this.

@kmr-srbh
Copy link
Contributor Author

@certik got this. This is an issue with handling binary integer operations inside the dimension attribute. I am pushing a fix in a while.

(base) saurabh-kumar@Awadh:~/Projects/System/lfortran$ lfortran ./examples/example.f90
integer binop
ASR verify pass error: ASR::ttype_t in ASR::FunctionType cannot be tied to a scope.
 --> ./examples/example.f90:5:1 - 9:14
  |
5 |    subroutine a(cs)
  |    ^^^^^^^^^^^^^^^^...
...
  |
9 |    end subroutine
  | ...^^^^^^^^^^^^^^ failed here


Note: Please report unclear, confusing or incorrect messages as bugs at
https://github.com/lfortran/lfortran/issues.

@Pranavchiku
Copy link
Contributor

Pranavchiku commented May 19, 2024

Yes, handling binary operation will do the job. Just transfer it to something like “INTEGER, DIMENSION(get_nx()-1) :: n”

@Pranavchiku
Copy link
Contributor

Pranavchiku commented May 19, 2024

An approach to fix this is to create a BaseExprReplacer that visits passed expression with special handling for Var_t,IntegerBinOp_t, etc. Apply same logic on visit_Var and then we can expand it.

@kmr-srbh
Copy link
Contributor Author

An approach to fix this is to create a BaseExprReplacer that visits passed expression with special handling for Var_t,IntegerBinOp_t, etc. Apply same logic on visit_Var and then we can expand it.

Let me have a look into it! 👍

@Pranavchiku
Copy link
Contributor

Let me have a look into it! 👍

i’ll ask you to get current PR to finish line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SNAP Issue or pull request related to compiling lanl/snap
Projects
None yet
4 participants