Skip to content

Commit 11e68c7

Browse files
[flang]Add vscale argument parsing (#67676)
Support for vector scale range arguments, for AArch64 scalable vector extension (SVE) support. Adds -msve-vector-bits to the flang frontend, and for flang fc1 the options are -mvscale-min and -mvscale-max (optional). These match the clang and clang cc1 options for the same purposes. A further patch will actually USE these arguments.
1 parent 414ff81 commit 11e68c7

File tree

8 files changed

+158
-4
lines changed

8 files changed

+158
-4
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,18 +4557,19 @@ foreach i = {8-15,18} in
45574557
HelpText<"Make the x"#i#" register call-saved (AArch64 only)">;
45584558

45594559
def msve_vector_bits_EQ : Joined<["-"], "msve-vector-bits=">, Group<m_aarch64_Features_Group>,
4560+
Visibility<[ClangOption, FlangOption]>,
45604561
HelpText<"Specify the size in bits of an SVE vector register. Defaults to the"
45614562
" vector length agnostic value of \"scalable\". (AArch64 only)">;
45624563
} // let Flags = [TargetSpecific]
45634564

45644565
def mvscale_min_EQ : Joined<["-"], "mvscale-min=">,
45654566
Group<m_aarch64_Features_Group>, Flags<[NoXarchOption]>,
4566-
Visibility<[ClangOption, CC1Option]>,
4567+
Visibility<[ClangOption, CC1Option, FC1Option]>,
45674568
HelpText<"Specify the vscale minimum. Defaults to \"1\". (AArch64/RISC-V only)">,
45684569
MarshallingInfoInt<LangOpts<"VScaleMin">>;
45694570
def mvscale_max_EQ : Joined<["-"], "mvscale-max=">,
45704571
Group<m_aarch64_Features_Group>, Flags<[NoXarchOption]>,
4571-
Visibility<[ClangOption, CC1Option]>,
4572+
Visibility<[ClangOption, CC1Option, FC1Option]>,
45724573
HelpText<"Specify the vscale maximum. Defaults to the"
45734574
" vector length agnostic value of \"0\". (AArch64/RISC-V only)">,
45744575
MarshallingInfoInt<LangOpts<"VScaleMax">>;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
109
#include "Flang.h"
1110
#include "CommonArgs.h"
1211

@@ -170,6 +169,38 @@ void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
170169
}
171170
}
172171

172+
void Flang::AddAArch64TargetArgs(const ArgList &Args,
173+
ArgStringList &CmdArgs) const {
174+
// Handle -msve_vector_bits=<bits>
175+
if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
176+
StringRef Val = A->getValue();
177+
const Driver &D = getToolChain().getDriver();
178+
if (Val.equals("128") || Val.equals("256") || Val.equals("512") ||
179+
Val.equals("1024") || Val.equals("2048") || Val.equals("128+") ||
180+
Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") ||
181+
Val.equals("2048+")) {
182+
unsigned Bits = 0;
183+
if (Val.endswith("+"))
184+
Val = Val.substr(0, Val.size() - 1);
185+
else {
186+
[[maybe_unused]] bool Invalid = Val.getAsInteger(10, Bits);
187+
assert(!Invalid && "Failed to parse value");
188+
CmdArgs.push_back(
189+
Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128)));
190+
}
191+
192+
[[maybe_unused]] bool Invalid = Val.getAsInteger(10, Bits);
193+
assert(!Invalid && "Failed to parse value");
194+
CmdArgs.push_back(
195+
Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128)));
196+
// Silently drop requests for vector-length agnostic code as it's implied.
197+
} else if (!Val.equals("scalable"))
198+
// Handle the unsupported values passed to msve-vector-bits.
199+
D.Diag(diag::err_drv_unsupported_option_argument)
200+
<< A->getSpelling() << Val;
201+
}
202+
}
203+
173204
void Flang::addTargetOptions(const ArgList &Args,
174205
ArgStringList &CmdArgs) const {
175206
const ToolChain &TC = getToolChain();
@@ -186,9 +217,13 @@ void Flang::addTargetOptions(const ArgList &Args,
186217
switch (TC.getArch()) {
187218
default:
188219
break;
220+
case llvm::Triple::aarch64:
221+
getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
222+
AddAArch64TargetArgs(Args, CmdArgs);
223+
break;
224+
189225
case llvm::Triple::r600:
190226
case llvm::Triple::amdgcn:
191-
case llvm::Triple::aarch64:
192227
case llvm::Triple::riscv64:
193228
case llvm::Triple::x86_64:
194229
getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);

clang/lib/Driver/ToolChains/Flang.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class LLVM_LIBRARY_VISIBILITY Flang : public Tool {
5656
void addTargetOptions(const llvm::opt::ArgList &Args,
5757
llvm::opt::ArgStringList &CmdArgs) const;
5858

59+
/// Add specific options for AArch64 target.
60+
///
61+
/// \param [in] Args The list of input driver arguments
62+
/// \param [out] CmdArgs The list of output command arguments
63+
void AddAArch64TargetArgs(const llvm::opt::ArgList &Args,
64+
llvm::opt::ArgStringList &CmdArgs) const;
65+
5966
/// Extract offload options from the driver arguments and add them to
6067
/// the command arguments.
6168
/// \param [in] C The current compilation for the driver invocation

flang/include/flang/Frontend/LangOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@ LANGOPT(OpenMPNoThreadState, 1, 0)
5353
/// Assume that no thread in a parallel region will encounter a parallel region
5454
LANGOPT(OpenMPNoNestedParallelism, 1, 0)
5555

56+
LANGOPT(VScaleMin, 32, 0) ///< Minimum vscale range value
57+
LANGOPT(VScaleMax, 32, 0) ///< Maximum vscale range value
58+
5659
#undef LANGOPT
5760
#undef ENUM_LANGOPT

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,41 @@ static bool parseFloatingPointArgs(CompilerInvocation &invoc,
986986
return true;
987987
}
988988

989+
/// Parses vscale range options and populates the CompilerInvocation
990+
/// accordingly.
991+
/// Returns false if new errors are generated.
992+
///
993+
/// \param [out] invoc Stores the processed arguments
994+
/// \param [in] args The compiler invocation arguments to parse
995+
/// \param [out] diags DiagnosticsEngine to report erros with
996+
static bool parseVScaleArgs(CompilerInvocation &invoc, llvm::opt::ArgList &args,
997+
clang::DiagnosticsEngine &diags) {
998+
LangOptions &opts = invoc.getLangOpts();
999+
if (const auto arg =
1000+
args.getLastArg(clang::driver::options::OPT_mvscale_min_EQ)) {
1001+
llvm::StringRef argValue = llvm::StringRef(arg->getValue());
1002+
unsigned VScaleMin;
1003+
if (argValue.getAsInteger(/*Radix=*/10, VScaleMin)) {
1004+
diags.Report(clang::diag::err_drv_unsupported_option_argument)
1005+
<< arg->getSpelling() << argValue;
1006+
return false;
1007+
}
1008+
opts.VScaleMin = VScaleMin;
1009+
}
1010+
if (const auto arg =
1011+
args.getLastArg(clang::driver::options::OPT_mvscale_max_EQ)) {
1012+
llvm::StringRef argValue = llvm::StringRef(arg->getValue());
1013+
unsigned VScaleMax;
1014+
if (argValue.getAsInteger(/*Radix=w*/ 10, VScaleMax)) {
1015+
diags.Report(clang::diag::err_drv_unsupported_option_argument)
1016+
<< arg->getSpelling() << argValue;
1017+
return false;
1018+
}
1019+
opts.VScaleMax = VScaleMax;
1020+
}
1021+
return true;
1022+
}
1023+
9891024
bool CompilerInvocation::createFromArgs(
9901025
CompilerInvocation &res, llvm::ArrayRef<const char *> commandLineArgs,
9911026
clang::DiagnosticsEngine &diags, const char *argv0) {
@@ -1079,6 +1114,8 @@ bool CompilerInvocation::createFromArgs(
10791114

10801115
success &= parseFloatingPointArgs(res, args, diags);
10811116

1117+
success &= parseVScaleArgs(res, args, diags);
1118+
10821119
// Set the string to be used as the return value of the COMPILER_OPTIONS
10831120
// intrinsic of iso_fortran_env. This is either passed in from the parent
10841121
// compiler driver invocation with an environment variable, or failing that
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
! -----------------------------------------------------------------------------
2+
! Tests for the -msve-vector-bits flag (taken from the clang test)
3+
! -----------------------------------------------------------------------------
4+
5+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
6+
! RUN: -msve-vector-bits=128 2>&1 | FileCheck --check-prefix=CHECK-128 %s
7+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
8+
! RUN: -msve-vector-bits=256 2>&1 | FileCheck --check-prefix=CHECK-256 %s
9+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
10+
! RUN: -msve-vector-bits=512 2>&1 | FileCheck --check-prefix=CHECK-512 %s
11+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
12+
! RUN: -msve-vector-bits=1024 2>&1 | FileCheck --check-prefix=CHECK-1024 %s
13+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
14+
! RUN: -msve-vector-bits=2048 2>&1 | FileCheck --check-prefix=CHECK-2048 %s
15+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
16+
! RUN: -msve-vector-bits=128+ 2>&1 | FileCheck --check-prefix=CHECK-128P %s
17+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
18+
! RUN: -msve-vector-bits=256+ 2>&1 | FileCheck --check-prefix=CHECK-256P %s
19+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
20+
! RUN: -msve-vector-bits=512+ 2>&1 | FileCheck --check-prefix=CHECK-512P %s
21+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
22+
! RUN: -msve-vector-bits=1024+ 2>&1 | FileCheck --check-prefix=CHECK-1024P %s
23+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
24+
! RUN: -msve-vector-bits=2048+ 2>&1 | FileCheck --check-prefix=CHECK-2048P %s
25+
! RUN: %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
26+
! RUN: -msve-vector-bits=scalable 2>&1 | FileCheck --check-prefix=CHECK-SCALABLE %s
27+
28+
! CHECK-128: "-fc1"
29+
! CHECK-128-SAME: "-mvscale-max=1" "-mvscale-min=1"
30+
! CHECK-256: "-fc1"
31+
! CHECK-256-SAME: "-mvscale-max=2" "-mvscale-min=2"
32+
! CHECK-512: "-fc1"
33+
! CHECK-512-SAME: "-mvscale-max=4" "-mvscale-min=4"
34+
! CHECK-1024: "-fc1"
35+
! CHECK-1024-SAME: "-mvscale-max=8" "-mvscale-min=8"
36+
! CHECK-2048: "-fc1"
37+
! CHECK-2048-SAME: "-mvscale-max=16" "-mvscale-min=16"
38+
39+
! CHECK-128P: "-fc1"
40+
! CHECK-128P-SAME: "-mvscale-min=1"
41+
! CHECK-128P-NOT: "-mvscale-max"
42+
! CHECK-256P: "-fc1"
43+
! CHECK-256P-SAME: "-mvscale-min=2"
44+
! CHECK-256P-NOT: "-mvscale-max"
45+
! CHECK-512P: "-fc1"
46+
! CHECK-512P-SAME: "-mvscale-min=4"
47+
! CHECK-512P-NOT: "-mvscale-max"
48+
! CHECK-1024P: "-fc1"
49+
! CHECK-1024P-SAME: "-mvscale-min=8"
50+
! CHECK-1024P-NOT: "-mvscale-max"
51+
! CHECK-2048P: "-fc1"
52+
! CHECK-2048P-SAME: "-mvscale-min=16"
53+
! CHECK-2048P-NOT: "-mvscale-max"
54+
! CHECK-SCALABLE-NOT: "-mvscale-min=
55+
! CHECK-SCALABLE-NOT: "-mvscale-max=
56+
57+
! Error out if an unsupported value is passed to -msve-vector-bits.
58+
! -----------------------------------------------------------------------------
59+
! RUN: not %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
60+
! RUN: -msve-vector-bits=64 2>&1 | FileCheck --check-prefix=CHECK-BAD-VALUE-ERROR %s
61+
! RUN: not %flang -c %s -### --target=aarch64-none-linux-gnu -march=armv8-a+sve \
62+
! RUN: -msve-vector-bits=A 2>&1 | FileCheck --check-prefix=CHECK-BAD-VALUE-ERROR %s
63+
64+
! CHECK-BAD-VALUE-ERROR: error: unsupported argument '{{.*}}' to option '-msve-vector-bits='
65+

flang/test/Driver/driver-help-hidden.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
! CHECK-NEXT: -mllvm <value> Additional arguments to forward to LLVM's option processing
111111
! CHECK-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
112112
! CHECK-NEXT: -module-dir <dir> Put MODULE files in <dir>
113+
! CHECK-NEXT: -msve-vector-bits=<value>
114+
! CHECK-NEXT: Specify the size in bits of an SVE vector register. Defaults to the vector length agnostic value of "scalable". (AArch64 only)
113115
! CHECK-NEXT: --no-offload-arch=<value>
114116
! CHECK-NEXT: Remove CUDA/HIP offloading device architecture (e.g. sm_35, gfx906) from the list of devices to compile for. 'all' resets the list to its default value.
115117
! CHECK-NEXT: -nocpp Disable predefined and command line preprocessor macros

flang/test/Driver/driver-help.f90

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
! HELP-NEXT: -mllvm <value> Additional arguments to forward to LLVM's option processing
9999
! HELP-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
100100
! HELP-NEXT: -module-dir <dir> Put MODULE files in <dir>
101+
! HELP-NEXT: -msve-vector-bits=<value>
102+
! HELP-NEXT: Specify the size in bits of an SVE vector register. Defaults to the vector length agnostic value of "scalable". (AArch64 only)
101103
! HELP-NEXT: --no-offload-arch=<value>
102104
! HELP-NEXT: Remove CUDA/HIP offloading device architecture (e.g. sm_35, gfx906) from the list of devices to compile for. 'all' resets the list to its default value.
103105
! HELP-NEXT: -nocpp Disable predefined and command line preprocessor macros
@@ -228,6 +230,8 @@
228230
! HELP-FC1-NEXT: -mreassociate Allow reassociation transformations for floating-point instructions
229231
! HELP-FC1-NEXT: -mrelocation-model <value>
230232
! HELP-FC1-NEXT: The relocation model to use
233+
! HELP-FC1-NEXT: -mvscale-max=<value> Specify the vscale maximum. Defaults to the vector length agnostic value of "0". (AArch64/RISC-V only)
234+
! HELP-FC1-NEXT: -mvscale-min=<value> Specify the vscale minimum. Defaults to "1". (AArch64/RISC-V only)
231235
! HELP-FC1-NEXT: -nocpp Disable predefined and command line preprocessor macros
232236
! HELP-FC1-NEXT: -opt-record-file <value>
233237
! HELP-FC1-NEXT: File name to use for YAML optimization record output

0 commit comments

Comments
 (0)