Skip to content

Commit 5d4e072

Browse files
committed
[C++20] [Modules] [Reduced BMI] Don't eagerly write static entities in
module purview For, ``` export module A; static int impl() { ... } export int func() { return impl(); } ``` Previously, even with reduced BMI, the function `impl` will be emitted into the BMI. After the patch, the static entities in module purview won't get emitted eagerly. Now the static entities may only be emitted if required. Note that, this restriction is actually more relaxed than the language standard required. The language spec said, the program is ill-formed if any TU-local entities get exposed. However, we can't do this since there are many static entities in the headers of existing libraries. Forbidding that will cause many existing program fail immediately. Another note here is, we can't do this for non-static non-exported entities, they can be used for other module units within the same module.
1 parent 135472b commit 5d4e072

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4892,8 +4892,21 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
48924892
if (D->isFromASTFile())
48934893
continue;
48944894

4895-
if (GeneratingReducedBMI && D->isFromExplicitGlobalModule())
4896-
continue;
4895+
if (GeneratingReducedBMI) {
4896+
if (D->isFromExplicitGlobalModule())
4897+
continue;
4898+
4899+
// Don't force emitting static entities.
4900+
//
4901+
// Technically, all static entities shouldn't be in reduced BMI. The
4902+
// language also specifies that the program exposes TU-local entities
4903+
// is ill-formed. However, in practice, there are a lot of projects
4904+
// uses `static inline` in the headers. So we can't get rid of all
4905+
// static entities in reduced BMI now.
4906+
if (auto *ND = dyn_cast<NamedDecl>(D);
4907+
ND && ND->getFormalLinkage() == Linkage::Internal)
4908+
continue;
4909+
}
48974910

48984911
GetDeclRef(D);
48994912
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test that the static function only used in non-inline functions won't get emitted
2+
// into the BMI.
3+
//
4+
// RUN: rm -rf %t
5+
// RUN: mkdir -p %t
6+
//
7+
// RUN: %clang_cc1 -std=c++20 %s -emit-reduced-module-interface -o %t/S.pcm
8+
// RUN: llvm-bcanalyzer --dump --disable-histogram --show-binary-blobs %t/S.pcm > %t/S.dump
9+
// RUN: cat %t/S.dump | FileCheck %s
10+
11+
export module S;
12+
static int static_func() {
13+
return 43;
14+
}
15+
16+
export int func() {
17+
return static_func();
18+
}
19+
20+
// CHECK: <DECL_FUNCTION
21+
// Checks that we won't see a second function
22+
// CHECK-NOT: <DECL_FUNCTION

0 commit comments

Comments
 (0)