On 11/21/26 23:31, Thomas Koenig wrote:
Am 21.11.26 um 18:02 schrieb Steve Kargl:
On 11/21/26 02:22, Thomas Koenig wrote:
Hello world,
I have looked at reassociation for a bit. Fortran allows expressions to
be reassociated, but parentheses must be honored. This we do (at
least as
long as -fprotect-parens is in force).
The above is specifically stated in the standard.
F2023: 10.1.5.2.4(2)
Once the interpretation of a numeric intrinsic operation is
established, the processor may evaluate any mathematically
equivalent expression, provided that the integrity of parentheses
is not violated.
Note 2 on page 159, states a left-to-right evaluation order. So,
x = a + b + c + d from l-to-r is x = ((a + b) + c) + d. But,
Note 2 on page 163 shows allowed alternate forms. The RHS can
be evaluated as x = (a + b) + (c + d) or x = a + (b + c) + d.
There is also F2023:10.1.7.
It is not necessary for a processor to evaluate all of the
operands of an expression, or to evaluate entirely each
operand, if the value of the expression can be determined
otherwise.
OK.
But Fortran forbids reassociation
across different expressions or statements,
Can you point me to where the standard makes such a statement.
Coming back to my example:
real(8) function add_v2 (a,b,c,d) result(res)
real(8), value :: a, b, c, d
real(8) :: tmp
tmp = a + b
tmp = tmp + c
res = tmp + d
end function add_v2
is currently, with -fassociative-math, translated as if it were
written
real(8) function add_v2 (a,b,c,d) result(res)
real(8), value :: a, b, c, d
real(8) :: tmp1, tmp2
tmp1 = (a+b)
tmp2 = (c+d)
res = tmp1 + tmp2
end function add_v2
which is not permitted by the standard, as far as I have been able to
read clause 10.
Can you point to the text in the standard that supports this claim?
Clause 10 concerns the evaluation of expressions and assignment.
AFAICT, it does not discuss the possibility of re-ordering of
statements. The closest I can find is in clause 5.
5.3.5 Execution sequence
...
With the following exceptions, the effect of execution is as
if the executable constructs are executed in the order in which
they appear in the main program or subprogram until a STOP, ERROR
STOP, RETURN, or END statement is executed.
The exceptions are all concerned with branching. Taken to an
extreme, the above suggests that each statement must be evaluated
in order without any optimization between statements. That is,
we might as well write an interpreter instead of a compiler.
For a pure function foo(),
x = 2 + foo(1.)
y = 3 + foo(1.)
it seems that you're suggesting the foo(1.) must be called twice
as the statements are independent (except I already quoted 10.1.7).
This suggests the standard forbids common subexpression elimination.
Common subexpression is fine, as long as parentheses are not permitted.
t1 = a + b + c
t2 = a + c
can be reassociated to
t2 = a + c
t1 = t1 + b
without violating the standard.
I suspect you meant
t2 = a + c
t1 = t2 + b
That's not what 5.3.5 implies. To maintain order
of execution with respect to the assignment. It would be
t3 = a + c
t1 = t2 + b
t2 = t3
Going back to 5.3.5 and your original program
tmp = a + b
tmp = tmp + c
res = tmp + d
"The effect of execution is as-if the constructs are evaluated
in order" as
tmp = a + b + c + d
--
steve