Skip to content

Commit 76eb5dd

Browse files
committed
SILGen: Clean up ad-hoc SubstitutionList construction when calling intrinsics
Change emitApplyOfLibraryIntrinsic() to take a SubstitutionMap, and use the correct abstractions to build the map. This gets rid of the last remaining uses of gatherAllSubstitutions() in SIL.
1 parent 5465c8c commit 76eb5dd

File tree

7 files changed

+133
-127
lines changed

7 files changed

+133
-127
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/DiagnosticsSIL.h"
2323
#include "swift/AST/ForeignErrorConvention.h"
2424
#include "swift/AST/Module.h"
25+
#include "swift/AST/SubstitutionMap.h"
2526
#include "swift/Basic/Range.h"
2627
#include "swift/Basic/Unicode.h"
2728
#include "swift/SIL/PrettyStackTrace.h"
@@ -4533,9 +4534,13 @@ RValue SILGenFunction::emitApplyExpr(Expr *e, SGFContext c) {
45334534
RValue
45344535
SILGenFunction::emitApplyOfLibraryIntrinsic(SILLocation loc,
45354536
FuncDecl *fn,
4536-
SubstitutionList subs,
4537+
const SubstitutionMap &subMap,
45374538
ArrayRef<ManagedValue> args,
45384539
SGFContext ctx) {
4540+
SmallVector<Substitution, 4> subs;
4541+
if (auto *genericSig = fn->getGenericSignature())
4542+
genericSig->getSubstitutions(subMap, subs);
4543+
45394544
auto callee = Callee::forDirect(*this, SILDeclRef(fn), loc);
45404545
callee.setSubstitutions(subs);
45414546

@@ -4785,13 +4790,11 @@ SILGenFunction::emitUninitializedArrayAllocation(Type ArrayTy,
47854790
auto &Ctx = getASTContext();
47864791
auto allocate = Ctx.getAllocateUninitializedArray(nullptr);
47874792

4788-
auto arrayElementTy = ArrayTy->castTo<BoundGenericType>()
4789-
->getGenericArgs()[0];
4790-
47914793
// Invoke the intrinsic, which returns a tuple.
4792-
Substitution sub{arrayElementTy, {}};
4794+
auto subMap = ArrayTy->getContextSubstitutionMap(SGM.M.getSwiftModule(),
4795+
Ctx.getArrayDecl());
47934796
auto result = emitApplyOfLibraryIntrinsic(Loc, allocate,
4794-
sub,
4797+
subMap,
47954798
ManagedValue::forUnmanaged(Length),
47964799
SGFContext());
47974800

@@ -4808,12 +4811,12 @@ void SILGenFunction::emitUninitializedArrayDeallocation(SILLocation loc,
48084811
auto &Ctx = getASTContext();
48094812
auto deallocate = Ctx.getDeallocateUninitializedArray(nullptr);
48104813

4811-
CanType arrayElementTy =
4812-
array->getType().castTo<BoundGenericType>().getGenericArgs()[0];
4814+
CanType arrayTy = array->getType().getSwiftRValueType();
48134815

48144816
// Invoke the intrinsic.
4815-
Substitution sub{arrayElementTy, {}};
4816-
emitApplyOfLibraryIntrinsic(loc, deallocate, sub,
4817+
auto subMap = arrayTy->getContextSubstitutionMap(SGM.M.getSwiftModule(),
4818+
Ctx.getArrayDecl());
4819+
emitApplyOfLibraryIntrinsic(loc, deallocate, subMap,
48174820
ManagedValue::forUnmanaged(array),
48184821
SGFContext());
48194822
}

lib/SILGen/SILGenBridging.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,15 +499,21 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &gen,
499499
// Call into the stdlib intrinsic.
500500
if (auto bridgeAnything =
501501
gen.getASTContext().getBridgeAnythingToObjectiveC(nullptr)) {
502-
Substitution sub(loweredNativeTy, {});
502+
auto *genericSig = bridgeAnything->getGenericSignature();
503+
auto subMap = genericSig->getSubstitutionMap(
504+
[&](SubstitutableType *t) -> Type {
505+
return loweredNativeTy;
506+
},
507+
MakeAbstractConformanceForGenericType());
508+
503509
// Put the value into memory if necessary.
504510
assert(v.getType().isTrivial(gen.SGM.M) || v.hasCleanup());
505511
if (v.getType().isObject()) {
506512
auto tmp = gen.emitTemporaryAllocation(loc, v.getType());
507513
v.forwardInto(gen, loc, tmp);
508514
v = gen.emitManagedBufferWithCleanup(tmp);
509515
}
510-
return gen.emitApplyOfLibraryIntrinsic(loc, bridgeAnything, sub, v,
516+
return gen.emitApplyOfLibraryIntrinsic(loc, bridgeAnything, subMap, v,
511517
SGFContext())
512518
.getAsSingleValue(gen, loc);
513519
}
@@ -768,7 +774,7 @@ static ManagedValue emitCBridgedToNativeValue(SILGenFunction &gen,
768774
auto optionalMV = ManagedValue(optionalV, v.getCleanup());
769775
return gen.emitApplyOfLibraryIntrinsic(loc,
770776
gen.getASTContext().getBridgeAnyObjectToAny(nullptr),
771-
{}, optionalMV, SGFContext())
777+
SubstitutionMap(), optionalMV, SGFContext())
772778
.getAsSingleValue(gen, loc);
773779
}
774780

lib/SILGen/SILGenConvert.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/ASTContext.h"
2121
#include "swift/AST/Decl.h"
2222
#include "swift/AST/ProtocolConformance.h"
23+
#include "swift/AST/SubstitutionMap.h"
2324
#include "swift/AST/Types.h"
2425
#include "swift/Basic/type_traits.h"
2526
#include "swift/SIL/SILArgument.h"
@@ -204,7 +205,7 @@ SILGenFunction::emitPreconditionOptionalHasValue(SILLocation loc,
204205
ManagedValue args[4];
205206
emitSourceLocationArgs(*this, loc, args);
206207

207-
emitApplyOfLibraryIntrinsic(loc, diagnoseFailure, {}, args,
208+
emitApplyOfLibraryIntrinsic(loc, diagnoseFailure, SubstitutionMap(), args,
208209
SGFContext());
209210
}
210211

@@ -388,12 +389,20 @@ SILGenFunction::emitPointerToPointer(SILLocation loc,
388389
auto origValue = emitManagedBufferWithCleanup(origBuf);
389390

390391
// Invoke the conversion intrinsic to convert to the destination type.
391-
Substitution subs[2] = {
392-
getPointerSubstitution(inputType),
393-
getPointerSubstitution(outputType),
394-
};
392+
auto *M = SGM.M.getSwiftModule();
393+
auto *proto = getPointerProtocol();
394+
auto firstSubMap = inputType->getContextSubstitutionMap(M, proto);
395+
auto secondSubMap = outputType->getContextSubstitutionMap(M, proto);
396+
397+
auto *genericSig = converter->getGenericSignature();
398+
auto subMap =
399+
SubstitutionMap::combineSubstitutionMaps(firstSubMap,
400+
secondSubMap,
401+
CombineSubstitutionMaps::AtIndex,
402+
1, 0,
403+
genericSig);
395404

396-
return emitApplyOfLibraryIntrinsic(loc, converter, subs, origValue, C);
405+
return emitApplyOfLibraryIntrinsic(loc, converter, subMap, origValue, C);
397406
}
398407

399408

@@ -509,9 +518,10 @@ ManagedValue SILGenFunction::emitExistentialErasure(
509518
if (!getEmbeddedNSErrorFn)
510519
return emitUndef(loc, existentialTL.getLoweredType());
511520

512-
Substitution getEmbeddedNSErrorSubstitutions[1] = {
513-
Substitution(concreteFormalType, conformances)
514-
};
521+
auto getEmbeddedNSErrorSubstitutions =
522+
SubstitutionMap::getProtocolSubstitutions(ctx.getErrorDecl(),
523+
concreteFormalType,
524+
conformances[0]);
515525

516526
ManagedValue concreteValue = F(SGFContext());
517527
ManagedValue potentialNSError =

lib/SILGen/SILGenDynamicCast.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -583,46 +583,28 @@ static RValue emitCollectionDowncastExpr(SILGenFunction &SGF,
583583
SGFContext C,
584584
bool conditional) {
585585
// Compute substitutions for the intrinsic call.
586-
auto fromCollection = cast<BoundGenericStructType>(
587-
sourceType->getCanonicalType());
588-
auto toCollection = cast<BoundGenericStructType>(
589-
destType->getCanonicalType());
586+
auto fromCollection = sourceType->getCanonicalType();
587+
auto toCollection = destType->getCanonicalType();
590588
// Get the intrinsic function.
591589
auto &ctx = SGF.getASTContext();
592590
FuncDecl *fn = nullptr;
593-
if (fromCollection->getDecl() == ctx.getArrayDecl()) {
591+
if (fromCollection->getAnyNominal() == ctx.getArrayDecl()) {
594592
fn = conditional ? SGF.SGM.getArrayConditionalCast(loc)
595593
: SGF.SGM.getArrayForceCast(loc);
596-
} else if (fromCollection->getDecl() == ctx.getDictionaryDecl()) {
594+
} else if (fromCollection->getAnyNominal() == ctx.getDictionaryDecl()) {
597595
fn = (conditional
598596
? SGF.SGM.getDictionaryDownCastConditional(loc)
599597
: SGF.SGM.getDictionaryDownCast(loc));
600-
} else if (fromCollection->getDecl() == ctx.getSetDecl()) {
598+
} else if (fromCollection->getAnyNominal() == ctx.getSetDecl()) {
601599
fn = (conditional
602600
? SGF.SGM.getSetDownCastConditional(loc)
603601
: SGF.SGM.getSetDownCast(loc));
604602
} else {
605603
llvm_unreachable("unsupported collection upcast kind");
606604
}
607605

608-
// This will have been diagnosed by the accessors above.
609-
if (!fn) return SGF.emitUndefRValue(loc, destType);
610-
611-
auto fnGenericParams = fn->getGenericSignature()->getGenericParams();
612-
auto fromSubsts = fromCollection->gatherAllSubstitutions(
613-
SGF.SGM.SwiftModule, nullptr);
614-
auto toSubsts = toCollection->gatherAllSubstitutions(
615-
SGF.SGM.SwiftModule, nullptr);
616-
assert(fnGenericParams.size() == fromSubsts.size() + toSubsts.size() &&
617-
"wrong number of generic collection parameters");
618-
(void) fnGenericParams;
619-
620-
// Form type parameter substitutions.
621-
SmallVector<Substitution, 4> subs;
622-
subs.append(fromSubsts.begin(), fromSubsts.end());
623-
subs.append(toSubsts.begin(), toSubsts.end());
624-
625-
return SGF.emitApplyOfLibraryIntrinsic(loc, fn, subs, {source}, C);
606+
return SGF.emitCollectionConversion(loc, fn, fromCollection, toCollection,
607+
source, C);
626608
}
627609

628610
static ManagedValue

0 commit comments

Comments
 (0)