Skip to content

Commit 0b9cbef

Browse files
committed
[flang] Add -f[no-]vectorize flags
1 parent 20e9049 commit 0b9cbef

File tree

9 files changed

+74
-47
lines changed

9 files changed

+74
-47
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,9 @@ void applyOverrideOptions(SmallVectorImpl<const char *> &Args,
856856
llvm::StringSet<> &SavedStrings,
857857
raw_ostream *OS = nullptr);
858858

859+
bool shouldEnableVectorizerAtOLevel(const llvm::opt::ArgList &Args,
860+
bool isSlpVec);
861+
859862
} // end namespace driver
860863
} // end namespace clang
861864

clang/include/clang/Driver/Options.td

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,11 +4038,15 @@ defm assumptions : BoolFOption<"assumptions",
40384038
"Disable codegen and compile-time checks for C++23's [[assume]] attribute">,
40394039
PosFlag<SetTrue>>;
40404040

4041+
4042+
let Visibility = [ClangOption, FlangOption] in {
40414043
def fvectorize : Flag<["-"], "fvectorize">, Group<f_Group>,
40424044
HelpText<"Enable the loop vectorization passes">;
40434045
def fno_vectorize : Flag<["-"], "fno-vectorize">, Group<f_Group>;
40444046
def : Flag<["-"], "ftree-vectorize">, Alias<fvectorize>;
40454047
def : Flag<["-"], "fno-tree-vectorize">, Alias<fno_vectorize>;
4048+
}
4049+
40464050
def fslp_vectorize : Flag<["-"], "fslp-vectorize">, Group<f_Group>,
40474051
HelpText<"Enable the superword-level parallelism vectorization passes">;
40484052
def fno_slp_vectorize : Flag<["-"], "fno-slp-vectorize">, Group<f_Group>;
@@ -7323,6 +7327,10 @@ let Visibility = [CC1Option, FC1Option] in {
73237327
def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
73247328
HelpText<"Link and internalize needed symbols from the given bitcode file "
73257329
"before performing optimizations.">;
7330+
7331+
def vectorize_loops : Flag<["-"], "vectorize-loops">,
7332+
HelpText<"Run the Loop vectorization passes">,
7333+
MarshallingInfoFlag<CodeGenOpts<"VectorizeLoop">>;
73267334
} // let Visibility = [CC1Option, FC1Option]
73277335

73287336
let Visibility = [CC1Option] in {
@@ -7439,9 +7447,6 @@ defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
74397447
PosFlag<SetTrue, [], [ClangOption], "Link builtin bitcodes after the "
74407448
"optimization pipeline">,
74417449
NegFlag<SetFalse, [], [ClangOption]>>;
7442-
def vectorize_loops : Flag<["-"], "vectorize-loops">,
7443-
HelpText<"Run the Loop vectorization passes">,
7444-
MarshallingInfoFlag<CodeGenOpts<"VectorizeLoop">>;
74457450
def vectorize_slp : Flag<["-"], "vectorize-slp">,
74467451
HelpText<"Run the SLP vectorization passes">,
74477452
MarshallingInfoFlag<CodeGenOpts<"VectorizeSLP">>;

clang/lib/Driver/Driver.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6980,3 +6980,37 @@ void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
69806980
++S;
69816981
}
69826982
}
6983+
6984+
/// Vectorize at all optimization levels greater than 1 except for -Oz.
6985+
/// For -Oz the loop vectorizer is disabled, while the slp vectorizer is
6986+
/// enabled.
6987+
bool driver::shouldEnableVectorizerAtOLevel(const ArgList &Args,
6988+
bool isSlpVec) {
6989+
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
6990+
if (A->getOption().matches(options::OPT_O4) ||
6991+
A->getOption().matches(options::OPT_Ofast))
6992+
return true;
6993+
6994+
if (A->getOption().matches(options::OPT_O0))
6995+
return false;
6996+
6997+
assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
6998+
6999+
// Vectorize -Os.
7000+
StringRef S(A->getValue());
7001+
if (S == "s")
7002+
return true;
7003+
7004+
// Don't vectorize -Oz, unless it's the slp vectorizer.
7005+
if (S == "z")
7006+
return isSlpVec;
7007+
7008+
unsigned OptLevel = 0;
7009+
if (S.getAsInteger(10, OptLevel))
7010+
return false;
7011+
7012+
return OptLevel > 1;
7013+
}
7014+
7015+
return false;
7016+
}

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -503,39 +503,6 @@ static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args,
503503
}
504504
}
505505

506-
/// Vectorize at all optimization levels greater than 1 except for -Oz.
507-
/// For -Oz the loop vectorizer is disabled, while the slp vectorizer is
508-
/// enabled.
509-
static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
510-
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
511-
if (A->getOption().matches(options::OPT_O4) ||
512-
A->getOption().matches(options::OPT_Ofast))
513-
return true;
514-
515-
if (A->getOption().matches(options::OPT_O0))
516-
return false;
517-
518-
assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
519-
520-
// Vectorize -Os.
521-
StringRef S(A->getValue());
522-
if (S == "s")
523-
return true;
524-
525-
// Don't vectorize -Oz, unless it's the slp vectorizer.
526-
if (S == "z")
527-
return isSlpVec;
528-
529-
unsigned OptLevel = 0;
530-
if (S.getAsInteger(10, OptLevel))
531-
return false;
532-
533-
return OptLevel > 1;
534-
}
535-
536-
return false;
537-
}
538-
539506
/// Add -x lang to \p CmdArgs for \p Input.
540507
static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
541508
ArgStringList &CmdArgs) {

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ void Flang::addCodegenOptions(const ArgList &Args,
143143
!stackArrays->getOption().matches(options::OPT_fno_stack_arrays))
144144
CmdArgs.push_back("-fstack-arrays");
145145

146+
// Enable vectorization per default according to the optimization level
147+
// selected. For optimization levels that want vectorization we use the alias
148+
// option to simplify the hasFlag logic.
149+
bool enableVec = shouldEnableVectorizerAtOLevel(Args, false);
150+
OptSpecifier vectorizeAliasOption =
151+
enableVec ? options::OPT_O_Group : options::OPT_fvectorize;
152+
if (Args.hasFlag(options::OPT_fvectorize, vectorizeAliasOption,
153+
options::OPT_fno_vectorize, enableVec))
154+
CmdArgs.push_back("-vectorize-loops");
155+
146156
if (shouldLoopVersion(Args))
147157
CmdArgs.push_back("-fversion-loops-for-stride");
148158

flang/include/flang/Frontend/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CODEGENOPT(PrepareForFullLTO , 1, 0) ///< Set when -flto is enabled on the
3131
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)
34+
CODEGENOPT(VectorizeLoop, 1, 0) ///< Enable loop vectorization.
3435
CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning.
3536
CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass
3637

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/Basic/AllDiagnostics.h"
2424
#include "clang/Basic/DiagnosticDriver.h"
2525
#include "clang/Basic/DiagnosticOptions.h"
26+
#include "clang/Driver/Driver.h"
2627
#include "clang/Driver/DriverDiagnostic.h"
2728
#include "clang/Driver/OptionUtils.h"
2829
#include "clang/Driver/Options.h"
@@ -242,6 +243,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
242243
clang::driver::options::OPT_fno_stack_arrays, false))
243244
opts.StackArrays = 1;
244245

246+
if (args.getLastArg(clang::driver::options::OPT_vectorize_loops))
247+
opts.VectorizeLoop = 1;
248+
245249
if (args.hasFlag(clang::driver::options::OPT_floop_versioning,
246250
clang::driver::options::OPT_fno_loop_versioning, false))
247251
opts.LoopVersioning = 1;

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,9 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
982982
llvm::StandardInstrumentations si(llvmModule->getContext(),
983983
opts.DebugPassManager);
984984
si.registerCallbacks(pic, &mam);
985+
986+
pto.LoopVectorization = opts.VectorizeLoop;
987+
985988
llvm::PassBuilder pb(targetMachine, pto, pgoOpt, &pic);
986989

987990
// Attempt to load pass plugins and register their callbacks with PB.

flang/test/Driver/optimization-remark.f90

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55
! DEFINE: %{output} = -emit-llvm -flang-deprecated-no-hlfir -o /dev/null 2>&1
66

77
! Check fc1 can handle -Rpass
8-
! RUN: %flang_fc1 %s -O1 -Rpass %{output} 2>&1 | FileCheck %s --check-prefix=REMARKS
8+
! RUN: %flang_fc1 %s -O1 -vectorize-loops -Rpass %{output} 2>&1 | FileCheck %s --check-prefix=REMARKS
99

1010
! Check that we can override -Rpass= with -Rno-pass.
11-
! RUN: %flang_fc1 %s -O1 -Rpass -Rno-pass %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
11+
! RUN: %flang_fc1 %s -O1 -vectorize-loops -Rpass -Rno-pass %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
1212

1313
! Check -Rno-pass, -Rno-pass-analysis, -Rno-pass-missed nothing emitted
14-
! RUN: %flang %s -O1 -Rno-pass -S %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
15-
! RUN: %flang %s -O1 -Rno-pass-missed -S %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
16-
! RUN: %flang %s -O1 -Rno-pass-analysis -S %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
14+
! RUN: %flang %s -O2 -Rno-pass -S %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
15+
! RUN: %flang %s -O2 -Rno-pass-missed -S %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
16+
! RUN: %flang %s -O2 -Rno-pass-analysis -S %{output} 2>&1 | FileCheck %s --allow-empty --check-prefix=NO-REMARKS
1717

1818
! Check valid -Rpass regex
19-
! RUN: %flang %s -O1 -Rpass=loop -S %{output} 2>&1 | FileCheck %s --check-prefix=PASS-REGEX-LOOP-ONLY
19+
! RUN: %flang %s -O2 -Rpass=loop -S %{output} 2>&1 | FileCheck %s --check-prefix=PASS-REGEX-LOOP-ONLY
2020

2121
! Check valid -Rpass-missed regex
22-
! RUN: %flang %s -O1 -Rpass-missed=loop -S %{output} 2>&1 | FileCheck %s --check-prefix=MISSED-REGEX-LOOP-ONLY
22+
! RUN: %flang %s -O2 -Rpass-missed=loop -S %{output} 2>&1 | FileCheck %s --check-prefix=MISSED-REGEX-LOOP-ONLY
2323

2424
! Check valid -Rpass-analysis regex
25-
! RUN: %flang %s -O1 -Rpass-analysis=loop -S %{output} 2>&1 | FileCheck %s --check-prefix=ANALYSIS-REGEX-LOOP-ONLY
25+
! RUN: %flang %s -O2 -Rpass-analysis=loop -S %{output} 2>&1 | FileCheck %s --check-prefix=ANALYSIS-REGEX-LOOP-ONLY
2626

2727
! Check full -Rpass message is emitted
28-
! RUN: %flang %s -O1 -Rpass -S %{output} 2>&1 | FileCheck %s --check-prefix=PASS
28+
! RUN: %flang %s -O2 -Rpass -S %{output} 2>&1 | FileCheck %s --check-prefix=PASS
2929

3030
! Check full -Rpass-missed message is emitted
31-
! RUN: %flang %s -O1 -Rpass-missed -S %{output} 2>&1 | FileCheck %s --check-prefix=MISSED
31+
! RUN: %flang %s -O2 -Rpass-missed -S %{output} 2>&1 | FileCheck %s --check-prefix=MISSED
3232

3333
! Check full -Rpass-analysis message is emitted
34-
! RUN: %flang %s -O1 -Rpass-analysis -S -o /dev/null 2>&1 | FileCheck %s --check-prefix=ANALYSIS
34+
! RUN: %flang %s -O2 -Rpass-analysis -S -o /dev/null 2>&1 | FileCheck %s --check-prefix=ANALYSIS
3535

3636
! REMARKS: remark:
3737
! NO-REMARKS-NOT: remark:

0 commit comments

Comments
 (0)