[llvm] [NFC][TableGen] Emit more readable builtin string table. (PR #105445)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 22 06:56:18 PDT 2024
https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/105445
>From bbdb8d083648837c714549f997ac1bfdb35fa8cf 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/TableGen/StringToOffsetTable.h | 27 +++++++++++++++++++
llvm/utils/TableGen/IntrinsicEmitter.cpp | 4 +--
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/TableGen/StringToOffsetTable.h b/llvm/include/llvm/TableGen/StringToOffsetTable.h
index 7fb9d02d77c704..d9bb434599a4ed 100644
--- a/llvm/include/llvm/TableGen/StringToOffsetTable.h
+++ b/llvm/include/llvm/TableGen/StringToOffsetTable.h
@@ -12,6 +12,7 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
+#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <cctype>
#include <optional>
@@ -52,6 +53,32 @@ class StringToOffsetTable {
return II->second;
}
+ // Emit the string using string literal concatenation, for better readability
+ // and searchability.
+ void EmitStringLiteralDef(raw_ostream &OS, const Twine &Decl,
+ const Twine &Indent = " ") const {
+ OS << formatv(R"(
+#ifdef __GNUC__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Woverlength-strings"
+#endif
+{0}{1} = )",
+ Indent, Decl);
+
+ for (StringRef Str : split(AggregateString, '\0')) {
+ OS << "\n" << Indent << " \"";
+ OS.write_escaped(Str);
+ OS << "\\0\"";
+ }
+ OS << formatv(R"(;
+#ifdef __GNUC__
+#pragma GCC diagnostic pop
+#endif
+)",
+ Indent);
+ }
+
+ // Emit the string as one single string.
void EmitString(raw_ostream &O) {
// Escape the string.
SmallString<256> Str;
diff --git a/llvm/utils/TableGen/IntrinsicEmitter.cpp b/llvm/utils/TableGen/IntrinsicEmitter.cpp
index 5d972157828784..8e536c99f627f5 100644
--- a/llvm/utils/TableGen/IntrinsicEmitter.cpp
+++ b/llvm/utils/TableGen/IntrinsicEmitter.cpp
@@ -669,9 +669,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 {
More information about the llvm-commits
mailing list