[clang] [ARM] Using cp15 while mtp =auto and arch is arm_arch6k and support thumb2 (PR #130027)

Peter Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 6 02:49:58 PST 2025


================
@@ -240,7 +247,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 (isHardTPSupported(Triple) ? ReadTPMode::TPIDRURO : ReadTPMode::Soft);
+  return (isHardTPAndThumb2(Triple) ? ReadTPMode::TPIDRURO : ReadTPMode::Soft);
----------------
smithp35 wrote:

I think Vladi's comment is correct in that we need to exclude M-profile from `isHardTPSupported(Triple)`. The M-profile architecture does not have cp15 or any of the thread registers.

I agree that we don't want to use the hardware thread register automatically for v6k.

Using the source file:
```
__thread int x;

int* func(void) {
  return &x;
}
```
Then `clang --target=arm-none-eabi -S -O1 tls.c -o - -march=armv6k -mtp=cp15 -marm` will succeed as Arm state can access cp15` but if I add `-mthumb` then I get:
```
clang: error: hardware TLS register is not supported for the thumbv6k sub-architecture
```
Even worse if I sneak Thumb past clang with an attribute (compile with -marm)
```
__thread int x;

__attribute__((target("thumb"))) int* func(void) {
  return &x;
}
```
Then clang will crash.

Having two functions with duplicate logic is not ideal. What I suggest is to do something like:

return `(isHardTPSupported(Triple) && hasThumb2(Triple)) ?  ReadTPMode::TPIDRURO : ReadTPMode::Soft);`

where hasThumb2 will need to be written.



https://github.com/llvm/llvm-project/pull/130027


More information about the cfe-commits mailing list