Skip to content

Commit c33abb7

Browse files
committed
[silgen] Add support for borrowing an RValue.
I also changed the way RValues are copied API wise to match how borrows are copied. rdar://33358110
1 parent b14bb7b commit c33abb7

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

lib/SILGen/ManagedValue.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using namespace swift;
2323
using namespace Lowering;
2424

2525
/// Emit a copy of this value with independent ownership.
26-
ManagedValue ManagedValue::copy(SILGenFunction &SGF, SILLocation loc) {
26+
ManagedValue ManagedValue::copy(SILGenFunction &SGF, SILLocation loc) const {
2727
auto &lowering = SGF.getTypeLowering(getType());
2828
if (lowering.isTrivial())
2929
return *this;

lib/SILGen/ManagedValue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class ManagedValue {
236236
}
237237

238238
/// Emit a copy of this value with independent ownership.
239-
ManagedValue copy(SILGenFunction &SGF, SILLocation loc);
239+
ManagedValue copy(SILGenFunction &SGF, SILLocation loc) const;
240240

241241
/// Emit a copy of this value with independent ownership into the current
242242
/// formal evaluation scope.

lib/SILGen/RValue.cpp

+20-9
Original file line numberDiff line numberDiff line change
@@ -672,16 +672,26 @@ void RValue::extractElements(SmallVectorImpl<RValue> &elements) && {
672672
makeUsed();
673673
}
674674

675-
RValue::RValue(const RValue &copied, SILGenFunction &SGF, SILLocation l)
676-
: type(copied.type),
677-
elementsToBeAdded(copied.elementsToBeAdded)
678-
{
679-
assert((copied.isComplete() || copied.isInSpecialState())
680-
&& "can't copy incomplete rvalue");
681-
values.reserve(copied.values.size());
682-
for (ManagedValue value : copied.values) {
683-
values.push_back(value.copy(SGF, l));
675+
RValue RValue::copy(SILGenFunction &SGF, SILLocation loc) const & {
676+
assert((isComplete() || isInSpecialState()) &&
677+
"can't copy an incomplete rvalue");
678+
std::vector<ManagedValue> copiedValues;
679+
copiedValues.reserve(values.size());
680+
for (ManagedValue v : values) {
681+
copiedValues.emplace_back(v.copy(SGF, loc));
684682
}
683+
return RValue(std::move(copiedValues), type, elementsToBeAdded);
684+
}
685+
686+
RValue RValue::borrow(SILGenFunction &SGF, SILLocation loc) const & {
687+
assert((isComplete() || isInSpecialState()) &&
688+
"can't borrow incomplete rvalue");
689+
std::vector<ManagedValue> borrowedValues;
690+
borrowedValues.reserve(values.size());
691+
for (ManagedValue v : values) {
692+
borrowedValues.emplace_back(v.borrow(SGF, loc));
693+
}
694+
return RValue(std::move(borrowedValues), type, elementsToBeAdded);
685695
}
686696

687697
ManagedValue RValue::materialize(SILGenFunction &SGF, SILLocation loc) && {
@@ -731,6 +741,7 @@ bool RValue::areObviouslySameValue(SILValue lhs, SILValue rhs) {
731741
void RValue::dump() const {
732742
dump(llvm::errs());
733743
}
744+
734745
void RValue::dump(raw_ostream &OS, unsigned indent) const {
735746
if (isInContext()) {
736747
OS.indent(indent) << "InContext\n";

lib/SILGen/RValue.h

+23-13
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ class Initialization;
3333
class SILGenFunction;
3434

3535
/// An "exploded" SIL rvalue, in which tuple values are recursively
36-
/// destructured. (In SILGen we don't try to explode structs, because doing so
37-
/// would require considering resilience, a job we want to delegate to IRGen).
36+
/// destructured.
37+
///
38+
/// *NOTE* In SILGen we don't try to explode structs, because doing so would
39+
/// require considering resilience, a job we want to delegate to IRGen.
3840
class RValue {
3941
std::vector<ManagedValue> values;
4042
CanType type;
@@ -60,12 +62,18 @@ class RValue {
6062
elementsToBeAdded = Used;
6163
values = {};
6264
}
63-
64-
/// Private constructor used by copy().
65-
RValue(const RValue &copied, SILGenFunction &SGF, SILLocation l);
66-
67-
/// Construct an RValue from a pre-exploded set of
68-
/// ManagedValues. Used to implement the extractElement* methods.
65+
66+
/// Private constructor used by copy() and borrow().
67+
RValue(std::vector<ManagedValue> &&values, CanType type,
68+
unsigned elementsToBeAdded)
69+
: values(std::move(values)), type(type),
70+
elementsToBeAdded(elementsToBeAdded) {}
71+
72+
/// Construct an RValue from a pre-exploded set of ManagedValues.
73+
///
74+
/// Used to implement the extractElement* methods. *NOTE* This constructor
75+
/// assumes that the constructed RValue is fully formed and thus has
76+
/// elementsToBeAdded set to zero.
6977
RValue(ArrayRef<ManagedValue> values, CanType type);
7078

7179
RValue(unsigned state) : elementsToBeAdded(state) {
@@ -106,8 +114,9 @@ class RValue {
106114
/// will be exploded.
107115
RValue(SILGenFunction &SGF, SILLocation l, CanType type, ManagedValue v);
108116

109-
/// Construct an RValue from a pre-exploded set of
110-
/// ManagedValues. Used to implement the extractElement* methods.
117+
/// Construct an RValue from a pre-exploded set of ManagedValues.
118+
///
119+
/// This is used to implement the extractElement* methods.
111120
static RValue withPreExplodedElements(ArrayRef<ManagedValue> values,
112121
CanType type);
113122

@@ -259,9 +268,10 @@ class RValue {
259268
}
260269

261270
/// Emit an equivalent value with independent ownership.
262-
RValue copy(SILGenFunction &SGF, SILLocation l) const & {
263-
return RValue(*this, SGF, l);
264-
}
271+
RValue copy(SILGenFunction &SGF, SILLocation loc) const &;
272+
273+
/// Borrow all subvalues of the rvalue.
274+
RValue borrow(SILGenFunction &SGF, SILLocation loc) const &;
265275

266276
static bool areObviouslySameValue(SILValue lhs, SILValue rhs);
267277
bool isObviouslyEqual(const RValue &rhs) const;

0 commit comments

Comments
 (0)