Skip to content

Commit f05b081

Browse files
authored
[clang] Adjust -mlarge-data-threshold handling (#77958)
Make it apply to x86-64 medium and large code models since that's what the backend does. Limit logic to exclude x86-32. Default to 0, let the driver set it to 65536 for the medium code model if one is not passed. Set it to 0 for the large code model by default to match gcc and since some users make assumptions about the large code model that any small data will break.
1 parent 1048b59 commit f05b081

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def warn_unsupported_branch_protection: Warning <
472472
def err_sls_hardening_arm_not_supported : Error<
473473
"-mharden-sls is only supported on armv7-a or later">;
474474
def warn_drv_large_data_threshold_invalid_code_model: Warning<
475-
"'%0' only applies to medium code model">,
475+
"'%0' only applies to medium and large code models">,
476476
InGroup<UnusedCommandLineArgument>;
477477

478478
def note_drv_command_failed_diag_msg : Note<

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,8 +4392,8 @@ def mcmodel_EQ : Joined<["-"], "mcmodel=">, Group<m_Group>,
43924392
Visibility<[ClangOption, CC1Option]>,
43934393
MarshallingInfoString<TargetOpts<"CodeModel">, [{"default"}]>;
43944394
def mlarge_data_threshold_EQ : Joined<["-"], "mlarge-data-threshold=">, Group<m_Group>,
4395-
Visibility<[ClangOption, CC1Option]>,
4396-
MarshallingInfoInt<TargetOpts<"LargeDataThreshold">, "65535">;
4395+
Flags<[TargetSpecific]>, Visibility<[ClangOption, CC1Option]>,
4396+
MarshallingInfoInt<TargetOpts<"LargeDataThreshold">, "0">;
43974397
def mtls_size_EQ : Joined<["-"], "mtls-size=">, Group<m_Group>,
43984398
Visibility<[ClangOption, CC1Option]>,
43994399
HelpText<"Specify bit size of immediate TLS offsets (AArch64 ELF only): "

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ void CodeGenModule::Release() {
12011201
llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
12021202
getModule().setCodeModel(codeModel);
12031203

1204-
if (CM == llvm::CodeModel::Medium &&
1204+
if ((CM == llvm::CodeModel::Medium || CM == llvm::CodeModel::Large) &&
12051205
Context.getTargetInfo().getTriple().getArch() ==
12061206
llvm::Triple::x86_64) {
12071207
getModule().setLargeDataThreshold(getCodeGenOpts().LargeDataThreshold);

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5752,20 +5752,24 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
57525752
}
57535753
}
57545754

5755-
if (Arg *A = Args.getLastArg(options::OPT_mlarge_data_threshold_EQ)) {
5756-
if (!Triple.isX86()) {
5757-
D.Diag(diag::err_drv_unsupported_opt_for_target)
5758-
<< A->getOption().getName() << TripleStr;
5759-
} else {
5760-
bool IsMediumCM = false;
5761-
if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ))
5762-
IsMediumCM = StringRef(A->getValue()) == "medium";
5763-
if (!IsMediumCM) {
5755+
if (Triple.getArch() == llvm::Triple::x86_64) {
5756+
bool IsMediumCM = false;
5757+
bool IsLargeCM = false;
5758+
if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
5759+
IsMediumCM = StringRef(A->getValue()) == "medium";
5760+
IsLargeCM = StringRef(A->getValue()) == "large";
5761+
}
5762+
if (Arg *A = Args.getLastArg(options::OPT_mlarge_data_threshold_EQ)) {
5763+
if (!IsMediumCM && !IsLargeCM) {
57645764
D.Diag(diag::warn_drv_large_data_threshold_invalid_code_model)
57655765
<< A->getOption().getRenderName();
57665766
} else {
57675767
A->render(Args, CmdArgs);
57685768
}
5769+
} else if (IsMediumCM) {
5770+
CmdArgs.push_back("-mlarge-data-threshold=65536");
5771+
} else if (IsLargeCM) {
5772+
CmdArgs.push_back("-mlarge-data-threshold=0");
57695773
}
57705774
}
57715775

clang/test/CodeGen/large-data-threshold.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=medium | FileCheck %s --check-prefix=IR-DEFAULT
44
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=medium -mlarge-data-threshold=200 | FileCheck %s --check-prefix=IR-CUSTOM
5+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=large -mlarge-data-threshold=200 | FileCheck %s --check-prefix=IR-CUSTOM
6+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=small -mlarge-data-threshold=200 | FileCheck %s --check-prefix=IR-SMALL
57
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -S %s -o - -mcmodel=medium -mlarge-data-threshold=200 | FileCheck %s --check-prefix=ASM-SMALL
68
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -S %s -o - -mcmodel=medium -mlarge-data-threshold=2 | FileCheck %s --check-prefix=ASM-LARGE
79

8-
// IR-DEFAULT: !{i32 1, !"Large Data Threshold", i64 65535}
10+
// IR-DEFAULT: !{i32 1, !"Large Data Threshold", i64 0}
911
// IR-CUSTOM: !{i32 1, !"Large Data Threshold", i64 200}
12+
// IR-SMALL-NOT: !"Large Data Threshold"
1013

1114
// ASM-SMALL-NOT: movabsq
1215
// ASM-LARGE: movabsq
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// RUN: %clang --target=x86_64 -### -c -mcmodel=large -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARG %s
12
// RUN: %clang --target=x86_64 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARG %s
2-
// RUN: %clang --target=x86_64 -### -c -mcmodel=small -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=SMALL %s
3+
// RUN: %clang --target=x86_64 -### -c -mcmodel=large %s 2>&1 | FileCheck --check-prefix=ARG-LARGE-DEFAULT %s
4+
// RUN: %clang --target=x86_64 -### -c -mcmodel=medium %s 2>&1 | FileCheck --check-prefix=ARG-MEDIUM-DEFAULT %s
5+
// RUN: %clang --target=x86_64 -### -c -mcmodel=small -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=SMALL --implicit-check-not=mlarge-data-threshold %s
36
// RUN: not %clang --target=riscv32 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARCH %s
47

58
// ARG: "-mlarge-data-threshold=200"
9+
// ARG-MEDIUM-DEFAULT: "-mlarge-data-threshold=65536"
10+
// ARG-LARGE-DEFAULT: "-mlarge-data-threshold=0"
611

7-
// SMALL: 'mlarge-data-threshold=' only applies to medium code model
8-
// ARCH: unsupported option 'mlarge-data-threshold=' for target 'riscv32'
12+
// SMALL: 'mlarge-data-threshold=' only applies to medium and large code models
13+
// ARCH: unsupported option '-mlarge-data-threshold=' for target

0 commit comments

Comments
 (0)