Skip to content

Commit 92a966b

Browse files
authored
[RISCV] Add -march=unset to cancel and ignore a previous -march. (#148321)
-mcpu is used to determine the ISA string if an explicit -march is not present on the command line. If there is a -march present it always has priority over -mcpu regardless of where it appears in the command line. This can cause issues if -march appears in a Makefile and a user wants to override it with an -mcpu on the command line. The user would need to provide a potentially long ISA string to -march that matches the -mcpu in order to override the MakeFile. This issue can also be seen on Compiler Explorer where the rv64gc toolchain is passed -march=rv64gc before any user command line options are added. If you pass -mcpu=spacemit-x60, vectors will not be enabled due to the hidden -march. This patch adds a new option for -march, "unset" that makes the -march ignored for purposes of prioritizing over -mcpu. Now a user can write -march=unset -mcpu=<cpu_name>. This is also implemented by gcc for ARM32. An alternative would be to allow -march to take a cpu name, but that requires "-march=<cpu_name> -mcpu=<cpu_name>" or "-march=<cpu_name> -mtune=<cpu_name>" to ensure the tune cpu also gets updated. IMHO, needing to repeat the CPU name twice isn't friendly and invites mistakes. gcc has implemented this as well.
1 parent d1827f0 commit 92a966b

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ RISC-V Support
234234
- Add support for `__attribute__((interrupt("rnmi")))` to be used with the `Smrnmi` extension.
235235
With this the `Smrnmi` extension is fully supported.
236236

237+
- Add `-march=unset` to clear any previous `-march=` value. This ISA string will
238+
be computed from `-mcpu` or the platform default.
239+
237240
CUDA/HIP Language Changes
238241
^^^^^^^^^^^^^^^^^^^^^^^^^
239242

clang/lib/Driver/ToolChains/Arch/RISCV.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,12 @@ std::string riscv::getRISCVArch(const llvm::opt::ArgList &Args,
273273
// Clang does not yet support MULTILIB_REUSE, so we use `rv{XLEN}imafdc`
274274
// instead of `rv{XLEN}gc` though they are (currently) equivalent.
275275

276-
// 1. If `-march=` is specified, use it.
277-
if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
278-
return A->getValue();
276+
// 1. If `-march=` is specified, use it unless the value is "unset".
277+
if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
278+
StringRef MArch = A->getValue();
279+
if (MArch != "unset")
280+
return MArch.str();
281+
}
279282

280283
// 2. Get march (isa string) based on `-mcpu=`
281284
if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
@@ -300,7 +303,7 @@ std::string riscv::getRISCVArch(const llvm::opt::ArgList &Args,
300303

301304
StringRef MArch = llvm::RISCV::getMArchFromMcpu(CPU);
302305
// Bypass if target cpu's default march is empty.
303-
if (MArch != "")
306+
if (!MArch.empty())
304307
return MArch.str();
305308
}
306309

clang/test/Driver/riscv-cpus.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@
403403
// MCPU-MARCH: "-nostdsysteminc" "-target-cpu" "sifive-e31" "-target-feature" "+m" "-target-feature" "+c"
404404
// MCPU-MARCH: "-target-abi" "ilp32"
405405

406+
// -march=unset erases previous march
407+
// RUN: %clang --target=riscv32 -### -c %s 2>&1 -march=rv32imc -march=unset -mcpu=sifive-e31 | FileCheck -check-prefix=MARCH-UNSET %s
408+
// MARCH-UNSET: "-nostdsysteminc" "-target-cpu" "sifive-e31" "-target-feature" "+m" "-target-feature" "+a" "-target-feature" "+c"
409+
// MARCH-UNSET-SAME: "-target-abi" "ilp32"
410+
406411
// Check interaction between -mcpu and mtune, -mtune won't affect arch related
407412
// target feature, but -mcpu will.
408413
//

0 commit comments

Comments
 (0)