[flang-commits] [flang] [flang] Skip optimized bufferization on volatile refs (PR #140781)

via flang-commits flang-commits at lists.llvm.org
Wed May 21 01:21:38 PDT 2025


https://github.com/jeanPerier approved this pull request.

Thanks for the fix!

Your fix makes sense to me as a general fix (we are probably lucky no operation with read effect to unknown value did not hit this before, they are probably not very common, or goes with write effects that made the code exit before).

I think there is still a case for doing optimized bufferization on volatile refs, but it is probably fine and safer to bail without more thinking.

Fortran does not really have a strong memory model like C++ that would allow precisely defining VOLATILE. The only thing the F2023 standard is saying in 8.5.20 is : 

_"The Fortran processor should use the most recent definition of a volatile object each time its value is required. When a volatile object is defined by means of Fortran, it should make that definition available to the non-Fortran parts of the program as soon as possible."_

It is not clear to my that this should prevent us from the evaluating RHS of `x = x + y` directly into `x` if `x` or `y` is VOLATILE and there is no aliasing. VOLATILE is not mentioned in 15.5.2.14 as something allowing aliasing between Fortran entities, so I think it is still safe to assume that `x` and `y` do not overlap even when both are VOLATILE.

At least gfortran and ifx still optimize assignments when they are VOLATILE read/writes: https://godbolt.org/z/b9x317ezK

To clarify, I am not asking you to update the patch to ignore VOLATILE effects here (we would still need a bit more thinking to make sure things are OK for the cases more complex than `x = x +y` from my example). I just do not want  future us to believe it would be incorrect to do so if we need do.

https://github.com/llvm/llvm-project/pull/140781


More information about the flang-commits mailing list