https://github.com/tblah updated https://github.com/llvm/llvm-project/pull/218746
>From 9fc22f69d7a3f45e5defd55c50292046e95cbc81 Mon Sep 17 00:00:00 2001 From: Tom Eccles <[email protected]> Date: Tue, 25 Aug 2026 17:12:17 +0100 Subject: [PATCH 1/2] [flang] Enable FP sum reassociation by default at O1 and above Enable Fortran-standard-compliant reassociation within individual REAL and COMPLEX sum expressions by default at -O1 and above. Keep left-to-right lowering at -O0 and preserve the explicit positive and negative Flang override flags. bbc uses -O2 by default, so enable the same lowering by default in bbc with a boolean option. Assisted-by: Codex --- clang/include/clang/Options/FlangOptions.td | 6 ++++-- flang/docs/ReleaseNotes.md | 5 +++++ flang/lib/Frontend/CompilerInvocation.cpp | 3 ++- flang/test/Driver/driver-help.f90 | 2 ++ flang/test/Lower/math-lowering/exponentiation.f90 | 6 +++--- flang/test/Lower/split-sum-expression-tree-lowering.f90 | 9 ++++++--- flang/tools/bbc/bbc.cpp | 7 +++++++ 7 files changed, 29 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Options/FlangOptions.td b/clang/include/clang/Options/FlangOptions.td index 7a3dfd84fd4e7..d5f7ac7f66b23 100644 --- a/clang/include/clang/Options/FlangOptions.td +++ b/clang/include/clang/Options/FlangOptions.td @@ -318,7 +318,8 @@ defm fp_sum_reassociation PosFlag<SetTrue, [], [], "Enable Fortran-standard compliant reassociation within " "individual REAL and COMPLEX sum expressions. This may " - "change exact floating-point results">, + "change exact floating-point results. Enabled by default " + "with -O1 and higher">, NegFlag<SetFalse, [], [], "Disable reassociation within individual REAL and COMPLEX " "sum expressions">>, @@ -326,7 +327,8 @@ defm fp_sum_reassociation Enable Fortran-standard compliant reassociation within individual ``REAL`` and ``COMPLEX`` sum expressions. This can improve optimization opportunities and may change exact floating-point results while - preserving standard-conforming Fortran semantics. + preserving standard-conforming Fortran semantics. It is enabled by + default with ``-O1`` and higher. }]>; def freal_sum_reassociation : Flag<["-"], "freal-sum-reassociation">, diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md index bbc7084c4a757..5697419710c36 100644 --- a/flang/docs/ReleaseNotes.md +++ b/flang/docs/ReleaseNotes.md @@ -33,6 +33,11 @@ page](https://llvm.org/releases/). ## Non-comprehensive list of changes in this release +- Fortran-standard-compliant reassociation within individual `REAL` and + `COMPLEX` sum expressions is now enabled by default at `-O1` and above. + This may change exact floating-point results. Flang users can restore + left-to-right evaluation with `-fno-fp-sum-reassociation`. + - The legacy array-value operations (`fir.array_load`, `fir.array_fetch`, `fir.array_update`, `fir.array_modify`, `fir.array_access`, `fir.array_amend`, `fir.array_merge_store`) have been removed from FIR, diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index 87a25f3101ddd..0a6dc48717c3e 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -318,7 +318,8 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts, opts.EnableSafeTrampoline = 1; if (args.hasFlag(clang::options::OPT_ffp_sum_reassociation, - clang::options::OPT_fno_fp_sum_reassociation, false)) + clang::options::OPT_fno_fp_sum_reassociation, + opts.OptimizationLevel > 0)) opts.SplitSumExpressionTree = 1; if (args.getLastArg(clang::options::OPT_floop_interchange)) diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90 index 1ca446d159305..56843777aa2d0 100644 --- a/flang/test/Driver/driver-help.f90 +++ b/flang/test/Driver/driver-help.f90 @@ -10,6 +10,7 @@ ! HELP: -ffp-sum-reassociation ! HELP: Enable Fortran-standard compliant reassociation within individual REAL and COMPLEX sum expressions ! HELP: may change exact floating-point results +! HELP: Enabled by default with -O1 and higher ! HELP-NOT: -freal-sum-reassociation ! HELP-FC1:USAGE: flang @@ -18,6 +19,7 @@ ! HELP-FC1: -ffp-sum-reassociation ! HELP-FC1: Enable Fortran-standard compliant reassociation within individual REAL and COMPLEX sum expressions ! HELP-FC1: may change exact floating-point results +! HELP-FC1: Enabled by default with -O1 and higher ! HELP-FC1-NOT: -freal-sum-reassociation ! ERROR: error: unknown argument '-helps'; did you mean '-help' diff --git a/flang/test/Lower/math-lowering/exponentiation.f90 b/flang/test/Lower/math-lowering/exponentiation.f90 index 6a007d08671d6..4d3ebd10ef5e8 100644 --- a/flang/test/Lower/math-lowering/exponentiation.f90 +++ b/flang/test/Lower/math-lowering/exponentiation.f90 @@ -1,8 +1,8 @@ -! RUN: bbc -emit-fir %s -o - --math-runtime=fast | FileCheck --check-prefixes=ALL,FAST %s +! RUN: bbc -emit-fir %s -o - --math-runtime=fast -ffp-sum-reassociation=false | FileCheck --check-prefixes=ALL,FAST %s ! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=fast %s -o - | FileCheck --check-prefixes=ALL,FAST %s -! RUN: bbc -emit-fir %s -o - --math-runtime=relaxed | FileCheck --check-prefixes=ALL,RELAXED %s +! RUN: bbc -emit-fir %s -o - --math-runtime=relaxed -ffp-sum-reassociation=false | FileCheck --check-prefixes=ALL,RELAXED %s ! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=relaxed %s -o - | FileCheck --check-prefixes=ALL,RELAXED %s -! RUN: bbc -emit-fir %s -o - --math-runtime=precise | FileCheck --check-prefixes=ALL,PRECISE %s +! RUN: bbc -emit-fir %s -o - --math-runtime=precise -ffp-sum-reassociation=false | FileCheck --check-prefixes=ALL,PRECISE %s ! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=precise %s -o - | FileCheck --check-prefixes=ALL,PRECISE %s function test_real4(x, y, s, i, k) diff --git a/flang/test/Lower/split-sum-expression-tree-lowering.f90 b/flang/test/Lower/split-sum-expression-tree-lowering.f90 index 4b9b49219d8b8..2da6c48e7326d 100644 --- a/flang/test/Lower/split-sum-expression-tree-lowering.f90 +++ b/flang/test/Lower/split-sum-expression-tree-lowering.f90 @@ -1,6 +1,9 @@ -! RUN: %flang_fc1 -emit-hlfir -ffp-sum-reassociation -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf -! RUN: %flang_fc1 -emit-hlfir -fno-fp-sum-reassociation -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE -! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE +! RUN: %flang_fc1 -emit-hlfir -O0 -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE +! RUN: %flang_fc1 -emit-hlfir -O1 -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf +! RUN: %flang_fc1 -emit-hlfir -O0 -ffp-sum-reassociation -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf +! RUN: %flang_fc1 -emit-hlfir -O1 -fno-fp-sum-reassociation -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE +! RUN: bbc -emit-hlfir -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf +! RUN: bbc -emit-hlfir -ffp-sum-reassociation=false -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE ! Default: (((x + a*b) + c*d) + e*f) ! Rewritten: ((c*d + e*f) + (x + a*b)) diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp index 7860a0fc77cf3..2469a4aa4c8f6 100644 --- a/flang/tools/bbc/bbc.cpp +++ b/flang/tools/bbc/bbc.cpp @@ -282,6 +282,12 @@ static llvm::cl::opt<bool> "the LHS of the intrinsic assignment"), llvm::cl::init(true)); +static llvm::cl::opt<bool> fpSumReassociation( + "ffp-sum-reassociation", + llvm::cl::desc("Enable Fortran-standard compliant reassociation within " + "individual REAL and COMPLEX sum expressions"), + llvm::cl::init(true)); + static llvm::cl::opt<bool> stackRepackArrays( "fstack-repack-arrays", llvm::cl::desc("Allocate temporary arrays for -frepack-arrays " @@ -506,6 +512,7 @@ static llvm::LogicalResult convertFortranSourceToMLIR( loweringOptions.setIntegerWrapAround(integerWrapAround); loweringOptions.setInitGlobalZero(initGlobalZero); loweringOptions.setReallocateLHS(reallocateLHS); + loweringOptions.setSplitSumExpressionTree(fpSumReassociation); loweringOptions.setStackRepackArrays(stackRepackArrays); loweringOptions.setRepackArrays(repackArrays); loweringOptions.setRepackArraysWhole(repackArraysWhole); >From 8c200e120518d3ea2f8b49d6a2ff3be5ba0da93f Mon Sep 17 00:00:00 2001 From: Tom Eccles <[email protected]> Date: Sun, 6 Sep 2026 18:45:57 +0100 Subject: [PATCH 2/2] Enable FP sum reassociation at all optimization levels I decided to do it at O0 after Jean's feedback on the RFC/Discourse. Thinking more about it, in badly written programs this could theoretically change program flow and so it would be very confusing if a slightly different numerical result was given at -O0 (e.g. a debug build) than -O3. Assisted-by: Codex --- clang/include/clang/Options/FlangOptions.td | 5 ++--- flang/docs/ReleaseNotes.md | 6 +++--- flang/lib/Frontend/CompilerInvocation.cpp | 3 +-- flang/test/Driver/driver-help.f90 | 4 ++-- flang/test/Lower/math-lowering/exponentiation.f90 | 6 +++--- flang/test/Lower/split-sum-expression-tree-lowering.f90 | 4 ++-- 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/clang/include/clang/Options/FlangOptions.td b/clang/include/clang/Options/FlangOptions.td index d5f7ac7f66b23..12ffcbfe0f936 100644 --- a/clang/include/clang/Options/FlangOptions.td +++ b/clang/include/clang/Options/FlangOptions.td @@ -318,8 +318,7 @@ defm fp_sum_reassociation PosFlag<SetTrue, [], [], "Enable Fortran-standard compliant reassociation within " "individual REAL and COMPLEX sum expressions. This may " - "change exact floating-point results. Enabled by default " - "with -O1 and higher">, + "change exact floating-point results. Enabled by default">, NegFlag<SetFalse, [], [], "Disable reassociation within individual REAL and COMPLEX " "sum expressions">>, @@ -328,7 +327,7 @@ defm fp_sum_reassociation ``REAL`` and ``COMPLEX`` sum expressions. This can improve optimization opportunities and may change exact floating-point results while preserving standard-conforming Fortran semantics. It is enabled by - default with ``-O1`` and higher. + default at all optimization levels. }]>; def freal_sum_reassociation : Flag<["-"], "freal-sum-reassociation">, diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md index 5697419710c36..ae757cbe1a8e6 100644 --- a/flang/docs/ReleaseNotes.md +++ b/flang/docs/ReleaseNotes.md @@ -34,9 +34,9 @@ page](https://llvm.org/releases/). ## Non-comprehensive list of changes in this release - Fortran-standard-compliant reassociation within individual `REAL` and - `COMPLEX` sum expressions is now enabled by default at `-O1` and above. - This may change exact floating-point results. Flang users can restore - left-to-right evaluation with `-fno-fp-sum-reassociation`. + `COMPLEX` sum expressions is now enabled by default at all optimization + levels. This may change exact floating-point results. Flang users can + restore left-to-right evaluation with `-fno-fp-sum-reassociation`. - The legacy array-value operations (`fir.array_load`, `fir.array_fetch`, `fir.array_update`, `fir.array_modify`, `fir.array_access`, diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index 0a6dc48717c3e..851e997f5a7ba 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -318,8 +318,7 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts, opts.EnableSafeTrampoline = 1; if (args.hasFlag(clang::options::OPT_ffp_sum_reassociation, - clang::options::OPT_fno_fp_sum_reassociation, - opts.OptimizationLevel > 0)) + clang::options::OPT_fno_fp_sum_reassociation, true)) opts.SplitSumExpressionTree = 1; if (args.getLastArg(clang::options::OPT_floop_interchange)) diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90 index 56843777aa2d0..ddffedac5d9f6 100644 --- a/flang/test/Driver/driver-help.f90 +++ b/flang/test/Driver/driver-help.f90 @@ -10,7 +10,7 @@ ! HELP: -ffp-sum-reassociation ! HELP: Enable Fortran-standard compliant reassociation within individual REAL and COMPLEX sum expressions ! HELP: may change exact floating-point results -! HELP: Enabled by default with -O1 and higher +! HELP: Enabled by default ! HELP-NOT: -freal-sum-reassociation ! HELP-FC1:USAGE: flang @@ -19,7 +19,7 @@ ! HELP-FC1: -ffp-sum-reassociation ! HELP-FC1: Enable Fortran-standard compliant reassociation within individual REAL and COMPLEX sum expressions ! HELP-FC1: may change exact floating-point results -! HELP-FC1: Enabled by default with -O1 and higher +! HELP-FC1: Enabled by default ! HELP-FC1-NOT: -freal-sum-reassociation ! ERROR: error: unknown argument '-helps'; did you mean '-help' diff --git a/flang/test/Lower/math-lowering/exponentiation.f90 b/flang/test/Lower/math-lowering/exponentiation.f90 index 4d3ebd10ef5e8..95cbfad7a83a8 100644 --- a/flang/test/Lower/math-lowering/exponentiation.f90 +++ b/flang/test/Lower/math-lowering/exponentiation.f90 @@ -1,9 +1,9 @@ ! RUN: bbc -emit-fir %s -o - --math-runtime=fast -ffp-sum-reassociation=false | FileCheck --check-prefixes=ALL,FAST %s -! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=fast %s -o - | FileCheck --check-prefixes=ALL,FAST %s +! RUN: %flang_fc1 -emit-fir -fno-fp-sum-reassociation -mllvm -math-runtime=fast %s -o - | FileCheck --check-prefixes=ALL,FAST %s ! RUN: bbc -emit-fir %s -o - --math-runtime=relaxed -ffp-sum-reassociation=false | FileCheck --check-prefixes=ALL,RELAXED %s -! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=relaxed %s -o - | FileCheck --check-prefixes=ALL,RELAXED %s +! RUN: %flang_fc1 -emit-fir -fno-fp-sum-reassociation -mllvm -math-runtime=relaxed %s -o - | FileCheck --check-prefixes=ALL,RELAXED %s ! RUN: bbc -emit-fir %s -o - --math-runtime=precise -ffp-sum-reassociation=false | FileCheck --check-prefixes=ALL,PRECISE %s -! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=precise %s -o - | FileCheck --check-prefixes=ALL,PRECISE %s +! RUN: %flang_fc1 -emit-fir -fno-fp-sum-reassociation -mllvm -math-runtime=precise %s -o - | FileCheck --check-prefixes=ALL,PRECISE %s function test_real4(x, y, s, i, k) real :: x, y, test_real4 diff --git a/flang/test/Lower/split-sum-expression-tree-lowering.f90 b/flang/test/Lower/split-sum-expression-tree-lowering.f90 index 2da6c48e7326d..7d418d09cc319 100644 --- a/flang/test/Lower/split-sum-expression-tree-lowering.f90 +++ b/flang/test/Lower/split-sum-expression-tree-lowering.f90 @@ -1,6 +1,6 @@ -! RUN: %flang_fc1 -emit-hlfir -O0 -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE +! RUN: %flang_fc1 -emit-hlfir -O0 -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf ! RUN: %flang_fc1 -emit-hlfir -O1 -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf -! RUN: %flang_fc1 -emit-hlfir -O0 -ffp-sum-reassociation -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf +! RUN: %flang_fc1 -emit-hlfir -O0 -fno-fp-sum-reassociation -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE ! RUN: %flang_fc1 -emit-hlfir -O1 -fno-fp-sum-reassociation -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE ! RUN: bbc -emit-hlfir -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE --implicit-check-not=arith.negf ! RUN: bbc -emit-hlfir -ffp-sum-reassociation=false -o - %s | FileCheck %s --check-prefixes=DEFAULT,NO-REWRITE _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
