On 01/12/2026 00:54, Dan Cross wrote:
> In article <11ej…@dont-email.me>, > Thomas Koenig <tkoe…@netcologne.de> wrote:
>> Dan Cross <cro…@spitfire.i.gajendra.net> schrieb: >>
>>> Rules for undefined behavior in Fortran obviously different from >>> those of C, since Fortran is a different langauge.
>> >> Fortran has not concept of undefined behavior in the standard per se.
> > It was pretty clear to me that the reference to UB in the ticket > you linked to was just a shorthand. >
>> It has correct code, code that violates numbered constraints (which >> require a diagnostic) and code that violates a "shall" directive, >> which is errouneous but for which no diagnostic is required. >> >> Integer overflow is among the restrictions that a processor (Fortran >> standardese for the combination of compiler, run-time system etc) is >> not required to catch, a.k.a. the programmer's fault. And Fortran >> allows reassociation as long as parentheses are not violated.
> > Ok. The point I was trying to make is that the context was not > C; it was Fortran. Thus, references to whatever C's UB rules > are don't really apply.
While that's true, AFAIUI (and Thomas will hopefully correct me if I am wrong!) the Fortran rules for signed and unsigned integer arithmetic here are the same as for C. Fortran does not use the term "undefined behaviour", but the meaning is the same. And in this particular case, in both C and Fortran, addition is fully commutative and associative for both signed and unsigned types in the standard language. It is only when additional semantics are applied (such as trapping on overflow) that there is any difference.
>
>>> C came up in this context as a parenthetical, "if one considers >>> the assembly equivalent of this...". Here, it was clear that C >>> syntax was merely used as a pseudocode. Arguments about C's >>> behavior and definition are beside the point.
>> >> There are a few points to consider here. Gcc is fairly C-centric, >> even now. Also, tools like the sanitizers could cause problems >> with integer reassociation.
Sanitizers should not be reducing or limiting the efficiency of code generated when the sanitizer is not enabled! It's perfectly fine to disable re-association (or whatever else is appropriate) when generating code with run-time checks from sanitizers - that is to be expected.
> > Ok, but that's a different matter. >
>> But I have been thinking about introducing a -freassoc-integer >> flag to gcc, and enabling it by default by gfortran. Maybe at >> the beginning of stage 1 for gcc 18.
> > Cool. >
It sounds to me that this is a limitation - or missed optimisation opportunity - in general in gcc, not a Fortran specific thing or something that should need a flag to enable. Note that I have no idea how any of this plays out in real timing on big modern processors - register renaming, OOO, super-scaler, etc., all make these things complicated. But I can see it is definitely suboptimal for the kind of processors I do know about, such as embedded microcontrollers. I've made an additional comment in the bug report, and hope that will be of some use.