Skip to content

Commit be1f8cd

Browse files
committed
[IRGen] Properly handle conversion between ptr and int for non-matching sizes in direct error returns
rdar://133006541 This caused issues on 32 bit platforms when merging a 64 bit and ptr types for direct error returns.
1 parent 8b7a75c commit be1f8cd

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

lib/IRGen/GenCall.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -4518,12 +4518,12 @@ void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
45184518
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
45194519
llvm::Value *elt = values[combined.errorValueMapping[i]];
45204520
auto *nativeTy = structTy->getElementType(i);
4521-
elt = convertForAsyncDirect(IGF, elt, nativeTy, /*forExtraction*/ true);
4521+
elt = convertForDirectError(IGF, elt, nativeTy, /*forExtraction*/ true);
45224522
errorExplosion.add(elt);
45234523
}
45244524
} else {
45254525
auto *converted =
4526-
convertForAsyncDirect(IGF, values[combined.errorValueMapping[0]],
4526+
convertForDirectError(IGF, values[combined.errorValueMapping[0]],
45274527
combined.combinedTy, /*forExtraction*/ true);
45284528
errorExplosion.add(converted);
45294529
}
@@ -4541,12 +4541,12 @@ void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
45414541
dyn_cast<llvm::StructType>(nativeSchema.getExpandedType(IGF.IGM))) {
45424542
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
45434543
auto *nativeTy = structTy->getElementType(i);
4544-
auto *converted = convertForAsyncDirect(IGF, values[i], nativeTy,
4544+
auto *converted = convertForDirectError(IGF, values[i], nativeTy,
45454545
/*forExtraction*/ true);
45464546
resultExplosion.add(converted);
45474547
}
45484548
} else {
4549-
auto *converted = convertForAsyncDirect(
4549+
auto *converted = convertForDirectError(
45504550
IGF, values[0], combined.combinedTy, /*forExtraction*/ true);
45514551
resultExplosion.add(converted);
45524552
}
@@ -5414,7 +5414,7 @@ llvm::Value* IRGenFunction::coerceValue(llvm::Value *value, llvm::Type *toTy,
54145414
return loaded;
54155415
}
54165416

5417-
llvm::Value *irgen::convertForAsyncDirect(IRGenFunction &IGF,
5417+
llvm::Value *irgen::convertForDirectError(IRGenFunction &IGF,
54185418
llvm::Value *value, llvm::Type *toTy,
54195419
bool forExtraction) {
54205420
auto &Builder = IGF.Builder;
@@ -5424,12 +5424,9 @@ llvm::Value *irgen::convertForAsyncDirect(IRGenFunction &IGF,
54245424
if (toTy->isPointerTy()) {
54255425
if (fromTy->isPointerTy())
54265426
return Builder.CreateBitCast(value, toTy);
5427-
if (fromTy == IGF.IGM.IntPtrTy)
5428-
return Builder.CreateIntToPtr(value, toTy);
5427+
return Builder.CreateIntToPtr(value, toTy);
54295428
} else if (fromTy->isPointerTy()) {
5430-
if (toTy == IGF.IGM.IntPtrTy) {
5431-
return Builder.CreatePtrToInt(value, toTy);
5432-
}
5429+
return Builder.CreatePtrToInt(value, toTy);
54335430
}
54345431

54355432
if (forExtraction) {
@@ -5887,12 +5884,12 @@ void IRGenFunction::emitScalarReturn(SILType returnResultType,
58875884
for (unsigned i = 0, e = native.size(); i != e; ++i) {
58885885
llvm::Value *elt = native.claimNext();
58895886
auto *nativeTy = structTy->getElementType(i);
5890-
elt = convertForAsyncDirect(*this, elt, nativeTy,
5887+
elt = convertForDirectError(*this, elt, nativeTy,
58915888
/*forExtraction*/ false);
58925889
nativeAgg = Builder.CreateInsertValue(nativeAgg, elt, i);
58935890
}
58945891
} else {
5895-
nativeAgg = convertForAsyncDirect(*this, native.claimNext(), combinedTy,
5892+
nativeAgg = convertForDirectError(*this, native.claimNext(), combinedTy,
58965893
/*forExtraction*/ false);
58975894
}
58985895
}
@@ -6224,7 +6221,7 @@ void irgen::emitAsyncReturn(IRGenFunction &IGF, AsyncContextLayout &asyncLayout,
62246221
for (unsigned i = 0, e = result.size(); i != e; ++i) {
62256222
llvm::Value *elt = result.claimNext();
62266223
auto *nativeTy = structTy->getElementType(i);
6227-
elt = convertForAsyncDirect(IGF, elt, nativeTy,
6224+
elt = convertForDirectError(IGF, elt, nativeTy,
62286225
/*forExtraction*/ false);
62296226
nativeAgg = IGF.Builder.CreateInsertValue(nativeAgg, elt, i);
62306227
}
@@ -6234,7 +6231,7 @@ void irgen::emitAsyncReturn(IRGenFunction &IGF, AsyncContextLayout &asyncLayout,
62346231
nativeResultsStorage.push_back(out.claimNext());
62356232
}
62366233
} else {
6237-
auto *converted = convertForAsyncDirect(
6234+
auto *converted = convertForDirectError(
62386235
IGF, result.claimNext(), combinedTy, /*forExtraction*/ false);
62396236
nativeResultsStorage.push_back(converted);
62406237
}

lib/IRGen/GenCall.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ namespace irgen {
274274
void forwardAsyncCallResult(IRGenFunction &IGF, CanSILFunctionType fnType,
275275
AsyncContextLayout &layout, llvm::CallInst *call);
276276

277-
/// Converts a value for async direct errors.
278-
llvm::Value *convertForAsyncDirect(IRGenFunction &IGF, llvm::Value *value,
277+
/// Converts a value for direct error return.
278+
llvm::Value *convertForDirectError(IRGenFunction &IGF, llvm::Value *value,
279279
llvm::Type *toTy, bool forExtraction);
280280

281281
} // end namespace irgen

lib/IRGen/GenThunk.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,14 @@ void IRGenThunk::emit() {
381381
for (unsigned i : combined.errorValueMapping) {
382382
llvm::Value *elt = nativeError.claimNext();
383383
auto *nativeTy = structTy->getElementType(i);
384-
elt = convertForAsyncDirect(IGF, elt, nativeTy,
384+
elt = convertForDirectError(IGF, elt, nativeTy,
385385
/*forExtraction*/ false);
386386
expandedResult =
387387
IGF.Builder.CreateInsertValue(expandedResult, elt, i);
388388
}
389389
IGF.emitAllExtractValues(expandedResult, structTy, errorArgValues);
390390
} else if (!errorSchema.getExpandedType(IGM)->isVoidTy()) {
391-
errorArgValues = convertForAsyncDirect(IGF, nativeError.claimNext(),
391+
errorArgValues = convertForDirectError(IGF, nativeError.claimNext(),
392392
combined.combinedTy,
393393
/*forExtraction*/ false);
394394
}

lib/IRGen/IRGenSIL.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4432,7 +4432,7 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
44324432
for (unsigned i : combined.errorValueMapping) {
44334433
llvm::Value *elt = nativeError.claimNext();
44344434
auto *nativeTy = structTy->getElementType(i);
4435-
elt = convertForAsyncDirect(*this, elt, nativeTy,
4435+
elt = convertForDirectError(*this, elt, nativeTy,
44364436
/*forExtraction*/ false);
44374437
expandedResult = Builder.CreateInsertValue(expandedResult, elt, i);
44384438
}
@@ -4443,7 +4443,7 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
44434443
}
44444444
} else if (!errorSchema.getExpandedType(IGM)->isVoidTy()) {
44454445
out =
4446-
convertForAsyncDirect(*this, nativeError.claimNext(),
4446+
convertForDirectError(*this, nativeError.claimNext(),
44474447
combined.combinedTy, /*forExtraction*/ false);
44484448
}
44494449
} else {

test/IRGen/typed_throws_32_bit.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-swift-frontend -emit-ir -primary-file %s
2+
3+
// REQUIRES: CPU=arm64_32 || CPU=armv7k
4+
5+
public class MyClass {
6+
let x: Int64
7+
init(x: Int64) {
8+
self.x = x
9+
}
10+
}
11+
12+
public struct MyError: Error {
13+
let x: MyClass
14+
}
15+
16+
@inline(never)
17+
public func foo(f: () throws(MyError) -> Int64) throws(MyError) -> Int64 {
18+
return try f()
19+
}
20+
21+
public func bar(f: () throws(MyError) -> Int64) -> Int64 {
22+
do {
23+
return try foo(f: f)
24+
} catch {
25+
return error.x.x
26+
}
27+
}

0 commit comments

Comments
 (0)