[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 05:28:20 PDT 2024


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/105445

>From a1c8b172092d5406ad5fe37804b31b6be7b0a970 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..4d459e816efaa5 100644
--- a/llvm/include/llvm/TableGen/StringToOffsetTable.h
+++ b/llvm/include/llvm/TableGen/StringToOffsetTable.h
@@ -52,6 +52,33 @@ 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 << "\n"
+       << Indent << "#ifdef __GNUC__\n"
+       << Indent << "#pragma GCC diagnostic push\n"
+       << Indent << "#pragma GCC diagnostic ignored \"-Woverlength-strings\"\n"
+       << Indent << "#endif\n"
+       << Indent << Decl << " = {\n";
+    bool LastNull = true;
+    for (char C : AggregateString) {
+      if (LastNull)
+        OS << Indent << "  \"";
+      LastNull = C == '\0';
+      if (LastNull)
+        OS << "\\0\"\n";
+      else
+        OS.write_escaped(StringRef(&C, 1));
+    }
+    OS << Indent << "};\n"
+       << Indent << "#ifdef __GNUC__\n"
+       << Indent << "#pragma GCC diagnostic pop\n"
+       << Indent << "#endif\n";
+  }
+
+  // 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