Skip to content

Commit dfa7ff9

Browse files
committed
[C++20] [Modules] [Reduced BMI] Combine the signature of used modules
into the current module Following of #86912. After #86912, with reduced BMI, the BMI can keep unchange if the dependent modules only changes the implementation (without introduing new decls). However, this is not strictly correct. For example: ``` // a.cppm export module a; export inline int a() { ... } // b.cppm export module b; import a; export inline int b() { return a(); } ``` Since both `a()` and `b()` are inline, we need to make sure the BMI of `b.pcm` will change after the implementation of `a()` changes. We can't get that naturally since we won't record the body of `a()` during the writing process. We can't reuse ODRHash here since ODRHash won't calculate the called function recursively. So ODRHash will be problematic if `a()` calls other inline functions. Probably we can solve this by a new hash mechanism. But the safety and efficiency may a problem too. Here we just combine the hash value of the used modules conservatively.
1 parent 4cce9fb commit dfa7ff9

File tree

4 files changed

+131
-25
lines changed

4 files changed

+131
-25
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ class ASTWriter : public ASTDeserializationListener,
357357
/// contexts.
358358
llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
359359

360+
/// The external top level module during the writing process. Used to
361+
/// generate signature for the module file being written.
362+
///
363+
/// Only meaningful for standard C++ named modules. See the comments in
364+
/// createSignatureForNamedModule() for details.
365+
llvm::DenseSet<Module *> TouchedTopLevelModules;
366+
360367
/// An update to a Decl.
361368
class DeclUpdate {
362369
/// A DeclUpdateKind.

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,31 @@ ASTFileSignature ASTWriter::createSignatureForNamedModule() const {
12001200
for (auto [ExportImported, _] : WritingModule->Exports)
12011201
Hasher.update(ExportImported->Signature);
12021202

1203+
// We combine all the used modules to make sure the signature is precise.
1204+
// Consider the case like:
1205+
//
1206+
// // a.cppm
1207+
// export module a;
1208+
// export inline int a() { ... }
1209+
//
1210+
// // b.cppm
1211+
// export module b;
1212+
// import a;
1213+
// export inline int b() { return a(); }
1214+
//
1215+
// Since both `a()` and `b()` are inline, we need to make sure the BMI of
1216+
// `b.pcm` will change after the implementation of `a()` changes. We can't
1217+
// get that naturally since we won't record the body of `a()` during the
1218+
// writing process. We can't reuse ODRHash here since ODRHash won't calculate
1219+
// the called function recursively. So ODRHash will be problematic if `a()`
1220+
// calls other inline functions.
1221+
//
1222+
// Probably we can solve this by a new hash mechanism. But the safety and
1223+
// efficiency may a problem too. Here we just combine the hash value of the
1224+
// used modules conservatively.
1225+
for (Module *M : TouchedTopLevelModules)
1226+
Hasher.update(M->Signature);
1227+
12031228
return ASTFileSignature::create(Hasher.result());
12041229
}
12051230

@@ -6112,8 +6137,12 @@ LocalDeclID ASTWriter::GetDeclRef(const Decl *D) {
61126137

61136138
// If D comes from an AST file, its declaration ID is already known and
61146139
// fixed.
6115-
if (D->isFromASTFile())
6140+
if (D->isFromASTFile()) {
6141+
if (isWritingStdCXXNamedModules() && D->getOwningModule())
6142+
TouchedTopLevelModules.insert(D->getOwningModule()->getTopLevelModule());
6143+
61166144
return LocalDeclID(D->getGlobalID());
6145+
}
61176146

61186147
assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
61196148
LocalDeclID &ID = DeclIDs[D];
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Test that, in C++20 modules reduced BMI, the implementation detail changes
2+
// in non-inline function may not propagate while the inline function changes
3+
// can get propagate.
4+
//
5+
// RUN: rm -rf %t
6+
// RUN: split-file %s %t
7+
// RUN: cd %t
8+
//
9+
// RUN: %clang_cc1 -std=c++20 %t/a.cppm -emit-reduced-module-interface -o %t/a.pcm
10+
// RUN: %clang_cc1 -std=c++20 %t/a.v1.cppm -emit-reduced-module-interface -o %t/a.v1.pcm
11+
//
12+
// The BMI of A should differ since the different implementation.
13+
// RUN: not diff %t/a.pcm %t/a.v1.pcm &> /dev/null
14+
//
15+
// The BMI of B should change since the dependent inline function changes
16+
// RUN: %clang_cc1 -std=c++20 %t/b.cppm -emit-reduced-module-interface -fmodule-file=a=%t/a.pcm \
17+
// RUN: -o %t/b.pcm
18+
// RUN: %clang_cc1 -std=c++20 %t/b.cppm -emit-reduced-module-interface -fmodule-file=a=%t/a.v1.pcm \
19+
// RUN: -o %t/b.v1.pcm
20+
// RUN: not diff %t/b.v1.pcm %t/b.pcm &> /dev/null
21+
//
22+
// Test the case with unused partitions.
23+
// RUN: %clang_cc1 -std=c++20 %t/M-A.cppm -emit-reduced-module-interface -o %t/M-A.pcm
24+
// RUN: %clang_cc1 -std=c++20 %t/M-B.cppm -emit-reduced-module-interface -o %t/M-B.pcm
25+
// RUN: %clang_cc1 -std=c++20 %t/M.cppm -emit-reduced-module-interface -o %t/M.pcm \
26+
// RUN: -fmodule-file=M:partA=%t/M-A.pcm \
27+
// RUN: -fmodule-file=M:partB=%t/M-B.pcm
28+
// RUN: %clang_cc1 -std=c++20 %t/N.cppm -emit-reduced-module-interface -o %t/N.pcm \
29+
// RUN: -fmodule-file=M:partA=%t/M-A.pcm \
30+
// RUN: -fmodule-file=M:partB=%t/M-B.pcm \
31+
// RUN: -fmodule-file=M=%t/M.pcm
32+
//
33+
// Now we change `M-A.cppm` to `M-A.v1.cppm`.
34+
// RUN: %clang_cc1 -std=c++20 %t/M-A.v1.cppm -emit-reduced-module-interface -o %t/M-A.v1.pcm
35+
// RUN: %clang_cc1 -std=c++20 %t/M.cppm -emit-reduced-module-interface -o %t/M.v1.pcm \
36+
// RUN: -fmodule-file=M:partA=%t/M-A.v1.pcm \
37+
// RUN: -fmodule-file=M:partB=%t/M-B.pcm
38+
// RUN: %clang_cc1 -std=c++20 %t/N.cppm -emit-reduced-module-interface -o %t/N.v1.pcm \
39+
// RUN: -fmodule-file=M:partA=%t/M-A.v1.pcm \
40+
// RUN: -fmodule-file=M:partB=%t/M-B.pcm \
41+
// RUN: -fmodule-file=M=%t/M.v1.pcm
42+
//
43+
// The BMI of N can keep unchanged since the N didn't use the changed partition unit 'M:A'.
44+
// RUN: diff %t/N.v1.pcm %t/N.pcm &> /dev/null
45+
46+
//--- a.cppm
47+
export module a;
48+
export inline int a() {
49+
return 48;
50+
}
51+
52+
//--- a.v1.cppm
53+
export module a;
54+
export inline int a() {
55+
return 50;
56+
}
57+
58+
//--- b.cppm
59+
export module b;
60+
import a;
61+
export inline int b() {
62+
return a();
63+
}
64+
65+
//--- M-A.cppm
66+
export module M:partA;
67+
export inline int a() {
68+
return 43;
69+
}
70+
71+
//--- M-A.v1.cppm
72+
export module M:partA;
73+
export inline int a() {
74+
return 50;
75+
}
76+
77+
//--- M-B.cppm
78+
export module M:partB;
79+
export inline int b() {
80+
return 44;
81+
}
82+
83+
//--- M.cppm
84+
export module M;
85+
export import :partA;
86+
export import :partB;
87+
88+
//--- N.cppm
89+
export module N;
90+
import M;
91+
92+
export inline int n() {
93+
return b();
94+
}

clang/test/Modules/no-transitive-source-location-change.cppm

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
// Testing that adding a new line in a module interface unit won't cause the BMI
22
// of consuming module unit changes.
33
//
4-
// RUN: rm -rf %t
5-
// RUN: split-file %s %t
6-
//
7-
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm
8-
// RUN: %clang_cc1 -std=c++20 %t/A.v1.cppm -emit-module-interface -o %t/A.v1.pcm
9-
//
10-
// The BMI may not be the same since the source location differs.
11-
// RUN: not diff %t/A.pcm %t/A.v1.pcm &> /dev/null
12-
//
13-
// The BMI of B shouldn't change since all the locations remain the same.
14-
// RUN: %clang_cc1 -std=c++20 %t/B.cppm -emit-module-interface -fmodule-file=A=%t/A.pcm \
15-
// RUN: -o %t/B.pcm
16-
// RUN: %clang_cc1 -std=c++20 %t/B.cppm -emit-module-interface -fmodule-file=A=%t/A.v1.pcm \
17-
// RUN: -o %t/B.v1.pcm
18-
// RUN: diff %t/B.v1.pcm %t/B.pcm &> /dev/null
19-
//
20-
// The BMI of C may change since the locations for instantiations changes.
21-
// RUN: %clang_cc1 -std=c++20 %t/C.cppm -emit-module-interface -fmodule-file=A=%t/A.pcm \
22-
// RUN: -o %t/C.pcm
23-
// RUN: %clang_cc1 -std=c++20 %t/C.cppm -emit-module-interface -fmodule-file=A=%t/A.v1.pcm \
24-
// RUN: -o %t/C.v1.pcm
25-
// RUN: not diff %t/C.v1.pcm %t/C.pcm &> /dev/null
26-
//
27-
// Test again with reduced BMI.
284
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-reduced-module-interface -o %t/A.pcm
295
// RUN: %clang_cc1 -std=c++20 %t/A.v1.cppm -emit-reduced-module-interface -o %t/A.v1.pcm
306
//

0 commit comments

Comments
 (0)