Skip to content

TableGen: Use StringOffsetTable for RuntimeLibcall names #148839

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

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
13 changes: 10 additions & 3 deletions llvm/include/llvm/IR/RuntimeLibcalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/StringTable.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/Support/AtomicOrdering.h"
Expand Down Expand Up @@ -77,12 +78,16 @@ struct RuntimeLibcallsInfo {
/// Get the libcall routine name for the specified libcall.
// FIXME: This should be removed. Only LibcallImpl should have a name.
const char *getLibcallName(RTLIB::Libcall Call) const {
return LibCallImplNames[LibcallImpls[Call]];
return getLibcallImplName(LibcallImpls[Call]);
}

/// Get the libcall routine name for the specified libcall implementation.
// FIXME: Change to return StringRef
static const char *getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
return LibCallImplNames[CallImpl];
if (CallImpl == RTLIB::Unsupported)
return nullptr;
return RuntimeLibcallImplNameTable[RuntimeLibcallNameOffsetTable[CallImpl]]
.data();
}

/// Return the lowering's selection of implementation call for \p Call
Expand Down Expand Up @@ -144,7 +149,9 @@ struct RuntimeLibcallsInfo {

/// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
/// SHL_I32
LLVM_ABI static const char *const LibCallImplNames[RTLIB::NumLibcallImpls];
LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];

/// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];
Expand Down
7 changes: 6 additions & 1 deletion llvm/include/llvm/TableGen/StringToOffsetTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ namespace llvm {
class StringToOffsetTable {
StringMap<unsigned> StringOffset;
std::string AggregateString;

/// If this is to be a static class member, the prefix to use (i.e. class name
/// plus ::)
const StringRef ClassPrefix;
const bool AppendZero;

public:
StringToOffsetTable(bool AppendZero = true) : AppendZero(AppendZero) {
StringToOffsetTable(bool AppendZero = true, StringRef ClassPrefix = "")
: ClassPrefix(ClassPrefix), AppendZero(AppendZero) {
// Ensure we always put the empty string at offset zero. That lets empty
// initialization also be zero initialization for offsets into the table.
GetOrAddStringOffset("");
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/IR/RuntimeLibcalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/RuntimeLibcalls.h"
#include "llvm/ADT/StringTable.h"

using namespace llvm;
using namespace RTLIB;
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/TableGen/StringToOffsetTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS,
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
static constexpr char {}Storage[] = )",
Name);
{} constexpr char {}{}Storage[] = )",
ClassPrefix.empty() ? "static" : "", ClassPrefix, Name);

// MSVC silently miscompiles string literals longer than 64k in some
// circumstances. The build system sets EmitLongStrLiterals to false when it
Expand Down Expand Up @@ -83,10 +83,10 @@ static constexpr char {}Storage[] = )",
#pragma GCC diagnostic pop
#endif

static constexpr llvm::StringTable
{0} = {0}Storage;
{1}constexpr llvm::StringTable
{2}{0} = {0}Storage;
)",
Name);
Name, ClassPrefix.empty() ? "static " : "", ClassPrefix);
}

void StringToOffsetTable::EmitString(raw_ostream &O) const {
Expand Down
45 changes: 33 additions & 12 deletions llvm/test/TableGen/RuntimeLibcallEmitter.td
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,40 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
// CHECK-NEXT: RTLIB::Unsupported
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: const char *const llvm::RTLIB::RuntimeLibcallsInfo::LibCallImplNames[RTLIB::NumLibcallImpls] = {
// CHECK-NEXT: nullptr, // RTLIB::Unsupported
// CHECK-NEXT: "___memcpy", // RTLIB::___memcpy
// CHECK-NEXT: "___memset", // RTLIB::___memset
// CHECK-NEXT: "__ashlsi3", // RTLIB::__ashlsi3
// CHECK-NEXT: "__lshrdi3", // RTLIB::__lshrdi3
// CHECK-NEXT: "bzero", // RTLIB::bzero
// CHECK-NEXT: "calloc", // RTLIB::calloc
// CHECK-NEXT: "sqrtl", // RTLIB::sqrtl_f80
// CHECK-NEXT: "sqrtl", // RTLIB::sqrtl_f128
// CHECK-EMPTY:
// CHECK-NEXT: #ifdef __GNUC__
// CHECK-NEXT: #pragma GCC diagnostic push
// CHECK-NEXT: #pragma GCC diagnostic ignored "-Woverlength-strings"
// CHECK-NEXT: #endif
// CHECK-NEXT: constexpr char RTLIB::RuntimeLibcallsInfo::RuntimeLibcallImplNameTableStorage[] =
// CHECK-NEXT: "\0"
// CHECK-NEXT: "___memcpy\0"
// CHECK-NEXT: "___memset\0"
// CHECK-NEXT: "__ashlsi3\0"
// CHECK-NEXT: "__lshrdi3\0"
// CHECK-NEXT: "bzero\0"
// CHECK-NEXT: "calloc\0"
// CHECK-NEXT: "sqrtl\0"
// CHECK-NEXT: ;
// CHECK-NEXT: #ifdef __GNUC__
// CHECK-NEXT: #pragma GCC diagnostic pop
// CHECK-NEXT: #endif
// CHECK-EMPTY:
// CHECK-NEXT: constexpr llvm::StringTable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may need to be extern, because the constexpr variable has internal linkage by default. When built using gcc the libLLVMCore.a does not export the symbol.

// CHECK-NEXT: RTLIB::RuntimeLibcallsInfo::RuntimeLibcallImplNameTable = RuntimeLibcallImplNameTableStorage;
// CHECK-EMPTY:
// CHECK-NEXT: const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
// CHECK-NEXT: 0, //
// CHECK-NEXT: 1, // ___memcpy
// CHECK-NEXT: 11, // ___memset
// CHECK-NEXT: 21, // __ashlsi3
// CHECK-NEXT: 31, // __lshrdi3
// CHECK-NEXT: 41, // bzero
// CHECK-NEXT: 47, // calloc
// CHECK-NEXT: 54, // sqrtl
// CHECK-NEXT: 54, // sqrtl
// CHECK-NEXT: };

// CHECK: const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::ImplToLibcall[RTLIB::NumLibcallImpls] = {
// CHECK-NEXT: const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::ImplToLibcall[RTLIB::NumLibcallImpls] = {
// CHECK-NEXT: RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported
// CHECK-NEXT: RTLIB::MEMCPY, // RTLIB::___memcpy
// CHECK-NEXT: RTLIB::MEMSET, // RTLIB::___memset
Expand Down
27 changes: 17 additions & 10 deletions llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/SetTheory.h"
#include "llvm/TableGen/StringToOffsetTable.h"
#include "llvm/TableGen/TableGenBackend.h"

using namespace llvm;
Expand Down Expand Up @@ -305,8 +307,6 @@ void RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum(raw_ostream &OS) const {

void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames(
raw_ostream &OS) const {
// TODO: Emit libcall names as string offset table.

OS << "const RTLIB::LibcallImpl "
"llvm::RTLIB::RuntimeLibcallsInfo::"
"DefaultLibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {\n";
Expand All @@ -331,17 +331,24 @@ void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames(
"};\n\n";

// Emit the implementation names
OS << "const char *const llvm::RTLIB::RuntimeLibcallsInfo::"
"LibCallImplNames[RTLIB::NumLibcallImpls] = {\n"
" nullptr, // RTLIB::Unsupported\n";
StringToOffsetTable Table(/*AppendZero=*/true,
"RTLIB::RuntimeLibcallsInfo::");

for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
Table.GetOrAddStringOffset(LibCallImpl.getLibcallFuncName());

Table.EmitStringTableDef(OS, "RuntimeLibcallImplNameTable");
OS << R"(
const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
)";

OS << formatv(" {}, // {}\n", Table.GetStringOffset(""),
""); // Unsupported entry
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
OS << " \"" << LibCallImpl.getLibcallFuncName() << "\", // ";
LibCallImpl.emitEnumEntry(OS);
OS << '\n';
StringRef ImplName = LibCallImpl.getLibcallFuncName();
OS << formatv(" {}, // {}\n", Table.GetStringOffset(ImplName), ImplName);
}

OS << "};\n\n";
OS << "};\n";

// Emit the reverse mapping from implementation libraries to RTLIB::Libcall
OS << "const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::"
Expand Down
Loading