Skip to content

[CodeGen] Don't codegen the weak function when there is a defined non-weak symbol #76040

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 7 additions & 1 deletion llvm/include/llvm/MC/MCSymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/CodeGen/MachineFunctionPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/MC/MCParser/ELFAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/Thumb/asm-fn-weak.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; 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
}

attributes #0 = { naked }
16 changes: 16 additions & 0 deletions llvm/test/CodeGen/Thumb/asm-fn.ll
Original file line number Diff line number Diff line change
@@ -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 }
16 changes: 16 additions & 0 deletions llvm/test/CodeGen/Thumb/asm-weak-fn-weak.ll
Original file line number Diff line number Diff line change
@@ -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 }
17 changes: 17 additions & 0 deletions llvm/test/CodeGen/Thumb/asm-weak-fn.ll
Original file line number Diff line number Diff line change
@@ -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 }