Skip to content

Commit 0af0d5f

Browse files
committed
[ownership] Replace ValueOwnershipKind::Trivial with ValueOwnershipKind::Any.
In a previous commit, I banned in the verifier any SILValue from producing ValueOwnershipKind::Any in preparation for this. This change arises out of discussions in between John, Andy, and I around ValueOwnershipKind::Trivial. The specific realization was that this ownership kind was an unnecessary conflation of the a type system idea (triviality) with an ownership idea (@any, an ownership kind that is compatible with any other ownership kind at value merge points and can only create). This caused the ownership model to have to contort to handle the non-payloaded or trivial cases of non-trivial enums. This is unnecessary if we just eliminate the any case and in the verifier separately verify that trivial => @any (notice that we do not verify that @any => trivial). NOTE: This is technically an NFC intended change since I am just replacing Trivial with Any. That is why if you look at the tests you will see that I actually did not need to update anything except removing some @trivial ownership since @any ownership is represented without writing @any in the parsed sil. rdar://46294760
1 parent 1c9bb72 commit 0af0d5f

File tree

228 files changed

+1491
-1707
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

228 files changed

+1491
-1707
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,8 @@ class SILBuilder {
20952095
/// lowering for the non-address value.
20962096
void emitDestroyValueOperation(SILLocation Loc, SILValue v) {
20972097
assert(!v->getType().isAddress());
2098-
if (v.getOwnershipKind() == ValueOwnershipKind::Trivial)
2098+
if (F->hasQualifiedOwnership() &&
2099+
v.getOwnershipKind() == ValueOwnershipKind::Any)
20992100
return;
21002101
auto &lowering = getTypeLowering(v->getType());
21012102
lowering.emitDestroyValue(*this, Loc, v);

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,8 +2130,8 @@ void SILCloner<ImplClass>::visitUncheckedOwnershipConversionInst(
21302130
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
21312131
ValueOwnershipKind Kind = SILValue(Inst).getOwnershipKind();
21322132
if (getOpValue(Inst->getOperand()).getOwnershipKind() ==
2133-
ValueOwnershipKind::Trivial) {
2134-
Kind = ValueOwnershipKind::Trivial;
2133+
ValueOwnershipKind::Any) {
2134+
Kind = ValueOwnershipKind::Any;
21352135
}
21362136
recordClonedInstruction(Inst, getBuilder().createUncheckedOwnershipConversion(
21372137
getOpLocation(Inst->getLoc()),

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5023,7 +5023,7 @@ class EnumInst : public InstructionBase<SILInstructionKind::EnumInst,
50235023
: InstructionBase(DebugLoc, ResultTy,
50245024
Operand
50255025
? Operand.getOwnershipKind()
5026-
: ValueOwnershipKind(ValueOwnershipKind::Trivial)),
5026+
: ValueOwnershipKind(ValueOwnershipKind::Any)),
50275027
Element(Element) {
50285028
if (Operand) {
50295029
OptionalOperand.emplace(this, Operand);

include/swift/SIL/SILValue.h

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/Basic/Range.h"
2121
#include "swift/Basic/ArrayRefView.h"
22+
#include "swift/Basic/STLExtras.h"
2223
#include "swift/SIL/SILNode.h"
2324
#include "swift/SIL/SILType.h"
2425
#include "llvm/ADT/ArrayRef.h"
@@ -86,17 +87,6 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
8687
/// have.
8788
struct ValueOwnershipKind {
8889
enum innerty : uint8_t {
89-
/// A SILValue with Trivial ownership kind is an independent value that can
90-
/// not be owned. Ownership does not place any constraints on how a SILValue
91-
/// with Trivial ownership kind can be used. Other side effects (e.g. Memory
92-
/// dependencies) must still be respected. A SILValue with Trivial ownership
93-
/// kind must be of Trivial SILType (i.e. SILType::isTrivial(SILModule &)
94-
/// must return true).
95-
///
96-
/// Some examples of SIL types with Trivial ownership are: Builtin.Int32,
97-
/// Builtin.RawPointer, aggregates containing all trivial types.
98-
Trivial,
99-
10090
/// A SILValue with `Unowned` ownership kind is an independent value that
10191
/// has a lifetime that is only guaranteed to last until the next program
10292
/// visible side-effect. To maintain the lifetime of an unowned value, it
@@ -127,10 +117,11 @@ struct ValueOwnershipKind {
127117
/// instruction exactly once along any path through the program.
128118
Guaranteed,
129119

130-
/// A SILValue with undefined ownership. It can pair with /Any/ ownership
131-
/// kinds. This means that it could take on /any/ ownership semantics. This
132-
/// is meant only to model SILUndef and to express certain situations where
133-
/// we use unqualified ownership. Expected to tighten over time.
120+
/// A SILValue with Any ownership kind is an independent value outside of
121+
/// the ownership system. It is used to model trivially typed values as well
122+
/// as trivial cases of non-trivial enums. Naturally Any can be merged with
123+
/// any ValueOwnershipKind allowing us to naturally model merge and branch
124+
/// points in the SSA graph.
134125
Any,
135126

136127
LastValueOwnershipKind = Any,
@@ -162,10 +153,6 @@ struct ValueOwnershipKind {
162153

163154
Optional<ValueOwnershipKind> merge(ValueOwnershipKind RHS) const;
164155

165-
bool isTrivialOr(ValueOwnershipKind Kind) const {
166-
return Value == Trivial || Value == Kind;
167-
}
168-
169156
/// Given that there is an aggregate value (like a struct or enum) with this
170157
/// ownership kind, and a subobject of type Proj is being projected from the
171158
/// aggregate, return Trivial if Proj has trivial type and the aggregate's
@@ -180,7 +167,6 @@ struct ValueOwnershipKind {
180167
/// kinds.
181168
UseLifetimeConstraint getForwardingLifetimeConstraint() const {
182169
switch (Value) {
183-
case ValueOwnershipKind::Trivial:
184170
case ValueOwnershipKind::Any:
185171
case ValueOwnershipKind::Guaranteed:
186172
case ValueOwnershipKind::Unowned:
@@ -199,12 +185,16 @@ struct ValueOwnershipKind {
199185
return merge(other).hasValue();
200186
}
201187

202-
/// Returns true if \p Other is compatible with ValueOwnershipKind::Trivial or
203-
/// this. See isCompatibleWith for more information on what "compatibility"
204-
/// means.
205-
bool isTrivialOrCompatibleWith(ValueOwnershipKind other) const {
206-
return isCompatibleWith(ValueOwnershipKind::Trivial) ||
207-
isCompatibleWith(other);
188+
template <typename RangeTy>
189+
static Optional<ValueOwnershipKind> merge(RangeTy &&r) {
190+
auto initial = Optional<ValueOwnershipKind>(ValueOwnershipKind::Any);
191+
return accumulate(
192+
std::forward<RangeTy>(r), initial,
193+
[](Optional<ValueOwnershipKind> acc, ValueOwnershipKind x) {
194+
if (!acc)
195+
return acc;
196+
return acc.getValue().merge(x);
197+
});
208198
}
209199
};
210200

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 469; // @_hasStorage
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 470; // Last change: Remove @trivial
5656

5757
using DeclIDField = BCFixed<31>;
5858

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ SILArgument *LoadableStorageAllocation::replaceArgType(SILBuilder &argBuilder,
13261326
arg) == pass.largeLoadableArgs.end());
13271327

13281328
arg = arg->getParent()->replaceFunctionArgument(
1329-
arg->getIndex(), newSILType, ValueOwnershipKind::Trivial, arg->getDecl());
1329+
arg->getIndex(), newSILType, ValueOwnershipKind::Any, arg->getDecl());
13301330

13311331
copyArg->replaceAllUsesWith(arg);
13321332
copyArg->eraseFromParent();
@@ -1352,7 +1352,7 @@ void LoadableStorageAllocation::insertIndirectReturnArgs() {
13521352
ctx.getIdentifier("$return_value"),
13531353
pass.F->getDeclContext());
13541354
pass.F->begin()->insertFunctionArgument(0, resultStorageType.getAddressType(),
1355-
ValueOwnershipKind::Trivial, var);
1355+
ValueOwnershipKind::Any, var);
13561356
}
13571357

13581358
void LoadableStorageAllocation::convertIndirectFunctionArgs() {

lib/ParseSIL/ParseSIL.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,12 @@ namespace {
329329
bool parseSILOwnership(ValueOwnershipKind &OwnershipKind) {
330330
// We parse here @ <identifier>.
331331
if (!P.consumeIf(tok::at_sign)) {
332-
// Add error here.
333-
return true;
332+
// If we fail, we must have @any ownership.
333+
OwnershipKind = ValueOwnershipKind::Any;
334+
return false;
334335
}
335336

336-
StringRef AllOwnershipKinds[4] = {"trivial", "unowned", "owned",
337+
StringRef AllOwnershipKinds[3] = {"unowned", "owned",
337338
"guaranteed"};
338339
return parseSILIdentifierSwitch(OwnershipKind, AllOwnershipKinds,
339340
diag::expected_sil_value_ownership_kind);

0 commit comments

Comments
 (0)