Skip to content

Commit 3f09273

Browse files
committed
[C++20] [Modules] Fix crash when emitting module inits for duplicated modules
Close #67893 The root cause of the crash is an oversight that we missed the point that the same module can be imported multiple times. And we should use `SmallSetVector` instead of `SmallVector` to filter the case.
1 parent 7bc09a4 commit 3f09273

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,13 @@ void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
666666
// Module initializers for imported modules are emitted first.
667667

668668
// Collect all the modules that we import
669-
SmallVector<Module *> AllImports;
669+
llvm::SmallSetVector<Module *, 8> AllImports;
670670
// Ones that we export
671671
for (auto I : Primary->Exports)
672-
AllImports.push_back(I.getPointer());
672+
AllImports.insert(I.getPointer());
673673
// Ones that we only import.
674674
for (Module *M : Primary->Imports)
675-
AllImports.push_back(M);
675+
AllImports.insert(M);
676676
// Ones that we import in the global module fragment or the private module
677677
// fragment.
678678
llvm::for_each(Primary->submodules(), [&AllImports](Module *SubM) {
@@ -683,7 +683,7 @@ void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
683683
"The global mdoule fragments and the private module fragments are "
684684
"not allowed to export import modules.");
685685
for (Module *M : SubM->Imports)
686-
AllImports.push_back(M);
686+
AllImports.insert(M);
687687
});
688688

689689
SmallVector<llvm::Function *, 8> ModuleInits;

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,6 +2938,9 @@ void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
29382938
// Third any associated with the Privat eMOdule Fragment, if present.
29392939
if (auto PMF = Primary->getPrivateModuleFragment()) {
29402940
for (Decl *D : getContext().getModuleInitializers(PMF)) {
2941+
// Skip import decls, the inits for those are called explicitly.
2942+
if (isa<ImportDecl>(D))
2943+
continue;
29412944
assert(isa<VarDecl>(D) && "PMF initializer decl is not a var?");
29422945
EmitTopLevelDecl(D);
29432946
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: cd %t
4+
//
5+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cppm \
6+
// RUN: -emit-module-interface -o %t/a.pcm
7+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.cppm \
8+
// RUN: -emit-module-interface -fmodule-file=a=%t/a.pcm -o %t/m.pcm
9+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.pcm \
10+
// RUN: -S -emit-llvm -o - | FileCheck %t/m.cppm
11+
12+
//--- a.cppm
13+
export module a;
14+
export struct A {
15+
A(){};
16+
};
17+
export A __dynamic_inited_a;
18+
19+
//--- m.cppm
20+
export module m;
21+
import a;
22+
export import a;
23+
24+
25+
// CHECK: define void @_ZGIW1m
26+
// CHECK: store i8 1, ptr @_ZGIW1m__in_chrg
27+
// CHECK: call{{.*}}@_ZGIW1a

clang/test/Modules/pr67893.cppm

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: cd %t
4+
//
5+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cppm \
6+
// RUN: -emit-module-interface -o %t/a.pcm
7+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.cppm \
8+
// RUN: -emit-module-interface -fmodule-file=a=%t/a.pcm -o %t/m.pcm
9+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.pcm \
10+
// RUN: -S -emit-llvm -o - | FileCheck %t/m.cppm
11+
12+
//--- a.cppm
13+
export module a;
14+
export struct A {
15+
A(){};
16+
};
17+
export A __dynamic_inited_a;
18+
19+
//--- m.cppm
20+
module;
21+
import a;
22+
export module m;
23+
import a;
24+
module :private;
25+
import a;
26+
27+
// CHECK: define void @_ZGIW1m
28+
// CHECK: store i8 1, ptr @_ZGIW1m__in_chrg
29+
// CHECK: call{{.*}}@_ZGIW1a

0 commit comments

Comments
 (0)