Skip to content

Commit d360281

Browse files
authored
[flang] add -floop-interchange and enable it with opt levels (#140182)
Enable the use of -floop-interchange from the flang driver. Enable in flang LLVM's loop interchange at levels -O2, -O3, -Ofast, and -Os.
1 parent 2cf6099 commit d360281

File tree

9 files changed

+45
-2
lines changed

9 files changed

+45
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,9 +4186,9 @@ def ftrap_function_EQ : Joined<["-"], "ftrap-function=">, Group<f_Group>,
41864186
HelpText<"Issue call to specified function rather than a trap instruction">,
41874187
MarshallingInfoString<CodeGenOpts<"TrapFuncName">>;
41884188
def floop_interchange : Flag<["-"], "floop-interchange">, Group<f_Group>,
4189-
HelpText<"Enable the loop interchange pass">, Visibility<[ClangOption, CC1Option]>;
4189+
HelpText<"Enable the loop interchange pass">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
41904190
def fno_loop_interchange: Flag<["-"], "fno-loop-interchange">, Group<f_Group>,
4191-
HelpText<"Disable the loop interchange pass">, Visibility<[ClangOption, CC1Option]>;
4191+
HelpText<"Disable the loop interchange pass">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
41924192
def funroll_loops : Flag<["-"], "funroll-loops">, Group<f_Group>,
41934193
HelpText<"Turn on loop unroller">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
41944194
def fno_unroll_loops : Flag<["-"], "fno-unroll-loops">, Group<f_Group>,

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,3 +3150,16 @@ void tools::handleVectorizeSLPArgs(const ArgList &Args,
31503150
options::OPT_fno_slp_vectorize, EnableSLPVec))
31513151
CmdArgs.push_back("-vectorize-slp");
31523152
}
3153+
3154+
void tools::handleInterchangeLoopsArgs(const ArgList &Args,
3155+
ArgStringList &CmdArgs) {
3156+
// FIXME: instead of relying on shouldEnableVectorizerAtOLevel, we may want to
3157+
// implement a separate function to infer loop interchange from opt level.
3158+
// For now, enable loop-interchange at the same opt levels as loop-vectorize.
3159+
bool EnableInterchange = shouldEnableVectorizerAtOLevel(Args, false);
3160+
OptSpecifier InterchangeAliasOption =
3161+
EnableInterchange ? options::OPT_O_Group : options::OPT_floop_interchange;
3162+
if (Args.hasFlag(options::OPT_floop_interchange, InterchangeAliasOption,
3163+
options::OPT_fno_loop_interchange, EnableInterchange))
3164+
CmdArgs.push_back("-floop-interchange");
3165+
}

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ void renderCommonIntegerOverflowOptions(const llvm::opt::ArgList &Args,
259259
bool shouldEnableVectorizerAtOLevel(const llvm::opt::ArgList &Args,
260260
bool isSlpVec);
261261

262+
/// Enable -floop-interchange based on the optimization level selected.
263+
void handleInterchangeLoopsArgs(const llvm::opt::ArgList &Args,
264+
llvm::opt::ArgStringList &CmdArgs);
265+
262266
/// Enable -fvectorize based on the optimization level selected.
263267
void handleVectorizeLoopsArgs(const llvm::opt::ArgList &Args,
264268
llvm::opt::ArgStringList &CmdArgs);

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ void Flang::addCodegenOptions(const ArgList &Args,
152152
!stackArrays->getOption().matches(options::OPT_fno_stack_arrays))
153153
CmdArgs.push_back("-fstack-arrays");
154154

155+
handleInterchangeLoopsArgs(Args, CmdArgs);
155156
handleVectorizeLoopsArgs(Args, CmdArgs);
156157
handleVectorizeSLPArgs(Args, CmdArgs);
157158

flang/docs/ReleaseNotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ page](https://llvm.org/releases/).
3232

3333
## New Compiler Flags
3434

35+
* -floop-interchange is now recognized by flang.
36+
* -floop-interchange is enabled by default at -O2 and above.
37+
3538
## Windows Support
3639

3740
## Fortran Language Changes in Flang

flang/include/flang/Frontend/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CODEGENOPT(PrepareForThinLTO , 1, 0) ///< Set when -flto=thin is enabled on the
3535
CODEGENOPT(StackArrays, 1, 0) ///< -fstack-arrays (enable the stack-arrays pass)
3636
CODEGENOPT(VectorizeLoop, 1, 0) ///< Enable loop vectorization.
3737
CODEGENOPT(VectorizeSLP, 1, 0) ///< Enable SLP vectorization.
38+
CODEGENOPT(InterchangeLoops, 1, 0) ///< Enable loop interchange.
3839
CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning.
3940
CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling
4041
CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
270270
clang::driver::options::OPT_fno_stack_arrays, false))
271271
opts.StackArrays = 1;
272272

273+
if (args.getLastArg(clang::driver::options::OPT_floop_interchange))
274+
opts.InterchangeLoops = 1;
275+
273276
if (args.getLastArg(clang::driver::options::OPT_vectorize_loops))
274277
opts.VectorizeLoop = 1;
275278

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
922922
if (ci.isTimingEnabled())
923923
si.getTimePasses().setOutStream(ci.getTimingStreamLLVM());
924924
pto.LoopUnrolling = opts.UnrollLoops;
925+
pto.LoopInterchange = opts.InterchangeLoops;
925926
pto.LoopInterleaving = opts.UnrollLoops;
926927
pto.LoopVectorization = opts.VectorizeLoop;
927928
pto.SLPVectorization = opts.VectorizeSLP;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: %flang -### -S -floop-interchange %s 2>&1 | FileCheck -check-prefix=CHECK-LOOP-INTERCHANGE %s
2+
! RUN: %flang -### -S -fno-loop-interchange %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
3+
! RUN: %flang -### -S -O0 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
4+
! RUN: %flang -### -S -O1 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
5+
! RUN: %flang -### -S -O2 %s 2>&1 | FileCheck -check-prefix=CHECK-LOOP-INTERCHANGE %s
6+
! RUN: %flang -### -S -O3 %s 2>&1 | FileCheck -check-prefix=CHECK-LOOP-INTERCHANGE %s
7+
! RUN: %flang -### -S -Os %s 2>&1 | FileCheck -check-prefix=CHECK-LOOP-INTERCHANGE %s
8+
! RUN: %flang -### -S -Oz %s 2>&1 | FileCheck -check-prefix=CHECK-NO-LOOP-INTERCHANGE %s
9+
! CHECK-LOOP-INTERCHANGE: "-floop-interchange"
10+
! CHECK-NO-LOOP-INTERCHANGE-NOT: "-floop-interchange"
11+
! 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
12+
! 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
13+
! CHECK-LOOP-INTERCHANGE-PASS: loop-interchange
14+
! CHECK-NO-LOOP-INTERCHANGE-PASS-NOT: loop-interchange
15+
16+
program test
17+
end program

0 commit comments

Comments
 (0)