@@ -296,9 +296,15 @@ bool LargeSILTypeMapper::shouldTransformResults(GenericEnvironment *genEnv,
296
296
if (!modifiableFunction (loweredTy)) {
297
297
return false ;
298
298
}
299
+
299
300
if (loweredTy->getNumResults () != 1 ) {
300
- return false ;
301
+ auto resultType = loweredTy->getAllResultsType ();
302
+ auto newResultType = getNewSILType (genEnv, resultType, Mod);
303
+ bool hasFuncSig = containsFunctionSignature (genEnv, Mod,
304
+ resultType, newResultType);
305
+ return hasFuncSig;
301
306
}
307
+
302
308
auto singleResult = loweredTy->getSingleResult ();
303
309
auto resultStorageType = singleResult.getSILStorageType ();
304
310
auto newResultStorageType = getNewSILType (genEnv, resultStorageType, Mod);
@@ -1287,8 +1293,7 @@ SILArgument *LoadableStorageAllocation::replaceArgType(SILBuilder &argBuilder,
1287
1293
void LoadableStorageAllocation::insertIndirectReturnArgs () {
1288
1294
GenericEnvironment *genEnv = pass.F ->getGenericEnvironment ();
1289
1295
auto loweredTy = pass.F ->getLoweredFunctionType ();
1290
- auto singleResult = loweredTy->getSingleResult ();
1291
- SILType resultStorageType = singleResult.getSILStorageType ();
1296
+ SILType resultStorageType = loweredTy->getAllResultsType ();
1292
1297
auto canType = resultStorageType.getASTType ();
1293
1298
if (canType->hasTypeParameter ()) {
1294
1299
assert (genEnv && " Expected a GenericEnv" );
@@ -1368,19 +1373,26 @@ void LoadableStorageAllocation::convertApplyResults() {
1368
1373
pass.Mod )) {
1369
1374
continue ;
1370
1375
}
1371
- auto singleResult = origSILFunctionType->getSingleResult ();
1372
- auto resultStorageType = singleResult.getSILStorageType ();
1376
+ auto resultStorageType = origSILFunctionType->getAllResultsType ();
1373
1377
if (!isLargeLoadableType (genEnv, resultStorageType, pass.Mod )) {
1374
- // Make sure it is a function type
1375
- if (!resultStorageType.is <SILFunctionType>()) {
1376
- // Check if it is an optional function type
1377
- auto optionalType = resultStorageType.getOptionalObjectType ();
1378
- assert (optionalType &&
1379
- " Expected SILFunctionType or Optional for the result type" );
1380
- assert (optionalType.is <SILFunctionType>() &&
1381
- " Expected a SILFunctionType inside the optional Type" );
1382
- (void )optionalType;
1383
- }
1378
+ // Make sure it contains a function type
1379
+ auto numFuncTy = llvm::count_if (origSILFunctionType->getResults (),
1380
+ [](const SILResultInfo &origResult) {
1381
+ auto resultStorageTy = origResult.getSILStorageType ();
1382
+ // Check if it is a function type
1383
+ if (resultStorageTy.is <SILFunctionType>()) {
1384
+ return true ;
1385
+ }
1386
+ // Check if it is an optional function type
1387
+ auto optionalType = resultStorageTy.getOptionalObjectType ();
1388
+ if (optionalType && optionalType.is <SILFunctionType>()) {
1389
+ return true ;
1390
+ }
1391
+ return false ;
1392
+ });
1393
+ assert (numFuncTy != 0 &&
1394
+ " Expected a SILFunctionType inside the result Type" );
1395
+ (void )numFuncTy;
1384
1396
continue ;
1385
1397
}
1386
1398
auto newSILType =
@@ -2213,10 +2225,25 @@ static bool rewriteFunctionReturn(StructLoweringState &pass) {
2213
2225
return true ;
2214
2226
} else if (containsFunctionSignature (genEnv, pass.Mod , resultTy,
2215
2227
newSILType) &&
2216
- (resultTy != newSILType) && (loweredTy->getNumResults () == 1 )) {
2217
- SILResultInfo origResultInfo = loweredTy->getSingleResult ();
2218
- SILResultInfo newSILResultInfo (newSILType.getASTType (),
2219
- origResultInfo.getConvention ());
2228
+ (resultTy != newSILType)) {
2229
+
2230
+ llvm::SmallVector<SILResultInfo, 2 > newSILResultInfo;
2231
+ if (auto tupleType = newSILType.getAs <TupleType>()) {
2232
+ auto originalResults = loweredTy->getResults ();
2233
+ for (unsigned int i = 0 ; i < originalResults.size (); ++i) {
2234
+ auto origResultInfo = originalResults[i];
2235
+ auto canElem = tupleType.getElementType (i);
2236
+ SILType objectType = SILType::getPrimitiveObjectType (canElem);
2237
+ auto newResult = SILResultInfo (objectType.getASTType (), origResultInfo.getConvention ());
2238
+ newSILResultInfo.push_back (newResult);
2239
+ }
2240
+ } else {
2241
+ assert (loweredTy->getNumResults () == 1 && " Expected a single result" );
2242
+ auto origResultInfo = loweredTy->getSingleResult ();
2243
+ auto newResult = SILResultInfo (newSILType.getASTType (), origResultInfo.getConvention ());
2244
+ newSILResultInfo.push_back (newResult);
2245
+ }
2246
+
2220
2247
auto NewTy = SILFunctionType::get (
2221
2248
loweredTy->getGenericSignature (), loweredTy->getExtInfo (),
2222
2249
loweredTy->getCoroutineKind (),
@@ -2302,9 +2329,6 @@ getOperandTypeWithCastIfNecessary(SILInstruction *containingInstr, SILValue op,
2302
2329
if (!genEnv && funcType->isPolymorphic ()) {
2303
2330
genEnv = getGenericEnvironment (funcType);
2304
2331
}
2305
- if (!Mapper.shouldTransformFunctionType (genEnv, funcType, Mod)) {
2306
- return op;
2307
- }
2308
2332
auto newFnType = Mapper.getNewSILFunctionType (genEnv, funcType, Mod);
2309
2333
SILType newSILType = SILType::getPrimitiveObjectType (newFnType);
2310
2334
if (nonOptionalType.isAddress ()) {
0 commit comments