flang-compiler / f18-llvm-project

Fork of llvm/llvm-project for f18. In sync with f18-mlir and f18.
http://llvm.org
28 stars 16 forks source link

[sync] Add some semantic checks for allocatable argument association #1557

Closed PeixinQiao closed 2 years ago

PeixinQiao commented 2 years ago

Allocatable sub-objects are not themselves allocatable (9.5.3.1 NOTE 2). The actual argument shall have deferred the same type parameters as the dummy argument if the argument is allocatable or pointer variable. Currently programs not following these get one crash during lowering or execution.

Cherry-pick https://reviews.llvm.org/D122899 and https://reviews.llvm.org/D122779.

jeanPerier commented 2 years ago

They are rejecting valid programs and still accepting some bad programs. Here is an illustration of a valid program that your patch would reject:

interface
subroutine explicit(x)
character(10), allocatable :: x(:)
end subroutine
end interface
character(10), allocatable :: x(:)
call explicit(x)
end

And here is an illustration of a bad program that your patch would still accept:

subroutine test(n)
interface
subroutine deferred_len(x)
character(:), allocatable :: x(:)
end subroutine
end interface
integer :: n
character(n), allocatable :: x(:)
call deferred_len(x)
end subroutine
PeixinQiao commented 2 years ago

They are rejecting valid programs and still accepting some bad programs. Here is an illustration of a valid program that your patch would reject: ... And here is an illustration of a bad program that your patch would still accept: ...

Well, I didn't notice these. I think I need one function to check the length type parameter match to handle this case.

Last point, is that the added check could be more generic: it also applies to POINTERs, and could be be done on parametrized derived type. It sounds fine to me if you leave out PDTs in a first patch, but I think POINTERs should be dealt with if ALLOCATABLEs are (the crashes you saw with bad programs would most likely occur with pointers).

For pointers, there is no such restrictions. I checked both gfortran and ifort, and current f18, passing array section and substring works fine for pointers. Maybe I missed some cases, but I prefer not to report error messages for pointers. I will leave PDTs later. Currently, I am not so familiar with how the PDTs works in F18, and don't get the test cases. These test cases are derived in classic-flang or workloads compiler by classic-flang (I mean using Huawei Bisheng based on open-source classic-flang).

All in all, I will fix all the above concerns and submit it in upstream.

PeixinQiao commented 2 years ago

Updated in https://reviews.llvm.org/D122779.

PeixinQiao commented 2 years ago

@jeanPerier Yes, please.