Skip to content

Commit 80b0b00

Browse files
committed
[CodeGen] Don't codegen the weak function when there is a defined non-weak symbol
1 parent 50736d7 commit 80b0b00

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

llvm/include/llvm/MC/MCSymbol.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class MCSymbol {
104104
/// This symbol is weak external.
105105
mutable unsigned IsWeakExternal : 1;
106106

107+
/// This symbol is weak.
108+
mutable unsigned IsWeak : 1;
109+
107110
/// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
108111
/// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
109112
unsigned Kind : 3;
@@ -163,7 +166,7 @@ class MCSymbol {
163166
MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
164167
: IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
165168
IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
166-
IsWeakExternal(false), Kind(Kind), IsUsedInReloc(false),
169+
IsWeakExternal(false), IsWeak(false), Kind(Kind), IsUsedInReloc(false),
167170
SymbolContents(SymContentsUnset), CommonAlignLog2(0), Flags(0) {
168171
Offset = 0;
169172
HasName = !!Name;
@@ -411,6 +414,9 @@ class MCSymbol {
411414

412415
bool isWeakExternal() const { return IsWeakExternal; }
413416

417+
bool isWeak() const { return IsWeak; }
418+
void setWeak(bool Value) const { IsWeak = Value; }
419+
414420
/// print - Print the value to the stream \p OS.
415421
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
416422

llvm/lib/CodeGen/MachineFunctionPass.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/IR/Dominators.h"
2828
#include "llvm/IR/Function.h"
2929
#include "llvm/IR/PrintPasses.h"
30+
#include "llvm/Target/TargetMachine.h"
3031

3132
using namespace llvm;
3233
using namespace ore;
@@ -43,6 +44,12 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
4344
return false;
4445

4546
MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
47+
const TargetMachine &TM = MMI.getTarget();
48+
MCSymbol *Symbol = TM.getSymbol(&F);
49+
// Don't codegen the weak function when there is a defined non-weak symbol.
50+
if (Symbol->isDefined() && !Symbol->isWeak() && F.hasWeakLinkage()) {
51+
return false;
52+
}
4653
MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
4754

4855
MachineFunctionProperties &MFProps = MF.getProperties();

llvm/lib/MC/MCParser/ELFAsmParser.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
189189

190190
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
191191

192+
if (Attr == MCSA_Weak)
193+
Sym->setWeak(true);
192194
getStreamer().emitSymbolAttribute(Sym, Attr);
193195

194196
if (getLexer().is(AsmToken::EndOfStatement))

llvm/test/CodeGen/Thumb/asm-fn-weak.ll

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
; RUN: not llc -mtriple=thumbv6m-none-unknown-eabi < %s 2>&1 | FileCheck %s
2-
3-
; CHECK: error: symbol '__aeabi_uidivmod' is already defined
4-
; FIXME: We want to discard the weak function.
1+
; RUN: llc -mtriple=thumbv6m-none-unknown-eabi < %s | FileCheck %s
52

3+
; CHECK: .globl __aeabi_uidivmod
4+
; CHECK-NEXT: .type __aeabi_uidivmod,%function
5+
; CHECK-NEXT: __aeabi_uidivmod:
6+
; CHECK-NEXT: str r0, [r2, #96]
7+
; CHECK-NEXT: str r1, [r2, #100]
68
module asm ".global __aeabi_uidivmod"
79
module asm ".type __aeabi_uidivmod, %function"
810
module asm "__aeabi_uidivmod:"
911
module asm "str r0, [r2, #0x060]"
1012
module asm "str r1, [r2, #0x064]"
1113

14+
; CHECK-NOT: __aeabi_uidivmod
1215
define weak void @__aeabi_uidivmod() #0 {
1316
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}"()
1417
unreachable

0 commit comments

Comments
 (0)