Skip to content

Commit 0569fed

Browse files
authored
Merge pull request #5444 from gottesmm/return_copyvalue_result_from_typelowering
2 parents adf36cb + 7f2ec61 commit 0569fed

File tree

3 files changed

+290
-268
lines changed

3 files changed

+290
-268
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -280,43 +280,44 @@ class TypeLowering {
280280
/// Emit a lowered 'retain_value' operation.
281281
///
282282
/// This type must be loadable.
283-
virtual void emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
284-
SILValue value,
285-
LoweringStyle style) const = 0;
283+
virtual SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
284+
SILValue value,
285+
LoweringStyle style) const = 0;
286286

287287
/// Emit a lowered 'retain_value' operation.
288288
///
289289
/// This type must be loadable.
290-
void emitLoweredCopyValueShallow(SILBuilder &B, SILLocation loc,
291-
SILValue value) const {
292-
emitLoweredCopyValue(B, loc, value, LoweringStyle::Shallow);
290+
SILValue emitLoweredCopyValueShallow(SILBuilder &B, SILLocation loc,
291+
SILValue value) const {
292+
return emitLoweredCopyValue(B, loc, value, LoweringStyle::Shallow);
293293
}
294294

295295
/// Emit a lowered 'retain_value' operation.
296296
///
297297
/// This type must be loadable.
298-
void emitLoweredRetainValueDeepNoEnum(SILBuilder &B, SILLocation loc,
299-
SILValue value) const {
300-
emitLoweredCopyValue(B, loc, value, LoweringStyle::DeepNoEnum);
298+
SILValue emitLoweredCopyValueDeepNoEnum(SILBuilder &B, SILLocation loc,
299+
SILValue value) const {
300+
return emitLoweredCopyValue(B, loc, value, LoweringStyle::DeepNoEnum);
301301
}
302302

303303
/// Given a primitively loaded value of this type (which must be
304-
/// loadable), +1 it.
304+
/// loadable), Perform a copy of this value. This is equivalent to performing
305+
/// +1 on class values.
305306
///
306307
/// This should be used for duplicating a value from place to place
307308
/// with exactly the same semantics. For example, it performs an
308309
/// unowned_retain on a value of [unknown] type. It is therefore
309310
/// not necessarily the right thing to do on a semantic load.
310-
virtual void emitCopyValue(SILBuilder &B, SILLocation loc,
311-
SILValue value) const = 0;
311+
virtual SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
312+
SILValue value) const = 0;
312313

313-
void emitLoweredCopyChildValue(SILBuilder &B, SILLocation loc,
314-
SILValue value,
315-
LoweringStyle style) const {
314+
SILValue emitLoweredCopyChildValue(SILBuilder &B, SILLocation loc,
315+
SILValue value,
316+
LoweringStyle style) const {
316317
if (style != LoweringStyle::Shallow) {
317-
emitLoweredCopyValue(B, loc, value, style);
318+
return emitLoweredCopyValue(B, loc, value, style);
318319
} else {
319-
emitCopyValue(B, loc, value);
320+
return emitCopyValue(B, loc, value);
320321
}
321322
}
322323

lib/SIL/TypeLowering.cpp

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,17 @@ namespace {
538538
// Trivial
539539
}
540540

541-
void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
542-
LoweringStyle style) const override {
541+
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
542+
SILValue value,
543+
LoweringStyle style) const override {
543544
// Trivial
545+
return value;
544546
}
545547

546-
void emitCopyValue(SILBuilder &B, SILLocation loc,
547-
SILValue value) const override {
548+
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
549+
SILValue value) const override {
548550
// Trivial
551+
return value;
549552
}
550553

551554
void emitDestroyValue(SILBuilder &B, SILLocation loc,
@@ -615,6 +618,9 @@ namespace {
615618
IsNotReferenceCounted) {
616619
}
617620

621+
virtual SILValue rebuildAggregate(SILBuilder &B, SILLocation loc,
622+
ArrayRef<SILValue> values) const = 0;
623+
618624
ArrayRef<Child> getChildren(SILModule &M) const {
619625
if (Children.data() == nullptr) {
620626
SmallVector<Child, 4> children;
@@ -657,22 +663,31 @@ namespace {
657663
});
658664
}
659665

660-
void emitCopyValue(SILBuilder &B, SILLocation loc,
661-
SILValue aggValue) const override {
666+
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
667+
SILValue aggValue) const override {
662668
B.createRetainValue(loc, aggValue, Atomicity::Atomic);
669+
return aggValue;
663670
}
664671

665-
void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue aggValue,
666-
LoweringStyle style) const override {
672+
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
673+
SILValue aggValue,
674+
LoweringStyle style) const override {
675+
llvm::SmallVector<SILValue, 8> loweredChildValues;
667676
for (auto &child : getChildren(B.getModule())) {
668677
auto &childLowering = child.getLowering();
669678
SILValue childValue = asImpl().emitRValueProject(B, loc, aggValue,
670679
child.getIndex(),
671680
childLowering);
672681
if (!childLowering.isTrivial()) {
673-
childLowering.emitLoweredCopyChildValue(B, loc, childValue, style);
682+
SILValue loweredChildValue = childLowering.emitLoweredCopyChildValue(
683+
B, loc, childValue, style);
684+
loweredChildValues.push_back(loweredChildValue);
685+
} else {
686+
loweredChildValues.push_back(childValue);
674687
}
675688
}
689+
690+
return rebuildAggregate(B, loc, loweredChildValues);
676691
}
677692

678693
void emitDestroyValue(SILBuilder &B, SILLocation loc,
@@ -713,7 +728,7 @@ namespace {
713728
}
714729

715730
SILValue rebuildAggregate(SILBuilder &B, SILLocation loc,
716-
ArrayRef<SILValue> values) const {
731+
ArrayRef<SILValue> values) const override {
717732
return B.createTuple(loc, getLoweredType(), values);
718733
}
719734

@@ -748,7 +763,7 @@ namespace {
748763
}
749764

750765
SILValue rebuildAggregate(SILBuilder &B, SILLocation loc,
751-
ArrayRef<SILValue> values) const {
766+
ArrayRef<SILValue> values) const override {
752767
return B.createStruct(loc, getLoweredType(), values);
753768
}
754769

@@ -773,14 +788,16 @@ namespace {
773788
: NonTrivialLoadableTypeLowering(SILType::getPrimitiveObjectType(type),
774789
IsNotReferenceCounted) {}
775790

776-
void emitCopyValue(SILBuilder &B, SILLocation loc,
777-
SILValue value) const override {
791+
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
792+
SILValue value) const override {
778793
B.createRetainValue(loc, value, Atomicity::Atomic);
794+
return value;
779795
}
780796

781-
void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
797+
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
782798
LoweringStyle style) const override {
783799
B.createRetainValue(loc, value, Atomicity::Atomic);
800+
return value;
784801
}
785802

786803
void emitDestroyValue(SILBuilder &B, SILLocation loc,
@@ -802,9 +819,10 @@ namespace {
802819
LeafLoadableTypeLowering(SILType type, IsReferenceCounted_t isRefCounted)
803820
: NonTrivialLoadableTypeLowering(type, isRefCounted) {}
804821

805-
void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
806-
LoweringStyle style) const override {
807-
emitCopyValue(B, loc, value);
822+
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
823+
SILValue value,
824+
LoweringStyle style) const override {
825+
return emitCopyValue(B, loc, value);
808826
}
809827

810828
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
@@ -820,10 +838,11 @@ namespace {
820838
ReferenceTypeLowering(SILType type)
821839
: LeafLoadableTypeLowering(type, IsReferenceCounted) {}
822840

823-
void emitCopyValue(SILBuilder &B, SILLocation loc,
824-
SILValue value) const override {
841+
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
842+
SILValue value) const override {
825843
if (!isa<FunctionRefInst>(value))
826844
B.createStrongRetain(loc, value, Atomicity::Atomic);
845+
return value;
827846
}
828847

829848
void emitDestroyValue(SILBuilder &B, SILLocation loc,
@@ -838,9 +857,10 @@ namespace {
838857
LoadableUnownedTypeLowering(SILType type)
839858
: LeafLoadableTypeLowering(type, IsReferenceCounted) {}
840859

841-
void emitCopyValue(SILBuilder &B, SILLocation loc,
842-
SILValue value) const override {
860+
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
861+
SILValue value) const override {
843862
B.createUnownedRetain(loc, value, Atomicity::Atomic);
863+
return value;
844864
}
845865

846866
void emitDestroyValue(SILBuilder &B, SILLocation loc,
@@ -883,13 +903,14 @@ namespace {
883903
B.emitDestroyAddrAndFold(loc, value);
884904
}
885905

886-
void emitCopyValue(SILBuilder &B, SILLocation loc,
887-
SILValue value) const override {
906+
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
907+
SILValue value) const override {
888908
llvm_unreachable("type is not loadable!");
889909
}
890910

891-
void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
892-
LoweringStyle style) const override {
911+
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
912+
SILValue value,
913+
LoweringStyle style) const override {
893914
llvm_unreachable("type is not loadable!");
894915
}
895916

0 commit comments

Comments
 (0)