Skip to content

Commit 529c5b7

Browse files
authored
[flang] Add -f[no-]slp-vectorize flags (#132801)
Add -f[no-]slp-vectorize to the flang driver. Add corresponding -fvectorize-slp to the flang frontend. Enable -fslp-vectorize at -O2 and higher in flang to match the current behaviour in clang. --------- Signed-off-by: Kajetan Puchalski <[email protected]>
1 parent 649cbcc commit 529c5b7

File tree

9 files changed

+59
-31
lines changed

9 files changed

+59
-31
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4030,13 +4030,14 @@ def fvectorize : Flag<["-"], "fvectorize">, Group<f_Group>,
40304030
def fno_vectorize : Flag<["-"], "fno-vectorize">, Group<f_Group>;
40314031
def : Flag<["-"], "ftree-vectorize">, Alias<fvectorize>;
40324032
def : Flag<["-"], "fno-tree-vectorize">, Alias<fno_vectorize>;
4033-
}
40344033

40354034
def fslp_vectorize : Flag<["-"], "fslp-vectorize">, Group<f_Group>,
40364035
HelpText<"Enable the superword-level parallelism vectorization passes">;
40374036
def fno_slp_vectorize : Flag<["-"], "fno-slp-vectorize">, Group<f_Group>;
40384037
def : Flag<["-"], "ftree-slp-vectorize">, Alias<fslp_vectorize>;
40394038
def : Flag<["-"], "fno-tree-slp-vectorize">, Alias<fno_slp_vectorize>;
4039+
}
4040+
40404041
def Wlarge_by_value_copy_def : Flag<["-"], "Wlarge-by-value-copy">,
40414042
HelpText<"Warn if a function definition returns or accepts an object larger "
40424043
"in bytes than a given value">, Flags<[HelpHidden]>;
@@ -7384,6 +7385,9 @@ def mlink_bitcode_file
73847385
def vectorize_loops : Flag<["-"], "vectorize-loops">,
73857386
HelpText<"Run the Loop vectorization passes">,
73867387
MarshallingInfoFlag<CodeGenOpts<"VectorizeLoop">>;
7388+
def vectorize_slp : Flag<["-"], "vectorize-slp">,
7389+
HelpText<"Run the SLP vectorization passes">,
7390+
MarshallingInfoFlag<CodeGenOpts<"VectorizeSLP">>;
73877391
} // let Visibility = [CC1Option, FC1Option]
73887392

73897393
let Visibility = [CC1Option] in {
@@ -7499,9 +7503,6 @@ defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
74997503
PosFlag<SetTrue, [], [ClangOption], "Link builtin bitcodes after the "
75007504
"optimization pipeline">,
75017505
NegFlag<SetFalse, [], [ClangOption]>>;
7502-
def vectorize_slp : Flag<["-"], "vectorize-slp">,
7503-
HelpText<"Run the SLP vectorization passes">,
7504-
MarshallingInfoFlag<CodeGenOpts<"VectorizeSLP">>;
75057506
def linker_option : Joined<["--"], "linker-option=">,
75067507
HelpText<"Add linker option">,
75077508
MarshallingInfoStringVector<CodeGenOpts<"LinkerOptions">>;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7597,24 +7597,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
75977597
Args.addOptOutFlag(CmdArgs, options::OPT_fgnu_inline_asm,
75987598
options::OPT_fno_gnu_inline_asm);
75997599

7600-
// Enable vectorization per default according to the optimization level
7601-
// selected. For optimization levels that want vectorization we use the alias
7602-
// option to simplify the hasFlag logic.
7603-
bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
7604-
OptSpecifier VectorizeAliasOption =
7605-
EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;
7606-
if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
7607-
options::OPT_fno_vectorize, EnableVec))
7608-
CmdArgs.push_back("-vectorize-loops");
7609-
7610-
// -fslp-vectorize is enabled based on the optimization level selected.
7611-
bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
7612-
OptSpecifier SLPVectAliasOption =
7613-
EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;
7614-
if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
7615-
options::OPT_fno_slp_vectorize, EnableSLPVec))
7616-
CmdArgs.push_back("-vectorize-slp");
7617-
7600+
handleVectorizeLoopsArgs(Args, CmdArgs);
7601+
handleVectorizeSLPArgs(Args, CmdArgs);
76187602
ParseMPreferVectorWidth(D, Args, CmdArgs);
76197603

76207604
Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ);

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,3 +3171,23 @@ bool tools::shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
31713171

31723172
return false;
31733173
}
3174+
3175+
void tools::handleVectorizeLoopsArgs(const ArgList &Args,
3176+
ArgStringList &CmdArgs) {
3177+
bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
3178+
OptSpecifier vectorizeAliasOption =
3179+
EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;
3180+
if (Args.hasFlag(options::OPT_fvectorize, vectorizeAliasOption,
3181+
options::OPT_fno_vectorize, EnableVec))
3182+
CmdArgs.push_back("-vectorize-loops");
3183+
}
3184+
3185+
void tools::handleVectorizeSLPArgs(const ArgList &Args,
3186+
ArgStringList &CmdArgs) {
3187+
bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
3188+
OptSpecifier SLPVectAliasOption =
3189+
EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;
3190+
if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
3191+
options::OPT_fno_slp_vectorize, EnableSLPVec))
3192+
CmdArgs.push_back("-vectorize-slp");
3193+
}

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ void renderCommonIntegerOverflowOptions(const llvm::opt::ArgList &Args,
267267

268268
bool shouldEnableVectorizerAtOLevel(const llvm::opt::ArgList &Args,
269269
bool isSlpVec);
270+
271+
/// Enable -fvectorize based on the optimization level selected.
272+
void handleVectorizeLoopsArgs(const llvm::opt::ArgList &Args,
273+
llvm::opt::ArgStringList &CmdArgs);
274+
275+
/// Enable -fslp-vectorize based on the optimization level selected.
276+
void handleVectorizeSLPArgs(const llvm::opt::ArgList &Args,
277+
llvm::opt::ArgStringList &CmdArgs);
270278
} // end namespace tools
271279
} // end namespace driver
272280
} // end namespace clang

clang/lib/Driver/ToolChains/Flang.cpp

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

154-
// Enable vectorization per default according to the optimization level
155-
// selected. For optimization levels that want vectorization we use the alias
156-
// option to simplify the hasFlag logic.
157-
bool enableVec = shouldEnableVectorizerAtOLevel(Args, false);
158-
OptSpecifier vectorizeAliasOption =
159-
enableVec ? options::OPT_O_Group : options::OPT_fvectorize;
160-
if (Args.hasFlag(options::OPT_fvectorize, vectorizeAliasOption,
161-
options::OPT_fno_vectorize, enableVec))
162-
CmdArgs.push_back("-vectorize-loops");
154+
handleVectorizeLoopsArgs(Args, CmdArgs);
155+
handleVectorizeSLPArgs(Args, CmdArgs);
163156

164157
if (shouldLoopVersion(Args))
165158
CmdArgs.push_back("-fversion-loops-for-stride");

flang/include/flang/Frontend/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ CODEGENOPT(PrepareForThinLTO , 1, 0) ///< Set when -flto=thin is enabled on the
3232
///< compile step.
3333
CODEGENOPT(StackArrays, 1, 0) ///< -fstack-arrays (enable the stack-arrays pass)
3434
CODEGENOPT(VectorizeLoop, 1, 0) ///< Enable loop vectorization.
35+
CODEGENOPT(VectorizeSLP, 1, 0) ///< Enable SLP vectorization.
3536
CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning.
3637
CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling
3738
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
@@ -246,6 +246,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
246246
if (args.getLastArg(clang::driver::options::OPT_vectorize_loops))
247247
opts.VectorizeLoop = 1;
248248

249+
if (args.getLastArg(clang::driver::options::OPT_vectorize_slp))
250+
opts.VectorizeSLP = 1;
251+
249252
if (args.hasFlag(clang::driver::options::OPT_floop_versioning,
250253
clang::driver::options::OPT_fno_loop_versioning, false))
251254
opts.LoopVersioning = 1;

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
933933
pto.LoopUnrolling = opts.UnrollLoops;
934934
pto.LoopInterleaving = opts.UnrollLoops;
935935
pto.LoopVectorization = opts.VectorizeLoop;
936+
pto.SLPVectorization = opts.VectorizeSLP;
936937

937938
llvm::PassBuilder pb(targetMachine, pto, pgoOpt, &pic);
938939

flang/test/Driver/slp-vectorize.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: %flang -### -S -fslp-vectorize %s 2>&1 | FileCheck -check-prefix=CHECK-SLP-VECTORIZE %s
2+
! RUN: %flang -### -S -fno-slp-vectorize %s 2>&1 | FileCheck -check-prefix=CHECK-NO-SLP-VECTORIZE %s
3+
! RUN: %flang -### -S -O0 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-SLP-VECTORIZE %s
4+
! RUN: %flang -### -S -O1 %s 2>&1 | FileCheck -check-prefix=CHECK-NO-SLP-VECTORIZE %s
5+
! RUN: %flang -### -S -O2 %s 2>&1 | FileCheck -check-prefix=CHECK-SLP-VECTORIZE %s
6+
! RUN: %flang -### -S -O3 %s 2>&1 | FileCheck -check-prefix=CHECK-SLP-VECTORIZE %s
7+
! RUN: %flang -### -S -Os %s 2>&1 | FileCheck -check-prefix=CHECK-SLP-VECTORIZE %s
8+
! RUN: %flang -### -S -Oz %s 2>&1 | FileCheck -check-prefix=CHECK-SLP-VECTORIZE %s
9+
! RUN: %flang_fc1 -emit-llvm -O2 -vectorize-slp -mllvm -print-pipeline-passes %s 2>&1 | FileCheck -check-prefix=CHECK-SLP-VECTORIZER %s
10+
! RUN: %flang_fc1 -emit-llvm -O2 -mllvm -print-pipeline-passes %s 2>&1 | FileCheck -check-prefix=CHECK-NO-SLP-VECTORIZER %s
11+
! CHECK-SLP-VECTORIZE: "-vectorize-slp"
12+
! CHECK-NO-SLP-VECTORIZE-NOT: "-no-vectorize-slp"
13+
! CHECK-SLP-VECTORIZER: slp-vectorizer
14+
! CHECK-NO-SLP-VECTORIZER-NOT: slp-vectorizer
15+
16+
program test
17+
end program

0 commit comments

Comments
 (0)