llvm-project/clang/test/Modules/placement-new-reachable.cpp
Chuanqi Xu f21ead0675
[C++20] [Modules] [Reduced BMI] Remove unreachable decls GMF in redued BMI (#88359)
Following of https://github.com/llvm/llvm-project/pull/76930

This follows the idea of "only writes what we writes", which I think is
the most natural and efficient way to implement this optimization.

We start writing the BMI from the first declaration in module purview
instead of the global module fragment, so that everything in the GMF
untouched won't be written in the BMI naturally.

The exception is, as I said in
https://github.com/llvm/llvm-project/pull/76930, when we write a
declaration we need to write its decl context, and when we write the
decl context, we need to write everything from it. So when we see
`std::vector`, we basically need to write everything under namespace
std. This violates our intention. To fix this, this patch delays the
writing of namespace in the GMF.

From my local measurement, the size of the BMI decrease to 90M from 112M
for a local modules build. I think this is significant.

This feature will be covered under the experimental reduced BMI so that
it won't affect any existing users. So I'd like to land this when the CI
gets green.

Documents will be added seperately.
2024-04-12 12:51:58 +08:00

48 lines
1.3 KiB
C++

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm
// RUN: %clang_cc1 -std=c++20 %t/Use.cpp -fprebuilt-module-path=%t -fsyntax-only -verify
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-reduced-module-interface -o %t/A.pcm
// RUN: %clang_cc1 -std=c++20 %t/Use.cpp -fprebuilt-module-path=%t -fsyntax-only -verify
//--- placement.h
namespace std {
using size_t = decltype(sizeof(0));
}
void *operator new(std::size_t, void *p) { return p; }
//--- A.cppm
module;
#include "placement.h"
export module A;
export template<class T>
struct A {
A(void *p) : ptr(new (p) T(43)) {}
private:
void *ptr;
};
export struct B {
B(void *p) : ptr(new (p) int(43)) {}
private:
void *ptr;
};
// The use of operator new in the current module unit is only in the non-inline
// function definitions. So it may be optimized out.
using ::operator new;
//--- Use.cpp
// expected-no-diagnostics
import A;
void bar(int *);
void foo(void *ptr) {
A<int> a(nullptr); // Good. It should be OK to construct A.
B b(nullptr);
void *p = ::operator new(sizeof(int), ptr); // Bad. The placement allocation in module A is not visible.
void *q = new (ptr) int(43); // Good. We don't call the placement allocation function directly.
}