Skip to content

Commit 2c60d59

Browse files
authored
[Flang] Support -mrvv-vector-bits flag (#77588)
This patch adds support for the -mrvv-vector-bits flag in the Flang driver, and translates them to -mvscale-min/-mvscale-max. The code was copied from the Clang toolchain (similarly to what was done for AArch64's -msve-vector-bits flag) so it also supports the same -mrvv-vector-bits=zvl mode. Note that Flang doesn't yet define the __riscv_v_fixed_vlen macro, so the help text has been updated to highlight that it's only defined for Clang.
1 parent 7cc9ae9 commit 2c60d59

File tree

6 files changed

+120
-5
lines changed

6 files changed

+120
-5
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4585,11 +4585,13 @@ let Flags = [TargetSpecific] in {
45854585
def menable_experimental_extensions : Flag<["-"], "menable-experimental-extensions">, Group<m_Group>,
45864586
HelpText<"Enable use of experimental RISC-V extensions.">;
45874587
def mrvv_vector_bits_EQ : Joined<["-"], "mrvv-vector-bits=">, Group<m_Group>,
4588-
HelpText<"Specify the size in bits of an RVV vector register. Defaults to "
4589-
"the vector length agnostic value of \"scalable\". Accepts power of "
4590-
"2 values between 64 and 65536. Also accepts \"zvl\" "
4591-
"to use the value implied by -march/-mcpu. Value will be reflected "
4592-
"in __riscv_v_fixed_vlen preprocessor define (RISC-V only)">;
4588+
Visibility<[ClangOption, FlangOption]>,
4589+
HelpText<"Specify the size in bits of an RVV vector register">,
4590+
DocBrief<"Defaults to the vector length agnostic value of \"scalable\". "
4591+
"Accepts power of 2 values between 64 and 65536. Also accepts "
4592+
"\"zvl\" to use the value implied by -march/-mcpu. On Clang, value "
4593+
"will be reflected in __riscv_v_fixed_vlen preprocessor define "
4594+
"(RISC-V only)">;
45934595

45944596
def munaligned_access : Flag<["-"], "munaligned-access">, Group<m_Group>,
45954597
HelpText<"Allow memory accesses to be unaligned (AArch32/AArch64/LoongArch/RISC-V only)">;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Flang.h"
10+
#include "Arch/RISCV.h"
1011
#include "CommonArgs.h"
1112

1213
#include "clang/Basic/CodeGenOptions.h"
1314
#include "clang/Driver/Options.h"
1415
#include "llvm/Frontend/Debug/Options.h"
1516
#include "llvm/Support/FileSystem.h"
1617
#include "llvm/Support/Path.h"
18+
#include "llvm/Support/RISCVISAInfo.h"
19+
#include "llvm/TargetParser/RISCVTargetParser.h"
1720

1821
#include <cassert>
1922

@@ -203,6 +206,51 @@ void Flang::AddAArch64TargetArgs(const ArgList &Args,
203206
}
204207
}
205208

209+
void Flang::AddRISCVTargetArgs(const ArgList &Args,
210+
ArgStringList &CmdArgs) const {
211+
const llvm::Triple &Triple = getToolChain().getTriple();
212+
// Handle -mrvv-vector-bits=<bits>
213+
if (Arg *A = Args.getLastArg(options::OPT_mrvv_vector_bits_EQ)) {
214+
StringRef Val = A->getValue();
215+
const Driver &D = getToolChain().getDriver();
216+
217+
// Get minimum VLen from march.
218+
unsigned MinVLen = 0;
219+
StringRef Arch = riscv::getRISCVArch(Args, Triple);
220+
auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
221+
Arch, /*EnableExperimentalExtensions*/ true);
222+
// Ignore parsing error.
223+
if (!errorToBool(ISAInfo.takeError()))
224+
MinVLen = (*ISAInfo)->getMinVLen();
225+
226+
// If the value is "zvl", use MinVLen from march. Otherwise, try to parse
227+
// as integer as long as we have a MinVLen.
228+
unsigned Bits = 0;
229+
if (Val.equals("zvl") && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
230+
Bits = MinVLen;
231+
} else if (!Val.getAsInteger(10, Bits)) {
232+
// Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that
233+
// at least MinVLen.
234+
if (Bits < MinVLen || Bits < llvm::RISCV::RVVBitsPerBlock ||
235+
Bits > 65536 || !llvm::isPowerOf2_32(Bits))
236+
Bits = 0;
237+
}
238+
239+
// If we got a valid value try to use it.
240+
if (Bits != 0) {
241+
unsigned VScaleMin = Bits / llvm::RISCV::RVVBitsPerBlock;
242+
CmdArgs.push_back(
243+
Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin)));
244+
CmdArgs.push_back(
245+
Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin)));
246+
} else if (!Val.equals("scalable")) {
247+
// Handle the unsupported values passed to mrvv-vector-bits.
248+
D.Diag(diag::err_drv_unsupported_option_argument)
249+
<< A->getSpelling() << Val;
250+
}
251+
}
252+
}
253+
206254
static void addVSDefines(const ToolChain &TC, const ArgList &Args,
207255
ArgStringList &CmdArgs) {
208256

@@ -321,6 +369,9 @@ void Flang::addTargetOptions(const ArgList &Args,
321369
AddAMDGPUTargetArgs(Args, CmdArgs);
322370
break;
323371
case llvm::Triple::riscv64:
372+
getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
373+
AddRISCVTargetArgs(Args, CmdArgs);
374+
break;
324375
case llvm::Triple::x86_64:
325376
getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
326377
break;

clang/lib/Driver/ToolChains/Flang.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ class LLVM_LIBRARY_VISIBILITY Flang : public Tool {
7070
void AddAMDGPUTargetArgs(const llvm::opt::ArgList &Args,
7171
llvm::opt::ArgStringList &CmdArgs) const;
7272

73+
/// Add specific options for RISC-V target.
74+
///
75+
/// \param [in] Args The list of input driver arguments
76+
/// \param [out] CmdArgs The list of output command arguments
77+
void AddRISCVTargetArgs(const llvm::opt::ArgList &Args,
78+
llvm::opt::ArgStringList &CmdArgs) const;
79+
7380
/// Extract offload options from the driver arguments and add them to
7481
/// the command arguments.
7582
/// \param [in] C The current compilation for the driver invocation

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
! CHECK-NEXT: -mllvm <value> Additional arguments to forward to LLVM's option processing
123123
! CHECK-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
124124
! CHECK-NEXT: -module-dir <dir> Put MODULE files in <dir>
125+
! CHECK-NEXT: -mrvv-vector-bits=<value>
126+
! CHECK-NEXT: Specify the size in bits of an RVV vector register
125127
! CHECK-NEXT: -msve-vector-bits=<value>
126128
! CHECK-NEXT: Specify the size in bits of an SVE vector register. Defaults to the vector length agnostic value of "scalable". (AArch64 only)
127129
! CHECK-NEXT: --no-offload-arch=<value>

flang/test/Driver/driver-help.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@
108108
! HELP-NEXT: -mllvm <value> Additional arguments to forward to LLVM's option processing
109109
! HELP-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
110110
! HELP-NEXT: -module-dir <dir> Put MODULE files in <dir>
111+
! HELP-NEXT: -mrvv-vector-bits=<value>
112+
! HELP-NEXT: Specify the size in bits of an RVV vector register
111113
! HELP-NEXT: -msve-vector-bits=<value>
112114
! HELP-NEXT: Specify the size in bits of an SVE vector register. Defaults to the vector length agnostic value of "scalable". (AArch64 only)
113115
! HELP-NEXT: --no-offload-arch=<value>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
! -----------------------------------------------------------------------------
2+
! Tests for the -mrvv-vector-bits flag (taken from the clang test)
3+
! -----------------------------------------------------------------------------
4+
5+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
6+
! RUN: -mrvv-vector-bits=128 2>&1 | FileCheck --check-prefix=CHECK-128 %s
7+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
8+
! RUN: -mrvv-vector-bits=256 2>&1 | FileCheck --check-prefix=CHECK-256 %s
9+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
10+
! RUN: -mrvv-vector-bits=512 2>&1 | FileCheck --check-prefix=CHECK-512 %s
11+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
12+
! RUN: -mrvv-vector-bits=1024 2>&1 | FileCheck --check-prefix=CHECK-1024 %s
13+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
14+
! RUN: -mrvv-vector-bits=2048 2>&1 | FileCheck --check-prefix=CHECK-2048 %s
15+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
16+
! RUN: -mrvv-vector-bits=scalable 2>&1 | FileCheck --check-prefix=CHECK-SCALABLE %s
17+
18+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gcv_zvl256b \
19+
! RUN: -mrvv-vector-bits=zvl 2>&1 | FileCheck --check-prefix=CHECK-256 %s
20+
! RUN: %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gcv_zvl512b \
21+
! RUN: -mrvv-vector-bits=zvl 2>&1 | FileCheck --check-prefix=CHECK-512 %s
22+
23+
! CHECK-128: "-fc1"
24+
! CHECK-128-SAME: "-mvscale-max=2" "-mvscale-min=2"
25+
! CHECK-256: "-fc1"
26+
! CHECK-256-SAME: "-mvscale-max=4" "-mvscale-min=4"
27+
! CHECK-512: "-fc1"
28+
! CHECK-512-SAME: "-mvscale-max=8" "-mvscale-min=8"
29+
! CHECK-1024: "-fc1"
30+
! CHECK-1024-SAME: "-mvscale-max=16" "-mvscale-min=16"
31+
! CHECK-2048: "-fc1"
32+
! CHECK-2048-SAME: "-mvscale-max=32" "-mvscale-min=32"
33+
34+
! CHECK-SCALABLE-NOT: "-mvscale-min=
35+
! CHECK-SCALABLE-NOT: "-mvscale-max=
36+
37+
! Error out if an unsupported value is passed to -mrvv-vector-bits.
38+
! -----------------------------------------------------------------------------
39+
! RUN: not %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
40+
! RUN: -mrvv-vector-bits=16 2>&1 | FileCheck --check-prefix=CHECK-BAD-VALUE-ERROR %s
41+
! RUN: not %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
42+
! RUN: -mrvv-vector-bits=A 2>&1 | FileCheck --check-prefix=CHECK-BAD-VALUE-ERROR %s
43+
! RUN: not %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc_zve64x \
44+
! RUN: -mrvv-vector-bits=131072 2>&1 | FileCheck --check-prefix=CHECK-BAD-VALUE-ERROR %s
45+
! RUN: not %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gc \
46+
! RUN: -mrvv-vector-bits=zvl 2>&1 | FileCheck --check-prefix=CHECK-BAD-VALUE-ERROR %s
47+
! RUN: not %flang -c %s -### --target=riscv64-linux-gnu -march=rv64gcv \
48+
! RUN: -mrvv-vector-bits=64 2>&1 | FileCheck --check-prefix=CHECK-BAD-VALUE-ERROR %s
49+
!
50+
! CHECK-BAD-VALUE-ERROR: error: unsupported argument '{{.*}}' to option '-mrvv-vector-bits='
51+

0 commit comments

Comments
 (0)