Skip to content

Fix partial apply forwarder emission for coroutines that are methods of structs with type parameters #76743

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 1 commit into from
Nov 21, 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
77 changes: 25 additions & 52 deletions lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,11 +1120,11 @@ class PartialApplicationForwarderEmission {
virtual void addDynamicFunctionContext(Explosion &explosion) = 0;
virtual void addDynamicFunctionPointer(Explosion &explosion) = 0;

virtual void addSelf(Explosion &explosion) { addArgument(explosion); }
virtual void addWitnessSelfMetadata(llvm::Value *value) {
void addSelf(Explosion &explosion) { addArgument(explosion); }
void addWitnessSelfMetadata(llvm::Value *value) {
addArgument(value);
}
virtual void addWitnessSelfWitnessTable(llvm::Value *value) {
void addWitnessSelfWitnessTable(llvm::Value *value) {
addArgument(value);
}
virtual void forwardErrorResult() = 0;
Expand Down Expand Up @@ -1412,12 +1412,6 @@ class CoroPartialApplicationForwarderEmission
: public PartialApplicationForwarderEmission {
using super = PartialApplicationForwarderEmission;

private:
llvm::Value *Self;
llvm::Value *FirstData;
llvm::Value *SecondData;
WitnessMetadata Witness;

public:
CoroPartialApplicationForwarderEmission(
IRGenModule &IGM, IRGenFunction &subIGF, llvm::Function *fwd,
Expand All @@ -1428,8 +1422,7 @@ class CoroPartialApplicationForwarderEmission
ArrayRef<ParameterConvention> conventions)
: PartialApplicationForwarderEmission(
IGM, subIGF, fwd, staticFnPtr, calleeHasContext, origSig, origType,
substType, outType, subs, layout, conventions),
Self(nullptr), FirstData(nullptr), SecondData(nullptr) {}
substType, outType, subs, layout, conventions) {}

void begin() override {
auto unsubstType = substType->getUnsubstitutedType(IGM.getSILModule());
Expand Down Expand Up @@ -1473,41 +1466,13 @@ class CoroPartialApplicationForwarderEmission
void gatherArgumentsFromApply() override {
super::gatherArgumentsFromApply(false);
}
llvm::Value *getDynamicFunctionPointer() override {
llvm::Value *Ret = SecondData;
SecondData = nullptr;
return Ret;
}
llvm::Value *getDynamicFunctionContext() override {
llvm::Value *Ret = FirstData;
FirstData = nullptr;
return Ret;
}
llvm::Value *getDynamicFunctionPointer() override { return args.takeLast(); }
llvm::Value *getDynamicFunctionContext() override { return args.takeLast(); }
void addDynamicFunctionContext(Explosion &explosion) override {
assert(!Self && "context value overrides 'self'");
FirstData = explosion.claimNext();
addArgument(explosion);
}
void addDynamicFunctionPointer(Explosion &explosion) override {
SecondData = explosion.claimNext();
}
void addSelf(Explosion &explosion) override {
assert(!FirstData && "'self' overrides another context value");
if (!hasSelfContextParameter(origType)) {
// witness methods can be declared on types that are not classes. Pass
// such "self" argument as a plain argument.
addArgument(explosion);
return;
}
Self = explosion.claimNext();
FirstData = Self;
}

void addWitnessSelfMetadata(llvm::Value *value) override {
Witness.SelfMetadata = value;
}

void addWitnessSelfWitnessTable(llvm::Value *value) override {
Witness.SelfWitnessTable = value;
addArgument(explosion);
}

void forwardErrorResult() override {
Expand All @@ -1528,13 +1493,26 @@ class CoroPartialApplicationForwarderEmission
}

Explosion callCoroutine(FunctionPointer &fnPtr) {
Callee callee({origType, substType, subs}, fnPtr, FirstData, SecondData);
bool isWitnessMethodCallee = origType->getRepresentation() ==
SILFunctionTypeRepresentation::WitnessMethod;

WitnessMetadata witnessMetadata;
if (isWitnessMethodCallee) {
witnessMetadata.SelfWitnessTable = args.takeLast();
witnessMetadata.SelfMetadata = args.takeLast();
}

llvm::Value *selfValue = nullptr;
if (calleeHasContext || hasSelfContextParameter(origType))
selfValue = args.takeLast();

Callee callee({origType, substType, subs}, fnPtr, selfValue);

std::unique_ptr<CallEmission> emitSuspend =
getCallEmission(subIGF, Self, std::move(callee));
getCallEmission(subIGF, callee.getSwiftContext(), std::move(callee));

emitSuspend->begin();
emitSuspend->setArgs(args, /*isOutlined=*/false, &Witness);
emitSuspend->setArgs(args, /*isOutlined=*/false, &witnessMetadata);
Explosion yieldedValues;
emitSuspend->emitToExplosion(yieldedValues, /*isOutlined=*/false);
emitSuspend->end();
Expand Down Expand Up @@ -1940,12 +1918,7 @@ static llvm::Value *emitPartialApplicationForwarder(
} else {
argValue = subIGF.Builder.CreateBitCast(rawData, expectedArgTy);
}
if (haveContextArgument) {
Explosion e;
e.add(argValue);
emission->addDynamicFunctionContext(e);
} else
emission->addArgument(argValue);
emission->addArgument(argValue);

// If there's a data pointer required, grab it and load out the
// extra, previously-curried parameters.
Expand Down
34 changes: 34 additions & 0 deletions test/AutoDiff/validation-test/modify_accessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,39 @@ ModifyAccessorTests.test("SimpleModifyAccessor") {
expectEqual((100, 20), valueWithGradient(at: 10, of: modify_struct))
}

ModifyAccessorTests.test("GenericModifyAccessor") {
struct S<T : Differentiable & SignedNumeric & Comparable>: Differentiable {
private var _x : T

func _endMutation() {}

var x: T {
get{_x}
set(newValue) { _x = newValue }
_modify {
defer { _endMutation() }
if (x > -x) {
yield &_x
} else {
yield &_x
}
}
}

init(_ x : T) {
self._x = x
}
}

func modify_struct(_ x : Float) -> Float {
var s = S<Float>(x)
s.x *= s.x
return s.x
}

expectEqual((100, 20), valueWithGradient(at: 10, of: modify_struct))
}


runAllTests()