[llvm] [NFC][TableGen] Emit more readable builtin string table. (PR #105445)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 20 15:42:18 PDT 2024
https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/105445
- Adopt `SequenceToOffsetTable` to emit the string table in `EmitIntrinsicToBuiltinMap`.
- `SequenceToOffsetTable` emits a string table using string literal concatenation of individual null terminated fragments, one fragment on each line, making the table more readable as well searchable.
- Adopt `StringRef` to be used as the sequence type in `SequenceToOffsetTable` by providing `value_type` and reverse iterators.
- Reduces string table size for both Clang and MS builtins by several bytes: Clang: 134915 -> 134001, MS: 68->56 bytes.
>From 438d447366f1fb2f6f7df89498e3d46e7e0863d2 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 20 Aug 2024 15:09:52 -0700
Subject: [PATCH] [NFC][TableGen] Emit more readable builtin string table.
- Adopt `SequenceToOffsetTable` to emit the string table in
`EmitIntrinsicToBuiltinMap`.
- `SequenceToOffsetTable` emits a string table using string literal
concatenation of individual null terminated fragments, one fragment on each
line, making the table more readable as well searchable.
- Adopt `StringRef` to be used as the sequence type in `SequenceToOffsetTable`
by providing `value_type` and reverse iterators.
- Reduces string table size for both Clang and MS builtins by several
bytes: Clang: 134915 -> 134001, MS: 68->56 bytes.
---
llvm/include/llvm/ADT/StringRef.h | 11 +++++++++++
llvm/utils/TableGen/IntrinsicEmitter.cpp | 15 +++++++--------
2 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 049f22b03e46e8..32cf0a2218e5e9 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -17,6 +17,7 @@
#include <cassert>
#include <cstddef>
#include <cstring>
+#include <iterator>
#include <limits>
#include <string>
#include <string_view>
@@ -54,6 +55,8 @@ namespace llvm {
using iterator = const char *;
using const_iterator = const char *;
using size_type = size_t;
+ using value_type = char;
+ using reverse_iterator = std::reverse_iterator<iterator>;
private:
/// The start of the string, in an external buffer.
@@ -112,6 +115,14 @@ namespace llvm {
iterator end() const { return Data + Length; }
+ reverse_iterator rbegin() const {
+ return std::make_reverse_iterator(end());
+ }
+
+ reverse_iterator rend() const {
+ return std::make_reverse_iterator(begin());
+ }
+
const unsigned char *bytes_begin() const {
return reinterpret_cast<const unsigned char *>(begin());
}
diff --git a/llvm/utils/TableGen/IntrinsicEmitter.cpp b/llvm/utils/TableGen/IntrinsicEmitter.cpp
index 5d972157828784..09f9ad33eaf159 100644
--- a/llvm/utils/TableGen/IntrinsicEmitter.cpp
+++ b/llvm/utils/TableGen/IntrinsicEmitter.cpp
@@ -24,7 +24,6 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
-#include "llvm/TableGen/StringToOffsetTable.h"
#include "llvm/TableGen/TableGenBackend.h"
#include <algorithm>
#include <array>
@@ -637,15 +636,17 @@ void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
// Populate the string table with the names of all the builtins after
// removing this common prefix.
- StringToOffsetTable Table;
+ SequenceToOffsetTable<StringRef> Table;
for (const auto &[TargetPrefix, Entry] : BuiltinMap) {
auto &[Map, CommonPrefix] = Entry;
for (auto &[BuiltinName, EnumName] : Map) {
StringRef Suffix = BuiltinName.substr(CommonPrefix->size());
- Table.GetOrAddStringOffset(Suffix);
+ Table.add(Suffix);
}
}
+ Table.layout();
+
OS << formatv(R"(
// Get the LLVM intrinsic that corresponds to a builtin. This is used by the
// C front-end. The builtin name is passed in as BuiltinName, and a target
@@ -669,9 +670,7 @@ Intrinsic::getIntrinsicFor{1}Builtin(StringRef TargetPrefix,
}
if (!Table.empty()) {
- OS << " static constexpr char BuiltinNames[] = {\n";
- Table.EmitCharArray(OS);
- OS << " };\n\n";
+ Table.emitStringLiteralDef(OS, " static constexpr char BuiltinNames[]");
OS << R"(
struct BuiltinEntry {
@@ -704,8 +703,8 @@ Intrinsic::getIntrinsicFor{1}Builtin(StringRef TargetPrefix,
TargetPrefix);
for (const auto &[BuiltinName, EnumName] : Map) {
StringRef Suffix = BuiltinName.substr(CommonPrefix->size());
- OS << formatv(" {{{0}, {1}}, // {2}\n", EnumName,
- *Table.GetStringOffset(Suffix), BuiltinName);
+ OS << formatv(" {{{0}, {1}}, // {2}\n", EnumName, Table.get(Suffix),
+ BuiltinName);
}
OS << formatv(" }; // {0}Names\n\n", TargetPrefix);
}
More information about the llvm-commits
mailing list