[PATCH] D143137: [RISCV] Permit tail call to an externally-defined function with weak linkage

Liao Chunyu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 1 22:12:32 PST 2023


liaolucy created this revision.
liaolucy added reviewers: kito-cheng, asb, jrtc27, craig.topper, MaskRay.
Herald added subscribers: luke, VincentWu, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, shiva0217, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, kristof.beyls, arichardson.
Herald added a project: All.
liaolucy requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD.
Herald added a project: LLVM.

As described in D45395 <https://reviews.llvm.org/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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143137

Files:
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/test/CodeGen/RISCV/tail-calls.ll


Index: llvm/test/CodeGen/RISCV/tail-calls.ll
===================================================================
--- llvm/test/CodeGen/RISCV/tail-calls.ll
+++ llvm/test/CodeGen/RISCV/tail-calls.ll
@@ -109,15 +109,11 @@
   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
Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -12745,7 +12745,6 @@
     CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
     const SmallVector<CCValAssign, 16> &ArgLocs) const {
 
-  auto &Callee = CLI.Callee;
   auto CalleeCC = CLI.CallConv;
   auto &Outs = CLI.Outs;
   auto &Caller = MF.getFunction();
@@ -12782,16 +12781,6 @@
   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<GlobalAddressSDNode>(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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143137.494166.patch
Type: text/x-patch
Size: 2068 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230202/be9ecf70/attachment.bin>


More information about the llvm-commits mailing list