Skip to content

Commit 17e128e

Browse files
[flang]Add vscale argument parsing
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 9243b1b commit 17e128e

File tree

8 files changed

+95
-3
lines changed

8 files changed

+95
-3
lines changed

clang/include/clang/Driver/Options.td

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

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

45574558
def mvscale_min_EQ : Joined<["-"], "mvscale-min=">,
45584559
Group<m_aarch64_Features_Group>, Flags<[NoXarchOption]>,
4559-
Visibility<[ClangOption, CC1Option]>,
4560+
Visibility<[ClangOption, CC1Option, FC1Option]>,
45604561
HelpText<"Specify the vscale minimum. Defaults to \"1\". (AArch64/RISC-V only)">,
45614562
MarshallingInfoInt<LangOpts<"VScaleMin">>;
45624563
def mvscale_max_EQ : Joined<["-"], "mvscale-max=">,
45634564
Group<m_aarch64_Features_Group>, Flags<[NoXarchOption]>,
4564-
Visibility<[ClangOption, CC1Option]>,
4565+
Visibility<[ClangOption, CC1Option, FC1Option]>,
45654566
HelpText<"Specify the vscale maximum. Defaults to the"
45664567
" vector length agnostic value of \"0\". (AArch64/RISC-V only)">,
45674568
MarshallingInfoInt<LangOpts<"VScaleMax">>;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,39 @@ void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
170170
}
171171
}
172172

173+
174+
void Flang::AddAArch64TargetArgs(const ArgList &Args,
175+
ArgStringList &CmdArgs) const {
176+
// Handle -msve_vector_bits=<bits>
177+
if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
178+
StringRef Val = A->getValue();
179+
const Driver &D = getToolChain().getDriver();
180+
if (Val.equals("128") || Val.equals("256") || Val.equals("512") ||
181+
Val.equals("1024") || Val.equals("2048") || Val.equals("128+") ||
182+
Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") ||
183+
Val.equals("2048+")) {
184+
unsigned Bits = 0;
185+
if (Val.endswith("+"))
186+
Val = Val.substr(0, Val.size() - 1);
187+
else {
188+
bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid;
189+
assert(!Invalid && "Failed to parse value");
190+
CmdArgs.push_back(
191+
Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128)));
192+
}
193+
194+
bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid;
195+
assert(!Invalid && "Failed to parse value");
196+
CmdArgs.push_back(
197+
Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128)));
198+
// Silently drop requests for vector-length agnostic code as it's implied.
199+
} else if (!Val.equals("scalable"))
200+
// Handle the unsupported values passed to msve-vector-bits.
201+
D.Diag(diag::err_drv_unsupported_option_argument)
202+
<< A->getSpelling() << Val;
203+
}
204+
}
205+
173206
void Flang::addTargetOptions(const ArgList &Args,
174207
ArgStringList &CmdArgs) const {
175208
const ToolChain &TC = getToolChain();
@@ -186,9 +219,13 @@ void Flang::addTargetOptions(const ArgList &Args,
186219
switch (TC.getArch()) {
187220
default:
188221
break;
222+
case llvm::Triple::aarch64:
223+
getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
224+
AddAArch64TargetArgs(Args, CmdArgs);
225+
break;
226+
189227
case llvm::Triple::r600:
190228
case llvm::Triple::amdgcn:
191-
case llvm::Triple::aarch64:
192229
case llvm::Triple::riscv64:
193230
case llvm::Triple::x86_64:
194231
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

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
4343
#include "clang/Basic/Diagnostic.h"
4444
#include "clang/Basic/DiagnosticFrontend.h"
45+
#include "clang/Basic/TargetInfo.h"
4546
#include "clang/Driver/DriverDiagnostic.h"
4647
#include "llvm/ADT/SmallString.h"
4748
#include "llvm/ADT/StringRef.h"

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)