mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-05-14 01:46:41 +00:00

Previously, distinct lambdas would get merged, and multiple definitions of the same lambda would not get merged, because we attempted to identify lambdas by their ordinal position within their lexical DeclContext. This failed for lambdas within namespace-scope variables and within variable templates, where the lexical position in the context containing the variable didn't uniquely identify the lambda. In this patch, we instead identify lambda closure types by index within their context declaration, which does uniquely identify them in a way that's consistent across modules. This change causes a deserialization cycle between the type of a variable with deduced type and a lambda appearing as the initializer of the variable -- reading the variable's type requires reading and merging the lambda, and reading the lambda requires reading and merging the variable. This is addressed by deferring loading the deduced type of a variable until after we finish recursive deserialization. This also exposes a pre-existing subtle issue where loading a variable declaration would trigger immediate loading of its initializer, which could recursively refer back to properties of the variable. This particularly causes problems if the initializer contains a lambda-expression, but can be problematic in general. That is addressed by switching to lazily loading the initializers of variables rather than always loading them with the variable declaration. As well as fixing a deserialization cycle, that should improve laziness of deserialization in general. LambdaDefinitionData had 63 spare bits in it, presumably caused by an off-by-one-error in some previous change. This change claims 32 of those bits as a counter for the lambda within its context. We could probably move the numbering to separate storage, like we do for the device-side mangling number, to optimize the likely-common case where all three numbers (host-side mangling number, device-side mangling number, and index within the context declaration) are zero, but that's not done in this change. Fixes #60985. Reviewed By: #clang-language-wg, aaron.ballman Differential Revision: https://reviews.llvm.org/D145737
121 lines
3.7 KiB
C++
121 lines
3.7 KiB
C++
// RUN: rm -rf %t
|
|
// RUN: mkdir -p %t
|
|
// RUN: split-file %s %t
|
|
//
|
|
// RUN: %clang_cc1 -std=c++20 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%t/module.modulemap %t/use.cpp -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s
|
|
// RUN: %clang_cc1 -DDEFINE_LOCALLY -std=c++20 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%t/module.modulemap %t/use.cpp -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s
|
|
|
|
//--- module.modulemap
|
|
module a { header "a.h" export * }
|
|
module b { header "b.h" export * }
|
|
module c { header "c.h" export * }
|
|
|
|
//--- nonmodular.h
|
|
void not_constant();
|
|
|
|
template<typename T> struct A {
|
|
template<T M> static inline T N = [] { not_constant(); return M; } ();
|
|
};
|
|
|
|
template<typename T, T M> inline T N = [] { not_constant(); return M; } ();
|
|
|
|
template<typename T, T M> inline auto L = [] {};
|
|
|
|
template<typename T> int Z;
|
|
|
|
// These lambdas should not be merged, despite having the same context decl and
|
|
// mangling number (but different signatures).
|
|
inline auto MultipleLambdas = ((void)[](int*) { return 1; }, [] { return 2; });
|
|
|
|
//--- a.h
|
|
#include "nonmodular.h"
|
|
|
|
//--- b.h
|
|
#include "a.h"
|
|
|
|
int b1() { return A<int>::N<1>; }
|
|
int b2() { return N<int, 1>; }
|
|
|
|
inline auto x1 = L<int, 1>;
|
|
inline auto x2 = L<int, 2>;
|
|
|
|
inline constexpr int *P = &Z<decltype([] { static int n; return &n; }())>;
|
|
inline constexpr int *xP = P;
|
|
|
|
static_assert(!__is_same(decltype(x1), decltype(x2)));
|
|
|
|
//--- c.h
|
|
#include "a.h"
|
|
|
|
int c1() { return A<int>::N<2>; }
|
|
int c2() { return N<int, 2>; }
|
|
|
|
inline auto y2 = L<int, 2>;
|
|
inline auto y1 = L<int, 1>;
|
|
|
|
inline constexpr int *P = &Z<decltype([] { static int n; return &n; }())>;
|
|
inline constexpr int *yP = P;
|
|
|
|
//--- use.cpp
|
|
#ifdef DEFINE_LOCALLY
|
|
#include "nonmodular.h"
|
|
|
|
inline constexpr int *P = &Z<decltype([] { static int n; return &n; }())>;
|
|
inline constexpr int *zP = P;
|
|
|
|
auto z0 = L<int, 0>;
|
|
auto z2 = L<int, 2>;
|
|
auto z1 = L<int, 1>;
|
|
#endif
|
|
|
|
#include "b.h"
|
|
#include "c.h"
|
|
|
|
int b1v = b1();
|
|
int b2v = b2();
|
|
int c1v = c1();
|
|
int c2v = c2();
|
|
|
|
// We should merge together matching lambdas.
|
|
static_assert(__is_same(decltype(x1), decltype(y1)));
|
|
static_assert(__is_same(decltype(x2), decltype(y2)));
|
|
static_assert(!__is_same(decltype(x1), decltype(x2)));
|
|
static_assert(!__is_same(decltype(y1), decltype(y2)));
|
|
static_assert(!__is_same(decltype(x1), decltype(y2)));
|
|
static_assert(!__is_same(decltype(x2), decltype(y1)));
|
|
static_assert(xP == yP);
|
|
#ifdef DEFINE_LOCALLY
|
|
static_assert(!__is_same(decltype(x1), decltype(z0)));
|
|
static_assert(!__is_same(decltype(x2), decltype(z0)));
|
|
static_assert(__is_same(decltype(x1), decltype(z1)));
|
|
static_assert(__is_same(decltype(x2), decltype(z2)));
|
|
static_assert(xP == zP);
|
|
#endif
|
|
|
|
static_assert(MultipleLambdas() == 2);
|
|
|
|
// We should not merge the instantiated lambdas from `b.h` and `c.h` together,
|
|
// even though they will both have anonymous declaration number #1 within
|
|
// A<int> and within the TU, respectively.
|
|
|
|
// CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_Z1NIiLi1EE) {
|
|
// CHECK: load i8, ptr @_ZGV1NIiLi1EE, align 8
|
|
// CHECK: call {{.*}} i32 @_ZNK1NIiLi1EEMUlvE_clEv(
|
|
// CHECK: store i32 {{.*}}, ptr @_Z1NIiLi1EE
|
|
|
|
// CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_ZN1AIiE1NILi1EEE) {
|
|
// CHECK: load i8, ptr @_ZGVN1AIiE1NILi1EEE, align 8
|
|
// CHECK: call {{.*}} i32 @_ZNK1AIiE1NILi1EEMUlvE_clEv(
|
|
// CHECK: store i32 {{.*}}, ptr @_ZN1AIiE1NILi1EEE
|
|
|
|
// CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_Z1NIiLi2EE) {
|
|
// CHECK: load i8, ptr @_ZGV1NIiLi2EE, align 8
|
|
// CHECK: call {{.*}} i32 @_ZNK1NIiLi2EEMUlvE_clEv(
|
|
// CHECK: store i32 {{.*}}, ptr @_Z1NIiLi2EE
|
|
|
|
// CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_ZN1AIiE1NILi2EEE) {
|
|
// CHECK: load i8, ptr @_ZGVN1AIiE1NILi2EEE, align 8
|
|
// CHECK: call {{.*}} i32 @_ZNK1AIiE1NILi2EEMUlvE_clEv(
|
|
// CHECK: store i32 {{.*}}, ptr @_ZN1AIiE1NILi2EEE
|
|
|