Skip to content
Merged
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
23 changes: 23 additions & 0 deletions llvm/include/llvm/IR/RuntimeLibcalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define LLVM_IR_RUNTIME_LIBCALLS_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Bitset.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/StringTable.h"
#include "llvm/IR/CallingConv.h"
Expand Down Expand Up @@ -54,8 +55,22 @@ static inline auto libcall_impls() {
static_cast<RTLIB::LibcallImpl>(RTLIB::NumLibcallImpls));
}

/// Manage a bitset representing the list of available libcalls for a module.
class LibcallImplBitset : public Bitset<RTLIB::NumLibcallImpls> {
public:
constexpr LibcallImplBitset() = default;
constexpr LibcallImplBitset(
const std::array<uint64_t, (RTLIB::NumLibcallImpls + 63) / 64> &Src)
: Bitset(Src) {}
};

/// A simple container for information about the supported runtime calls.
struct RuntimeLibcallsInfo {
private:
/// Bitset of libcalls a module may emit a call to.
LibcallImplBitset AvailableLibcallImpls;

public:
explicit RuntimeLibcallsInfo(
const Triple &TT,
ExceptionHandling ExceptionModel = ExceptionHandling::None,
Expand Down Expand Up @@ -129,6 +144,14 @@ struct RuntimeLibcallsInfo {
return getLibcallName(RTLIB::MEMMOVE);
}

bool isAvailable(RTLIB::LibcallImpl Impl) const {
return AvailableLibcallImpls.test(Impl);
}

void setAvailable(RTLIB::LibcallImpl Impl) {
AvailableLibcallImpls.set(Impl);
}

/// Return the libcall provided by \p Impl
static RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) {
return ImplToLibcall[Impl];
Expand Down
21 changes: 18 additions & 3 deletions llvm/test/TableGen/RuntimeLibcallEmitter-calling-conv.td
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def MSP430LibraryWithCondCC : SystemRuntimeLibrary<isMSP430,
// CHECK-NEXT: Entry = DefaultCC;
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x0000000000001a
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::MALLOC, RTLIB::impl_malloc}, // malloc
// CHECK-NEXT: };
Expand All @@ -70,9 +75,14 @@ def MSP430LibraryWithCondCC : SystemRuntimeLibrary<isMSP430,
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: if (TT.getArch() == Triple::avr) {
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::MALLOC, RTLIB::impl_malloc}, // malloc
// CHECK-NEXT: };
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x0000000000001a
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::MALLOC, RTLIB::impl_malloc}, // malloc
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: for (const auto [Func, Impl] : LibraryCalls) {
// CHECK-NEXT: setLibcallImpl(Func, Impl);
Expand All @@ -92,6 +102,11 @@ def MSP430LibraryWithCondCC : SystemRuntimeLibrary<isMSP430,
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: if (TT.getArch() == Triple::msp430) {
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x00000000000010
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::MALLOC, RTLIB::impl_malloc}, // malloc
// CHECK-NEXT: };
Expand Down
61 changes: 49 additions & 12 deletions llvm/test/TableGen/RuntimeLibcallEmitter-conflict-warning.td
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,69 @@ def dup1 : RuntimeLibcallImpl<ANOTHER_DUP>;
// func_a and func_b both provide SOME_FUNC.

// CHECK: if (isTargetArchA()) {
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::SOME_FUNC, RTLIB::impl_func_b}, // func_b
// CHECK-NEXT: };
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x00000000000018
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:

// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::SOME_FUNC, RTLIB::impl_func_b}, // func_b
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: for (const auto [Func, Impl] : LibraryCalls) {
// CHECK-NEXT: setLibcallImpl(Func, Impl);
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: return;
// CHECK-NEXT: }

// ERR: :[[@LINE+1]]:5: warning: conflicting implementations for libcall SOME_FUNC: func_b, func_a
def TheSystemLibraryA : SystemRuntimeLibrary<isTargetArchA,
(add func_b, func_a)
>;

// CHECK: if (isTargetArchB()) {
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::OTHER_FUNC, RTLIB::impl_other_func}, // other_func
// CHECK-NEXT: {RTLIB::SOME_FUNC, RTLIB::impl_func_a}, // func_a
// CHECK-NEXT: };
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x00000000000058
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::OTHER_FUNC, RTLIB::impl_other_func}, // other_func
// CHECK-NEXT: {RTLIB::SOME_FUNC, RTLIB::impl_func_a}, // func_a
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: for (const auto [Func, Impl] : LibraryCalls) {
// CHECK-NEXT: setLibcallImpl(Func, Impl);
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: return;
// CHECK-NEXT: }

// ERR: :[[@LINE+1]]:5: warning: conflicting implementations for libcall SOME_FUNC: func_a, func_b
def TheSystemLibraryB : SystemRuntimeLibrary<isTargetArchB,
(add func_a, other_func, func_b)
>;

// CHECK: if (isTargetArchC()) {
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::ANOTHER_DUP, RTLIB::impl_dup1}, // dup1
// CHECK-NEXT: {RTLIB::OTHER_FUNC, RTLIB::impl_other_func}, // other_func
// CHECK-NEXT: {RTLIB::SOME_FUNC, RTLIB::impl_func_a}, // func_a
// CHECK-NEXT: };
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x0000000000007e
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::ANOTHER_DUP, RTLIB::impl_dup1}, // dup1
// CHECK-NEXT: {RTLIB::OTHER_FUNC, RTLIB::impl_other_func}, // other_func
// CHECK-NEXT: {RTLIB::SOME_FUNC, RTLIB::impl_func_a}, // func_a
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: for (const auto [Func, Impl] : LibraryCalls) {
// CHECK-NEXT: setLibcallImpl(Func, Impl);
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: return;
// CHECK-NEXT: }

// ERR: :[[@LINE+3]]:5: warning: conflicting implementations for libcall ANOTHER_DUP: dup1, dup0
// ERR: :[[@LINE+2]]:5: warning: conflicting implementations for libcall SOME_FUNC: func_a, func_b
Expand Down
36 changes: 28 additions & 8 deletions llvm/test/TableGen/RuntimeLibcallEmitter.td
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,20 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: if (TT.getArch() == Triple::blah) {
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::BZERO, RTLIB::impl_bzero}, // bzero
// CHECK-NEXT: {RTLIB::CALLOC, RTLIB::impl_calloc}, // calloc
// CHECK-NEXT: {RTLIB::SQRT_F128, RTLIB::impl_sqrtl_f128}, // sqrtl
// CHECK-NEXT: };
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x000000000000e0
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: for (const auto [Func, Impl] : LibraryCalls) {
// CHECK-NEXT: setLibcallImpl(Func, Impl);
// CHECK-NEXT: }
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::BZERO, RTLIB::impl_bzero}, // bzero
// CHECK-NEXT: {RTLIB::CALLOC, RTLIB::impl_calloc}, // calloc
// CHECK-NEXT: {RTLIB::SQRT_F128, RTLIB::impl_sqrtl_f128}, // sqrtl
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: for (const auto [Func, Impl] : LibraryCalls) {
// CHECK-NEXT: setLibcallImpl(Func, Impl);
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: if (TT.hasCompilerRT()) {
// CHECK-NEXT: static const LibcallImplPair LibraryCalls_hasCompilerRT[] = {
Expand Down Expand Up @@ -233,6 +238,11 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: if (TT.getArch() == Triple::buzz) {
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x00000000000118
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::SHL_I32, RTLIB::impl___ashlsi3}, // __ashlsi3
// CHECK-NEXT: {RTLIB::SQRT_F80, RTLIB::impl_sqrtl_f80}, // sqrtl
Expand All @@ -247,6 +257,11 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: if (TT.getArch() == Triple::foo) {
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x000000000000a0
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::BZERO, RTLIB::impl_bzero}, // bzero
// CHECK-NEXT: {RTLIB::SQRT_F128, RTLIB::impl_sqrtl_f128}, // sqrtl
Expand All @@ -271,6 +286,11 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: if (TT.getArch() == Triple::simple) {
// CHECK-NEXT: static constexpr LibcallImplBitset SystemAvailableImpls({
// CHECK-NEXT: 0x00000000000158
// CHECK-NEXT: });
// CHECK-NEXT: AvailableLibcallImpls = SystemAvailableImpls;
// CHECK-EMPTY:
// CHECK-NEXT: static const LibcallImplPair LibraryCalls[] = {
// CHECK-NEXT: {RTLIB::CALLOC, RTLIB::impl_calloc}, // calloc
// CHECK-NEXT: {RTLIB::SHL_I32, RTLIB::impl___ashlsi3}, // __ashlsi3
Expand Down
31 changes: 30 additions & 1 deletion llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,12 @@ void RuntimeLibcallEmitter::emitSystemRuntimeLibrarySetCalls(
PredicateSorter.insert(
PredicateWithCC()); // No predicate or CC override first.

constexpr unsigned BitsPerStorageElt = 64;
DenseMap<PredicateWithCC, LibcallsWithCC> Pred2Funcs;

SmallVector<uint64_t, 32> BitsetValues(
divideCeil(RuntimeLibcallImplDefList.size(), BitsPerStorageElt));

for (const Record *Elt : *Elements) {
const RuntimeLibcallImpl *LibCallImpl = getRuntimeLibcallImpl(Elt);
if (!LibCallImpl) {
Expand All @@ -597,23 +602,47 @@ void RuntimeLibcallEmitter::emitSystemRuntimeLibrarySetCalls(
continue;
}

size_t BitIdx = LibCallImpl->getEnumVal();
uint64_t BitmaskVal = uint64_t(1) << (BitIdx % BitsPerStorageElt);
size_t BitsetIdx = BitIdx / BitsPerStorageElt;

auto It = Func2Preds.find(LibCallImpl);
if (It == Func2Preds.end()) {
BitsetValues[BitsetIdx] |= BitmaskVal;
Pred2Funcs[PredicateWithCC()].LibcallImpls.push_back(LibCallImpl);
continue;
}

for (const Record *Pred : It->second.first) {
const Record *CC = It->second.second;
PredicateWithCC Key(Pred, CC);
AvailabilityPredicate SubsetPredicate(Pred);
if (SubsetPredicate.isAlwaysAvailable())
BitsetValues[BitsetIdx] |= BitmaskVal;

PredicateWithCC Key(Pred, CC);
auto &Entry = Pred2Funcs[Key];
Entry.LibcallImpls.push_back(LibCallImpl);
Entry.CallingConv = It->second.second;
PredicateSorter.insert(Key);
}
}

OS << " static constexpr LibcallImplBitset SystemAvailableImpls({\n"
<< indent(6);

ListSeparator LS;
unsigned EntryCount = 0;
for (uint64_t Bits : BitsetValues) {
if (EntryCount++ == 4) {
EntryCount = 1;
OS << ",\n" << indent(6);
} else
OS << LS;
OS << format_hex(Bits, 16);
}
OS << "\n });\n"
" AvailableLibcallImpls = SystemAvailableImpls;\n\n";

SmallVector<PredicateWithCC, 0> SortedPredicates =
PredicateSorter.takeVector();

Expand Down
Loading