[Flang] Miscomparison in spec2017 527.cam4_r with size=ref
Note 1: Failure observed with AArch64 machine. Have not tried other platforms. Note 2: Failure observed with (-O3) and without optimisations. Note 3: size=test, size=train passes
@jpenix-quic Were you able to run this benchmark successfully with llvm/flang?
Unfortunately not--I'm seeing the same behavior (pass on test data, miscompare on ref data on aarch64). Previously I had only run using the test data--apologies for the mixup on my end!
I also hit a failure on x86_64, so this is not aarch64 specific.
I spent some time on this issue but I have not yet been able to find the reason for the miscomparison.
The allowable relative tolerance for this benchmark is 0.0035 but llvm/flang achieves only around 0.058. Although there is a 16x reduction in precision, since it does not go above 0.1, i think it is a precision issue.
The following attempts did not work.
-> Using fma's did not help (tried by switching this feature ON in llvm globally). There is no improvement in the precision of the result.
-> Using -fdefault-real-8 did not work due to compilation failures.
-> Tracing the differences proved difficult with differences being propagated to multiple arrays across different functions.
While there is a reference (gfortran) that passes, comparing floating point numbers is a bit difficult since they are mostly not bitwise matching, minor differences in formatting make eye-balling the results a bit difficult. Since there is no debug info available, a debugging session is not possible. While printing elements is possible, the sizes of the arrays are huge here.
I am looking for suggestions on things to try out and for help in general.
I am still seeing this miscompare as of the last time I tested at af8c49dc1ec44339d915d988ffe0f38da68ca0e7. Please let me know if you are no longer seeing this issue!
That said, I've been looking at this and (assuming I didn't miss a fix or already reported issue) I think the miscompare might be related to non-default lower bounds. A reproducer is below:
program p
implicit none
type mytype
real, pointer :: ptr(:)
end type mytype
type(mytype) :: temp
real, pointer :: temp_ptr(:)
integer :: i
allocate(temp%ptr(2:6))
do i = 2,6
temp%ptr(i) = i
enddo
temp_ptr => temp%ptr
print *, temp%ptr(2), temp_ptr(2)
end
gfortran and ifort both print 2.0 2.0 for this program (please see https://godbolt.org/z/sj4jExr56) but flang at a2ef44a5d65932c7bb0f483217826856325b60df prints 2.0 3.0 for me. I think this is valid Fortran and gfortran/ifort's behavior is expected but I am not 100% certain (I mainly tried to match what it looked like cam4 was doing). I'm also not sure if this is the only issue--this is just what I ran into when trying to trace backwards through the arrays.
I'm planning to try to resolve this (assuming this is an actual issue and I didn't make a mistake in the testcase, etc.), but @kiranchandramohan and @jeanPerier please let me know if you have any thoughts/suggestions on this or want to handle this!
Many thanks for investigating this @jpenix-quic. This is great work!
This looks to me like a bug. @tblah has been looking into this and he also mentioned some issues with arrays earlier today. He can probably confirm the issue.
@jeanPerier replied to a message in slack that there are some known issues regarding derived-type components having a lower-bound. But that was with reference to a TODO. https://flang-compiler.slack.com/archives/C013C6AE45Q/p1669028224843949
Please wait till you hear from them.
Thanks for your work @jpenix-quic.
I have been looking into this too and I came to the same conclusion. Please contact me if there is anything I can help with to resolve this issue.
Thanks for the investigation @jpenix-quic, I confirm this is a bug. It is not directly related to the component lower bounds TODO, the issue seems to be somewhere in genExprBox, where a fir::BoxValue is created for temp%ptr without setting its lower bounds.
I believe it is something that will be fixed by HLFIR changes anyway (But I have not lowered pointer assignments and derived type component there yet). A fix in the current lowering might still be worth it to get cam4 passing, but I would not prioritize it. But if you want to have a go, please do (I suspect the lower bounds could be lost here: https://github.com/llvm/llvm-project/blob/011b2af0f43aa888f843ce4011d947b80145bac5/flang/lib/Lower/Bridge.cpp), I did not check this code is actually exercised by your reproducer.
Thanks, @jeanPerier. A fix for cam4 would be great.
I suspect the lower bounds could be lost here: https://github.com/llvm/llvm-project/blob/011b2af0f43aa888f843ce4011d947b80145bac5/flang/lib/Lower/Bridge.cpp
@jeanPerier Did you miss a line-number here? Or were you just pointing to the file?
I took a quick look. I simplified the test case:
subroutine test(temp, temp_ptr)
implicit none
type mytype
real, pointer :: ptr(:)
end type mytype
type(mytype) :: temp
real, pointer :: temp_ptr(:)
temp_ptr => temp%ptr
end
$ bbc -emit-fir test.f90
func.func @_QPtest(%arg0: !fir.ref<!fir.type<_QFtestTmytype{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>> {fir.bindc_name = "temp"}, %arg1: !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>> {fir.bindc_name = "temp_ptr"}) {
%0 = fir.field_index ptr, !fir.type<_QFtestTmytype{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
%1 = fir.coordinate_of %arg0, %0 : (!fir.ref<!fir.type<_QFtestTmytype{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
%2 = fir.load %1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
%c0 = arith.constant 0 : index
%3:3 = fir.box_dims %2, %c0 : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, index) -> (index, index, index)
%4 = fir.shift %3#0 : (index) -> !fir.shift<1>
%5 = fir.rebox %2(%4) : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, !fir.shift<1>) -> !fir.box<!fir.array<?xf32>>
%6 = fir.rebox %5 : (!fir.box<!fir.array<?xf32>>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
fir.store %6 to %arg1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
return
}
The problem should be the rebox in pointer assignment (in Bridge.cpp). The correct FIR should be like the following:
func.func @_QPtest(%arg0: !fir.ref<!fir.type<_QFtestTmytype{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>> {fir.bindc_name = "temp"}, %arg1: !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>> {fir.bindc_name = "temp_ptr"}) {
%0 = fir.field_index ptr, !fir.type<_QFtestTmytype{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
%1 = fir.coordinate_of %arg0, %0 : (!fir.ref<!fir.type<_QFtestTmytype{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
%2 = fir.load %1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
%c0 = arith.constant 0 : index
%3:3 = fir.box_dims %2, %c0 : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, index) -> (index, index, index)
%4 = fir.shift %3#0 : (index) -> !fir.shift<1>
%5 = fir.rebox %2(%4) : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, !fir.shift<1>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
fir.store %5 to %arg1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
return
}
@jpenix-quic Do you want to give it a try?
Thanks @kiranchandramohan @tblah and @jeanPerier for confirming and providing suggestions/pointers on this! Also, thanks @PeixinQiao for the simplified test case and guidance on what the correct IR should be! I'll try to fix this but I'll be sure to reach out if I run into any issues/difficulties.
Sorry, I gave a bad reference in my comment above. I suspect the rebox is bad because lower bounds are lost here: https://github.com/llvm/llvm-project/blob/011b2af0f43aa888f843ce4011d947b80145bac5/flang/lib/Lower/ConvertExpr.cpp#L5550, but I did not verify this.
Apologies for the delay in getting back to this!
@jeanPerier, that does seem to be where the lower bounds were getting lost, thanks!
I ran into a few questions that I got stuck on, so I created a review with the questions I have here: https://reviews.llvm.org/D139800. I believe I added everyone here as a reviewer, but if anyone has any suggestions or advice I would greatly appreciate it!