Skip to content

Commit 7e42545

Browse files
authored
[Driver] Reject unsupported -mcmodel= (#70262)
-mcmodel= is supported for a few architectures. Reject the option for other architectures. * -mcmodel= is unsupported on x86-32. * -mcmodel=large is unsupported for PIC on AArch64. * -mcmodel= is unsupported for aarch64_32 triples. * https://reviews.llvm.org/D67066 (for RISC-V) made -mcmodel=medany/-mcmodel=medlow aliases for all architectures. Restrict this to RISC-V. * llvm/lib/Target/Sparc has some small/medium/large support, but the values listed on https://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html had been supported before https://reviews.llvm.org/D67066. Consider -mcmodel= unsupported for Sparc. * https://reviews.llvm.org/D106371 translated -mcmodel=medium to -mcmodel=large on AIX, even for 32-bit systems. Retain this behavior but reject -mcmodel= for other PPC32 systems. In general the accept/reject behavior is more similar to GCC. err_drv_invalid_argument_to_option is less clear than err_drv_unsupported_option_argument. As the supported values are different for different architectures, add a err_drv_unsupported_option_argument_for_target for better clarity.
1 parent cf0f6a1 commit 7e42545

File tree

7 files changed

+39
-35
lines changed

7 files changed

+39
-35
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def err_drv_unsupported_opt_for_language_mode : Error<
2020
"unsupported option '%0' for language mode '%1'">;
2121
def err_drv_unsupported_option_argument : Error<
2222
"unsupported argument '%1' to option '%0'">;
23+
def err_drv_unsupported_option_argument_for_target : Error<
24+
"unsupported argument '%1' to option '%0' for target '%2'">;
2325
def err_drv_unknown_stdin_type : Error<
2426
"-E or -x required when input is from standard input">;
2527
def err_drv_unknown_stdin_type_clang_cl : Error<

clang/include/clang/Driver/Options.td

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,14 +4480,6 @@ def msave_restore : Flag<["-"], "msave-restore">, Group<m_riscv_Features_Group>,
44804480
def mno_save_restore : Flag<["-"], "mno-save-restore">, Group<m_riscv_Features_Group>,
44814481
HelpText<"Disable using library calls for save and restore">;
44824482
} // let Flags = [TargetSpecific]
4483-
def mcmodel_EQ_medlow : Flag<["-"], "mcmodel=medlow">, Group<m_Group>,
4484-
Visibility<[ClangOption, CC1Option]>,
4485-
Alias<mcmodel_EQ>, AliasArgs<["small"]>,
4486-
HelpText<"Equivalent to -mcmodel=small, compatible with RISC-V gcc.">;
4487-
def mcmodel_EQ_medany : Flag<["-"], "mcmodel=medany">, Group<m_Group>,
4488-
Visibility<[ClangOption, CC1Option]>,
4489-
Alias<mcmodel_EQ>, AliasArgs<["medium"]>,
4490-
HelpText<"Equivalent to -mcmodel=medium, compatible with RISC-V gcc.">;
44914483
let Flags = [TargetSpecific] in {
44924484
def menable_experimental_extensions : Flag<["-"], "menable-experimental-extensions">, Group<m_Group>,
44934485
HelpText<"Enable use of experimental RISC-V extensions.">;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5722,18 +5722,33 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
57225722

57235723
if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
57245724
StringRef CM = A->getValue();
5725-
if (CM == "small" || CM == "kernel" || CM == "medium" || CM == "large" ||
5726-
CM == "tiny") {
5727-
if (Triple.isOSAIX() && CM == "medium")
5728-
CmdArgs.push_back("-mcmodel=large");
5729-
else if (Triple.isAArch64() && (CM == "kernel" || CM == "medium"))
5730-
D.Diag(diag::err_drv_invalid_argument_to_option)
5731-
<< CM << A->getOption().getName();
5732-
else
5733-
A->render(Args, CmdArgs);
5725+
bool Ok = false;
5726+
if (Triple.isOSAIX() && CM == "medium") {
5727+
CM = "large";
5728+
Ok = true;
5729+
}
5730+
if (Triple.isAArch64(64)) {
5731+
Ok = CM == "tiny" || CM == "small" || CM == "large";
5732+
if (CM == "large" && RelocationModel != llvm::Reloc::Static)
5733+
D.Diag(diag::err_drv_argument_only_allowed_with)
5734+
<< A->getAsString(Args) << "-fno-pic";
5735+
} else if (Triple.isPPC64()) {
5736+
Ok = CM == "small" || CM == "medium" || CM == "large";
5737+
} else if (Triple.isRISCV()) {
5738+
if (CM == "medlow")
5739+
CM = "small";
5740+
else if (CM == "medany")
5741+
CM = "medium";
5742+
Ok = CM == "small" || CM == "medium";
5743+
} else if (Triple.getArch() == llvm::Triple::x86_64) {
5744+
Ok = llvm::is_contained({"small", "kernel", "medium", "large", "tiny"},
5745+
CM);
5746+
}
5747+
if (Ok) {
5748+
CmdArgs.push_back(Args.MakeArgString("-mcmodel=" + CM));
57345749
} else {
5735-
D.Diag(diag::err_drv_invalid_argument_to_option)
5736-
<< CM << A->getOption().getName();
5750+
D.Diag(diag::err_drv_unsupported_option_argument_for_target)
5751+
<< A->getSpelling() << CM << TripleStr;
57375752
}
57385753
}
57395754

clang/test/CodeGen/RISCV/riscv-sdata-module-flag.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
// RUN: | FileCheck %s -check-prefix=RV64-ANDROID
2828
// RUN: %clang --target=riscv64-unknown-elf %s -S -emit-llvm -fpic -o - \
2929
// RUN: | FileCheck %s -check-prefix=RV64-PIC
30-
// RUN: %clang --target=riscv64-unknown-elf %s -S -emit-llvm -mcmodel=large -o - \
31-
// RUN: | FileCheck %s -check-prefix=RV64-LARGE
3230

3331
void test(void) {}
3432

clang/test/Driver/mcmodel.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
// RUN: not %clang -### -c --target=i686 -mcmodel=medium %s 2>&1 | FileCheck --check-prefix=ERR-MEDIUM %s
12
// RUN: %clang --target=x86_64 -### -c -mcmodel=tiny %s 2>&1 | FileCheck --check-prefix=TINY %s
23
// RUN: %clang --target=x86_64 -### -c -mcmodel=small %s 2>&1 | FileCheck --check-prefix=SMALL %s
34
// RUN: %clang --target=x86_64 -### -S -mcmodel=kernel %s 2>&1 | FileCheck --check-prefix=KERNEL %s
45
// RUN: %clang --target=x86_64 -### -c -mcmodel=medium %s 2>&1 | FileCheck --check-prefix=MEDIUM %s
56
// RUN: %clang --target=x86_64 -### -S -mcmodel=large %s 2>&1 | FileCheck --check-prefix=LARGE %s
7+
// RUN: not %clang -### -c --target=powerpc-linux-gnu -mcmodel=medium %s 2>&1 | FileCheck --check-prefix=ERR-MEDIUM %s
68
// RUN: %clang --target=powerpc-unknown-aix -### -S -mcmodel=medium %s 2> %t.log
79
// RUN: FileCheck --check-prefix=AIX-MCMEDIUM-OVERRIDE %s < %t.log
810
// RUN: not %clang -### -c -mcmodel=lager %s 2>&1 | FileCheck --check-prefix=INVALID %s
911
// RUN: %clang --target=aarch64 -### -S -mcmodel=large -fno-pic %s 2>&1 | FileCheck --check-prefix=LARGE %s
12+
// RUN: not %clang --target=aarch64 -### -S -mcmodel=large -fpic %s 2>&1 | FileCheck --check-prefix=AARCH64-PIC-LARGE %s
1013
// RUN: not %clang -### -c --target=aarch64 -mcmodel=medium %s 2>&1 | FileCheck --check-prefix=ERR-MEDIUM %s
1114
// RUN: not %clang -### -c --target=aarch64 -mcmodel=kernel %s 2>&1 | FileCheck --check-prefix=ERR-KERNEL %s
15+
// RUN: not %clang --target=aarch64_32-linux -### -S -mcmodel=small %s 2>&1 | FileCheck --check-prefix=ERR-AARCH64_32 %s
1216

1317
// TINY: "-mcmodel=tiny"
1418
// SMALL: "-mcmodel=small"
@@ -17,7 +21,11 @@
1721
// LARGE: "-mcmodel=large"
1822
// AIX-MCMEDIUM-OVERRIDE: "-mcmodel=large"
1923

20-
// INVALID: error: invalid argument 'lager' to -mcmodel=
24+
// INVALID: error: unsupported argument 'lager' to option '-mcmodel=' for target '{{.*}}'
2125

22-
// ERR-MEDIUM: error: invalid argument 'medium' to -mcmodel=
23-
// ERR-KERNEL: error: invalid argument 'kernel' to -mcmodel=
26+
// ERR-MEDIUM: error: unsupported argument 'medium' to option '-mcmodel=' for target '{{.*}}'
27+
// ERR-KERNEL: error: unsupported argument 'kernel' to option '-mcmodel=' for target '{{.*}}'
28+
// ERR-LARGE: error: unsupported argument 'large' to option '-mcmodel=' for target '{{.*}}'
29+
30+
// AARCH64-PIC-LARGE: error: invalid argument '-mcmodel=large' only allowed with '-fno-pic'
31+
// ERR-AARCH64_32: error: unsupported argument 'small' to option '-mcmodel=' for target 'aarch64_32-unknown-linux'

clang/test/Driver/riscv-sdata-warning.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,3 @@
22
// RUN: %clang -S --target=riscv32-unknown-elf -fpic -msmall-data-limit=8 %s 2>&1 \
33
// RUN: | FileCheck -check-prefix=CHECK-PIC-SDATA %s
44
// CHECK-PIC-SDATA: warning: ignoring '-msmall-data-limit=' with -mcmodel=large for -fpic or RV64
5-
6-
// RUN: %clang -S --target=riscv64-unknown-elf -mcmodel=large -msmall-data-limit=8 %s 2>&1 \
7-
// RUN: | FileCheck -check-prefix=CHECK-RV64-LARGE-SDATA %s
8-
// CHECK-RV64-LARGE-SDATA: warning: ignoring '-msmall-data-limit=' with -mcmodel=large for -fpic or RV64
9-
10-
// RUN: %clang -S --target=riscv64-linux-android -msmall-data-limit=8 %s 2>&1 \
11-
// RUN: | FileCheck -check-prefix=CHECK-RV64-LARGE-SDATA-ANDROID %s
12-
// CHECK-RV64-LARGE-SDATA-ANDROID: warning: ignoring '-msmall-data-limit=' with -mcmodel=large for -fpic or RV64

clang/test/Preprocessor/init-x86.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,6 @@
802802
// X86_64H:#define __x86_64h 1
803803
// X86_64H:#define __x86_64h__ 1
804804

805-
// RUN: %clang -xc - -E -dM -mcmodel=medium --target=i386-unknown-linux < /dev/null | FileCheck -match-full-lines -check-prefix X86_MEDIUM %s
806-
// X86_MEDIUM:#define __code_model_medium__ 1
807-
808805
// RUN: %clang_cc1 -E -dM -ffreestanding -fgnuc-version=4.2.1 -triple=x86_64-none-none-gnux32 < /dev/null | FileCheck -match-full-lines -check-prefix X32 %s
809806
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -fgnuc-version=4.2.1 -triple=x86_64-none-none-gnux32 < /dev/null | FileCheck -match-full-lines -check-prefix X32 -check-prefix X32-CXX %s
810807
//

0 commit comments

Comments
 (0)