From a42787d10854fe2fdaca019634aed03adebcb864 Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Tue, 26 Sep 2023 09:44:31 -0700 Subject: [PATCH] [clang] Add -mlarge-data-threshold for x86_64 medium code model (#66839) Error if not used with x86_64. Warn if not used with the medium code model (can update if other code models end up using this). Set TargetMachine option and add module flag. --- clang/include/clang/Basic/CodeGenOptions.h | 4 ++++ .../clang/Basic/DiagnosticDriverKinds.td | 3 +++ clang/include/clang/Basic/TargetOptions.h | 4 ++++ clang/include/clang/Driver/Options.td | 3 +++ clang/lib/CodeGen/BackendUtil.cpp | 1 + clang/lib/CodeGen/CodeGenModule.cpp | 6 ++++++ clang/lib/Driver/ToolChains/Clang.cpp | 17 +++++++++++++++++ clang/lib/Frontend/CompilerInvocation.cpp | 1 + clang/test/CodeGen/large-data-threshold.c | 19 +++++++++++++++++++ clang/test/Driver/large-data-threshold.c | 8 ++++++++ 10 files changed, 66 insertions(+) create mode 100644 clang/test/CodeGen/large-data-threshold.c create mode 100644 clang/test/Driver/large-data-threshold.c diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 08d4c103ce245..12be4e0025a70 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -174,6 +174,10 @@ class CodeGenOptions : public CodeGenOptionsBase { /// The code model to use (-mcmodel). std::string CodeModel; + /// The code model-specific large data threshold to use + /// (-mlarge-data-threshold). + uint64_t LargeDataThreshold; + /// The filename with path we use for coverage data files. The runtime /// allows further manipulation with the GCOV_PREFIX and GCOV_PREFIX_STRIP /// environment variables. diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 30d8f05dc92f9..2a48c063e243e 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -456,6 +456,9 @@ def warn_unsupported_branch_protection: Warning < "invalid branch protection option '%0' in '%1'">, InGroup; 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">, + InGroup; def note_drv_command_failed_diag_msg : Note< "diagnostic msg: %0">; diff --git a/clang/include/clang/Basic/TargetOptions.h b/clang/include/clang/Basic/TargetOptions.h index 0dc324ff9ed43..8bb03249b7f83 100644 --- a/clang/include/clang/Basic/TargetOptions.h +++ b/clang/include/clang/Basic/TargetOptions.h @@ -109,6 +109,10 @@ class TargetOptions { // code model. std::string CodeModel; + // The large data threshold used for certain code models on certain + // architectures. + uint64_t LargeDataThreshold; + /// The version of the SDK which was used during the compilation. /// The option is used for two different purposes: /// * on darwin the version is propagated to LLVM where it's used diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 0f93479170d73..f573f5a06ceb5 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4248,6 +4248,9 @@ def inline_asm_EQ : Joined<["-"], "inline-asm=">, Group, def mcmodel_EQ : Joined<["-"], "mcmodel=">, Group, Visibility<[ClangOption, CC1Option]>, MarshallingInfoString, [{"default"}]>; +def mlarge_data_threshold_EQ : Joined<["-"], "mlarge-data-threshold=">, Group, + Visibility<[ClangOption, CC1Option]>, + MarshallingInfoInt>; def mtls_size_EQ : Joined<["-"], "mtls-size=">, Group, Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>, HelpText<"Specify bit size of immediate TLS offsets (AArch64 ELF only): " diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index e2afc5d566281..ccaf1e97e8e1b 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -572,6 +572,7 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { return; TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, Options, RM, CM, OptLevel)); + TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold); } bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses, diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 130cedcd4f2bf..25945759e23f2 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1146,6 +1146,12 @@ void CodeGenModule::Release() { if (CM != ~0u) { llvm::CodeModel::Model codeModel = static_cast(CM); getModule().setCodeModel(codeModel); + + if (CM == llvm::CodeModel::Medium && + 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 c02b58a9e067d..dda6aef641904 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5714,6 +5714,23 @@ 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) { + D.Diag(diag::warn_drv_large_data_threshold_invalid_code_model) + << A->getOption().getRenderName(); + } else { + A->render(Args, CmdArgs); + } + } + } + if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { StringRef Value = A->getValue(); unsigned TLSSize = 0; diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 9293b910c57d6..f2fe9046a6c1f 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -574,6 +574,7 @@ static bool FixupInvocation(CompilerInvocation &Invocation, llvm::Triple::ArchType Arch = T.getArch(); CodeGenOpts.CodeModel = TargetOpts.CodeModel; + CodeGenOpts.LargeDataThreshold = TargetOpts.LargeDataThreshold; if (LangOpts.getExceptionHandling() != LangOptions::ExceptionHandlingKind::None && diff --git a/clang/test/CodeGen/large-data-threshold.c b/clang/test/CodeGen/large-data-threshold.c new file mode 100644 index 0000000000000..650a7fbb0094e --- /dev/null +++ b/clang/test/CodeGen/large-data-threshold.c @@ -0,0 +1,19 @@ +// REQUIRES: x86-registered-target + +// 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 -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 0} +// IR-CUSTOM: !{i32 1, !"Large Data Threshold", i64 200} + +// ASM-SMALL-NOT: movabsq +// ASM-LARGE: movabsq + +static int i; + +int f() { + return i; +} + diff --git a/clang/test/Driver/large-data-threshold.c b/clang/test/Driver/large-data-threshold.c new file mode 100644 index 0000000000000..9914286701c6c --- /dev/null +++ b/clang/test/Driver/large-data-threshold.c @@ -0,0 +1,8 @@ +// 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: not %clang --target=riscv32 -### -c -mcmodel=medium -mlarge-data-threshold=200 %s 2>&1 | FileCheck --check-prefix=ARCH %s + +// ARG: "-mlarge-data-threshold=200" + +// SMALL: 'mlarge-data-threshold=' only applies to medium code model +// ARCH: unsupported option 'mlarge-data-threshold=' for target 'riscv32'