Skip to content
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

Windows extern(D) ABIs: Pass non-PODs by ref to hidden copy #3612

Merged
merged 1 commit into from
Nov 11, 2020
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
27 changes: 16 additions & 11 deletions gen/abi-win64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,26 @@ struct Win64TargetABI : TargetABI {

const bool isMSVCpp = isMSVC && linkage == LINKcpp;

// Handle non-PODs:
if (isReturnValue) {
// MSVC++ enforces sret for non-PODs, incl. aggregates with ctors
// (which by itself doesn't make it a non-POD for D).
const bool excludeStructsWithCtor = isMSVCpp;
if (!isPOD(t, excludeStructsWithCtor))
// Enforce sret for non-PODs.
// MSVC++ additionally enforces it for all structs with ctors.
if (!isPOD(t, isMSVCpp))
return true;
} else {
// Contrary to return values, POD-ness is ignored for arguments.
// MSVC++ seems to enforce by-ref passing only for aggregates with
// MSVC++ seems to enforce by-ref passing only for structs with
// copy ctor (incl. `= delete`).
if (isMSVCpp && t->ty == Tstruct) {
StructDeclaration *sd = static_cast<TypeStruct *>(t)->sym;
assert(sd);
if (sd->postblit || sd->hasCopyCtor)
return true;
if (isMSVCpp) {
if (t->ty == Tstruct) {
StructDeclaration *sd = static_cast<TypeStruct *>(t)->sym;
assert(sd);
if (sd->postblit || sd->hasCopyCtor)
return true;
}
}
// non-MSVC++: pass all non-PODs by ref to hidden copy
else if (!isPOD(t)) {
return true;
}
}

Expand Down
10 changes: 6 additions & 4 deletions gen/abi-x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ struct X86TargetABI : TargetABI {
}

bool passByVal(TypeFunction *tf, Type *t) override {
// indirectly by-value for non-POD args on Posix
if (!isMSVC && !isPOD(t))
// indirectly by-value for non-POD args (except for MSVC++)
const bool isMSVCpp = isMSVC && tf->linkage == LINKcpp;
if (!isMSVCpp && !isPOD(t))
return false;

// pass all structs and static arrays with the LLVM byval attribute
Expand All @@ -147,8 +148,9 @@ struct X86TargetABI : TargetABI {
}
}

// Posix: non-POD args are passed indirectly by-value
if (!isMSVC) {
// non-POD args are passed indirectly by-value (except for MSVC++)
const bool isMSVCpp = isMSVC && fty.type->linkage == LINKcpp;
if (!isMSVCpp) {
for (auto arg : fty.args) {
if (!arg->byref && !isPOD(arg->type))
indirectByvalRewrite.applyTo(*arg);
Expand Down
26 changes: 26 additions & 0 deletions tests/codegen/no_abi_blit_for_nonpod.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Tests that a small non-POD isn't implicitly blit when being passed to/
// returned from a called function.

// RUN: %ldc -run %s

struct S
{
S* self;
this(this) { self = &this; }
~this() { assert(self == &this); }
}

S foo(S param)
{
return param; // copy-construct return value
// destruct param
}

void main()
{
S s;
s.self = &s;

S r = foo(s); // copy-construct tmp arg from s
// destruct r and s
}