Skip to content

[Serialization] Read the initializer for interesting static variables before consuming it #92353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4186,12 +4186,35 @@ void ASTReader::PassInterestingDeclsToConsumer() {
GetDecl(ID);
EagerlyDeserializedDecls.clear();

while (!PotentiallyInterestingDecls.empty()) {
Decl *D = PotentiallyInterestingDecls.front();
PotentiallyInterestingDecls.pop_front();
auto ConsumingPotentialInterestingDecls = [this]() {
while (!PotentiallyInterestingDecls.empty()) {
Decl *D = PotentiallyInterestingDecls.front();
PotentiallyInterestingDecls.pop_front();
if (isConsumerInterestedIn(D))
PassInterestingDeclToConsumer(D);
}
};
std::deque<Decl *> MaybeInterestingDecls =
std::move(PotentiallyInterestingDecls);
assert(PotentiallyInterestingDecls.empty());
while (!MaybeInterestingDecls.empty()) {
Decl *D = MaybeInterestingDecls.front();
MaybeInterestingDecls.pop_front();
// Since we load the variable's initializers lazily, it'd be problematic
// if the initializers dependent on each other. So here we try to load the
// initializers of static variables to make sure they are passed to code
// generator by order. If we read anything interesting, we would consume
// that before emitting the current declaration.
if (auto *VD = dyn_cast<VarDecl>(D);
VD && VD->isFileVarDecl() && !VD->isExternallyVisible())
VD->getInit();
ConsumingPotentialInterestingDecls();
if (isConsumerInterestedIn(D))
PassInterestingDeclToConsumer(D);
}

// If we add any new potential interesting decl in the last call, consume it.
ConsumingPotentialInterestingDecls();
}

void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Expand Down
65 changes: 65 additions & 0 deletions clang/test/Modules/pr91418.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 -x c++-header %t/foo.h \
// RUN: -emit-pch -o %t/foo.pch
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/use.cpp -include-pch \
// RUN: %t/foo.pch -emit-llvm -o - | FileCheck %t/use.cpp

//--- foo.h
#ifndef FOO_H
#define FOO_H
typedef float __m128 __attribute__((__vector_size__(16), __aligned__(16)));

static __inline__ __m128 __attribute__((__always_inline__, __min_vector_width__(128)))
_mm_setr_ps(float __z, float __y, float __x, float __w)
{
return __extension__ (__m128){ __z, __y, __x, __w };
}

typedef __m128 VR;

inline VR MakeVR( float X, float Y, float Z, float W )
{
return _mm_setr_ps( X, Y, Z, W );
}

extern "C" float sqrtf(float);

namespace VectorSinConstantsSSE
{
float a = (16 * sqrtf(0.225f));
VR A = MakeVR(a, a, a, a);
static const float b = (16 * sqrtf(0.225f));
static const VR B = MakeVR(b, b, b, b);
}

#endif // FOO_H

//--- use.cpp
#include "foo.h"
float use() {
return VectorSinConstantsSSE::A[0] + VectorSinConstantsSSE::A[1] +
VectorSinConstantsSSE::A[2] + VectorSinConstantsSSE::A[3] +
VectorSinConstantsSSE::B[0] + VectorSinConstantsSSE::B[1] +
VectorSinConstantsSSE::B[2] + VectorSinConstantsSSE::B[3];
}

// CHECK: define{{.*}}@__cxx_global_var_init(
// CHECK: store{{.*}}, ptr @_ZN21VectorSinConstantsSSE1aE

// CHECK: define{{.*}}@__cxx_global_var_init.1(
// CHECK: store{{.*}}, ptr @_ZN21VectorSinConstantsSSE1AE

// CHECK: define{{.*}}@__cxx_global_var_init.2(
// CHECK: store{{.*}}, ptr @_ZN21VectorSinConstantsSSEL1BE

// CHECK: define{{.*}}@__cxx_global_var_init.3(
// CHECK: store{{.*}}, ptr @_ZN21VectorSinConstantsSSEL1bE

// CHECK: @_GLOBAL__sub_I_use.cpp
// CHECK: call{{.*}}@__cxx_global_var_init(
// CHECK: call{{.*}}@__cxx_global_var_init.1(
// CHECK: call{{.*}}@__cxx_global_var_init.3(
// CHECK: call{{.*}}@__cxx_global_var_init.2(
Loading
Loading