https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/77958
>From 3a54757173faffe07da55223c52621691afad54d Mon Sep 17 00:00:00 2001 From: Arthur Eubanks <aeuba...@google.com> Date: Fri, 12 Jan 2024 18:13:06 +0000 Subject: [PATCH 1/2] [clang] Adjust -mlarge-data-threshold handling Make it apply to x86-64 medium and large code models since that's what the backend does. Warn if it's used for 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. --- .../clang/Basic/DiagnosticDriverKinds.td | 2 +- clang/include/clang/Driver/Options.td | 2 +- clang/lib/CodeGen/CodeGenModule.cpp | 2 +- clang/lib/Driver/ToolChains/Clang.cpp | 27 ++++++++++++------- clang/test/CodeGen/large-data-threshold.c | 5 +++- clang/test/Driver/large-data-threshold.c | 12 ++++++--- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 0a8a77fadbeb1b..8b5232a6df3958 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -472,7 +472,7 @@ def warn_unsupported_branch_protection: Warning < def err_sls_hardening_arm_not_supported : Error< "-mharden-sls is only supported on armv7-a or later">; def warn_drv_large_data_threshold_invalid_code_model: Warning< - "'%0' only applies to medium code model">, + "'%0' only applies to medium and large code models">, InGroup<UnusedCommandLineArgument>; def note_drv_command_failed_diag_msg : Note< diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 7f97d6b6faa398..e8afa1ea4126d8 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4393,7 +4393,7 @@ def mcmodel_EQ : Joined<["-"], "mcmodel=">, Group<m_Group>, MarshallingInfoString<TargetOpts<"CodeModel">, [{"default"}]>; def mlarge_data_threshold_EQ : Joined<["-"], "mlarge-data-threshold=">, Group<m_Group>, Visibility<[ClangOption, CC1Option]>, - MarshallingInfoInt<TargetOpts<"LargeDataThreshold">, "65535">; + MarshallingInfoInt<TargetOpts<"LargeDataThreshold">, "0">; def mtls_size_EQ : Joined<["-"], "mtls-size=">, Group<m_Group>, Visibility<[ClangOption, CC1Option]>, HelpText<"Specify bit size of immediate TLS offsets (AArch64 ELF only): " diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ad6fc71c1e5038..0cfe7a0133b7e3 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1201,7 +1201,7 @@ void CodeGenModule::Release() { llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM); getModule().setCodeModel(codeModel); - if (CM == llvm::CodeModel::Medium && + if ((CM == llvm::CodeModel::Medium || CM == llvm::CodeModel::Large) && Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64) { getModule().setLargeDataThreshold(getCodeGenOpts().LargeDataThreshold); diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 1ee7ae602f3ce5..27a50b2c32e94d 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5752,20 +5752,29 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } } - if (Arg *A = Args.getLastArg(options::OPT_mlarge_data_threshold_EQ)) { - if (!Triple.isX86()) { - D.Diag(diag::err_drv_unsupported_opt_for_target) - << A->getOption().getName() << TripleStr; - } else { - bool IsMediumCM = false; - if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) - IsMediumCM = StringRef(A->getValue()) == "medium"; - if (!IsMediumCM) { + if (Triple.getArch() == llvm::Triple::x86_64) { + bool IsMediumCM = false; + bool IsLargeCM = false; + if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { + IsMediumCM = StringRef(A->getValue()) == "medium"; + IsLargeCM = StringRef(A->getValue()) == "large"; + } + if (Arg *A = Args.getLastArg(options::OPT_mlarge_data_threshold_EQ)) { + if (!IsMediumCM && !IsLargeCM) { D.Diag(diag::warn_drv_large_data_threshold_invalid_code_model) << A->getOption().getRenderName(); } else { A->render(Args, CmdArgs); } + } else if (IsMediumCM) { + CmdArgs.push_back("-mlarge-data-threshold=65536"); + } else if (IsLargeCM) { + CmdArgs.push_back("-mlarge-data-threshold=0"); + } + } else { + if (Arg *A = Args.getLastArg(options::OPT_mlarge_data_threshold_EQ)) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getOption().getName() << TripleStr; } } diff --git a/clang/test/CodeGen/large-data-threshold.c b/clang/test/CodeGen/large-data-threshold.c index 29ae19e9b71899..d110ad2125c7bc 100644 --- a/clang/test/CodeGen/large-data-threshold.c +++ b/clang/test/CodeGen/large-data-threshold.c @@ -2,11 +2,14 @@ // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=medium | FileCheck %s --check-prefix=IR-DEFAULT // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=medium -mlarge-data-threshold=200 | FileCheck %s --check-prefix=IR-CUSTOM +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=large -mlarge-data-threshold=200 | FileCheck %s --check-prefix=IR-CUSTOM +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -o - -mcmodel=small -mlarge-data-threshold=200 | FileCheck %s --check-prefix=IR-SMALL // RUN: %clang_cc1 -triple x86_64-unknown-unknown -S %s -o - -mcmodel=medium -mlarge-data-threshold=200 | FileCheck %s --check-prefix=ASM-SMALL // RUN: %clang_cc1 -triple x86_64-unknown-unknown -S %s -o - -mcmodel=medium -mlarge-data-threshold=2 | FileCheck %s --check-prefix=ASM-LARGE -// IR-DEFAULT: !{i32 1, !"Large Data Threshold", i64 65535} +// IR-DEFAULT: !{i32 1, !"Large Data Threshold", i64 0} // IR-CUSTOM: !{i32 1, !"Large Data Threshold", i64 200} +// IR-SMALL-NOT: !"Large Data Threshold" // ASM-SMALL-NOT: movabsq // ASM-LARGE: movabsq diff --git a/clang/test/Driver/large-data-threshold.c b/clang/test/Driver/large-data-threshold.c index 9914286701c6c1..dfc51349f7bf16 100644 --- a/clang/test/Driver/large-data-threshold.c +++ b/clang/test/Driver/large-data-threshold.c @@ -1,8 +1,14 @@ +// RUN: %clang --target=x86_64 -### -c -mcmodel=large -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARG %s // RUN: %clang --target=x86_64 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARG %s -// RUN: %clang --target=x86_64 -### -c -mcmodel=small -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=SMALL %s +// RUN: %clang --target=x86_64 -### -c -mcmodel=large %s 2>&1 | FileCheck --check-prefix=ARG-LARGE-DEFAULT %s +// RUN: %clang --target=x86_64 -### -c -mcmodel=medium %s 2>&1 | FileCheck --check-prefix=ARG-MEDIUM-DEFAULT %s +// 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 +// RUN: not %clang --target=x86 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARCH %s // RUN: not %clang --target=riscv32 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARCH %s // ARG: "-mlarge-data-threshold=200" +// ARG-MEDIUM-DEFAULT: "-mlarge-data-threshold=65536" +// ARG-LARGE-DEFAULT: "-mlarge-data-threshold=0" -// SMALL: 'mlarge-data-threshold=' only applies to medium code model -// ARCH: unsupported option 'mlarge-data-threshold=' for target 'riscv32' +// SMALL: 'mlarge-data-threshold=' only applies to medium and large code models +// ARCH: unsupported option 'mlarge-data-threshold=' for target >From 9055153aa1e19724510df01d66de029892c08711 Mon Sep 17 00:00:00 2001 From: Arthur Eubanks <aeuba...@google.com> Date: Fri, 12 Jan 2024 19:14:09 +0000 Subject: [PATCH 2/2] fixups `Flags<[TargetSpecific]>` remove x86-32 test since the target doesn't accept mcmodel=medium anyway --- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/ToolChains/Clang.cpp | 5 ----- clang/test/Driver/large-data-threshold.c | 3 +-- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index e8afa1ea4126d8..c237d1f6619348 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4392,7 +4392,7 @@ def mcmodel_EQ : Joined<["-"], "mcmodel=">, Group<m_Group>, Visibility<[ClangOption, CC1Option]>, MarshallingInfoString<TargetOpts<"CodeModel">, [{"default"}]>; def mlarge_data_threshold_EQ : Joined<["-"], "mlarge-data-threshold=">, Group<m_Group>, - Visibility<[ClangOption, CC1Option]>, + Flags<[TargetSpecific]>, Visibility<[ClangOption, CC1Option]>, MarshallingInfoInt<TargetOpts<"LargeDataThreshold">, "0">; def mtls_size_EQ : Joined<["-"], "mtls-size=">, Group<m_Group>, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 27a50b2c32e94d..9edae3fec91a87 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5771,11 +5771,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } else if (IsLargeCM) { CmdArgs.push_back("-mlarge-data-threshold=0"); } - } else { - if (Arg *A = Args.getLastArg(options::OPT_mlarge_data_threshold_EQ)) { - D.Diag(diag::err_drv_unsupported_opt_for_target) - << A->getOption().getName() << TripleStr; - } } if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { diff --git a/clang/test/Driver/large-data-threshold.c b/clang/test/Driver/large-data-threshold.c index dfc51349f7bf16..6500edc0fc0ef2 100644 --- a/clang/test/Driver/large-data-threshold.c +++ b/clang/test/Driver/large-data-threshold.c @@ -3,7 +3,6 @@ // RUN: %clang --target=x86_64 -### -c -mcmodel=large %s 2>&1 | FileCheck --check-prefix=ARG-LARGE-DEFAULT %s // RUN: %clang --target=x86_64 -### -c -mcmodel=medium %s 2>&1 | FileCheck --check-prefix=ARG-MEDIUM-DEFAULT %s // 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 -// RUN: not %clang --target=x86 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARCH %s // RUN: not %clang --target=riscv32 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARCH %s // ARG: "-mlarge-data-threshold=200" @@ -11,4 +10,4 @@ // ARG-LARGE-DEFAULT: "-mlarge-data-threshold=0" // SMALL: 'mlarge-data-threshold=' only applies to medium and large code models -// ARCH: unsupported option 'mlarge-data-threshold=' for target +// ARCH: unsupported option '-mlarge-data-threshold=' for target _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits