From: LiaoChunyu Date: Fri, 3 Feb 2023 01:16:49 +0000 (+0800) Subject: [RISCV] Permit tail call to an externally-defined function with weak linkage X-Git-Tag: upstream/17.0.6~18752 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=41fdb4c53f44281e4c7e26c2571395b9fc692cc4;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp index 296a509..5ed580f 100644 --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -12745,7 +12745,6 @@ bool RISCVTargetLowering::isEligibleForTailCallOptimization( CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF, const SmallVector &ArgLocs) const { - auto &Callee = CLI.Callee; auto CalleeCC = CLI.CallConv; auto &Outs = CLI.Outs; auto &Caller = MF.getFunction(); @@ -12782,16 +12781,6 @@ bool RISCVTargetLowering::isEligibleForTailCallOptimization( if (IsCallerStructRet || IsCalleeStructRet) return false; - // Externally-defined functions with weak linkage should not be - // tail-called. The behaviour of branch instructions in this situation (as - // used for tail calls) is implementation-defined, so we cannot rely on the - // linker replacing the tail call with a return. - if (GlobalAddressSDNode *G = dyn_cast(Callee)) { - const GlobalValue *GV = G->getGlobal(); - if (GV->hasExternalWeakLinkage()) - return false; - } - // The callee has to preserve all registers the caller needs to preserve. const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo(); const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); diff --git a/llvm/test/CodeGen/RISCV/tail-calls.ll b/llvm/test/CodeGen/RISCV/tail-calls.ll index 54f8eac..e307942 100644 --- a/llvm/test/CodeGen/RISCV/tail-calls.ll +++ b/llvm/test/CodeGen/RISCV/tail-calls.ll @@ -109,15 +109,11 @@ entry: ret void } -; Externally-defined functions with weak linkage should not be tail-called. -; The behaviour of branch instructions in this situation (as used for tail -; calls) is implementation-defined, so we cannot rely on the linker replacing -; the tail call with a return. +; Perform tail call optimization for external weak symbol. declare extern_weak void @callee_weak() define void @caller_weak() nounwind { ; CHECK-LABEL: caller_weak -; CHECK-NOT: tail callee_weak -; CHECK: call callee_weak +; CHECK: tail callee_weak entry: tail call void @callee_weak() ret void