Skip to content

[IRGen] Move some typed error code in CallEmission into separate func… #75072

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
Jul 9, 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
5 changes: 5 additions & 0 deletions lib/IRGen/CallEmission.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ class CallEmission {
TemporarySet &temporaries,
bool isOutlined);

bool mayReturnTypedErrorDirectly() const;
void emitToUnmappedExplosionWithDirectTypedError(SILType resultType,
llvm::Value *result,
Explosion &out);

CallEmission(IRGenFunction &IGF, llvm::Value *selfValue, Callee &&callee)
: IGF(IGF), selfValue(selfValue), CurCallee(std::move(callee)) {}

Expand Down
167 changes: 90 additions & 77 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2745,17 +2745,7 @@ class SyncCallEmission final : public CallEmission {
Explosion &out) override {
SILFunctionConventions fnConv(getCallee().getOrigFunctionType(),
IGF.getSILModule());
bool mayReturnErrorDirectly = false;
if (!convertDirectToIndirectReturn &&
!fnConv.hasIndirectSILErrorResults() &&
fnConv.funcTy->hasErrorResult() && fnConv.isTypedError()) {
auto errorType =
fnConv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
auto &errorSchema =
IGF.IGM.getTypeInfo(errorType).nativeReturnValueSchema(IGF.IGM);

mayReturnErrorDirectly = !errorSchema.shouldReturnTypedErrorIndirectly();
}
bool mayReturnErrorDirectly = mayReturnTypedErrorDirectly();

// Bail out immediately on a void result.
llvm::Value *result = call;
Expand Down Expand Up @@ -2812,72 +2802,8 @@ class SyncCallEmission final : public CallEmission {

// Handle direct return of typed errors
if (mayReturnErrorDirectly && !nativeSchema.requiresIndirect()) {
auto errorType =
fnConv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
auto &errorSchema =
IGF.IGM.getTypeInfo(errorType).nativeReturnValueSchema(IGF.IGM);

auto combined =
combineResultAndTypedErrorType(IGF.IGM, nativeSchema, errorSchema);

if (combined.combinedTy->isVoidTy()) {
typedErrorExplosion = Explosion();
return;
}

Explosion nativeExplosion;
extractScalarResults(IGF, result->getType(), result, nativeExplosion);
auto values = nativeExplosion.claimAll();

auto convertIfNecessary = [&](llvm::Type *nativeTy,
llvm::Value *elt) -> llvm::Value * {
auto *eltTy = elt->getType();
if (nativeTy->isIntOrPtrTy() && eltTy->isIntOrPtrTy() &&
nativeTy->getPrimitiveSizeInBits() !=
eltTy->getPrimitiveSizeInBits()) {
return IGF.Builder.CreateTruncOrBitCast(elt, nativeTy);
}
return elt;
};

Explosion errorExplosion;
if (!errorSchema.empty()) {
if (auto *structTy = dyn_cast<llvm::StructType>(
errorSchema.getExpandedType(IGF.IGM))) {
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
llvm::Value *elt = values[combined.errorValueMapping[i]];
auto *nativeTy = structTy->getElementType(i);
elt = convertIfNecessary(nativeTy, elt);
errorExplosion.add(elt);
}
} else {
errorExplosion.add(convertIfNecessary(
combined.combinedTy, values[combined.errorValueMapping[0]]));
}

typedErrorExplosion =
errorSchema.mapFromNative(IGF.IGM, IGF, errorExplosion, errorType);
} else {
typedErrorExplosion = std::move(errorExplosion);
}

// If the regular result type is void, there is nothing to explode
if (!resultType.isVoid()) {
Explosion resultExplosion;
if (auto *structTy = dyn_cast<llvm::StructType>(
nativeSchema.getExpandedType(IGF.IGM))) {
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
auto *nativeTy = structTy->getElementType(i);
resultExplosion.add(convertIfNecessary(nativeTy, values[i]));
}
} else {
resultExplosion.add(
convertIfNecessary(combined.combinedTy, values[0]));
}
out = nativeSchema.mapFromNative(IGF.IGM, IGF, resultExplosion,
resultType);
}
return;
return emitToUnmappedExplosionWithDirectTypedError(resultType, result,
out);
}

if (result->getType()->isVoidTy())
Expand Down Expand Up @@ -4434,6 +4360,93 @@ void CallEmission::externalizeArguments(IRGenFunction &IGF, const Callee &callee
}
}

bool CallEmission::mayReturnTypedErrorDirectly() const {
SILFunctionConventions fnConv(getCallee().getOrigFunctionType(),
IGF.getSILModule());
bool mayReturnErrorDirectly = false;
if (!convertDirectToIndirectReturn && !fnConv.hasIndirectSILErrorResults() &&
fnConv.funcTy->hasErrorResult() && fnConv.isTypedError()) {
auto errorType =
fnConv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
auto &errorSchema =
IGF.IGM.getTypeInfo(errorType).nativeReturnValueSchema(IGF.IGM);

mayReturnErrorDirectly = !errorSchema.shouldReturnTypedErrorIndirectly();
}

return mayReturnErrorDirectly;
}

void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
SILType resultType, llvm::Value *result, Explosion &out) {
SILFunctionConventions fnConv(getCallee().getOrigFunctionType(),
IGF.getSILModule());
auto &nativeSchema =
IGF.IGM.getTypeInfo(resultType).nativeReturnValueSchema(IGF.IGM);
auto errorType =
fnConv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
auto &errorSchema =
IGF.IGM.getTypeInfo(errorType).nativeReturnValueSchema(IGF.IGM);

auto combined =
combineResultAndTypedErrorType(IGF.IGM, nativeSchema, errorSchema);

if (combined.combinedTy->isVoidTy()) {
typedErrorExplosion = Explosion();
return;
}

Explosion nativeExplosion;
extractScalarResults(IGF, result->getType(), result, nativeExplosion);
auto values = nativeExplosion.claimAll();

auto convertIfNecessary = [&](llvm::Type *nativeTy,
llvm::Value *elt) -> llvm::Value * {
auto *eltTy = elt->getType();
if (nativeTy->isIntOrPtrTy() && eltTy->isIntOrPtrTy() &&
nativeTy->getPrimitiveSizeInBits() != eltTy->getPrimitiveSizeInBits()) {
return IGF.Builder.CreateTruncOrBitCast(elt, nativeTy);
}
return elt;
};

Explosion errorExplosion;
if (!errorSchema.empty()) {
if (auto *structTy =
dyn_cast<llvm::StructType>(errorSchema.getExpandedType(IGF.IGM))) {
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
llvm::Value *elt = values[combined.errorValueMapping[i]];
auto *nativeTy = structTy->getElementType(i);
elt = convertIfNecessary(nativeTy, elt);
errorExplosion.add(elt);
}
} else {
errorExplosion.add(convertIfNecessary(
combined.combinedTy, values[combined.errorValueMapping[0]]));
}

typedErrorExplosion =
errorSchema.mapFromNative(IGF.IGM, IGF, errorExplosion, errorType);
} else {
typedErrorExplosion = std::move(errorExplosion);
}

// If the regular result type is void, there is nothing to explode
if (!resultType.isVoid()) {
Explosion resultExplosion;
if (auto *structTy =
dyn_cast<llvm::StructType>(nativeSchema.getExpandedType(IGF.IGM))) {
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
auto *nativeTy = structTy->getElementType(i);
resultExplosion.add(convertIfNecessary(nativeTy, values[i]));
}
} else {
resultExplosion.add(convertIfNecessary(combined.combinedTy, values[0]));
}
out = nativeSchema.mapFromNative(IGF.IGM, IGF, resultExplosion, resultType);
}
}

void CallEmission::setKeyPathAccessorArguments(Explosion &in, bool isOutlined,
Explosion &out) {
auto origCalleeType = CurCallee.getOrigFunctionType();
Expand Down