Skip to content

[cxx-interop] Avoid querying layout of dependent types during symbolic import #81228

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 4 commits into from
May 1, 2025
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
4 changes: 0 additions & 4 deletions benchmark/cxx-source/CxxSetToCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// This is a benchmark that tracks how quickly Swift can convert a C++ set
// to a Swift collection.

/* FIXME: rdar://150067288

import TestsUtils
import CxxStdlibPerformance
import Cxx
Expand Down Expand Up @@ -66,5 +64,3 @@ public func run_CxxSetOfU32_forEach(_ n: Int) {
}
}
}

*/
4 changes: 0 additions & 4 deletions benchmark/cxx-source/CxxStringConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//
//===----------------------------------------------------------------------===//

/* FIXME: rdar://150067288

import TestsUtils
import CxxStdlibPerformance
import CxxStdlib
Expand Down Expand Up @@ -60,5 +58,3 @@ public func run_cxxToSwift(_ n: Int) {
blackHole(x)
}
}

*/
4 changes: 0 additions & 4 deletions benchmark/cxx-source/CxxVectorSum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// This is a benchmark that tracks how quickly Swift can sum up a C++ vector
// as compared to the C++ implementation of such sum.

/* FIXME: rdar://150067288

import TestsUtils
import CxxStdlibPerformance
import Cxx
Expand Down Expand Up @@ -121,5 +119,3 @@ public func run_CxxVectorOfU32_Sum_Swift_Reduce(_ n: Int) {
}
blackHole(sum)
}

*/
8 changes: 7 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4480,11 +4480,17 @@ namespace {
}

Decl *VisitFieldDecl(const clang::FieldDecl *decl) {
if (decl->hasAttr<clang::NoUniqueAddressAttr>()) {
if (!Impl.importSymbolicCXXDecls &&
decl->hasAttr<clang::NoUniqueAddressAttr>()) {
if (const auto *rd = decl->getType()->getAsRecordDecl()) {
// Clang can store the next field in the padding of this one. Swift
// does not support this yet so let's not import the field and
// represent it with an opaque blob in codegen.
//
// This check is not relevant when importing the decl symbolically
// (since that isn't used for codegen). In fact, we need to avoid this
// check because symbolic imports can expose us to dependent types
// whose ASTRecordLayout cannot be queried.
const auto &fieldLayout =
decl->getASTContext().getASTRecordLayout(rd);
auto &clangCtx = decl->getASTContext();
Expand Down
40 changes: 36 additions & 4 deletions test/Interop/Cxx/class/Inputs/member-variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TEST_INTEROP_CXX_CLASS_INPUTS_MEMBER_VARIABLES_H

#include <cstddef>
#include <type_traits>
#include <optional>

class MyClass {
Expand All @@ -27,25 +28,56 @@ struct HasZeroSizedField {
void set_c(short c) { this->c = c; }
};

struct ReuseFieldPadding {
struct ReuseOptionalFieldPadding {
[[no_unique_address]] std::optional<int> a = {2};
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
int offset() const { return offsetof(ReuseFieldPadding, c); }
int offset() const { return offsetof(ReuseOptionalFieldPadding, c); }
std::optional<int> getOptional() { return a; }
};

using OptInt = std::optional<int>;

struct ReuseFieldPaddingWithTypedef {
struct ReuseOptionalFieldPaddingWithTypedef {
[[no_unique_address]] OptInt a;
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
int offset() const { return offsetof(ReuseFieldPadding, c); }
int offset() const { return offsetof(ReuseOptionalFieldPadding, c); }
};

// Using a mix of private and public fields prevents this class from being
// standard-layout, which is necessary to allow clang to reuse its padding.
template<typename T>
struct NonStandardLayoutClass {
private:
T x;
public:
char pad_me;
};
static_assert(std::is_standard_layout_v<NonStandardLayoutClass<int>> == false);

struct ReuseNonStandardLayoutFieldPadding {
[[no_unique_address]] NonStandardLayoutClass<int> a;
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
// C-style implementation of offsetof() to avoid non-standard-layout warning
int offset() const { return (char *) &this->c - (char *) this; }
};

template<typename T>
struct ReuseDependentFieldPadding {
[[no_unique_address]] struct { private: T x; public: char pad_me; } a;
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
// C-style implementation of offsetof() to avoid non-standard-layout warning
int offset() const { return (char *) &this->c - (char *) this; }
};

typedef ReuseDependentFieldPadding<int> ReuseDependentFieldPaddingInt;

inline int takesZeroSizedInCpp(HasZeroSizedField x) {
return x.a;
Expand Down
43 changes: 36 additions & 7 deletions test/Interop/Cxx/class/zero-sized-field.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xcc -std=c++20)
// RUN: %empty-directory(%t/index)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xcc -std=c++20 -Xfrontend -index-store-path -Xfrontend %t/index)
//
// REQUIRES: executable_test

Expand All @@ -24,12 +25,12 @@ FieldsTestSuite.test("Zero sized field") {
expectEqual(s.b.getNum(), 42)
}

FieldsTestSuite.test("Field padding reused") {
var s = ReuseFieldPadding()
FieldsTestSuite.test("Optional field padding reused") {
var s = ReuseOptionalFieldPadding()
let opt = s.getOptional()
expectEqual(Int(opt.pointee), 2)
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseFieldPadding>.offset(of: \.c)!)
expectEqual(Int(s.offset()), MemoryLayout<ReuseOptionalFieldPadding>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
Expand All @@ -40,10 +41,38 @@ FieldsTestSuite.test("Field padding reused") {
expectEqual(s2.get_c(), 6)
}

FieldsTestSuite.test("Typedef'd field padding reused") {
var s = ReuseFieldPaddingWithTypedef()
FieldsTestSuite.test("Typedef'd optional field padding reused") {
var s = ReuseOptionalFieldPaddingWithTypedef()
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseFieldPadding>.offset(of: \.c)!)
expectEqual(Int(s.offset()), MemoryLayout<ReuseOptionalFieldPadding>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
expectEqual(s.c, 6)
expectEqual(s.get_c(), 6)
let s2 = s
expectEqual(s2.c, 6)
expectEqual(s2.get_c(), 6)
}

FieldsTestSuite.test("Non-standard-layout field padding reused") {
var s = ReuseNonStandardLayoutFieldPadding()
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseNonStandardLayoutFieldPadding>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
expectEqual(s.c, 6)
expectEqual(s.get_c(), 6)
let s2 = s
expectEqual(s2.c, 6)
expectEqual(s2.get_c(), 6)
}

FieldsTestSuite.test("Non-standard-layout field padding in templated class reused") {
var s = ReuseDependentFieldPaddingInt()
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseDependentFieldPaddingInt>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
Expand Down