Skip to content

Commit 2a551ab

Browse files
authored
[Multilib] Add -fmultilib-flag command-line option (#110658)
This patch is the second step to extend the current multilib system to support the selection of library variants which do not correspond to existing command-line options. Proposal can be found in https://discourse.llvm.org/t/rfc-multilib-custom-flags/81058 The multilib mechanism supports libraries that target code generation or language options such as --target, -mcpu, -mfpu, -mbranch-protection. However, some library variants are particular to features that do not correspond to any command-line options. Examples include variants for multithreading and semihosting. This work introduces a way to instruct the multilib system to consider these features in library selection. The driver must be informed about the multilib custom flags with a new command-line option. ``` -fmultilib-flag=C ``` Where the grammar for C is: ``` C -> option option -> multithreaded | no-multithreaded | io-none | io-semihosting | io-linux-syscalls | ... ``` There must be one option instance for each flag specified: ``` -fmultilib-flag=multithreaded -fmultilib-flag=io-semihosting ``` Contradictory options are untied by *last one wins*. These options are to be used exclusively by the multilib mechanism in the Clang driver. Hence they are not forwarded to the compiler frontend.
1 parent d98ced1 commit 2a551ab

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5756,6 +5756,8 @@ def print_multi_directory : Flag<["-", "--"], "print-multi-directory">;
57565756
def print_multi_lib : Flag<["-", "--"], "print-multi-lib">;
57575757
def print_multi_flags : Flag<["-", "--"], "print-multi-flags-experimental">,
57585758
HelpText<"Print the flags used for selecting multilibs (experimental)">;
5759+
def fmultilib_flag : Joined<["-", "--"], "fmultilib-flag=">,
5760+
Visibility<[ClangOption]>;
57595761
def print_multi_os_directory : Flag<["-", "--"], "print-multi-os-directory">,
57605762
Flags<[Unsupported]>;
57615763
def print_target_triple : Flag<["-", "--"], "print-target-triple">,

clang/lib/Driver/ToolChain.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ bool ToolChain::defaultToIEEELongDouble() const {
196196
return PPC_LINUX_DEFAULT_IEEELONGDOUBLE && getTriple().isOSLinux();
197197
}
198198

199+
static void processMultilibCustomFlags(Multilib::flags_list &List,
200+
const llvm::opt::ArgList &Args) {
201+
for (const Arg *MultilibFlagArg :
202+
Args.filtered(options::OPT_fmultilib_flag)) {
203+
List.push_back(MultilibFlagArg->getAsString(Args));
204+
MultilibFlagArg->claim();
205+
}
206+
}
207+
199208
static void getAArch64MultilibFlags(const Driver &D,
200209
const llvm::Triple &Triple,
201210
const llvm::opt::ArgList &Args,
@@ -246,6 +255,8 @@ static void getAArch64MultilibFlags(const Driver &D,
246255
if (ABIArg) {
247256
Result.push_back(ABIArg->getAsString(Args));
248257
}
258+
259+
processMultilibCustomFlags(Result, Args);
249260
}
250261

251262
static void getARMMultilibFlags(const Driver &D,
@@ -313,6 +324,7 @@ static void getARMMultilibFlags(const Driver &D,
313324
if (Endian->getOption().matches(options::OPT_mbig_endian))
314325
Result.push_back(Endian->getAsString(Args));
315326
}
327+
processMultilibCustomFlags(Result, Args);
316328
}
317329

318330
static void getRISCVMultilibFlags(const Driver &D, const llvm::Triple &Triple,

clang/test/Driver/print-multi-selection-flags.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,10 @@
9090
// CHECK-RV32E-ORDER: --target=riscv32-unknown-none-elf
9191
// CHECK-RV32E-ORDER: -mabi=ilp32e
9292
// CHECK-RV32E-ORDER: -march=rv32e{{[0-9]+p[0-9]+}}_c{{[0-9]+p[0-9]+}}_zicsr{{[0-9]+p[0-9]+}}
93+
94+
// RUN: %clang -print-multi-flags-experimental --target=armv8m.main-none-eabi -fmultilib-flag=foo -fmultilib-flag=bar | FileCheck --check-prefixes=CHECK-MULTILIB-CUSTOM-FLAG,CHECK-ARM-MULTILIB-CUSTOM-FLAG %s
95+
// RUN: %clang -print-multi-flags-experimental --target=aarch64-none-eabi -fmultilib-flag=foo -fmultilib-flag=bar | FileCheck --check-prefixes=CHECK-MULTILIB-CUSTOM-FLAG,CHECK-AARCH64-MULTILIB-CUSTOM-FLAG %s
96+
// CHECK-ARM-MULTILIB-CUSTOM-FLAG: --target=thumbv8m.main-unknown-none-eabi
97+
// CHECK-AARCH64-MULTILIB-CUSTOM-FLAG: --target=aarch64-unknown-none-eabi
98+
// CHECK-MULTILIB-CUSTOM-FLAG-DAG: -fmultilib-flag=foo
99+
// CHECK-MULTILIB-CUSTOM-FLAG-DAG: -fmultilib-flag=bar

0 commit comments

Comments
 (0)