Issue 234970
Summary [clang][Modules] Reduced BMI: undefined reference to a defaulted constructor that an imported module used in constant evaluation
Labels clang
Assignees
Reporter mrwsl
    ## Version

- clang 23.1.3 (Arch Linux) and clang 23.1.0 (`ea7d852a70e8`), x86_64 Linux
- main: apt.llvm.org snapshot `e49bb1d1d2a3` and Compiler Explorer trunk `b3f0c0ca9469`

## Reproducer

Compiler Explorer: https://godbolt.org/z/z57Tb5xsM (trunk, and trunk with `-fno-modules-reduced-bmi`; the build log is in the output panes)

`base.h`:

```c++
#pragma once
struct Base {
  constexpr Base() noexcept {}
  constexpr virtual ~Base() noexcept {}
  constexpr Base( const Base& ) noexcept = default;
  constexpr Base( Base&& ) noexcept = default;
};
```

`m.cppm`:

```c++
module;
#include "base.h"
export module m;
export struct E : Base {
  int x = 0;
};
```

`a.cppm`:

```c++
module;
#include "base.h"
export module a;
import m;
consteval int moveInConstantEvaluation() {
  E e;
  E f = static_cast< E&& >( e );
  return f.x;
}
export int zero() { return moveInConstantEvaluation(); }
```

`main.cpp`:

```c++
import m;
import a;
E moveAtRuntime( E& e ) { return static_cast< E&& >( e ); }
int main() {
  E e;
  return moveAtRuntime( e ).x + zero();
}
```

```sh
clang++ -std=c++20 -c m.cppm -fmodule-output=m.pcm -o m.o
clang++ -std=c++20 -c a.cppm -fmodule-file=m=m.pcm -fmodule-output=a.pcm -o a.o
clang++ -std=c++20 -c main.cpp -fmodule-file=m=m.pcm -fmodule-file=a=a.pcm -o main.o
clang++ m.o a.o main.o
```

## Output

```
main.cpp:(.text._ZNW1m1EC2EOS0_[_ZNW1m1EC2EOS0_]+0x1d): undefined reference to `Base::Base(Base&&)'
```

`main.o` emits `E::E(E&&)`, but only references `Base::Base(Base&&)` (`U _ZN4BaseC2EOS_`). No other object defines it.

## Control experiments

Each row changes one thing in the reproducer above.

| Variant | `Base::Base(Base&&)` in `main.o` | Result |
| --- | --- | --- |
| as above | `U` | undefined reference |
| `-fno-modules-reduced-bmi` | `W` | links |
| two-phase compilation (`--precompile`, then compile the `.pcm`) | `W` | links |
| `a.cppm` without `#include "base.h"` | `W` | links |
| `a.cppm` moves in a `constexpr` variable initializer instead of a `consteval` function | `U` | undefined reference |
| `a.cppm` and `main.cpp` copy instead of move | | undefined reference to `Base::Base(const Base&)` |
| `a.cppm` moves only in an unevaluated operand (`noexcept( ... )`) | `W` | links |
| `a.cppm` moves at runtime instead | `U` | links, only because `a.o` defines it |

So an importer doesn't emit a defaulted constructor of a global module fragment class, if an imported module with a reduced BMI defined it during constant evaluation and has the class in its own global module fragment.

## Context

Found with libstdc++ 16 in C++26, where the copy / move constructors of `std::exception` are `constexpr ... = default`. A module that moves a `std::expected< T, E >` with `E : std::exception` during constant evaluation, and includes any std header that declares `std::exception` (e.g. `<string>`), makes every importer that moves an `E` at runtime fail to link with `undefined reference to std::exception::exception(std::exception&&)`.

Maybe related: #233468 (reduced BMI of an intermediate module, the importer crashes while generating an implicit destructor).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to