Skip to content

RuntimeLibcalls: Move exception call config to tablegen #151948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/RuntimeLibcalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ struct RuntimeLibcallsInfo {

/// Generated by tablegen.
void setTargetRuntimeLibcallSets(const Triple &TT,
ExceptionHandling ExceptionModel,
FloatABI::ABIType FloatABI);

/// Set default libcall names. If a target wants to opt-out of a libcall it
Expand Down
89 changes: 72 additions & 17 deletions llvm/include/llvm/IR/RuntimeLibcalls.td
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def hasSinCos : RuntimeLibcallPredicate<"hasSinCos(TT)">;
// FIXME: Way to combine predicates
def hasSinCos_f32_f64 : RuntimeLibcallPredicate<"hasSinCos_f32_f64(TT)">;

def ExceptionModelIsNotNone : RuntimeLibcallPredicate<
[{ExceptionModel != ExceptionHandling::None}]
>;

def ExceptionModelIsSjLj : RuntimeLibcallPredicate<
[{ExceptionModel == ExceptionHandling::SjLj}]
>;

//--------------------------------------------------------------------
// Declare all kinds of used libcalls
//--------------------------------------------------------------------
Expand Down Expand Up @@ -695,10 +703,6 @@ foreach MemSize = [ 1, 2, 4, 8, 16 ] in {
!cast<RuntimeLibcall>("MEMSET_ELEMENT_UNORDERED_ATOMIC_"#MemSize)>;
}

// Exception handling
def _Unwind_Resume : RuntimeLibcallImpl<UNWIND_RESUME>;
def __cxa_end_cleanup : RuntimeLibcallImpl<CXA_END_CLEANUP>;

// Atomic '__sync_*' libcalls.
foreach lc = LibCalls__sync in {
def __#!tolower(!cast<string>(lc)) : RuntimeLibcallImpl<lc>;
Expand Down Expand Up @@ -1002,9 +1006,20 @@ defm sincos : LibmLongDoubleLibCall;

def bzero : RuntimeLibcallImpl<BZERO>;
def __bzero : RuntimeLibcallImpl<BZERO>;
def _Unwind_SjLj_Resume : RuntimeLibcallImpl<UNWIND_RESUME>;
def _Unwind_SjLj_Register : RuntimeLibcallImpl<UNWIND_REGISTER>;
def _Unwind_SjLj_Unregister : RuntimeLibcallImpl<UNWIND_UNREGISTER>;

// Exception handling
defset list<RuntimeLibcallImpl> DefaultExceptionHandlingLibcalls = {
def _Unwind_Resume : RuntimeLibcallImpl<UNWIND_RESUME>;
def __cxa_end_cleanup : RuntimeLibcallImpl<CXA_END_CLEANUP>;
}

defset list<RuntimeLibcallImpl> SjLjExceptionHandlingLibcalls = {
def _Unwind_SjLj_Resume : RuntimeLibcallImpl<UNWIND_RESUME>;
def _Unwind_SjLj_Register : RuntimeLibcallImpl<UNWIND_REGISTER>;
def _Unwind_SjLj_Unregister : RuntimeLibcallImpl<UNWIND_UNREGISTER>;
}

// Only used on wasm?
def _Unwind_CallPersonality : RuntimeLibcallImpl<UNWIND_CALL_PERSONALITY>;

// Used on OpenBSD
Expand Down Expand Up @@ -1080,6 +1095,13 @@ defset list<RuntimeLibcallImpl> LibmF128FiniteLibcalls = {
// Common Libcall Sets
//===----------------------------------------------------------------------===//

defvar ExceptionModelCalls = (add
LibcallImpls<(add DefaultExceptionHandlingLibcalls),
ExceptionModelIsNotNone>,
LibcallImpls<(add SjLjExceptionHandlingLibcalls),
ExceptionModelIsSjLj>
);

// FIXME: Should move to explicit opt-in to different sets of libcalls
// instead of trying to remove from a default set. We have
// unreasonable defaults like reporting f80 calls on most targets when
Expand All @@ -1100,19 +1122,25 @@ defvar DefaultRuntimeLibcallImpls_f128 =
!filter(entry, AllDefaultRuntimeLibcallImpls,
!match(!cast<string>(entry.Provides), "_F128"));

defvar DefaultRuntimeLibcallImpls =
// FIXME: Ideally we would just use dags everywhere, but for the
// arm64ec case we need iterable lists so we can add the # prefix
defvar DefaultRuntimeLibcallImplsList =
!listremove(
!listremove(
!listremove(AllDefaultRuntimeLibcallImpls, Int128RTLibcalls),
DefaultRuntimeLibcallImpls_f80),
DefaultRuntimeLibcallImpls_ppcf128);

defvar DefaultRuntimeLibcallImpls = (add DefaultRuntimeLibcallImplsList, ExceptionModelCalls);

/// Default set of libcall impls for 32-bit architectures.
defvar DefaultLibcallImpls32 = DefaultRuntimeLibcallImpls;
defvar DefaultLibcallImpls32List = DefaultRuntimeLibcallImplsList;
defvar DefaultLibcallImpls32 = (add DefaultRuntimeLibcallImpls);

/// Default set of libcall impls for 64-bit architectures.
defvar DefaultLibcallImpls64 = !listconcat(DefaultRuntimeLibcallImpls,
Int128RTLibcalls);
defvar DefaultLibcallImpls64List = !listconcat(DefaultRuntimeLibcallImplsList,
Int128RTLibcalls);
defvar DefaultLibcallImpls64 = (add DefaultLibcallImpls32List);

defvar DarwinSinCosStret = LibcallImpls<(add __sincosf_stret, __sincos_stret),
darwinHasSinCosStret>;
Expand Down Expand Up @@ -1148,8 +1176,12 @@ defvar WindowsExclusions = !listconcat(WindowsMathRemovals, MostPowI);

// Targets which support windows should start with these as a base and
// add in calls for other OSes
defvar Win32DefaultLibcallImpls = !listremove(DefaultLibcallImpls32, WindowsExclusions);
defvar Win64DefaultLibcallImpls = !listremove(DefaultLibcallImpls64, WindowsExclusions);
defvar Win32DefaultLibcallImpls = (sub DefaultLibcallImpls32, WindowsExclusions);
defvar Win64DefaultLibcallImpls = (sub DefaultLibcallImpls64, WindowsExclusions);

// We need a list (not-dag) to use foreach to define all the prefixed
// versions, and there isn't an easy way to flatten a dag into a list.
defvar Win64DefaultLibcallImplsList = !listremove(DefaultLibcallImpls64List, WindowsExclusions);

defvar LibmHasFrexpF32 = LibcallImpls<(add frexpf), isNotOSWindowsOrIsCygwinMinGW>;
defvar LibmHasLdexpF32 = LibcallImpls<(add ldexpf), isNotOSWindowsOrIsCygwinMinGW>;
Expand Down Expand Up @@ -1244,12 +1276,13 @@ def AArch64SystemLibrary : SystemRuntimeLibrary<
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
DefaultLibmExp10,
DefaultStackProtector,
SecurityCheckCookieIfWinMSVC)
SecurityCheckCookieIfWinMSVC,
ExceptionModelCalls)
>;

// Prepend a # to every name
defset list<RuntimeLibcallImpl> WinArm64ECDefaultRuntimeLibcallImpls = {
foreach libcall = Win64DefaultLibcallImpls in {
foreach libcall = Win64DefaultLibcallImplsList in {
def arm64ec_#libcall : DuplicateLibcallImplWithPrefix<libcall, "#">;
}

Expand All @@ -1260,12 +1293,32 @@ defset list<RuntimeLibcallImpl> WinArm64ECDefaultRuntimeLibcallImpls = {

def arm64ec___stack_chk_fail : DuplicateLibcallImplWithPrefix<__stack_chk_fail, "#">;

defset list<RuntimeLibcallImpl> WinArm64ECDefaultExceptionHandlingLibcalls = {
foreach libcall = DefaultExceptionHandlingLibcalls in {
def arm64ec_#libcall : DuplicateLibcallImplWithPrefix<libcall, "#">;
}
}

defset list<RuntimeLibcallImpl> WinArm64ECSjLjExceptionHandlingLibcalls = {
foreach libcall = SjLjExceptionHandlingLibcalls in {
def arm64ec_#libcall : DuplicateLibcallImplWithPrefix<libcall, "#">;
}
}

defvar ExceptionModelCallsArm64EC = (add
LibcallImpls<(add WinArm64ECDefaultExceptionHandlingLibcalls),
ExceptionModelIsNotNone>,
LibcallImpls<(add WinArm64ECSjLjExceptionHandlingLibcalls),
ExceptionModelIsSjLj>
);

def WindowsARM64ECSystemLibrary
: SystemRuntimeLibrary<isWindowsArm64EC,
(add WinArm64ECDefaultRuntimeLibcallImpls,
arm64ec___stack_chk_fail,
LibcallImpls<(add __security_check_cookie_arm64ec),
isWindowsMSVCEnvironment>)>;
isWindowsMSVCEnvironment>,
ExceptionModelCallsArm64EC)>;

//===----------------------------------------------------------------------===//
// AMDGPU Runtime Libcalls
Expand Down Expand Up @@ -2176,7 +2229,8 @@ defvar X86CommonLibcalls =
// FIXME: MSVCRT doesn't have powi. The f128 case is added as a
// hack for one test relying on it.
__powitf2_f128,
DefaultStackProtector
DefaultStackProtector,
ExceptionModelCalls
);

defvar Windows32DivRemMulCalls =
Expand Down Expand Up @@ -2321,6 +2375,7 @@ def WasmSystemLibrary
(add DefaultRuntimeLibcallImpls, Int128RTLibcalls,
CompilerRTOnlyInt64Libcalls, CompilerRTOnlyInt128Libcalls,
exp10f, exp10,
_Unwind_CallPersonality,
emscripten_return_address,
__stack_chk_fail)>;

Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/IR/RuntimeLibcalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
ExceptionHandling ExceptionModel,
FloatABI::ABIType FloatABI,
EABI EABIVersion, StringRef ABIName) {
setTargetRuntimeLibcallSets(TT, FloatABI);

if (ExceptionModel == ExceptionHandling::SjLj)
setLibcallImpl(RTLIB::UNWIND_RESUME, RTLIB::_Unwind_SjLj_Resume);
setTargetRuntimeLibcallSets(TT, ExceptionModel, FloatABI);

if (TT.isARM() || TT.isThumb()) {
setARMLibcallNames(*this, TT, FloatABI, EABIVersion);
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/TableGen/RuntimeLibcallEmitter-calling-conv.td
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def MSP430LibraryWithCondCC : SystemRuntimeLibrary<isMSP430,
>;


// CHECK: void llvm::RTLIB::RuntimeLibcallsInfo::setTargetRuntimeLibcallSets(const llvm::Triple &TT, FloatABI::ABIType FloatABI) {
// CHECK: void llvm::RTLIB::RuntimeLibcallsInfo::setTargetRuntimeLibcallSets(const llvm::Triple &TT, ExceptionHandling ExceptionModel, FloatABI::ABIType FloatABI) {
// CHECK: if (TT.getArch() == Triple::avr && TT.isOSHurd()) {
// CHECK-NEXT: const CallingConv::ID DefaultCC = isFoo() ? CallingConv::Fast : CallingConv::GHC;
// CHECK-NEXT: for (CallingConv::ID &Entry : LibcallImplCallingConvs) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/TableGen/RuntimeLibcallEmitter.td
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
// CHECK-NEXT: };


// CHECK: void llvm::RTLIB::RuntimeLibcallsInfo::setTargetRuntimeLibcallSets(const llvm::Triple &TT, FloatABI::ABIType FloatABI) {
// CHECK: void llvm::RTLIB::RuntimeLibcallsInfo::setTargetRuntimeLibcallSets(const llvm::Triple &TT, ExceptionHandling ExceptionModel, FloatABI::ABIType FloatABI) {
// CHECK-NEXT: struct LibcallImplPair {
// CHECK-NEXT: RTLIB::Libcall Func;
// CHECK-NEXT: RTLIB::LibcallImpl Impl;
Expand Down
3 changes: 2 additions & 1 deletion llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
void RuntimeLibcallEmitter::emitSystemRuntimeLibrarySetCalls(
raw_ostream &OS) const {
OS << "void llvm::RTLIB::RuntimeLibcallsInfo::setTargetRuntimeLibcallSets("
"const llvm::Triple &TT, FloatABI::ABIType FloatABI) {\n"
"const llvm::Triple &TT, ExceptionHandling ExceptionModel, "
"FloatABI::ABIType FloatABI) {\n"
" struct LibcallImplPair {\n"
" RTLIB::Libcall Func;\n"
" RTLIB::LibcallImpl Impl;\n"
Expand Down