Skip to content

[C++20] [Modules] Named Module Units would generate initializers unconditionally #56794

@ChuanqiXu9

Description

@ChuanqiXu9

For, the following line

//--- A.cpp
export module A;

and option

clang++ -std=c++20 A.cppm --precompile -o A.pcm
clang++ -std=c++20 A.pcm -S -emit-llvm -o A.ll

It would generate:

@_ZGIW1A__in_chrg = internal global i8 0, align 1
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_ZGIW1A, ptr null }]

define void @_ZGIW1A() #0 section ".text.startup" {
entry:
  %0 = load i8, ptr @_ZGIW1A__in_chrg, align 1
  %guard.uninitialized = icmp eq i8 %0, 0
  br i1 %guard.uninitialized, label %init, label %exit, !prof !4

init:                                             ; preds = %entry
  store i8 1, ptr @_ZGIW1A__in_chrg, align 1
  br label %exit

exit:                                             ; preds = %init, %entry
  ret void
}

This is bad. A.cppm has nothing but it generates actual codes. It would be worse if we import something:

//--- B.cppm
export module B;
import A;

Then the generated code would be:

@_ZGIW1B__in_chrg = internal global i8 0, align 1
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_ZGIW1B, ptr null }]

declare void @_ZGIW1A()

define void @_ZGIW1B() #0 section ".text.startup" {
entry:
  %0 = load i8, ptr @_ZGIW1B__in_chrg, align 1
  %guard.uninitialized = icmp eq i8 %0, 0
  br i1 %guard.uninitialized, label %init, label %exit, !prof !4

init:                                             ; preds = %entry
  store i8 1, ptr @_ZGIW1B__in_chrg, align 1
  call void @_ZGIW1A()
  br label %exit

exit:                                             ; preds = %init, %entry
  ret void
}

Yeah, it calls the initializer from A. This is a call across TUs so it can't be inlined without LTO. As a result, the startup performance may decrease if there are many many imports.

This is not about the correctness, though.

Metadata

Metadata

Assignees

Labels

c++20clang:modulesC++20 modules and Clang Header Modules

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions