https://github.com/dzbarsky created 
https://github.com/llvm/llvm-project/pull/202844

Collect fixed language-option and sanitizer values in a `uint64_t` local array 
and append the array to the control-block record once. Generated values retain 
their serialized order, with compile-time checks that each LangOption fits the 
record element width.

Linked `clang` and `clangd` shrink by 91,584 and 91,520 bytes respectively; 
`ASTWriter.cpp.o` shrinks by 25,128 bytes with 396 fewer relocations, while 
linked fixups are unchanged.

PCH and PCM outputs are byte-identical and cross-import successfully, five 
canonical module/PCH tests pass, and batched PCH and module emission show no 
significant performance change.

Work towards #202616

AI tool disclosure: Co-authored with OpenAI Codex.


>From e939eac0ddafd7eb0324a0b03957b6bb3a1dfd7b Mon Sep 17 00:00:00 2001
From: David Zbarsky <[email protected]>
Date: Tue, 9 Jun 2026 23:52:34 -0400
Subject: [PATCH] [clang][Serialization] Batch serialized LangOptions values

Collect fixed language-option and sanitizer values in a uint64_t local array 
and append the array to the control-block record once. Generated values retain 
their serialized order, and compile-time assertions ensure each LangOption fits 
the record element width.

ASTWriter.cpp.o shrinks by 25,128 bytes, including 22,052 bytes of __text, and 
has 396 fewer relocations. Controlled links shrink clang by 91,584 bytes and 
clangd by 91,520 bytes; stripped sizes fall by 49,936 and 49,944 bytes, and 
linked fixups are unchanged.

PCH and PCM outputs are byte-identical and cross-import successfully. Five 
canonical Clang module/PCH tests pass. Batched PCH and module emission changes 
were -0.66% and +0.18%, with confidence intervals spanning zero; import 
performance was also unchanged.
---
 clang/lib/Serialization/ASTWriter.cpp | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index 257aee8728fed..976f5d2accc4c 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1608,14 +1608,21 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, 
StringRef isysroot) {
   // Language options.
   Record.clear();
   const LangOptions &LangOpts = PP.getLangOpts();
-#define LANGOPT(Name, Bits, Default, Compatibility, Description)               
\
-  Record.push_back(LangOpts.Name);
+  const uint64_t LanguageOptionValues[] = {
+#define LANGOPT(Name, Bits, Default, Compatibility, Description) LangOpts.Name,
 #define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description)    
\
-  Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
+  static_cast<unsigned>(LangOpts.get##Name()),
 #include "clang/Basic/LangOptions.def"
-#define SANITIZER(NAME, ID)                                                    
\
-  Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
+#define SANITIZER(NAME, ID) LangOpts.Sanitize.has(SanitizerKind::ID),
 #include "clang/Basic/Sanitizers.def"
+  };
+#define LANGOPT(Name, Bits, Default, Compatibility, Description)               
\
+  static_assert(Bits <= 64);
+#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description)    
\
+  static_assert(Bits <= 64);
+#include "clang/Basic/LangOptions.def"
+  static_assert(sizeof(LanguageOptionValues[0]) == sizeof(Record[0]));
+  llvm::append_range(Record, LanguageOptionValues);
 
   Record.push_back(LangOpts.ModuleFeatures.size());
   for (StringRef Feature : LangOpts.ModuleFeatures)

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to