Skip to content

[IRGen] Properly handle conversion between ptr and int for non-matchi… #75677

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
Aug 5, 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
25 changes: 11 additions & 14 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4518,12 +4518,12 @@ void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
llvm::Value *elt = values[combined.errorValueMapping[i]];
auto *nativeTy = structTy->getElementType(i);
elt = convertForAsyncDirect(IGF, elt, nativeTy, /*forExtraction*/ true);
elt = convertForDirectError(IGF, elt, nativeTy, /*forExtraction*/ true);
errorExplosion.add(elt);
}
} else {
auto *converted =
convertForAsyncDirect(IGF, values[combined.errorValueMapping[0]],
convertForDirectError(IGF, values[combined.errorValueMapping[0]],
combined.combinedTy, /*forExtraction*/ true);
errorExplosion.add(converted);
}
Expand All @@ -4541,12 +4541,12 @@ void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
dyn_cast<llvm::StructType>(nativeSchema.getExpandedType(IGF.IGM))) {
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
auto *nativeTy = structTy->getElementType(i);
auto *converted = convertForAsyncDirect(IGF, values[i], nativeTy,
auto *converted = convertForDirectError(IGF, values[i], nativeTy,
/*forExtraction*/ true);
resultExplosion.add(converted);
}
} else {
auto *converted = convertForAsyncDirect(
auto *converted = convertForDirectError(
IGF, values[0], combined.combinedTy, /*forExtraction*/ true);
resultExplosion.add(converted);
}
Expand Down Expand Up @@ -5414,7 +5414,7 @@ llvm::Value* IRGenFunction::coerceValue(llvm::Value *value, llvm::Type *toTy,
return loaded;
}

llvm::Value *irgen::convertForAsyncDirect(IRGenFunction &IGF,
llvm::Value *irgen::convertForDirectError(IRGenFunction &IGF,
llvm::Value *value, llvm::Type *toTy,
bool forExtraction) {
auto &Builder = IGF.Builder;
Expand All @@ -5424,12 +5424,9 @@ llvm::Value *irgen::convertForAsyncDirect(IRGenFunction &IGF,
if (toTy->isPointerTy()) {
if (fromTy->isPointerTy())
return Builder.CreateBitCast(value, toTy);
if (fromTy == IGF.IGM.IntPtrTy)
return Builder.CreateIntToPtr(value, toTy);
return Builder.CreateIntToPtr(value, toTy);
} else if (fromTy->isPointerTy()) {
if (toTy == IGF.IGM.IntPtrTy) {
return Builder.CreatePtrToInt(value, toTy);
}
return Builder.CreatePtrToInt(value, toTy);
}

if (forExtraction) {
Expand Down Expand Up @@ -5887,12 +5884,12 @@ void IRGenFunction::emitScalarReturn(SILType returnResultType,
for (unsigned i = 0, e = native.size(); i != e; ++i) {
llvm::Value *elt = native.claimNext();
auto *nativeTy = structTy->getElementType(i);
elt = convertForAsyncDirect(*this, elt, nativeTy,
elt = convertForDirectError(*this, elt, nativeTy,
/*forExtraction*/ false);
nativeAgg = Builder.CreateInsertValue(nativeAgg, elt, i);
}
} else {
nativeAgg = convertForAsyncDirect(*this, native.claimNext(), combinedTy,
nativeAgg = convertForDirectError(*this, native.claimNext(), combinedTy,
/*forExtraction*/ false);
}
}
Expand Down Expand Up @@ -6224,7 +6221,7 @@ void irgen::emitAsyncReturn(IRGenFunction &IGF, AsyncContextLayout &asyncLayout,
for (unsigned i = 0, e = result.size(); i != e; ++i) {
llvm::Value *elt = result.claimNext();
auto *nativeTy = structTy->getElementType(i);
elt = convertForAsyncDirect(IGF, elt, nativeTy,
elt = convertForDirectError(IGF, elt, nativeTy,
/*forExtraction*/ false);
nativeAgg = IGF.Builder.CreateInsertValue(nativeAgg, elt, i);
}
Expand All @@ -6234,7 +6231,7 @@ void irgen::emitAsyncReturn(IRGenFunction &IGF, AsyncContextLayout &asyncLayout,
nativeResultsStorage.push_back(out.claimNext());
}
} else {
auto *converted = convertForAsyncDirect(
auto *converted = convertForDirectError(
IGF, result.claimNext(), combinedTy, /*forExtraction*/ false);
nativeResultsStorage.push_back(converted);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/GenCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ namespace irgen {
void forwardAsyncCallResult(IRGenFunction &IGF, CanSILFunctionType fnType,
AsyncContextLayout &layout, llvm::CallInst *call);

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

} // end namespace irgen
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/GenThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,14 @@ void IRGenThunk::emit() {
for (unsigned i : combined.errorValueMapping) {
llvm::Value *elt = nativeError.claimNext();
auto *nativeTy = structTy->getElementType(i);
elt = convertForAsyncDirect(IGF, elt, nativeTy,
elt = convertForDirectError(IGF, elt, nativeTy,
/*forExtraction*/ false);
expandedResult =
IGF.Builder.CreateInsertValue(expandedResult, elt, i);
}
IGF.emitAllExtractValues(expandedResult, structTy, errorArgValues);
} else if (!errorSchema.getExpandedType(IGM)->isVoidTy()) {
errorArgValues = convertForAsyncDirect(IGF, nativeError.claimNext(),
errorArgValues = convertForDirectError(IGF, nativeError.claimNext(),
combined.combinedTy,
/*forExtraction*/ false);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4432,7 +4432,7 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
for (unsigned i : combined.errorValueMapping) {
llvm::Value *elt = nativeError.claimNext();
auto *nativeTy = structTy->getElementType(i);
elt = convertForAsyncDirect(*this, elt, nativeTy,
elt = convertForDirectError(*this, elt, nativeTy,
/*forExtraction*/ false);
expandedResult = Builder.CreateInsertValue(expandedResult, elt, i);
}
Expand All @@ -4443,7 +4443,7 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
}
} else if (!errorSchema.getExpandedType(IGM)->isVoidTy()) {
out =
convertForAsyncDirect(*this, nativeError.claimNext(),
convertForDirectError(*this, nativeError.claimNext(),
combined.combinedTy, /*forExtraction*/ false);
}
} else {
Expand Down
27 changes: 27 additions & 0 deletions test/IRGen/typed_throws_32_bit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-swift-frontend -emit-ir -primary-file %s

// REQUIRES: CPU=arm64_32 || CPU=armv7k

public class MyClass {
let x: Int64
init(x: Int64) {
self.x = x
}
}

public struct MyError: Error {
let x: MyClass
}

@inline(never)
public func foo(f: () throws(MyError) -> Int64) throws(MyError) -> Int64 {
return try f()
}

public func bar(f: () throws(MyError) -> Int64) -> Int64 {
do {
return try foo(f: f)
} catch {
return error.x.x
}
}