[flang-commits] [flang] 170a479 - [flang][Driver] Add option for real sum reassociation (#207377)
via flang-commits
flang-commits at lists.llvm.org
Thu Jul 9 02:52:58 PDT 2026
Author: Tom Eccles
Date: 2026-07-09T10:52:54+01:00
New Revision: 170a479b64ef5837c760b640d04a91ebc950d265
URL: https://github.com/llvm/llvm-project/commit/170a479b64ef5837c760b640d04a91ebc950d265
DIFF: https://github.com/llvm/llvm-project/commit/170a479b64ef5837c760b640d04a91ebc950d265.diff
LOG: [flang][Driver] Add option for real sum reassociation (#207377)
Compiler driver option for #207371: -freal-sum-reassociation.
Disabled by default.
Assisted-by: Codex
Added:
flang/test/Driver/real-sum-reassociation.f90
Modified:
clang/include/clang/Options/FlangOptions.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CodeGenOptions.def
flang/include/flang/Lower/LoweringOptions.def
flang/lib/Frontend/CompilerInvocation.cpp
flang/lib/Lower/Bridge.cpp
flang/test/Driver/driver-help.f90
flang/test/Lower/split-sum-expression-tree-lowering.f90
Removed:
################################################################################
diff --git a/clang/include/clang/Options/FlangOptions.td b/clang/include/clang/Options/FlangOptions.td
index 18242a465341b..bafc063663fe2 100644
--- a/clang/include/clang/Options/FlangOptions.td
+++ b/clang/include/clang/Options/FlangOptions.td
@@ -310,6 +310,23 @@ def ffast_real_mod : Flag<["-"], "ffast-real-mod">, Group<f_Group>,
def fno_fast_real_mod : Flag<["-"], "fno-fast-real-mod">, Group<f_Group>,
HelpText<"Disable optimization of MOD for REAL types in presence of -ffast-math">;
+defm real_sum_reassociation
+ : BoolOptionWithoutMarshalling<
+ "f", "real-sum-reassociation",
+ PosFlag<SetTrue, [], [],
+ "Enable Fortran-standard compliant reassociation within "
+ "individual REAL sum expressions. This may change exact "
+ "floating-point results">,
+ NegFlag<SetFalse, [], [],
+ "Disable reassociation within individual REAL sum "
+ "expressions">>,
+ DocBrief<[{
+ Enable Fortran-standard compliant reassociation within individual
+ ``REAL`` sum expressions. This can improve optimization opportunities
+ and may change exact floating-point results while preserving
+ standard-conforming Fortran semantics.
+ }]>;
+
defm init_global_zero : BoolOptionWithoutMarshalling<"f", "init-global-zero",
PosFlag<SetTrue, [], [], "Zero initialize globals without default initialization (default)">,
NegFlag<SetFalse, [], [], "Do not zero initialize globals without default initialization">>;
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 662c0c8d9bd4a..d9de24fc04f9a 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -240,6 +240,8 @@ void Flang::addCodegenOptions(const ArgList &Args,
Args.addOptInFlag(CmdArgs, options::OPT_fexperimental_loop_fusion,
options::OPT_fno_experimental_loop_fusion);
+ Args.addOptInFlag(CmdArgs, options::OPT_freal_sum_reassociation,
+ options::OPT_fno_real_sum_reassociation);
handleInterchangeLoopsArgs(Args, CmdArgs);
handleVectorizeLoopsArgs(Args, CmdArgs);
diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def
index a5907b6edbd97..53ddc20a6e810 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -54,6 +54,7 @@ CODEGENOPT(VectorizeSLP, 1, 0) ///< Enable SLP vectorization.
CODEGENOPT(InterchangeLoops, 1, 0) ///< Enable loop interchange.
CODEGENOPT(FuseLoops, 1, 0) ///< Enable loop fusion.
CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning.
+CODEGENOPT(SplitSumExpressionTree, 1, 0) ///< Split REAL addition expression trees.
CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling
CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass
CODEGENOPT(DwarfVersion, 3, 0) ///< Dwarf version
diff --git a/flang/include/flang/Lower/LoweringOptions.def b/flang/include/flang/Lower/LoweringOptions.def
index e89ad75704609..0b02ffd5a3b22 100644
--- a/flang/include/flang/Lower/LoweringOptions.def
+++ b/flang/include/flang/Lower/LoweringOptions.def
@@ -35,6 +35,10 @@ ENUM_LOWERINGOPT(Underscoring, unsigned, 1, 1)
/// On by default.
ENUM_LOWERINGOPT(ProtectParens, unsigned, 1, 1)
+/// If true, split REAL addition expression trees.
+/// Off by default.
+ENUM_LOWERINGOPT(SplitSumExpressionTree, unsigned, 1, 0)
+
/// If true, assume the behavior of integer overflow is defined
/// (i.e. wraps around as two's complement). Off by default.
ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index cbb094106298f..79ad08353b64c 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -299,6 +299,10 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
clang::options::OPT_fno_safe_trampoline, false))
opts.EnableSafeTrampoline = 1;
+ if (args.hasFlag(clang::options::OPT_freal_sum_reassociation,
+ clang::options::OPT_fno_real_sum_reassociation, false))
+ opts.SplitSumExpressionTree = 1;
+
if (args.getLastArg(clang::options::OPT_floop_interchange))
opts.InterchangeLoops = 1;
@@ -2002,6 +2006,7 @@ void CompilerInvocation::setLoweringOptions() {
loweringOpts.setIntegerWrapAround(langOptions.getSignedOverflowBehavior() ==
Fortran::common::LangOptions::SOB_Defined);
loweringOpts.setProtectParens(codegenOpts.ProtectParens);
+ loweringOpts.setSplitSumExpressionTree(codegenOpts.SplitSumExpressionTree);
Fortran::common::MathOptionsBase &mathOpts = loweringOpts.getMathOptions();
// TODO: when LangOptions are finalized, we can represent
// the math related options using Fortran::commmon::MathOptionsBase,
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 281ad99dc6637..8658a6fb29a44 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -91,10 +91,6 @@ static llvm::cl::opt<bool> forceLoopToExecuteOnce(
"always-execute-loop-body", llvm::cl::init(false),
llvm::cl::desc("force the body of a loop to execute at least once"));
-static llvm::cl::opt<bool> enableSplitSumExpressionTreeLowering(
- "enable-split-sum-expression-tree-lowering", llvm::cl::Hidden,
- llvm::cl::desc("Enable experimental split sum expression tree lowering"));
-
namespace {
/// Information for generating a structured or unstructured increment loop.
struct IncrementLoopInfo {
@@ -5576,7 +5572,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
auto evaluateRhs = [&](Fortran::lower::StatementContext &stmtCtx) {
const Fortran::lower::SomeExpr *rhsExpr = &assign.rhs;
std::optional<Fortran::lower::SomeExpr> rewritten;
- if (enableSplitSumExpressionTreeLowering &&
+ if (bridge.getLoweringOptions().getSplitSumExpressionTree() &&
Fortran::evaluate::CanBuildSplitSumExpressionTree(assign.lhs,
assign.rhs)) {
rewritten =
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index 4c3609db80b9a..e77fc460850b2 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -7,9 +7,15 @@
! HELP:USAGE: flang
! HELP-EMPTY:
! HELP-NEXT:OPTIONS:
+! HELP: -freal-sum-reassociation
+! HELP: Enable Fortran-standard compliant reassociation within individual REAL sum expressions
+! HELP: may change exact floating-point results
! HELP-FC1:USAGE: flang
! HELP-FC1-EMPTY:
! HELP-FC1-NEXT:OPTIONS:
+! HELP-FC1: -freal-sum-reassociation
+! HELP-FC1: Enable Fortran-standard compliant reassociation within individual REAL sum expressions
+! HELP-FC1: may change exact floating-point results
! ERROR: error: unknown argument '-helps'; did you mean '-help'
diff --git a/flang/test/Driver/real-sum-reassociation.f90 b/flang/test/Driver/real-sum-reassociation.f90
new file mode 100644
index 0000000000000..7d98da0aabb39
--- /dev/null
+++ b/flang/test/Driver/real-sum-reassociation.f90
@@ -0,0 +1,27 @@
+! Test driver handling of -freal-sum-reassociation and
+! -fno-real-sum-reassociation.
+
+! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \
+! RUN: | FileCheck %s --check-prefix=DISABLED
+
+! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \
+! RUN: -freal-sum-reassociation \
+! RUN: | FileCheck %s --check-prefix=ENABLED
+
+! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \
+! RUN: -fno-real-sum-reassociation \
+! RUN: | FileCheck %s --check-prefix=DISABLED
+
+! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \
+! RUN: -fno-real-sum-reassociation -freal-sum-reassociation \
+! RUN: | FileCheck %s --check-prefix=ENABLED
+
+! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \
+! RUN: -freal-sum-reassociation -fno-real-sum-reassociation \
+! RUN: | FileCheck %s --check-prefix=DISABLED
+
+! DISABLED: "-fc1"
+! DISABLED-NOT: "-freal-sum-reassociation"
+
+! ENABLED: "-fc1"
+! ENABLED-SAME: "-freal-sum-reassociation"
diff --git a/flang/test/Lower/split-sum-expression-tree-lowering.f90 b/flang/test/Lower/split-sum-expression-tree-lowering.f90
index e6287533ed75e..dc89793f7832d 100644
--- a/flang/test/Lower/split-sum-expression-tree-lowering.f90
+++ b/flang/test/Lower/split-sum-expression-tree-lowering.f90
@@ -1,4 +1,5 @@
-! RUN: %flang_fc1 -emit-hlfir -mllvm -enable-split-sum-expression-tree-lowering -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE
+! RUN: %flang_fc1 -emit-hlfir -freal-sum-reassociation -o - %s | FileCheck %s --check-prefixes=SPLIT,NO-REWRITE
+! RUN: %flang_fc1 -emit-hlfir -fno-real-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
! Default: (((x + a*b) + c*d) + e*f)
More information about the flang-commits
mailing list