From 1ecc5ae13b9061cfb37ef0c409c8a4fc4bad42c9 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Mon, 24 Jun 2024 11:03:55 +0800 Subject: [PATCH] [Serialization] Register Speical types before register decls We will only regsiter top level types and decls in ASTWriter and we will register the sub types and decls during the process of writing types and decls. So that the ID for the types in the sub level can be different if the writing decl process changes the order of the to-be- emitted type queues. This is not ideal since it causes unnecessary changes especially in no transitive changes model. This patch migrates the issue by regsitering special types before regsitering decls. This make sure that the special types in the 2nd top level can be registered early than the decls. But it might still be problematic if there are more levels in the special types. Luckily we just don't have such special types. --- clang/lib/Serialization/ASTWriter.cpp | 22 +++++++-------- .../Modules/no-implicit-declarations.cppm | 4 +-- .../Modules/no-transitive-decl-change-2.cppm | 28 +++++++++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 clang/test/Modules/no-transitive-decl-change-2.cppm diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index d99d85652c7b..5b39055cf9f2 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -5387,6 +5387,17 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot, } } + // Form the record of special types. + RecordData SpecialTypes; + AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); + AddTypeRef(Context.getFILEType(), SpecialTypes); + AddTypeRef(Context.getjmp_bufType(), SpecialTypes); + AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes); + AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes); + AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes); + AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes); + AddTypeRef(Context.getucontext_tType(), SpecialTypes); + PrepareWritingSpecialDecls(SemaRef); // Write the control block @@ -5418,17 +5429,6 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot, for (auto &Selector : AllSelectors) SemaRef.ObjC().updateOutOfDateSelector(Selector); - // Form the record of special types. - RecordData SpecialTypes; - AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); - AddTypeRef(Context.getFILEType(), SpecialTypes); - AddTypeRef(Context.getjmp_bufType(), SpecialTypes); - AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes); - AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes); - AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes); - AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes); - AddTypeRef(Context.getucontext_tType(), SpecialTypes); - if (Chain) { // Write the mapping information describing our module dependencies and how // each of those modules were mapped into our own offset/ID space, so that diff --git a/clang/test/Modules/no-implicit-declarations.cppm b/clang/test/Modules/no-implicit-declarations.cppm index 79c3c5e76f63..a5129d40194d 100644 --- a/clang/test/Modules/no-implicit-declarations.cppm +++ b/clang/test/Modules/no-implicit-declarations.cppm @@ -16,11 +16,11 @@ export int a = 43; // CHECK: diff --git a/clang/test/Modules/no-transitive-decl-change-2.cppm b/clang/test/Modules/no-transitive-decl-change-2.cppm new file mode 100644 index 000000000000..fe29d9169201 --- /dev/null +++ b/clang/test/Modules/no-transitive-decl-change-2.cppm @@ -0,0 +1,28 @@ +// Test that adding a new unused decl within reduced BMI may not produce a transitive change. +// +// RUN: rm -rf %t +// RUN: split-file %s %t +// +// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/A.cppm -o %t/A.pcm +// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/B.cppm -o %t/B.pcm \ +// RUN: -fmodule-file=A=%t/A.pcm +// +// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/A.v1.cppm -o %t/A.v1.pcm +// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/B.cppm -o %t/B.v1.pcm \ +// RUN: -fmodule-file=A=%t/A.v1.pcm +// +// RUN: diff %t/B.pcm %t/B.v1.pcm &> /dev/null + +//--- A.cppm +export module A; +export int a() { return 44; } + +//--- A.v1.cppm +export module A; +int a_impl() { return 48; } +export int a() { return a_impl(); } + +//--- B.cppm +export module B; +import A; +export int b() { return a(); }