Skip to content

Commit

Permalink
[C++20] [Modules] Skip calls to module initializer to modules if we k…
Browse files Browse the repository at this point in the history
…now 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.
  • Loading branch information
ChuanqiXu9 committed Jul 2, 2024
1 parent 91c0ef6 commit 7ee421d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/CodeGen/CGDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,10 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
// No Itanium initializer in header like modules.
if (M->isHeaderLikeModule())
continue;
// We're allowed to skip the initialization if we are sure it doesn't
// do any thing.
if (!M->isNamedModuleInterfaceHasInit())
continue;
llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
SmallString<256> FnName;
{
Expand Down
30 changes: 30 additions & 0 deletions clang/test/Modules/pr97244.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// REQUIRES: !system-windows
//
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Empty.cppm \
// RUN: -emit-module-interface -o %t/Empty.pcm
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Empty2.cppm \
// RUN: -fprebuilt-module-path=%t -emit-module-interface -o %t/Empty2.pcm
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/main.cpp \
// RUN: -fprebuilt-module-path=%t -emit-llvm -o - | FileCheck %t/main.cpp
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Empty2.pcm \
// RUN: -fprebuilt-module-path=%t -emit-llvm -o - | FileCheck %t/Empty2.cppm

//--- Empty.cppm
export module Empty;

//--- Empty2.cppm
export module Empty2;
import Empty;

// CHECK-NOT: _ZGIW5Empty

//--- main.cpp
import Empty;
import Empty2;

// CHECK-NOT: _ZGIW5Empty
// CHECK-NOT: _ZGIW6Empty2

0 comments on commit 7ee421d

Please sign in to comment.