Skip to content

Commit

Permalink
[C++20] [Modules] Fix crash when emitting module inits for duplicated…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
ChuanqiXu9 committed Oct 2, 2023
1 parent 7bc09a4 commit 3f09273
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
8 changes: 4 additions & 4 deletions clang/lib/CodeGen/CGDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,13 +666,13 @@ void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
// Module initializers for imported modules are emitted first.

// Collect all the modules that we import
SmallVector<Module *> AllImports;
llvm::SmallSetVector<Module *, 8> AllImports;
// Ones that we export
for (auto I : Primary->Exports)
AllImports.push_back(I.getPointer());
AllImports.insert(I.getPointer());
// Ones that we only import.
for (Module *M : Primary->Imports)
AllImports.push_back(M);
AllImports.insert(M);
// Ones that we import in the global module fragment or the private module
// fragment.
llvm::for_each(Primary->submodules(), [&AllImports](Module *SubM) {
Expand All @@ -683,7 +683,7 @@ void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
"The global mdoule fragments and the private module fragments are "
"not allowed to export import modules.");
for (Module *M : SubM->Imports)
AllImports.push_back(M);
AllImports.insert(M);
});

SmallVector<llvm::Function *, 8> ModuleInits;
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2938,6 +2938,9 @@ void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
// Third any associated with the Privat eMOdule Fragment, if present.
if (auto PMF = Primary->getPrivateModuleFragment()) {
for (Decl *D : getContext().getModuleInitializers(PMF)) {
// Skip import decls, the inits for those are called explicitly.
if (isa<ImportDecl>(D))
continue;
assert(isa<VarDecl>(D) && "PMF initializer decl is not a var?");
EmitTopLevelDecl(D);
}
Expand Down
27 changes: 27 additions & 0 deletions clang/test/Modules/module-init-duplicated-import.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: cd %t
//
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cppm \
// RUN: -emit-module-interface -o %t/a.pcm
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.cppm \
// RUN: -emit-module-interface -fmodule-file=a=%t/a.pcm -o %t/m.pcm
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.pcm \
// RUN: -S -emit-llvm -o - | FileCheck %t/m.cppm

//--- a.cppm
export module a;
export struct A {
A(){};
};
export A __dynamic_inited_a;

//--- m.cppm
export module m;
import a;
export import a;


// CHECK: define void @_ZGIW1m
// CHECK: store i8 1, ptr @_ZGIW1m__in_chrg
// CHECK: call{{.*}}@_ZGIW1a
29 changes: 29 additions & 0 deletions clang/test/Modules/pr67893.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: cd %t
//
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cppm \
// RUN: -emit-module-interface -o %t/a.pcm
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.cppm \
// RUN: -emit-module-interface -fmodule-file=a=%t/a.pcm -o %t/m.pcm
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.pcm \
// RUN: -S -emit-llvm -o - | FileCheck %t/m.cppm

//--- a.cppm
export module a;
export struct A {
A(){};
};
export A __dynamic_inited_a;

//--- m.cppm
module;
import a;
export module m;
import a;
module :private;
import a;

// CHECK: define void @_ZGIW1m
// CHECK: store i8 1, ptr @_ZGIW1m__in_chrg
// CHECK: call{{.*}}@_ZGIW1a

0 comments on commit 3f09273

Please sign in to comment.