Skip to content

Commit 7fc028c

Browse files
committed
IRGen: We need to map the direct type error explosion back to the native result in IRGenThunk
rdar://134730970
1 parent 42a55ab commit 7fc028c

File tree

4 files changed

+66
-45
lines changed

4 files changed

+66
-45
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6503,3 +6503,46 @@ llvm::FunctionType *FunctionPointer::getFunctionType() const {
65036503

65046504
return Sig.getType();
65056505
}
6506+
6507+
void irgen::buildDirectError(IRGenFunction &IGF,
6508+
const CombinedResultAndErrorType &combined,
6509+
const NativeConventionSchema &errorSchema,
6510+
SILType silErrorTy, Explosion &errorResult,
6511+
bool forAsync, Explosion &out) {
6512+
if (combined.combinedTy->isVoidTy()) {
6513+
return;
6514+
}
6515+
6516+
llvm::Value *expandedResult = llvm::UndefValue::get(combined.combinedTy);
6517+
auto *structTy = dyn_cast<llvm::StructType>(combined.combinedTy);
6518+
6519+
if (!errorSchema.getExpandedType(IGF.IGM)->isVoidTy()) {
6520+
auto nativeError =
6521+
errorSchema.mapIntoNative(IGF.IGM, IGF, errorResult, silErrorTy, false);
6522+
6523+
if (structTy) {
6524+
for (unsigned i : combined.errorValueMapping) {
6525+
llvm::Value *elt = nativeError.claimNext();
6526+
auto *nativeTy = structTy->getElementType(i);
6527+
elt = convertForDirectError(IGF, elt, nativeTy,
6528+
/*forExtraction*/ false);
6529+
expandedResult = IGF.Builder.CreateInsertValue(expandedResult, elt, i);
6530+
}
6531+
if (forAsync) {
6532+
IGF.emitAllExtractValues(expandedResult, structTy, out);
6533+
} else {
6534+
out = expandedResult;
6535+
}
6536+
} else if (!errorSchema.getExpandedType(IGF.IGM)->isVoidTy()) {
6537+
out = convertForDirectError(IGF, nativeError.claimNext(),
6538+
combined.combinedTy,
6539+
/*forExtraction*/ false);
6540+
}
6541+
} else {
6542+
if (forAsync && structTy) {
6543+
IGF.emitAllExtractValues(expandedResult, structTy, out);
6544+
} else {
6545+
out = expandedResult;
6546+
}
6547+
}
6548+
}

lib/IRGen/GenCall.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ namespace irgen {
278278
llvm::Value *convertForDirectError(IRGenFunction &IGF, llvm::Value *value,
279279
llvm::Type *toTy, bool forExtraction);
280280

281+
void buildDirectError(IRGenFunction &IGF,
282+
const CombinedResultAndErrorType &combined,
283+
const NativeConventionSchema &errorSchema,
284+
SILType silErrorTy, Explosion &errorResult,
285+
bool forAsync, Explosion &out);
281286
} // end namespace irgen
282287
} // end namespace swift
283288

lib/IRGen/GenThunk.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,21 @@ void IRGenThunk::emit() {
406406
errorArgValues.add(errorValue);
407407
emitAsyncReturn(IGF, *asyncLayout, origTy, errorArgValues.claimAll());
408408
} else {
409-
IGF.emitScalarReturn(IGF.CurFn->getReturnType(), *error);
409+
if (!error->empty()) {
410+
// Map the direct error explosion from the call back to the native
411+
// explosion for the return.
412+
SILType silErrorTy = conv.getSILErrorType(expansionContext);
413+
auto &errorTI = IGF.IGM.getTypeInfo(silErrorTy);
414+
auto &errorSchema = errorTI.nativeReturnValueSchema(IGF.IGM);
415+
auto combined =
416+
combineResultAndTypedErrorType(IGF.IGM, schema, errorSchema);
417+
Explosion nativeAgg;
418+
buildDirectError(IGF, combined, errorSchema, silErrorTy, *error,
419+
/*forAsync*/ false, nativeAgg);
420+
IGF.emitScalarReturn(IGF.CurFn->getReturnType(), nativeAgg);
421+
} else {
422+
IGF.emitScalarReturn(IGF.CurFn->getReturnType(), *error);
423+
}
410424
}
411425
IGF.Builder.emitBlock(successBB);
412426
} else {

lib/IRGen/IRGenSIL.cpp

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4419,48 +4419,6 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
44194419
getSILModule());
44204420
assert(!conv.hasIndirectSILErrorResults());
44214421

4422-
auto buildDirectError = [=](const CombinedResultAndErrorType &combined,
4423-
const NativeConventionSchema &errorSchema,
4424-
SILType silErrorTy, Explosion &errorResult,
4425-
bool forAsync, Explosion &out) {
4426-
if (combined.combinedTy->isVoidTy()) {
4427-
return;
4428-
}
4429-
4430-
llvm::Value *expandedResult = llvm::UndefValue::get(combined.combinedTy);
4431-
auto *structTy = dyn_cast<llvm::StructType>(combined.combinedTy);
4432-
4433-
if (!errorSchema.getExpandedType(IGM)->isVoidTy()) {
4434-
auto nativeError =
4435-
errorSchema.mapIntoNative(IGM, *this, errorResult, silErrorTy, false);
4436-
4437-
if (structTy) {
4438-
for (unsigned i : combined.errorValueMapping) {
4439-
llvm::Value *elt = nativeError.claimNext();
4440-
auto *nativeTy = structTy->getElementType(i);
4441-
elt = convertForDirectError(*this, elt, nativeTy,
4442-
/*forExtraction*/ false);
4443-
expandedResult = Builder.CreateInsertValue(expandedResult, elt, i);
4444-
}
4445-
if (forAsync) {
4446-
emitAllExtractValues(expandedResult, structTy, out);
4447-
} else {
4448-
out = expandedResult;
4449-
}
4450-
} else if (!errorSchema.getExpandedType(IGM)->isVoidTy()) {
4451-
out =
4452-
convertForDirectError(*this, nativeError.claimNext(),
4453-
combined.combinedTy, /*forExtraction*/ false);
4454-
}
4455-
} else {
4456-
if (forAsync && structTy) {
4457-
emitAllExtractValues(expandedResult, structTy, out);
4458-
} else {
4459-
out = expandedResult;
4460-
}
4461-
}
4462-
};
4463-
44644422
if (!isAsync()) {
44654423
auto fnTy = CurFn->getFunctionType();
44664424
auto retTy = fnTy->getReturnType();
@@ -4493,7 +4451,8 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
44934451
auto combined =
44944452
combineResultAndTypedErrorType(IGM, resultSchema, errorSchema);
44954453
Explosion nativeAgg;
4496-
buildDirectError(combined, errorSchema, silErrorTy, errorResult,
4454+
buildDirectError(*this, combined, errorSchema, silErrorTy,
4455+
errorResult,
44974456
/*forAsync*/ false, nativeAgg);
44984457

44994458
emitScalarReturn(combined.combinedTy, nativeAgg);
@@ -4543,7 +4502,7 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
45434502
Explosion nativeAgg;
45444503
auto combined =
45454504
combineResultAndTypedErrorType(IGM, resultSchema, errorSchema);
4546-
buildDirectError(combined, errorSchema, silErrorTy, exn,
4505+
buildDirectError(*this, combined, errorSchema, silErrorTy, exn,
45474506
/*forAsync*/ true, nativeAgg);
45484507
assert(exn.empty() && "Unclaimed typed error results");
45494508

0 commit comments

Comments
 (0)