[llvm] [PowerPC][AIX] Emit weak_definition symbols as weak externals (PR #156072)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 29 10:42:26 PDT 2025


https://github.com/aabhinavg1 created https://github.com/llvm/llvm-project/pull/156072

- On AIX, the assembler does not recognize '.weak_definition'.
- Emit **MCSA_WeakDefinition** symbols using the standard weak directive (.weak) and set storage class to **C_WEAKEXT** in XCOFF.
- Added test llvm/test/MC/PowerPC/aix-weak-definition.s
Fixes #130269 


Fixes #130269

Testing:
- command 
```python
ninja check-llvm-codegen-powerpc
````
```rust
===== Before Patch =====

[779/780] Running lit suite /llvm/test/CodeGen/PowerPC
Testing Time: 44.38s
Total Discovered Tests: 1952
  Unsupported      :   38 (1.95%)
  Passed           : 1907 (97.69%)
  Expectedly Failed:    7 (0.36%)
```
```rust

===== After Patch =====
[779/780] Running lit suite /llvm/test/CodeGen/PowerPC
Testing Time: 120.31s
Total Discovered Tests: 1952
  Unsupported      :    3 (0.15%)
  Passed           : 1941 (99.44%)
  Expectedly Failed:    8 (0.41%)
```



**Verification:**

```rust
$ ../bin/clang -target powerpc64-ibm-aix -S test.c -o test.s
$ cat test.s

    .file   "test.c",,"LLVM version 22.0.0git ..."
    .csect ..text..[PR],5
    .rename ..text..[PR],""
    # Start of file scope inline assembly
    .weak   foo
foo:
    blr
    # End of file scope inline assembly
    .machine    "PWR7"
```

**Input C code:**

```c
__asm__(".weak_definition foo\n"
        "foo:\n"
        "blr");
```

**Outcome:**

* `.weak_definition` in the source is correctly mapped to `.weak` in the assembly on AIX.
* Confirms the patch fixes the AIX assembler issue without affecting other targets.






>From 43fbfa2074f6631b65fcce8dd5e8cee513004e79 Mon Sep 17 00:00:00 2001
From: aabhinavg1 <tiwariabhinavak at gmail.com>
Date: Fri, 29 Aug 2025 22:50:27 +0530
Subject: [PATCH] [PowerPC][AIX] Emit weak_definition symbols as weak externals

- On AIX, the assembler does not recognize '.weak_definition'.
- Emit MCSA_WeakDefinition symbols using the standard weak directive (.weak)
  and set storage class to C_WEAKEXT in XCOFF.
- Added test llvm/test/MC/PowerPC/aix-weak-definition.s
---
 llvm/lib/MC/MCAsmStreamer.cpp              | 14 +++++++++++---
 llvm/lib/MC/MCXCOFFStreamer.cpp            |  7 +++++++
 llvm/test/MC/PowerPC/aix-weak-definition.s | 13 +++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/MC/PowerPC/aix-weak-definition.s

diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index be8c022f39ad1..7b1a1878915bc 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -764,10 +764,18 @@ bool MCAsmStreamer::emitSymbolAttribute(MCSymbol *Symbol,
     OS << "\t.extern\t";
     break;
   case MCSA_Weak:           OS << MAI->getWeakDirective(); break;
-  case MCSA_WeakDefinition:
-    OS << "\t.weak_definition\t";
+  case MCSA_WeakDefinition: {
+    // AIX, use the standard weak directive (.weak) instead of
+    //'.weak_definition' because the AIX assembler does not
+    // recognize the '.weak_definition' directive.
+    const llvm::Triple &TT = getContext().getTargetTriple();
+    if (TT.isOSAIX())
+      OS << MAI->getWeakDirective();
+    else
+      OS << "\t.weak_definition\t";
     break;
-      // .weak_reference
+  }
+    // .weak_reference
   case MCSA_WeakReference:  OS << MAI->getWeakRefDirective(); break;
   case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
   case MCSA_Cold:
diff --git a/llvm/lib/MC/MCXCOFFStreamer.cpp b/llvm/lib/MC/MCXCOFFStreamer.cpp
index 4bf14c11068cb..cd5398dc7bc3b 100644
--- a/llvm/lib/MC/MCXCOFFStreamer.cpp
+++ b/llvm/lib/MC/MCXCOFFStreamer.cpp
@@ -72,6 +72,13 @@ bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
     Symbol->setStorageClass(XCOFF::C_WEAKEXT);
     Symbol->setExternal(true);
     break;
+  case llvm::MCSA_WeakDefinition:
+    // On AIX/XCOFF, a weak definition symbol should be emitted
+    // as an external weak symbol (C_WEAKEXT), since the assembler
+    // does not support '.weak_definition' directly.
+    Symbol->setStorageClass(XCOFF::C_WEAKEXT);
+    Symbol->setExternal(true);
+    break;
   case llvm::MCSA_Hidden:
     Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
     break;
diff --git a/llvm/test/MC/PowerPC/aix-weak-definition.s b/llvm/test/MC/PowerPC/aix-weak-definition.s
new file mode 100644
index 0000000000000..6bab51b91c63d
--- /dev/null
+++ b/llvm/test/MC/PowerPC/aix-weak-definition.s
@@ -0,0 +1,13 @@
+## Check that weak_definition symbols are emitted as weak externals.
+# Note: On AIX, .weak_definition is mapped to .weak by LLVM's backend.
+# RUN: llvm-mc -triple powerpc-ibm-aix-xcoff %s -filetype=obj -o - | \
+# RUN:   llvm-objdump --syms - | FileCheck %s
+
+        .weak_definition foo  # LLVM IR WeakDefinition → .weak on AIX
+foo:
+        blr
+
+# CHECK:      SYMBOL TABLE:
+# CHECK-NEXT: 00000000      df *DEBUG*    00000000 .file
+# CHECK-NEXT: 00000000 l       .text    00000004
+# CHECK-NEXT: 00000000 w     F .text (csect: )  00000000 foo



More information about the llvm-commits mailing list