https://github.com/steffenlarsen updated https://github.com/llvm/llvm-project/pull/237198
>From ff062e7191bbccd001902ff41eee5b853455ee9e Mon Sep 17 00:00:00 2001 From: Steffen Holst Larsen <[email protected]> Date: Mon, 28 Sep 2026 05:27:33 -0500 Subject: [PATCH] [Clang][PCH] Use llvm.raw.sections for embedding the AST To avoid the size restriction that may be imposed by the address spaces of the target, embed the serialized Clang AST through the `llvm.raw.sections` named metadata instead of placing it in the `__clang_ast` global variable. The section names and the 8-byte alignment are unchanged. Second and last part of #214135. Assisted-by: Claude Opus 5.5 Signed-off-by: Steffen Holst Larsen <[email protected]> --- .../CodeGen/ObjectFilePCHContainerWriter.cpp | 35 ++++++++++--------- clang/test/Modules/gmodules-nodebug.cpp | 1 + clang/test/Modules/lsv-debuginfo.cpp | 9 +++-- clang/test/Modules/pch_container.m | 2 +- clang/test/PCH/pch-clangast-raw-section.c | 6 ++++ 5 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 clang/test/PCH/pch-clangast-raw-section.c diff --git a/clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp b/clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp index 88ab809e78a8e8..5196c1de57f788 100644 --- a/clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp +++ b/clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp @@ -297,25 +297,28 @@ class PCHContainerGenerator : public ASTConsumer { auto *NameAndContent = llvm::MDTuple::get(*VMContext, Ops); MD->addOperand(NameAndContent); } else { - auto Int8Ty = llvm::Type::getInt8Ty(*VMContext); - auto *Ty = llvm::ArrayType::get(Int8Ty, Size); - auto *Data = llvm::ConstantDataArray::getString( - *VMContext, StringRef(SerializedAST.data(), Size), - /*AddNull=*/false); - auto *ASTSym = new llvm::GlobalVariable( - *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, - Data, "__clang_ast"); - // The on-disk hashtable needs to be aligned. - ASTSym->setAlignment(llvm::Align(8)); - - // Mach-O also needs a segment name. + // Emit the serialized AST into a named section via llvm.raw.sections + // metadata, which the AsmPrinter emits directly at the MC layer. + // This avoids IR-level size constraints from the target's address space. + llvm::NamedMDNode *RawSections = + M->getOrInsertNamedMetadata("llvm.raw.sections"); + + StringRef SectionName; if (Triple.isOSBinFormatMachO()) - ASTSym->setSection("__CLANG,__clangast"); - // COFF has an eight character length limit. + SectionName = "__CLANG,__clangast"; else if (Triple.isOSBinFormatCOFF()) - ASTSym->setSection("clangast"); + SectionName = "clangast"; else - ASTSym->setSection("__clangast"); + SectionName = "__clangast"; + + auto *Int32Ty = llvm::Type::getInt32Ty(*VMContext); + llvm::Metadata *Ops[] = { + llvm::MDString::get(*VMContext, SectionName), + llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 8)), + llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0)), + llvm::MDString::get(*VMContext, + StringRef(SerializedAST.data(), Size))}; + RawSections->addOperand(llvm::MDTuple::get(*VMContext, Ops)); } LLVM_DEBUG({ diff --git a/clang/test/Modules/gmodules-nodebug.cpp b/clang/test/Modules/gmodules-nodebug.cpp index d83103768e838a..1be0dddfa149bd 100644 --- a/clang/test/Modules/gmodules-nodebug.cpp +++ b/clang/test/Modules/gmodules-nodebug.cpp @@ -12,3 +12,4 @@ __void_t<> func() {} // CHECK: !DICompileUnit // CHECK-NOT: __void_t +// CHECK: !{!"{{(__CLANG,)?(__)?}}clangast", diff --git a/clang/test/Modules/lsv-debuginfo.cpp b/clang/test/Modules/lsv-debuginfo.cpp index 40455727ecddae..189266f4ae324a 100644 --- a/clang/test/Modules/lsv-debuginfo.cpp +++ b/clang/test/Modules/lsv-debuginfo.cpp @@ -21,20 +21,23 @@ // RUN: cat %t-mod.ll | FileCheck %s // ADT -// CHECK: @__clang_ast = +// CHECK: !llvm.raw.sections = !{[[ADT_SEC:![0-9]+]]} +// CHECK: [[ADT_SEC]] = !{!"{{(__CLANG,)?(__)?}}clangast", // B -// CHECK: @__clang_ast = +// CHECK: !llvm.raw.sections = !{[[B_SEC:![0-9]+]]} // This type isn't anchored anywhere, expect a full definition. // CHECK: !DICompositeType({{.*}}, name: "AlignedCharArray<4U, 16U>", // CHECK-SAME: elements: +// CHECK: [[B_SEC]] = !{!"{{(__CLANG,)?(__)?}}clangast", // C -// CHECK: @__clang_ast = +// CHECK: !llvm.raw.sections = !{[[C_SEC:![0-9]+]]} // Here, too. // CHECK: !DICompositeType({{.*}}, name: "AlignedCharArray<4U, 16U>", // CHECK-SAME: elements: +// CHECK: [[C_SEC]] = !{!"{{(__CLANG,)?(__)?}}clangast", #include <B/B.h> diff --git a/clang/test/Modules/pch_container.m b/clang/test/Modules/pch_container.m index ed13baf2d26cef..c75af3cd451715 100644 --- a/clang/test/Modules/pch_container.m +++ b/clang/test/Modules/pch_container.m @@ -11,7 +11,7 @@ // CHECK: file format mach-o 64-bit x86-64 // CHECK: __clangast {{[0-9a-f]+}} {{[0-9a-f]+}} DATA // CHECK: file format elf64-x86-64 -// CHECK: __clangast {{[0-9a-f]+}} {{[0-9a-f]+}} DATA +// CHECK: __clangast {{[0-9a-f]+}} {{[0-9a-f]+}} // CHECK: file format coff-x86-64 // CHECK: clangast {{[0-9a-f]+}} {{[0-9a-f]+}} diff --git a/clang/test/PCH/pch-clangast-raw-section.c b/clang/test/PCH/pch-clangast-raw-section.c new file mode 100644 index 00000000000000..500fa53cfb8010 --- /dev/null +++ b/clang/test/PCH/pch-clangast-raw-section.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-pch -fmodule-format=obj %S/pchpch1.h -o - | llvm-readelf --sections - | FileCheck %s + +// Ensure the serialized AST is emitted via llvm.raw.sections metadata into +// a __clangast section with 8-byte alignment. + +// CHECK: __clangast PROGBITS {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} 00 0 0 8 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
