[clang] [PowerPC] Support local-dynamic TLS relocation on AIX (PR #66316)

Hubert Tong via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 3 16:11:28 PDT 2023


================
@@ -660,14 +671,16 @@ void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
          "GETtls[ld]ADDR[32] must read GPR3");
 
   if (Subtarget->isAIXABI()) {
-    // On AIX, the variable offset should already be in R4 and the region handle
-    // should already be in R3.
-    // For TLSGD, which currently is the only supported access model, we only
-    // need to generate an absolute branch to .__tls_get_addr.
+    // For TLSGD, the variable offset should already be in R4 and the region
+    // handle should already be in R3. We generate an absolute branch to
+    // .__tls_get_addr. For TLSLD, the module handle should already be in R3.
+    // We generate an absolute branch to .__tls_get_mod.
     Register VarOffsetReg = Subtarget->isPPC64() ? PPC::X4 : PPC::R4;
     (void)VarOffsetReg;
-    assert(MI->getOperand(2).isReg() &&
-           MI->getOperand(2).getReg() == VarOffsetReg &&
+    assert((MI->getOpcode() == PPC::GETtlsMOD32AIX ||
+            MI->getOpcode() == PPC::GETtlsMOD64AIX ||
+            (MI->getOperand(2).isReg() &&
+             MI->getOperand(2).getReg() == VarOffsetReg)) &&
            "GETtls[ld]ADDR[32] must read GPR4");
     EmitAIXTlsCallHelper(MI);
----------------
hubert-reinterpretcast wrote:

The helper functions have special calling convention properties. For example, they do not use the FP registers. The IBM XL compiler was able to take advantage of that.

For:
```c
__attribute__((tls_model("local-dynamic"))) __thread int x;
double g(int, double);
void f() {
  double gg = g(0, 1.);
  g(x, gg);
}
```

The IBM XL compilers were able to make use of the returned `double` staying in the register:
```
      28: 48 00 00 03   bla 0
                        00000028:  R_RBA        (idx: 36) .__tls_get_mod[PR]
      2c: 7c 66 18 2e   lwzx 3, 6, 3
      30: 4b ff ff d1   bl 0x0 <.f>
                        00000030:  R_RBR        (idx: 34) .g[PR]
```

Clang/LLVM loads the value from the stack:
```
      24: 48 00 00 03   bla 0
                0000000000000024:  R_RBA        (idx: 3) .__tls_get_mod[PR]
      28: e8 82 00 08   ld 4, 8(2)
                000000000000002a:  R_TOC        (idx: 17) x[TC]
      2c: 7c 63 22 aa   lwax 3, 3, 4
      30: 4b ff ff d1   bl 0x0 <.f>
                0000000000000030:  R_RBR        (idx: 1) .g[PR]
```

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


More information about the cfe-commits mailing list