Skip to content

Commit 19f54e1

Browse files
committed
SILGen: Clean up materializeForSet emission a bit
We can get the generic signature from the generic environment now, and for generic subscript protocol witnesses, using the signature of the conformance is wrong; it won't have the generic parameters of the subscript itself. Also, emit the materializeForSet callback in the right place in SILModule. Instead of adding it at the end, put it before the materializeForSet itself. This makes tests a bit easier to write.
1 parent 1f46086 commit 19f54e1

File tree

6 files changed

+114
-114
lines changed

6 files changed

+114
-114
lines changed

lib/SILGen/SILGenMaterializeForSet.cpp

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
#include "Scope.h"
2222
#include "Initialization.h"
2323
#include "swift/AST/AST.h"
24-
#include "swift/AST/DiagnosticsSIL.h"
2524
#include "swift/AST/Decl.h"
2625
#include "swift/AST/Types.h"
27-
#include "swift/AST/DiagnosticsCommon.h"
26+
#include "swift/AST/GenericEnvironment.h"
2827
#include "swift/AST/Mangle.h"
2928
#include "swift/AST/ASTMangler.h"
3029
#include "swift/AST/ProtocolConformance.h"
@@ -129,6 +128,7 @@ struct MaterializeForSetEmitter {
129128

130129
MaterializeForSetEmitter(SILGenModule &SGM, SILLinkage linkage,
131130
FuncDecl *witness, SubstitutionList subs,
131+
GenericEnvironment *genericEnv,
132132
Type selfInterfaceType, Type selfType)
133133
: SGM(SGM),
134134
Linkage(linkage),
@@ -138,7 +138,7 @@ struct MaterializeForSetEmitter {
138138
WitnessStorage(witness->getAccessorStorageDecl()),
139139
WitnessStoragePattern(AbstractionPattern::getInvalid()),
140140
WitnessSubs(subs),
141-
GenericEnv(nullptr),
141+
GenericEnv(genericEnv),
142142
SelfInterfaceType(selfInterfaceType->getCanonicalType()),
143143
SubstSelfType(selfType->getCanonicalType()),
144144
TheAccessSemantics(AccessSemantics::Ordinary),
@@ -164,6 +164,9 @@ struct MaterializeForSetEmitter {
164164
WitnessStorageType =
165165
SGM.Types.getLoweredType(WitnessStoragePattern, SubstStorageType)
166166
.getObjectType();
167+
168+
if (genericEnv)
169+
GenericSig = genericEnv->getGenericSignature()->getCanonicalSignature();
167170
}
168171

169172
public:
@@ -176,18 +179,7 @@ struct MaterializeForSetEmitter {
176179
FuncDecl *requirement, FuncDecl *witness,
177180
SubstitutionList witnessSubs) {
178181
MaterializeForSetEmitter emitter(SGM, linkage, witness, witnessSubs,
179-
selfInterfaceType, selfType);
180-
181-
if (conformance) {
182-
if (auto signature = conformance->getGenericSignature())
183-
emitter.GenericSig = signature->getCanonicalSignature();
184-
emitter.GenericEnv = genericEnv;
185-
} else {
186-
auto signature = requirement->getGenericSignatureOfContext();
187-
emitter.GenericSig = signature->getCanonicalSignature();
188-
emitter.GenericEnv = genericEnv;
189-
}
190-
182+
genericEnv, selfInterfaceType, selfType);
191183
emitter.RequirementStorage = requirement->getAccessorStorageDecl();
192184

193185
// Determine the desired abstraction pattern of the storage type
@@ -214,16 +206,11 @@ struct MaterializeForSetEmitter {
214206
Type selfType = witness->mapTypeIntoContext(selfInterfaceType);
215207

216208
SILDeclRef constant(witness);
217-
auto constantInfo = SGM.Types.getConstantInfo(constant);
218-
219209
MaterializeForSetEmitter emitter(SGM, constant.getLinkage(ForDefinition),
220210
witness, witnessSubs,
211+
witness->getGenericEnvironment(),
221212
selfInterfaceType, selfType);
222213

223-
if (auto signature = witness->getGenericSignatureOfContext())
224-
emitter.GenericSig = signature->getCanonicalSignature();
225-
emitter.GenericEnv = constantInfo.GenericEnv;
226-
227214
emitter.RequirementStorage = emitter.WitnessStorage;
228215
emitter.RequirementStoragePattern = emitter.WitnessStoragePattern;
229216
emitter.RequirementStorageType = emitter.WitnessStorageType;
@@ -551,13 +538,21 @@ SILFunction *MaterializeForSetEmitter::createCallback(SILFunction &F,
551538
SGM.Types.getMaterializeForSetCallbackType(WitnessStorage,
552539
GenericSig,
553540
SelfInterfaceType);
541+
542+
auto *genericEnv = GenericEnv;
543+
if (GenericEnv && GenericEnv->getGenericSignature()->areAllParamsConcrete())
544+
genericEnv = nullptr;
545+
554546
auto callback =
555-
SGM.M.getOrCreateFunction(Witness, CallbackName, Linkage,
556-
callbackType, IsBare,
557-
F.isTransparent(),
558-
F.isFragile());
547+
SGM.M.createFunction(Linkage, CallbackName, callbackType,
548+
genericEnv, SILLocation(Witness),
549+
IsBare, F.isTransparent(), F.isFragile(),
550+
IsNotThunk,
551+
/*ClassVisibility=*/SILFunction::NotRelevant,
552+
/*InlineStrategy=*/InlineDefault,
553+
/*EffectsKind=*/EffectsKind::Unspecified,
554+
/*InsertBefore=*/&F);
559555

560-
callback->setGenericEnvironment(GenericEnv);
561556
callback->setDebugScope(new (SGM.M) SILDebugScope(Witness, callback));
562557

563558
PrettyStackTraceSILFunction X("silgen materializeForSet callback", callback);

test/SILGen/addressors.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,14 @@ class G {
331331
// CHECK: store [[VALUE]] to [[T2]] : $*Int32
332332
// CHECK: strong_release [[OWNER]] : $Builtin.NativeObject
333333

334+
// materializeForSet callback for G.value
335+
// CHECK-LABEL: sil hidden [transparent] @_T010addressors1GC5values5Int32VfmytfU_ : $@convention(thin) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout G, @thick G.Type) -> () {
336+
// CHECK: bb0([[BUFFER:%0]] : $Builtin.RawPointer, [[STORAGE:%1]] : $*Builtin.UnsafeValueBuffer, [[SELF:%2]] : $*G, [[SELFTYPE:%3]] : $@thick G.Type):
337+
// CHECK: [[T0:%.*]] = project_value_buffer $Builtin.NativeObject in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
338+
// CHECK: [[OWNER:%.*]] = load [[T0]]
339+
// CHECK: strong_release [[OWNER]] : $Builtin.NativeObject
340+
// CHECK: dealloc_value_buffer $Builtin.NativeObject in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
341+
334342
// materializeForSet for G.value
335343
// CHECK-LABEL: sil hidden [transparent] @_T010addressors1GC5values5Int32Vfm : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @guaranteed G) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>) {
336344
// CHECK: bb0([[BUFFER:%0]] : $Builtin.RawPointer, [[STORAGE:%1]] : $*Builtin.UnsafeValueBuffer, [[SELF:%2]] : $G):
@@ -355,14 +363,6 @@ class G {
355363
// CHECK: [[RESULT:%.*]] = tuple ([[PTR]] : $Builtin.RawPointer, [[CALLBACK]] : $Optional<Builtin.RawPointer>)
356364
// CHECK: return [[RESULT]]
357365

358-
// materializeForSet callback for G.value
359-
// CHECK-LABEL: sil hidden [transparent] @_T010addressors1GC5values5Int32VfmytfU_ : $@convention(thin) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout G, @thick G.Type) -> () {
360-
// CHECK: bb0([[BUFFER:%0]] : $Builtin.RawPointer, [[STORAGE:%1]] : $*Builtin.UnsafeValueBuffer, [[SELF:%2]] : $*G, [[SELFTYPE:%3]] : $@thick G.Type):
361-
// CHECK: [[T0:%.*]] = project_value_buffer $Builtin.NativeObject in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
362-
// CHECK: [[OWNER:%.*]] = load [[T0]]
363-
// CHECK: strong_release [[OWNER]] : $Builtin.NativeObject
364-
// CHECK: dealloc_value_buffer $Builtin.NativeObject in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
365-
366366
class H {
367367
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer.allocate(capacity: 100)
368368

@@ -451,6 +451,14 @@ class I {
451451
// CHECK: store [[VALUE]] to [[T2]] : $*Int32
452452
// CHECK: strong_unpin [[OWNER]] : $Optional<Builtin.NativeObject>
453453

454+
// materializeForSet callback for I.value
455+
// CHECK-LABEL: sil hidden [transparent] @_T010addressors1IC5values5Int32VfmytfU_ : $@convention(thin) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout I, @thick I.Type) -> () {
456+
// CHECK: bb0([[BUFFER:%0]] : $Builtin.RawPointer, [[STORAGE:%1]] : $*Builtin.UnsafeValueBuffer, [[SELF:%2]] : $*I, [[SELFTYPE:%3]] : $@thick I.Type):
457+
// CHECK: [[T0:%.*]] = project_value_buffer $Optional<Builtin.NativeObject> in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
458+
// CHECK: [[OWNER:%.*]] = load [[T0]]
459+
// CHECK: strong_unpin [[OWNER]] : $Optional<Builtin.NativeObject>
460+
// CHECK: dealloc_value_buffer $Optional<Builtin.NativeObject> in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
461+
454462
// CHECK-LABEL: sil hidden [transparent] @_T010addressors1IC5values5Int32Vfm : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @guaranteed I) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>) {
455463
// CHECK: bb0([[BUFFER:%0]] : $Builtin.RawPointer, [[STORAGE:%1]] : $*Builtin.UnsafeValueBuffer, [[SELF:%2]] : $I):
456464
// Call the addressor.
@@ -474,14 +482,6 @@ class I {
474482
// CHECK: [[RESULT:%.*]] = tuple ([[PTR]] : $Builtin.RawPointer, [[CALLBACK]] : $Optional<Builtin.RawPointer>)
475483
// CHECK: return [[RESULT]]
476484

477-
// materializeForSet callback for I.value
478-
// CHECK-LABEL: sil hidden [transparent] @_T010addressors1IC5values5Int32VfmytfU_ : $@convention(thin) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout I, @thick I.Type) -> () {
479-
// CHECK: bb0([[BUFFER:%0]] : $Builtin.RawPointer, [[STORAGE:%1]] : $*Builtin.UnsafeValueBuffer, [[SELF:%2]] : $*I, [[SELFTYPE:%3]] : $@thick I.Type):
480-
// CHECK: [[T0:%.*]] = project_value_buffer $Optional<Builtin.NativeObject> in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
481-
// CHECK: [[OWNER:%.*]] = load [[T0]]
482-
// CHECK: strong_unpin [[OWNER]] : $Optional<Builtin.NativeObject>
483-
// CHECK: dealloc_value_buffer $Optional<Builtin.NativeObject> in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
484-
485485
struct RecInner {
486486
subscript(i: Int32) -> Int32 {
487487
mutating get { return i }

test/SILGen/c_materializeForSet_linkage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ extension NSReferencePoint: Pointable {}
1919
// CHECK-LABEL: sil shared [transparent] [fragile] @_T0SC7NSPointV1xSffm
2020
// CHECK-LABEL: sil shared [transparent] [fragile] @_T0SC7NSPointV1ySffm
2121

22+
// CHECK-LABEL: sil shared @_T0So16NSReferencePointC1xSffmytfU_
2223
// CHECK-LABEL: sil shared @_T0So16NSReferencePointC1xSffm
23-
// CHECK-LABEL: sil shared @_T0So16NSReferencePointC1ySffm
2424

25-
// CHECK-LABEL: sil shared @_T0So16NSReferencePointC1xSffmytfU_
2625
// CHECK-LABEL: sil shared @_T0So16NSReferencePointC1ySffmytfU_
26+
// CHECK-LABEL: sil shared @_T0So16NSReferencePointC1ySffm

test/SILGen/constrained_extensions.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ extension Array where Element == Int {
1010

1111
// CHECK-LABEL: sil @_T0Sa22constrained_extensionsSiRszlE16instancePropertySifg : $@convention(method) (@guaranteed Array<Int>) -> Int
1212
// CHECK-LABEL: sil @_T0Sa22constrained_extensionsSiRszlE16instancePropertySifs : $@convention(method) (Int, @inout Array<Int>) -> ()
13-
// CHECK-LABEL: sil [transparent] [fragile] @_T0Sa22constrained_extensionsSiRszlE16instancePropertySifm : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Array<Int>) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>)
1413
// CHECK-LABEL: sil [transparent] [fragile] @_T0Sa22constrained_extensionsSiRszlE16instancePropertySifmytfU_ : $@convention(thin) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Array<Int>, @thick Array<Int>.Type) -> ()
14+
// CHECK-LABEL: sil [transparent] [fragile] @_T0Sa22constrained_extensionsSiRszlE16instancePropertySifm : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Array<Int>) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>)
15+
1516
public var instanceProperty: Element {
1617
get {
1718
return self[0]
@@ -70,8 +71,8 @@ extension Dictionary where Key == Int {
7071

7172
// CHECK-LABEL: sil @_T0s10DictionaryV22constrained_extensionsSiRszr0_lE16instancePropertyq_fg : $@convention(method) <Key, Value where Key == Int> (@guaranteed Dictionary<Int, Value>) -> @out Value
7273
// CHECK-LABEL: sil @_T0s10DictionaryV22constrained_extensionsSiRszr0_lE16instancePropertyq_fs : $@convention(method) <Key, Value where Key == Int> (@in Value, @inout Dictionary<Int, Value>) -> ()
73-
// CHECK-LABEL: sil [transparent] [fragile] @_T0s10DictionaryV22constrained_extensionsSiRszr0_lE16instancePropertyq_fm : $@convention(method) <Key, Value where Key == Int> (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Dictionary<Int, Value>) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>)
7474
// CHECK-LABEL: sil [transparent] [fragile] @_T0s10DictionaryV22constrained_extensionsSiRszr0_lE16instancePropertyq_fmytfU_ : $@convention(thin) <Key, Value where Key == Int> (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Dictionary<Int, Value>, @thick Dictionary<Int, Value>.Type) -> ()
75+
// CHECK-LABEL: sil [transparent] [fragile] @_T0s10DictionaryV22constrained_extensionsSiRszr0_lE16instancePropertyq_fm : $@convention(method) <Key, Value where Key == Int> (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Dictionary<Int, Value>) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>)
7576
public var instanceProperty: Value {
7677
get {
7778
return self[0]!

0 commit comments

Comments
 (0)