[flang-commits] [clang] [flang] [Clang][Flang] Enable loop interchange by default at -O3 (PR #216920)
via flang-commits
flang-commits at lists.llvm.org
Mon Aug 17 22:46:28 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-driver
Author: Madhur Amilkanthwar (madhur13490)
<details>
<summary>Changes</summary>
PR #<!-- -->124911 enabled the LoopInterchange pass by default in the LLVM optimization pipeline (the PipelineTuningOptions default used by opt), but the clang and flang frontends override that default and keep it off. As a result, driving the compiler through clang or flang never runs loop interchange unless -floop-interchange is passed explicitly.
Default the -floop-interchange flag on at -O3 for both frontends so they match the pipeline default. The decision is made in the frontend (-cc1) for both clang and flang, mirroring -funroll-loops; the flang driver only forwards an explicit -f[no-]loop-interchange. Lower opt levels and an explicit -fno-loop-interchange keep it off.
Compile-time impact is largely due to enablement of the new pass:
https://llvm-compile-time-tracker.com/?config=Overview&stat=instructions%3Au&remote=madhur13490
---
Full diff: https://github.com/llvm/llvm-project/pull/216920.diff
5 Files Affected:
- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+4-3)
- (modified) clang/lib/Frontend/CompilerInvocation.cpp (+3-1)
- (modified) clang/test/Driver/clang_f_opts.c (+7)
- (modified) flang/lib/Frontend/CompilerInvocation.cpp (+4-2)
- (modified) flang/test/Driver/loop-interchange.f90 (+7-7)
``````````diff
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index a76f4aa6ae853..ad6280ae45277 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -3564,9 +3564,10 @@ void tools::handleVectorizeSLPArgs(const ArgList &Args,
void tools::handleInterchangeLoopsArgs(const ArgList &Args,
ArgStringList &CmdArgs) {
- if (Args.hasFlag(options::OPT_floop_interchange,
- options::OPT_fno_loop_interchange, false))
- CmdArgs.push_back("-floop-interchange");
+ // Forward the user's explicit choice; the frontend applies the -O3
+ // default when neither flag is present.
+ Args.AddLastArg(CmdArgs, options::OPT_floop_interchange,
+ options::OPT_fno_loop_interchange);
}
std::string tools::complexRangeKindToStr(LangOptions::ComplexRangeKind Range) {
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 70fc346d85920..f023df260d55e 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1945,8 +1945,10 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
Opts.UnrollLoops =
Args.hasFlag(OPT_funroll_loops, OPT_fno_unroll_loops,
(Opts.OptimizationLevel > 1));
+ // Loop interchange is only enabled by default at -O3.
Opts.InterchangeLoops =
- Args.hasFlag(OPT_floop_interchange, OPT_fno_loop_interchange, false);
+ Args.hasFlag(OPT_floop_interchange, OPT_fno_loop_interchange,
+ (Opts.OptimizationLevel == 3));
Opts.FuseLoops = Args.hasFlag(OPT_fexperimental_loop_fusion,
OPT_fno_experimental_loop_fusion, false);
Opts.BinutilsVersion =
diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c
index 5871f1580d6b7..c14885b04ad6f 100644
--- a/clang/test/Driver/clang_f_opts.c
+++ b/clang/test/Driver/clang_f_opts.c
@@ -51,6 +51,13 @@
// RUN: %clang -### -S -floop-interchange -fno-loop-interchange %s 2>&1 | FileCheck -check-prefix=CHECK-NO-INTERCHANGE-LOOPS %s
// CHECK-INTERCHANGE-LOOPS: "-floop-interchange"
// CHECK-NO-INTERCHANGE-LOOPS: "-fno-loop-interchange"
+//
+// Loop interchange is enabled by default only at -O3.
+// RUN: %clang -c -mllvm -print-pipeline-passes -O3 %s -o /dev/null 2>&1 | FileCheck --check-prefixes=INTERCHANGE-ON %s
+// RUN: %clang -c -mllvm -print-pipeline-passes -O2 %s -o /dev/null 2>&1 | FileCheck --check-prefixes=INTERCHANGE-OFF %s
+// RUN: %clang -c -fno-loop-interchange -mllvm -print-pipeline-passes -O3 %s -o /dev/null 2>&1 | FileCheck --check-prefixes=INTERCHANGE-OFF %s
+// INTERCHANGE-ON: loop-interchange
+// INTERCHANGE-OFF-NOT: loop-interchange
// RUN: %clang -### -S -fexperimental-loop-fusion %s -o /dev/null 2>&1 | FileCheck -check-prefix=CHECK-FUSE-LOOPS %s
// CHECK-FUSE-LOOPS: "-fexperimental-loop-fusion"
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index b57bc4583be38..d50dc4878e60d 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -320,8 +320,10 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
clang::options::OPT_fno_fp_sum_reassociation, false))
opts.SplitSumExpressionTree = 1;
- if (args.getLastArg(clang::options::OPT_floop_interchange))
- opts.InterchangeLoops = 1;
+ // Loop interchange is only enabled by default at -O3.
+ opts.InterchangeLoops = args.hasFlag(clang::options::OPT_floop_interchange,
+ clang::options::OPT_fno_loop_interchange,
+ opts.OptimizationLevel == 3);
if (args.getLastArg(clang::options::OPT_fexperimental_loop_fusion))
opts.FuseLoops = 1;
diff --git a/flang/test/Driver/loop-interchange.f90 b/flang/test/Driver/loop-interchange.f90
index 1e5a11902709c..c9d88cd63cf5b 100644
--- a/flang/test/Driver/loop-interchange.f90
+++ b/flang/test/Driver/loop-interchange.f90
@@ -1,15 +1,15 @@
+! The driver only forwards an explicit -f[no-]loop-interchange; the -O3
+! default is applied in the frontend, so it is not visible in -###.
! RUN: %flang -### -S -floop-interchange %s 2>&1 | FileCheck -check-prefix=CHECK-LOOP-INTERCHANGE %s
! RUN: %flang -### -S -fno-loop-interchange %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
-! RUN: %flang -### -S -O0 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
-! RUN: %flang -### -S -O1 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
-! RUN: %flang -### -S -O2 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
! RUN: %flang -### -S -O3 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
-! RUN: %flang -### -S -Os %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
-! RUN: %flang -### -S -Oz %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
! CHECK-LOOP-INTERCHANGE: "-floop-interchange"
! CHECK-NO-LOOP-INTERCHANGE-NOT: "-floop-interchange"
-! RUN: %flang_fc1 -emit-llvm -O2 -floop-interchange -mllvm -print-pipeline-passes -o /dev/null %s 2>&1 | FileCheck -check-prefix=CHECK-LOOP-INTERCHANGE-PASS %s
-! RUN: %flang_fc1 -emit-llvm -O2 -fno-loop-interchange -mllvm -print-pipeline-passes -o /dev/null %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE-PASS %s
+!
+! Loop interchange is enabled by default only at -O3.
+! RUN: %flang_fc1 -emit-llvm -O3 -mllvm -print-pipeline-passes -o /dev/null %s 2>&1 | FileCheck -check-prefix=CHECK-LOOP-INTERCHANGE-PASS %s
+! RUN: %flang_fc1 -emit-llvm -O2 -mllvm -print-pipeline-passes -o /dev/null %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE-PASS %s
+! RUN: %flang_fc1 -emit-llvm -O3 -fno-loop-interchange -mllvm -print-pipeline-passes -o /dev/null %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE-PASS %s
! CHECK-LOOP-INTERCHANGE-PASS: loop-interchange
! CHECK-NO-LOOP-INTERCHANGE-PASS-NOT: loop-interchange
``````````
</details>
https://github.com/llvm/llvm-project/pull/216920
More information about the flang-commits
mailing list