Skip to content

Commit 41fdb4c

Browse files
committed
[RISCV] Permit tail call to an externally-defined function with weak linkage
As described in D45395 `This has been modeled after ARM's tail call opt.` ARM's abi seems to limit weak symbol. I did not find the limitation for RISCV. (Please correct me if I am wrong) gcc seems to use the tail-call opt: https://godbolt.org/z/bjWE68n5o Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D143137
1 parent eba0bcc commit 41fdb4c

File tree

2 files changed

+2
-17
lines changed

2 files changed

+2
-17
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12745,7 +12745,6 @@ bool RISCVTargetLowering::isEligibleForTailCallOptimization(
1274512745
CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
1274612746
const SmallVector<CCValAssign, 16> &ArgLocs) const {
1274712747

12748-
auto &Callee = CLI.Callee;
1274912748
auto CalleeCC = CLI.CallConv;
1275012749
auto &Outs = CLI.Outs;
1275112750
auto &Caller = MF.getFunction();
@@ -12782,16 +12781,6 @@ bool RISCVTargetLowering::isEligibleForTailCallOptimization(
1278212781
if (IsCallerStructRet || IsCalleeStructRet)
1278312782
return false;
1278412783

12785-
// Externally-defined functions with weak linkage should not be
12786-
// tail-called. The behaviour of branch instructions in this situation (as
12787-
// used for tail calls) is implementation-defined, so we cannot rely on the
12788-
// linker replacing the tail call with a return.
12789-
if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
12790-
const GlobalValue *GV = G->getGlobal();
12791-
if (GV->hasExternalWeakLinkage())
12792-
return false;
12793-
}
12794-
1279512784
// The callee has to preserve all registers the caller needs to preserve.
1279612785
const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
1279712786
const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);

llvm/test/CodeGen/RISCV/tail-calls.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,11 @@ entry:
109109
ret void
110110
}
111111

112-
; Externally-defined functions with weak linkage should not be tail-called.
113-
; The behaviour of branch instructions in this situation (as used for tail
114-
; calls) is implementation-defined, so we cannot rely on the linker replacing
115-
; the tail call with a return.
112+
; Perform tail call optimization for external weak symbol.
116113
declare extern_weak void @callee_weak()
117114
define void @caller_weak() nounwind {
118115
; CHECK-LABEL: caller_weak
119-
; CHECK-NOT: tail callee_weak
120-
; CHECK: call callee_weak
116+
; CHECK: tail callee_weak
121117
entry:
122118
tail call void @callee_weak()
123119
ret void

0 commit comments

Comments
 (0)