[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:33:56 PDT 2024


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

>From 00ebf607191f3b4ad3903fa2058712511650da72 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       | 30 +++++++++++++++++++
 llvm/utils/TableGen/IntrinsicEmitter.cpp      |  4 +--
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/TableGen/StringToOffsetTable.h b/llvm/include/llvm/TableGen/StringToOffsetTable.h
index 7fb9d02d77c704..b0738d1bfa0633 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,35 @@ 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"(
+{0}#ifdef __GNUC__
+{0}#pragma GCC diagnostic push
+{0}#pragma GCC diagnostic ignored "-Woverlength-strings"
+{0}#endif
+{0}{1} = {{
+
+)",
+                  Indent, Decl);
+
+    for (StringRef Str : split(AggregateString, '\0')) {
+      OS << Indent << "  \"";
+      OS.write_escaped(Str);
+      OS << "\\0\"\n";
+    }
+    OS << formatv(R"(
+{0}};
+{0}#ifdef __GNUC__
+{0}#pragma GCC diagnostic pop
+{0}#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