Skip to content

[ARM] Introduce -mtp=auto and make it the default #128901

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
Mar 3, 2025
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
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ Deprecated Compiler Flags
Modified Compiler Flags
-----------------------

- The ARM AArch32 ``-mtp`` option accepts and defaults to ``auto``, a value of ``auto`` uses the best available method of providing the frame pointer supported by the hardware. This matches
the behavior of ``-mtp`` in gcc. This changes the default behavior for ARM targets that provide the ``TPIDRURO`` register as this will be used instead of a call to the ``__aeabi_read_tp``.
Programs that use ``__aeabi_read_tp`` but do not use the ``TPIDRURO`` register must use ``-mtp=soft``. Fixes #123864

Removed Compiler Flags
-------------------------

Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -4729,7 +4729,7 @@ def mexecute_only : Flag<["-"], "mexecute-only">, Group<m_arm_Features_Group>,
def mno_execute_only : Flag<["-"], "mno-execute-only">, Group<m_arm_Features_Group>,
HelpText<"Allow generation of data access to code sections (ARM only)">;
let Flags = [TargetSpecific] in {
def mtp_mode_EQ : Joined<["-"], "mtp=">, Group<m_arm_Features_Group>, Values<"soft,cp15,tpidrurw,tpidruro,tpidrprw,el0,el1,el2,el3,tpidr_el0,tpidr_el1,tpidr_el2,tpidr_el3,tpidrro_el0">,
def mtp_mode_EQ : Joined<["-"], "mtp=">, Group<m_arm_Features_Group>, Values<"soft,cp15,tpidrurw,tpidruro,tpidrprw,el0,el1,el2,el3,tpidr_el0,tpidr_el1,tpidr_el2,tpidr_el3,tpidrro_el0,auto">,
HelpText<"Thread pointer access method. "
"For AArch32: 'soft' uses a function call, or 'tpidrurw', 'tpidruro' or 'tpidrprw' use the three CP15 registers. 'cp15' is an alias for 'tpidruro'. "
"For AArch64: 'tpidr_el0', 'tpidr_el1', 'tpidr_el2', 'tpidr_el3' or 'tpidrro_el0' use the five system registers. 'elN' is an alias for 'tpidr_elN'.">;
Expand Down
15 changes: 9 additions & 6 deletions clang/lib/Driver/ToolChains/Arch/ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ bool arm::isHardTPSupported(const llvm::Triple &Triple) {
// Select mode for reading thread pointer (-mtp=soft/cp15).
arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args,
const llvm::Triple &Triple, bool ForAS) {
if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ);
if (A && A->getValue() != StringRef("auto")) {
arm::ReadTPMode ThreadPointer =
llvm::StringSwitch<arm::ReadTPMode>(A->getValue())
.Case("cp15", ReadTPMode::TPIDRURO)
Expand All @@ -239,7 +240,7 @@ arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args,
D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
return ReadTPMode::Invalid;
}
return ReadTPMode::Soft;
return (isHardTPSupported(Triple) ? ReadTPMode::TPIDRURO : ReadTPMode::Soft);
}

void arm::setArchNameInTriple(const Driver &D, const ArgList &Args,
Expand Down Expand Up @@ -574,12 +575,14 @@ llvm::ARM::FPUKind arm::getARMTargetFeatures(const Driver &D,
A->ignoreTargetSpecific();
}

if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::TPIDRURW)
arm::ReadTPMode TPMode = getReadTPMode(D, Args, Triple, ForAS);

if (TPMode == ReadTPMode::TPIDRURW)
Features.push_back("+read-tp-tpidrurw");
if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::TPIDRURO)
Features.push_back("+read-tp-tpidruro");
if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::TPIDRPRW)
else if (TPMode == ReadTPMode::TPIDRPRW)
Features.push_back("+read-tp-tpidrprw");
else if (TPMode == ReadTPMode::TPIDRURO)
Features.push_back("+read-tp-tpidruro");

const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ);
const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
Expand Down
6 changes: 5 additions & 1 deletion clang/test/Driver/arm-thread-pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@

// RUN: %clang --target=armv7-linux -### -S %s 2>&1 | \
// RUN: FileCheck -check-prefix=ARMv7_THREAD_POINTER_NON %s
// ARMv7_THREAD_POINTER_NON-NOT: "-target-feature" "+read-tp-tpidruro"
// ARMv7_THREAD_POINTER_NON: "-target-feature" "+read-tp-tpidruro"

// RUN: %clang --target=armv7-linux -mtp=auto -### -S %s 2>&1 | \
// RUN: FileCheck -check-prefix=ARMv7_THREAD_POINTER_Auto %s
// ARMv7_THREAD_POINTER_Auto: "-target-feature" "+read-tp-tpidruro"