Skip to content

[clang] Adjust -mlarge-data-threshold handling #77958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -4392,8 +4392,8 @@ 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]>,
MarshallingInfoInt<TargetOpts<"LargeDataThreshold">, "65535">;
Flags<[TargetSpecific]>, Visibility<[ClangOption, CC1Option]>,
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): "
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 13 additions & 9 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5752,20 +5752,24 @@ 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");
}
}

Expand Down
5 changes: 4 additions & 1 deletion clang/test/CodeGen/large-data-threshold.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions clang/test/Driver/large-data-threshold.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// 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=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