Skip to content

Commit 895cda7

Browse files
sebpopmadhur13490
andauthored
Introduce -fexperimental-loop-fuse to clang and flang (#142686)
This patch adds the flag -fexperimental-loop-fuse to the clang and flang drivers. This is primarily useful for experiments as we envision to enable the pass one day. The options are based on the same principles and reason on which we have `floop-interchange`. --------- Co-authored-by: Madhur Amilkanthwar <[email protected]>
1 parent e0a33cb commit 895cda7

File tree

15 files changed

+64
-1
lines changed

15 files changed

+64
-1
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ CODEGENOPT(TimeTrace , 1, 0, Benign) ///< Set when -ftime-trace is enabl
322322
VALUE_CODEGENOPT(TimeTraceGranularity, 32, 500, Benign) ///< Minimum time granularity (in microseconds),
323323
///< traced by time profiler
324324
CODEGENOPT(InterchangeLoops , 1, 0, Benign) ///< Run loop-interchange.
325+
CODEGENOPT(FuseLoops , 1, 0, Benign) ///< Run loop-fusion.
325326
CODEGENOPT(UnrollLoops , 1, 0, Benign) ///< Control whether loops are unrolled.
326327
CODEGENOPT(RerollLoops , 1, 0, Benign) ///< Control whether loops are rerolled.
327328
CODEGENOPT(NoUseJumpTables , 1, 0, Benign) ///< Set when -fno-jump-tables is enabled.

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4304,6 +4304,10 @@ def floop_interchange : Flag<["-"], "floop-interchange">, Group<f_Group>,
43044304
HelpText<"Enable the loop interchange pass">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
43054305
def fno_loop_interchange: Flag<["-"], "fno-loop-interchange">, Group<f_Group>,
43064306
HelpText<"Disable the loop interchange pass">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
4307+
defm experimental_loop_fusion
4308+
: OptInCC1FFlag<"experimental-loop-fusion", "Enable", "Disable",
4309+
"Enable the loop fusion pass",
4310+
[ClangOption, FlangOption, FC1Option]>;
43074311
def funroll_loops : Flag<["-"], "funroll-loops">, Group<f_Group>,
43084312
HelpText<"Turn on loop unroller">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
43094313
def fno_unroll_loops : Flag<["-"], "fno-unroll-loops">, Group<f_Group>,

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
896896
PipelineTuningOptions PTO;
897897
PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
898898
PTO.LoopInterchange = CodeGenOpts.InterchangeLoops;
899+
PTO.LoopFusion = CodeGenOpts.FuseLoops;
899900
// For historical reasons, loop interleaving is set to mirror setting for loop
900901
// unrolling.
901902
PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
@@ -1331,6 +1332,7 @@ runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
13311332
Conf.SampleProfile = std::move(SampleProfile);
13321333
Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
13331334
Conf.PTO.LoopInterchange = CGOpts.InterchangeLoops;
1335+
Conf.PTO.LoopFusion = CGOpts.FuseLoops;
13341336
// For historical reasons, loop interleaving is set to mirror setting for loop
13351337
// unrolling.
13361338
Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6854,6 +6854,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
68546854
options::OPT_fno_unroll_loops);
68556855
Args.AddLastArg(CmdArgs, options::OPT_floop_interchange,
68566856
options::OPT_fno_loop_interchange);
6857+
Args.addOptInFlag(CmdArgs, options::OPT_fexperimental_loop_fusion,
6858+
options::OPT_fno_experimental_loop_fusion);
68576859

68586860
Args.AddLastArg(CmdArgs, options::OPT_fstrict_flex_arrays_EQ);
68596861

clang/lib/Driver/ToolChains/Flang.cpp

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

154+
Args.addOptInFlag(CmdArgs, options::OPT_fexperimental_loop_fusion,
155+
options::OPT_fno_experimental_loop_fusion);
156+
154157
handleInterchangeLoopsArgs(Args, CmdArgs);
155158
handleVectorizeLoopsArgs(Args, CmdArgs);
156159
handleVectorizeSLPArgs(Args, CmdArgs);

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,9 @@ void CompilerInvocationBase::GenerateCodeGenArgs(const CodeGenOptions &Opts,
16801680
else
16811681
GenerateArg(Consumer, OPT_fno_loop_interchange);
16821682

1683+
if (Opts.FuseLoops)
1684+
GenerateArg(Consumer, OPT_fexperimental_loop_fusion);
1685+
16831686
if (!Opts.BinutilsVersion.empty())
16841687
GenerateArg(Consumer, OPT_fbinutils_version_EQ, Opts.BinutilsVersion);
16851688

@@ -2001,6 +2004,8 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
20012004
(Opts.OptimizationLevel > 1));
20022005
Opts.InterchangeLoops =
20032006
Args.hasFlag(OPT_floop_interchange, OPT_fno_loop_interchange, false);
2007+
Opts.FuseLoops = Args.hasFlag(OPT_fexperimental_loop_fusion,
2008+
OPT_fno_experimental_loop_fusion, false);
20042009
Opts.BinutilsVersion =
20052010
std::string(Args.getLastArgValue(OPT_fbinutils_version_EQ));
20062011

clang/test/Driver/clang_f_opts.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
// CHECK-INTERCHANGE-LOOPS: "-floop-interchange"
5353
// CHECK-NO-INTERCHANGE-LOOPS: "-fno-loop-interchange"
5454

55+
// RUN: %clang -### -S -fexperimental-loop-fusion %s 2>&1 | FileCheck -check-prefix=CHECK-FUSE-LOOPS %s
56+
// CHECK-FUSE-LOOPS: "-fexperimental-loop-fusion"
57+
//
58+
// RUN: %clang -c -fexperimental-loop-fusion -mllvm -print-pipeline-passes -O3 %s 2>&1 | FileCheck --check-prefixes=LOOP-FUSION-ON %s
59+
// RUN: %clang -c -mllvm -print-pipeline-passes -O3 %s 2>&1 | FileCheck --check-prefixes=LOOP-FUSION-OFF %s
60+
61+
// LOOP-FUSION-ON: loop-fusion
62+
// LOOP-FUSION-OFF-NOT: loop-fusion
63+
5564
// RUN: %clang -### -S -fprofile-sample-accurate %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-SAMPLE-ACCURATE %s
5665
// CHECK-PROFILE-SAMPLE-ACCURATE: "-fprofile-sample-accurate"
5766

flang/docs/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ page](https://llvm.org/releases/).
3535

3636
## New Compiler Flags
3737

38+
* -fexperimental-loop-fusion is now recognized by flang.
39+
3840
## Windows Support
3941

4042
## 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
@@ -43,6 +43,7 @@ CODEGENOPT(StackArrays, 1, 0) ///< -fstack-arrays (enable the stack-arrays pass)
4343
CODEGENOPT(VectorizeLoop, 1, 0) ///< Enable loop vectorization.
4444
CODEGENOPT(VectorizeSLP, 1, 0) ///< Enable SLP vectorization.
4545
CODEGENOPT(InterchangeLoops, 1, 0) ///< Enable loop interchange.
46+
CODEGENOPT(FuseLoops, 1, 0) ///< Enable loop fusion.
4647
CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning.
4748
CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling
4849
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
@@ -276,6 +276,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
276276
if (args.getLastArg(clang::driver::options::OPT_floop_interchange))
277277
opts.InterchangeLoops = 1;
278278

279+
if (args.getLastArg(clang::driver::options::OPT_fexperimental_loop_fusion))
280+
opts.FuseLoops = 1;
281+
279282
if (args.getLastArg(clang::driver::options::OPT_vectorize_loops))
280283
opts.VectorizeLoop = 1;
281284

0 commit comments

Comments
 (0)