Skip to content

Commit 615b7ee

Browse files
committed
Reapply "[LLVM][LTO] Factor out RTLib calls and allow them to be dropped (#98512)"
This reverts commit 740161a. I moved the `ISD` dependencies into the CodeGen portion of the handling, it's a little awkward but it's the easiest solution I can think of for now.
1 parent 6395603 commit 615b7ee

36 files changed

+566
-462
lines changed

lld/COFF/Driver.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,9 +2428,12 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
24282428
// file's symbol table. If any of those library functions are defined in a
24292429
// bitcode file in an archive member, we need to arrange to use LTO to
24302430
// compile those archive members by adding them to the link beforehand.
2431-
if (!ctx.bitcodeFileInstances.empty())
2432-
for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
2431+
if (!ctx.bitcodeFileInstances.empty()) {
2432+
llvm::Triple TT(
2433+
ctx.bitcodeFileInstances.front()->obj->getTargetTriple());
2434+
for (auto *s : lto::LTO::getRuntimeLibcallSymbols(TT))
24332435
ctx.symtab.addLibcall(s);
2436+
}
24342437

24352438
// Windows specific -- if __load_config_used can be resolved, resolve it.
24362439
if (ctx.symtab.findUnderscore("_load_config_used"))

lld/ELF/Driver.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,9 +2884,11 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
28842884
// to, i.e. if the symbol's definition is in bitcode. Any other required
28852885
// libcall symbols will be added to the link after LTO when we add the LTO
28862886
// object file to the link.
2887-
if (!ctx.bitcodeFiles.empty())
2888-
for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
2887+
if (!ctx.bitcodeFiles.empty()) {
2888+
llvm::Triple TT(ctx.bitcodeFiles.front()->obj->getTargetTriple());
2889+
for (auto *s : lto::LTO::getRuntimeLibcallSymbols(TT))
28892890
handleLibcall(s);
2891+
}
28902892

28912893
// Archive members defining __wrap symbols may be extracted.
28922894
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);

lld/wasm/Driver.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,9 +1320,11 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
13201320
// We only need to add libcall symbols to the link before LTO if the symbol's
13211321
// definition is in bitcode. Any other required libcall symbols will be added
13221322
// to the link after LTO when we add the LTO object file to the link.
1323-
if (!ctx.bitcodeFiles.empty())
1324-
for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
1323+
if (!ctx.bitcodeFiles.empty()) {
1324+
llvm::Triple TT(ctx.bitcodeFiles.front()->obj->getTargetTriple());
1325+
for (auto *s : lto::LTO::getRuntimeLibcallSymbols(TT))
13251326
handleLibcall(s);
1327+
}
13261328
if (errorCount())
13271329
return;
13281330

llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
2424
#include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
25-
#include "llvm/CodeGen/RuntimeLibcalls.h"
25+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
2626
#include "llvm/CodeGen/TargetOpcodes.h"
2727

2828
namespace llvm {

llvm/include/llvm/CodeGen/RuntimeLibcalls.h renamed to llvm/include/llvm/CodeGen/RuntimeLibcallUtil.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
1-
//===-- CodeGen/RuntimeLibcalls.h - Runtime Library Calls -------*- C++ -*-===//
1+
//===-- CodeGen/RuntimeLibcallUtil.h - Runtime Library Calls ----*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file defines the enum representing the list of runtime library calls
10-
// the backend may emit during code generation, and also some helper functions.
9+
// This file defines some helper functions for runtime library calls.
1110
//
1211
//===----------------------------------------------------------------------===//
1312

1413
#ifndef LLVM_CODEGEN_RUNTIMELIBCALLS_H
1514
#define LLVM_CODEGEN_RUNTIMELIBCALLS_H
1615

16+
#include "llvm/CodeGen/ISDOpcodes.h"
1717
#include "llvm/CodeGen/ValueTypes.h"
18+
#include "llvm/IR/RuntimeLibcalls.h"
1819
#include "llvm/Support/AtomicOrdering.h"
1920

2021
namespace llvm {
2122
namespace RTLIB {
22-
/// RTLIB::Libcall enum - This enum defines all of the runtime library calls
23-
/// the backend can emit. The various long double types cannot be merged,
24-
/// because 80-bit library functions use "xf" and 128-bit use "tf".
25-
///
26-
/// When adding PPCF128 functions here, note that their names generally need
27-
/// to be overridden for Darwin with the xxx$LDBL128 form. See
28-
/// PPCISelLowering.cpp.
29-
///
30-
enum Libcall {
31-
#define HANDLE_LIBCALL(code, name) code,
32-
#include "llvm/IR/RuntimeLibcalls.def"
33-
#undef HANDLE_LIBCALL
34-
};
3523

3624
/// GetFPLibCall - Helper to return the right libcall for the given floating
3725
/// point type, or UNKNOWN_LIBCALL if there is none.
@@ -103,6 +91,9 @@ Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize);
10391
/// UNKNOW_LIBCALL if there is none.
10492
Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize);
10593

94+
/// Initialize the default condition code on the libcalls.
95+
void initCmpLibcallCCs(ISD::CondCode *CmpLibcallCCs);
96+
10697
} // namespace RTLIB
10798
} // namespace llvm
10899

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "llvm/CodeGen/ISDOpcodes.h"
3232
#include "llvm/CodeGen/LowLevelTypeUtils.h"
3333
#include "llvm/CodeGen/MachineRegisterInfo.h"
34-
#include "llvm/CodeGen/RuntimeLibcalls.h"
34+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
3535
#include "llvm/CodeGen/SelectionDAG.h"
3636
#include "llvm/CodeGen/SelectionDAGNodes.h"
3737
#include "llvm/CodeGen/TargetCallingConv.h"
@@ -45,6 +45,7 @@
4545
#include "llvm/IR/InlineAsm.h"
4646
#include "llvm/IR/Instruction.h"
4747
#include "llvm/IR/Instructions.h"
48+
#include "llvm/IR/RuntimeLibcalls.h"
4849
#include "llvm/IR/Type.h"
4950
#include "llvm/Support/Alignment.h"
5051
#include "llvm/Support/AtomicOrdering.h"
@@ -3414,44 +3415,44 @@ class TargetLoweringBase {
34143415
return nullptr;
34153416
}
34163417

3417-
//===--------------------------------------------------------------------===//
3418-
// Runtime Library hooks
3419-
//
3420-
34213418
/// Rename the default libcall routine name for the specified libcall.
34223419
void setLibcallName(RTLIB::Libcall Call, const char *Name) {
3423-
LibcallRoutineNames[Call] = Name;
3420+
Libcalls.setLibcallName(Call, Name);
34243421
}
3422+
34253423
void setLibcallName(ArrayRef<RTLIB::Libcall> Calls, const char *Name) {
3426-
for (auto Call : Calls)
3427-
setLibcallName(Call, Name);
3424+
Libcalls.setLibcallName(Calls, Name);
34283425
}
34293426

34303427
/// Get the libcall routine name for the specified libcall.
34313428
const char *getLibcallName(RTLIB::Libcall Call) const {
3432-
return LibcallRoutineNames[Call];
3429+
return Libcalls.getLibcallName(Call);
34333430
}
34343431

34353432
/// Override the default CondCode to be used to test the result of the
34363433
/// comparison libcall against zero.
3434+
/// FIXME: This can't be merged with 'RuntimeLibcallsInfo' because of the ISD.
34373435
void setCmpLibcallCC(RTLIB::Libcall Call, ISD::CondCode CC) {
34383436
CmpLibcallCCs[Call] = CC;
34393437
}
34403438

3439+
34413440
/// Get the CondCode that's to be used to test the result of the comparison
34423441
/// libcall against zero.
3442+
/// FIXME: This can't be merged with 'RuntimeLibcallsInfo' because of the ISD.
34433443
ISD::CondCode getCmpLibcallCC(RTLIB::Libcall Call) const {
34443444
return CmpLibcallCCs[Call];
34453445
}
34463446

3447+
34473448
/// Set the CallingConv that should be used for the specified libcall.
34483449
void setLibcallCallingConv(RTLIB::Libcall Call, CallingConv::ID CC) {
3449-
LibcallCallingConvs[Call] = CC;
3450+
Libcalls.setLibcallCallingConv(Call, CC);
34503451
}
34513452

34523453
/// Get the CallingConv that should be used for the specified libcall.
34533454
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const {
3454-
return LibcallCallingConvs[Call];
3455+
return Libcalls.getLibcallCallingConv(Call);
34553456
}
34563457

34573458
/// Execute target specific actions to finalize target lowering.
@@ -3630,19 +3631,13 @@ class TargetLoweringBase {
36303631
std::map<std::pair<unsigned, MVT::SimpleValueType>, MVT::SimpleValueType>
36313632
PromoteToType;
36323633

3633-
/// Stores the name each libcall.
3634-
const char *LibcallRoutineNames[RTLIB::UNKNOWN_LIBCALL + 1];
3634+
/// The list of libcalls that the target will use.
3635+
RTLIB::RuntimeLibcallsInfo Libcalls;
36353636

36363637
/// The ISD::CondCode that should be used to test the result of each of the
36373638
/// comparison libcall against zero.
36383639
ISD::CondCode CmpLibcallCCs[RTLIB::UNKNOWN_LIBCALL];
36393640

3640-
/// Stores the CallingConv that should be used for each libcall.
3641-
CallingConv::ID LibcallCallingConvs[RTLIB::UNKNOWN_LIBCALL];
3642-
3643-
/// Set default libcall names and calling conventions.
3644-
void InitLibcalls(const Triple &TT);
3645-
36463641
/// The bits of IndexedModeActions used to store the legalisation actions
36473642
/// We store the data as | ML | MS | L | S | each taking 4 bits.
36483643
enum IndexedModeActionsBits {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//===- RuntimeLibcalls.h - Interface for runtime libcalls -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements a common interface to work with library calls into a
10+
// runtime that may be emitted by a given backend.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_IR_RUNTIME_LIBCALLS_H
15+
#define LLVM_IR_RUNTIME_LIBCALLS_H
16+
17+
#include "llvm/ADT/ArrayRef.h"
18+
#include "llvm/IR/CallingConv.h"
19+
#include "llvm/Support/AtomicOrdering.h"
20+
#include "llvm/TargetParser/Triple.h"
21+
22+
namespace llvm {
23+
namespace RTLIB {
24+
25+
/// RTLIB::Libcall enum - This enum defines all of the runtime library calls
26+
/// the backend can emit. The various long double types cannot be merged,
27+
/// because 80-bit library functions use "xf" and 128-bit use "tf".
28+
///
29+
/// When adding PPCF128 functions here, note that their names generally need
30+
/// to be overridden for Darwin with the xxx$LDBL128 form. See
31+
/// PPCISelLowering.cpp.
32+
///
33+
enum Libcall {
34+
#define HANDLE_LIBCALL(code, name) code,
35+
#include "llvm/IR/RuntimeLibcalls.def"
36+
#undef HANDLE_LIBCALL
37+
};
38+
39+
/// A simple container for information about the supported runtime calls.
40+
struct RuntimeLibcallsInfo {
41+
explicit RuntimeLibcallsInfo(const Triple &TT) {
42+
initLibcalls(TT);
43+
}
44+
45+
/// Rename the default libcall routine name for the specified libcall.
46+
void setLibcallName(RTLIB::Libcall Call, const char *Name) {
47+
LibcallRoutineNames[Call] = Name;
48+
}
49+
50+
void setLibcallName(ArrayRef<RTLIB::Libcall> Calls, const char *Name) {
51+
for (auto Call : Calls)
52+
setLibcallName(Call, Name);
53+
}
54+
55+
/// Get the libcall routine name for the specified libcall.
56+
const char *getLibcallName(RTLIB::Libcall Call) const {
57+
return LibcallRoutineNames[Call];
58+
}
59+
60+
/// Set the CallingConv that should be used for the specified libcall.
61+
void setLibcallCallingConv(RTLIB::Libcall Call, CallingConv::ID CC) {
62+
LibcallCallingConvs[Call] = CC;
63+
}
64+
65+
/// Get the CallingConv that should be used for the specified libcall.
66+
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const {
67+
return LibcallCallingConvs[Call];
68+
}
69+
70+
iterator_range<const char **> getLibcallNames() {
71+
return llvm::make_range(LibcallRoutineNames,
72+
LibcallRoutineNames + RTLIB::UNKNOWN_LIBCALL);
73+
}
74+
75+
private:
76+
/// Stores the name each libcall.
77+
const char *LibcallRoutineNames[RTLIB::UNKNOWN_LIBCALL + 1];
78+
79+
/// Stores the CallingConv that should be used for each libcall.
80+
CallingConv::ID LibcallCallingConvs[RTLIB::UNKNOWN_LIBCALL];
81+
82+
static bool darwinHasSinCos(const Triple &TT) {
83+
assert(TT.isOSDarwin() && "should be called with darwin triple");
84+
// Don't bother with 32 bit x86.
85+
if (TT.getArch() == Triple::x86)
86+
return false;
87+
// Macos < 10.9 has no sincos_stret.
88+
if (TT.isMacOSX())
89+
return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
90+
// iOS < 7.0 has no sincos_stret.
91+
if (TT.isiOS())
92+
return !TT.isOSVersionLT(7, 0);
93+
// Any other darwin such as WatchOS/TvOS is new enough.
94+
return true;
95+
}
96+
97+
/// Set default libcall names. If a target wants to opt-out of a libcall it
98+
/// should be placed here.
99+
void initLibcalls(const Triple &TT);
100+
};
101+
102+
} // namespace RTLIB
103+
} // namespace llvm
104+
105+
#endif // LLVM_IR_RUNTIME_LIBCALLS_H

llvm/include/llvm/LTO/LTO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class LTO {
301301

302302
/// Static method that returns a list of libcall symbols that can be generated
303303
/// by LTO but might not be visible from bitcode symbol table.
304-
static ArrayRef<const char *> getRuntimeLibcallSymbols();
304+
static SmallVector<const char *> getRuntimeLibcallSymbols(const Triple &TT);
305305

306306
private:
307307
Config Conf;

llvm/lib/CodeGen/AtomicExpandPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
2222
#include "llvm/CodeGen/AtomicExpand.h"
2323
#include "llvm/CodeGen/AtomicExpandUtils.h"
24-
#include "llvm/CodeGen/RuntimeLibcalls.h"
24+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
2525
#include "llvm/CodeGen/TargetLowering.h"
2626
#include "llvm/CodeGen/TargetPassConfig.h"
2727
#include "llvm/CodeGen/TargetSubtargetInfo.h"

llvm/lib/CodeGen/DwarfEHPrepare.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "llvm/Analysis/CFG.h"
1919
#include "llvm/Analysis/DomTreeUpdater.h"
2020
#include "llvm/Analysis/TargetTransformInfo.h"
21-
#include "llvm/CodeGen/RuntimeLibcalls.h"
21+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
2222
#include "llvm/CodeGen/TargetLowering.h"
2323
#include "llvm/CodeGen/TargetPassConfig.h"
2424
#include "llvm/CodeGen/TargetSubtargetInfo.h"

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include "llvm/CodeGen/MachineModuleInfo.h"
3939
#include "llvm/CodeGen/MachineOperand.h"
4040
#include "llvm/CodeGen/MachineRegisterInfo.h"
41-
#include "llvm/CodeGen/RuntimeLibcalls.h"
41+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
4242
#include "llvm/CodeGen/StackProtector.h"
4343
#include "llvm/CodeGen/SwitchLoweringUtils.h"
4444
#include "llvm/CodeGen/TargetFrameLowering.h"

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "llvm/CodeGen/MachineConstantPool.h"
2626
#include "llvm/CodeGen/MachineFrameInfo.h"
2727
#include "llvm/CodeGen/MachineRegisterInfo.h"
28-
#include "llvm/CodeGen/RuntimeLibcalls.h"
28+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
2929
#include "llvm/CodeGen/TargetFrameLowering.h"
3030
#include "llvm/CodeGen/TargetInstrInfo.h"
3131
#include "llvm/CodeGen/TargetLowering.h"

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "llvm/CodeGen/ISDOpcodes.h"
3838
#include "llvm/CodeGen/MachineFunction.h"
3939
#include "llvm/CodeGen/MachineMemOperand.h"
40-
#include "llvm/CodeGen/RuntimeLibcalls.h"
40+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
4141
#include "llvm/CodeGen/SDPatternMatch.h"
4242
#include "llvm/CodeGen/SelectionDAG.h"
4343
#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "llvm/CodeGen/MachineFunction.h"
2626
#include "llvm/CodeGen/MachineJumpTableInfo.h"
2727
#include "llvm/CodeGen/MachineMemOperand.h"
28-
#include "llvm/CodeGen/RuntimeLibcalls.h"
28+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
2929
#include "llvm/CodeGen/SelectionDAG.h"
3030
#include "llvm/CodeGen/SelectionDAGNodes.h"
3131
#include "llvm/CodeGen/TargetFrameLowering.h"

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "llvm/CodeGen/MachineFrameInfo.h"
3737
#include "llvm/CodeGen/MachineFunction.h"
3838
#include "llvm/CodeGen/MachineMemOperand.h"
39-
#include "llvm/CodeGen/RuntimeLibcalls.h"
39+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
4040
#include "llvm/CodeGen/SDPatternMatch.h"
4141
#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
4242
#include "llvm/CodeGen/SelectionDAGNodes.h"

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include "llvm/CodeGen/MachineModuleInfo.h"
4545
#include "llvm/CodeGen/MachineOperand.h"
4646
#include "llvm/CodeGen/MachineRegisterInfo.h"
47-
#include "llvm/CodeGen/RuntimeLibcalls.h"
47+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
4848
#include "llvm/CodeGen/SelectionDAG.h"
4949
#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
5050
#include "llvm/CodeGen/StackMaps.h"

llvm/lib/CodeGen/SelectionDAG/StatepointLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "llvm/CodeGen/MachineFrameInfo.h"
2727
#include "llvm/CodeGen/MachineFunction.h"
2828
#include "llvm/CodeGen/MachineMemOperand.h"
29-
#include "llvm/CodeGen/RuntimeLibcalls.h"
29+
#include "llvm/CodeGen/RuntimeLibcallUtil.h"
3030
#include "llvm/CodeGen/SelectionDAG.h"
3131
#include "llvm/CodeGen/SelectionDAGNodes.h"
3232
#include "llvm/CodeGen/StackMaps.h"

0 commit comments

Comments
 (0)