[llvm] [CodeGen] Don't codegen the weak function when there is a defined non-weak symbol (PR #76040)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 20 05:07:45 PST 2023
https://github.com/DianQK updated https://github.com/llvm/llvm-project/pull/76040
>From 50736d7f956aaad069d93c9665a9f8d40d99b259 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Tue, 19 Dec 2023 21:38:20 +0800
Subject: [PATCH 1/2] Pre-commit test cases
---
llvm/test/CodeGen/Thumb/asm-fn-weak.ll | 17 +++++++++++++++++
llvm/test/CodeGen/Thumb/asm-fn.ll | 16 ++++++++++++++++
llvm/test/CodeGen/Thumb/asm-weak-fn-weak.ll | 16 ++++++++++++++++
llvm/test/CodeGen/Thumb/asm-weak-fn.ll | 17 +++++++++++++++++
4 files changed, 66 insertions(+)
create mode 100644 llvm/test/CodeGen/Thumb/asm-fn-weak.ll
create mode 100644 llvm/test/CodeGen/Thumb/asm-fn.ll
create mode 100644 llvm/test/CodeGen/Thumb/asm-weak-fn-weak.ll
create mode 100644 llvm/test/CodeGen/Thumb/asm-weak-fn.ll
diff --git a/llvm/test/CodeGen/Thumb/asm-fn-weak.ll b/llvm/test/CodeGen/Thumb/asm-fn-weak.ll
new file mode 100644
index 00000000000000..308a1199ef9e2c
--- /dev/null
+++ b/llvm/test/CodeGen/Thumb/asm-fn-weak.ll
@@ -0,0 +1,17 @@
+; RUN: not llc -mtriple=thumbv6m-none-unknown-eabi < %s 2>&1 | FileCheck %s
+
+; CHECK: error: symbol '__aeabi_uidivmod' is already defined
+; FIXME: We want to discard the weak function.
+
+module asm ".global __aeabi_uidivmod"
+module asm ".type __aeabi_uidivmod, %function"
+module asm "__aeabi_uidivmod:"
+module asm "str r0, [r2, #0x060]"
+module asm "str r1, [r2, #0x064]"
+
+define weak void @__aeabi_uidivmod() #0 {
+ tail call void asm sideeffect alignstack "push {lr}\0Asub sp, sp, #4\0Amov r2, sp\0Abl __udivmodsi4\0Aldr r1, [sp]\0Aadd sp, sp, #4\0Apop {pc}", "~{cc},~{memory}"()
+ unreachable
+}
+
+attributes #0 = { naked }
diff --git a/llvm/test/CodeGen/Thumb/asm-fn.ll b/llvm/test/CodeGen/Thumb/asm-fn.ll
new file mode 100644
index 00000000000000..c6918b6e3ccb6d
--- /dev/null
+++ b/llvm/test/CodeGen/Thumb/asm-fn.ll
@@ -0,0 +1,16 @@
+; RUN: not llc -mtriple=thumbv6m-none-unknown-eabi < %s 2>&1 | FileCheck %s
+
+; CHECK: error: symbol '__aeabi_uidivmod' is already defined
+
+module asm ".global __aeabi_uidivmod"
+module asm ".type __aeabi_uidivmod, %function"
+module asm "__aeabi_uidivmod:"
+module asm "str r0, [r2, #0x060]"
+module asm "str r1, [r2, #0x064]"
+
+define void @__aeabi_uidivmod() #0 {
+ tail call void asm sideeffect alignstack "push {lr}\0Asub sp, sp, #4\0Amov r2, sp\0Abl __udivmodsi4\0Aldr r1, [sp]\0Aadd sp, sp, #4\0Apop {pc}", "~{cc},~{memory}"()
+ unreachable
+}
+
+attributes #0 = { naked }
diff --git a/llvm/test/CodeGen/Thumb/asm-weak-fn-weak.ll b/llvm/test/CodeGen/Thumb/asm-weak-fn-weak.ll
new file mode 100644
index 00000000000000..1a45ed55110a8a
--- /dev/null
+++ b/llvm/test/CodeGen/Thumb/asm-weak-fn-weak.ll
@@ -0,0 +1,16 @@
+; RUN: not llc -mtriple=thumbv6m-none-unknown-eabi < %s 2>&1 | FileCheck %s
+
+; CHECK: error: symbol '__aeabi_uidivmod' is already defined
+
+module asm ".weak __aeabi_uidivmod"
+module asm ".type __aeabi_uidivmod, %function"
+module asm "__aeabi_uidivmod:"
+module asm "str r0, [r2, #0x060]"
+module asm "str r1, [r2, #0x064]"
+
+define weak void @__aeabi_uidivmod() #0 {
+ tail call void asm sideeffect alignstack "push {lr}\0Asub sp, sp, #4\0Amov r2, sp\0Abl __udivmodsi4\0Aldr r1, [sp]\0Aadd sp, sp, #4\0Apop {pc}", "~{cc},~{memory}"()
+ unreachable
+}
+
+attributes #0 = { naked }
diff --git a/llvm/test/CodeGen/Thumb/asm-weak-fn.ll b/llvm/test/CodeGen/Thumb/asm-weak-fn.ll
new file mode 100644
index 00000000000000..24b915d0d4f00d
--- /dev/null
+++ b/llvm/test/CodeGen/Thumb/asm-weak-fn.ll
@@ -0,0 +1,17 @@
+; RUN: not llc -mtriple=thumbv6m-none-unknown-eabi < %s 2>&1 | FileCheck %s
+
+; CHECK: error: symbol '__aeabi_uidivmod' is already defined
+; FIXME: We want to discard the weak asm function.
+
+module asm ".weak __aeabi_uidivmod"
+module asm ".type __aeabi_uidivmod, %function"
+module asm "__aeabi_uidivmod:"
+module asm "str r0, [r2, #0x060]"
+module asm "str r1, [r2, #0x064]"
+
+define void @__aeabi_uidivmod() #0 {
+ tail call void asm sideeffect alignstack "push {lr}\0Asub sp, sp, #4\0Amov r2, sp\0Abl __udivmodsi4\0Aldr r1, [sp]\0Aadd sp, sp, #4\0Apop {pc}", "~{cc},~{memory}"()
+ unreachable
+}
+
+attributes #0 = { naked }
>From 80b0b0026babda30c641e489d37d902c1c0dbfa9 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Tue, 19 Dec 2023 21:43:50 +0800
Subject: [PATCH 2/2] [CodeGen] Don't codegen the weak function when there is a
defined non-weak symbol
---
llvm/include/llvm/MC/MCSymbol.h | 8 +++++++-
llvm/lib/CodeGen/MachineFunctionPass.cpp | 7 +++++++
llvm/lib/MC/MCParser/ELFAsmParser.cpp | 2 ++
llvm/test/CodeGen/Thumb/asm-fn-weak.ll | 11 +++++++----
4 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/llvm/include/llvm/MC/MCSymbol.h b/llvm/include/llvm/MC/MCSymbol.h
index 7be31223280273..aa30cab7bc09d2 100644
--- a/llvm/include/llvm/MC/MCSymbol.h
+++ b/llvm/include/llvm/MC/MCSymbol.h
@@ -104,6 +104,9 @@ class MCSymbol {
/// This symbol is weak external.
mutable unsigned IsWeakExternal : 1;
+ /// This symbol is weak.
+ mutable unsigned IsWeak : 1;
+
/// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
/// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
unsigned Kind : 3;
@@ -163,7 +166,7 @@ class MCSymbol {
MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
: IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
- IsWeakExternal(false), Kind(Kind), IsUsedInReloc(false),
+ IsWeakExternal(false), IsWeak(false), Kind(Kind), IsUsedInReloc(false),
SymbolContents(SymContentsUnset), CommonAlignLog2(0), Flags(0) {
Offset = 0;
HasName = !!Name;
@@ -411,6 +414,9 @@ class MCSymbol {
bool isWeakExternal() const { return IsWeakExternal; }
+ bool isWeak() const { return IsWeak; }
+ void setWeak(bool Value) const { IsWeak = Value; }
+
/// print - Print the value to the stream \p OS.
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
diff --git a/llvm/lib/CodeGen/MachineFunctionPass.cpp b/llvm/lib/CodeGen/MachineFunctionPass.cpp
index d57a912f418b72..668fb289a117d5 100644
--- a/llvm/lib/CodeGen/MachineFunctionPass.cpp
+++ b/llvm/lib/CodeGen/MachineFunctionPass.cpp
@@ -27,6 +27,7 @@
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/PrintPasses.h"
+#include "llvm/Target/TargetMachine.h"
using namespace llvm;
using namespace ore;
@@ -43,6 +44,12 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
return false;
MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
+ const TargetMachine &TM = MMI.getTarget();
+ MCSymbol *Symbol = TM.getSymbol(&F);
+ // Don't codegen the weak function when there is a defined non-weak symbol.
+ if (Symbol->isDefined() && !Symbol->isWeak() && F.hasWeakLinkage()) {
+ return false;
+ }
MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
MachineFunctionProperties &MFProps = MF.getProperties();
diff --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
index 93e1d2f44b8c56..a27bfe879f2075 100644
--- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp
+++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
@@ -189,6 +189,8 @@ bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
+ if (Attr == MCSA_Weak)
+ Sym->setWeak(true);
getStreamer().emitSymbolAttribute(Sym, Attr);
if (getLexer().is(AsmToken::EndOfStatement))
diff --git a/llvm/test/CodeGen/Thumb/asm-fn-weak.ll b/llvm/test/CodeGen/Thumb/asm-fn-weak.ll
index 308a1199ef9e2c..421db2e8a7f421 100644
--- a/llvm/test/CodeGen/Thumb/asm-fn-weak.ll
+++ b/llvm/test/CodeGen/Thumb/asm-fn-weak.ll
@@ -1,14 +1,17 @@
-; RUN: not llc -mtriple=thumbv6m-none-unknown-eabi < %s 2>&1 | FileCheck %s
-
-; CHECK: error: symbol '__aeabi_uidivmod' is already defined
-; FIXME: We want to discard the weak function.
+; RUN: llc -mtriple=thumbv6m-none-unknown-eabi < %s | FileCheck %s
+; CHECK: .globl __aeabi_uidivmod
+; CHECK-NEXT: .type __aeabi_uidivmod,%function
+; CHECK-NEXT: __aeabi_uidivmod:
+; CHECK-NEXT: str r0, [r2, #96]
+; CHECK-NEXT: str r1, [r2, #100]
module asm ".global __aeabi_uidivmod"
module asm ".type __aeabi_uidivmod, %function"
module asm "__aeabi_uidivmod:"
module asm "str r0, [r2, #0x060]"
module asm "str r1, [r2, #0x064]"
+; CHECK-NOT: __aeabi_uidivmod
define weak void @__aeabi_uidivmod() #0 {
tail call void asm sideeffect alignstack "push {lr}\0Asub sp, sp, #4\0Amov r2, sp\0Abl __udivmodsi4\0Aldr r1, [sp]\0Aadd sp, sp, #4\0Apop {pc}", "~{cc},~{memory}"()
unreachable
More information about the llvm-commits
mailing list