llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

flang bug in procedure arguments with local interfaces

Open zerothi opened this issue 4 years ago • 12 comments

Tested this with flang 11.1.0, 12.0.1 and 13.0.0.

Consider the following two codes:

m.f90:

module m
  implicit none

contains

  subroutine operate(z, process)

    real, intent(inout) :: z

    abstract interface
      subroutine my_sub(a)
        implicit none
        real, intent(inout) :: a
      end subroutine
    end interface
    procedure(my_sub) :: process

    call process(z)
    write(*,*) 'The result is:', z

  contains

    subroutine sint
    end subroutine sint

  end subroutine operate

end module m

and

n.f90:

module n
  implicit none
contains

  subroutine driver
    use m
    implicit none
    real :: x
    x = 1
    call operate(x, process=timestwo)
  end subroutine driver

  subroutine timestwo(y)
    implicit none
    real, intent(inout) :: y
    y = 2 * y
  end subroutine timestwo

end module n

Compiling with llvm generates error reports: They are simply compiled with:

flang -c m.f90
flang -c n.f90

llvm-11.1.0:

error: 'my_sub' must be an abstract interface or a procedure with an explicit interface
/opt/generic/llvm/11.1.0/bin/f18: semantic errors in n.f90

llvm-12.0.1:

./m.f18.mod:6:11: error: 'my_sub' must be an abstract interface or a procedure with an explicit interface
  procedure(my_sub)::process
            ^^^^^^
/opt/generic/llvm/12.0.1/bin/f18: Semantic errors in n.f90
flang: in /home/nicpa/tmp, f18 failed with exit status 0: /opt/generic/llvm/12.0.1/bin/f18 -module-suffix .f18.mod -intrinsic-module-directory /opt/generic/llvm/12.0.1/include/flang -c n.f90

llvm-13.0.0:

./n.f90:10:5: error: 'my_sub' is not a procedure
      call operate(x, process=timestwo)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
./m.f18.mod:6:11: error: 'my_sub' must be an abstract interface or a procedure with an explicit interface
  procedure(my_sub)::process
            ^^^^^^
error: Semantic errors in n.f90
flang: in /home/nicpa/tmp, flang-new failed with exit status 1: /opt/generic/llvm/13.0.0/bin/flang-new -fc1 -module-suffix .f18.mod -fdebug-unparse -fno-analyzed-objects-for-unparse -c n.f90

This compiles fine with multiple other compilers and as far as we (@jme52) can tell is standard compliant.

zerothi avatar Jan 26 '22 10:01 zerothi

@llvm/issue-subscribers-flang-frontend

llvmbot avatar Jan 26 '22 15:01 llvmbot

I was able to re-produce this with d832078904c.

banach-space avatar Mar 07 '22 10:03 banach-space

The problem is that the interface definition does not make it to the module file (m.mod). I'll take a look, but it will likely take me a while. No hard feelings if somebody with more experience and/or bandwidth wants to take over, just let me know.

ekieri avatar Mar 08 '22 21:03 ekieri

I have proposed a fix in https://reviews.llvm.org/D121738

ekieri avatar Mar 15 '22 20:03 ekieri

This is much appreciated, thank you @ekieri !

banach-space avatar Mar 16 '22 14:03 banach-space

Thank you. If I understand correctly, this fix was applied to the main branch, would it be possible to backport it to the 14.x branch?

jme52 avatar May 02 '22 10:05 jme52

Applied to main, yes. I am afraid a backport is unlikely. Flang is still a work in progress, with many important features pending implementation. If you need this fix now, I advice you to build flang yourself: you could build the main branch, or locally cherry-pick this fix to a release branch. Please see https://llvm.org/docs/GettingStarted.html and https://github.com/llvm/llvm-project/blob/main/flang/README.md#building-flang for build instructions.

ekieri avatar May 02 '22 16:05 ekieri

This bug was breaking compilation of our Fortran 2003 electronic structure software with flang. Even if the developer team can patch flang and build a fixed 14.x compiler, there's little point in doing that if our users cannot access a fixed release. No backport means that we will have to consider that our code does not support flang < 15.

jme52 avatar May 02 '22 19:05 jme52

@jme52 Technically speaking, there was no support for generating machine code or executables in LLVM Flang when the release 14 branch was created. Some bits were there, but not everything. And that’s mostly for F95. It’s a bit better now, but there are still a few key bits missing. So I am a bit surprised that you were able to build a F2003 application :) Which driver did you use - “flang” or “flang-new”? Just curious - it’s great to hear that people have been building real code with LLVM Flang!

@ekieri To back port, we could just follow https://llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches.

banach-space avatar May 02 '22 20:05 banach-space

At this point I was looking into the compilation of our code, I have not tested the executable yet. As for the driver, I was calling flang for compilation, is that it? (I cannot find the documentation page where drivers are discussed, if you could point me in its direction that would be helpful). Given your comment, I think I must have been using gfortran for machine code generation still...

jme52 avatar May 03 '22 10:05 jme52

@jme52 This helps a lot, thank you!

As for the driver, I was calling flang for compilation, is that it?

No :) That's just a bash script.

(I cannot find the documentation page where drivers are discussed, if you could point me in its direction that would be helpful).

Try here.

Given your comment, I think I must have been using gfortran for machine code generation still...

Indeed. It's really our fault not making this clearer. And you are not the first person to get confused by all this :) Perhaps this could help: https://reviews.llvm.org/D124848?

banach-space avatar May 03 '22 12:05 banach-space

Try here.

Thank you, that whole page is the document I had been looking for, it's really useful. Now I understand it.

I think the addition of that link to the bash script, as suggested, will be helpful. I think #55243 would also help.

jme52 avatar May 03 '22 13:05 jme52