Skip to content

Commit 7ee421d

Browse files
committed
[C++20] [Modules] Skip calls to module initializer to modules if we know the module doesn't init anything
Close #97244 This is an optimization allowed by the modules's ABI to skip calls to imported modules for which we know nothing will be initialized.
1 parent 91c0ef6 commit 7ee421d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,10 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
840840
// No Itanium initializer in header like modules.
841841
if (M->isHeaderLikeModule())
842842
continue;
843+
// We're allowed to skip the initialization if we are sure it doesn't
844+
// do any thing.
845+
if (!M->isNamedModuleInterfaceHasInit())
846+
continue;
843847
llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
844848
SmallString<256> FnName;
845849
{

clang/test/Modules/pr97244.cppm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// REQUIRES: !system-windows
2+
//
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t
5+
// RUN: split-file %s %t
6+
//
7+
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Empty.cppm \
8+
// RUN: -emit-module-interface -o %t/Empty.pcm
9+
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Empty2.cppm \
10+
// RUN: -fprebuilt-module-path=%t -emit-module-interface -o %t/Empty2.pcm
11+
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/main.cpp \
12+
// RUN: -fprebuilt-module-path=%t -emit-llvm -o - | FileCheck %t/main.cpp
13+
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Empty2.pcm \
14+
// RUN: -fprebuilt-module-path=%t -emit-llvm -o - | FileCheck %t/Empty2.cppm
15+
16+
//--- Empty.cppm
17+
export module Empty;
18+
19+
//--- Empty2.cppm
20+
export module Empty2;
21+
import Empty;
22+
23+
// CHECK-NOT: _ZGIW5Empty
24+
25+
//--- main.cpp
26+
import Empty;
27+
import Empty2;
28+
29+
// CHECK-NOT: _ZGIW5Empty
30+
// CHECK-NOT: _ZGIW6Empty2

0 commit comments

Comments
 (0)