Skip to content

Commit 00e272d

Browse files
authored
Merge pull request #84924 from meg-gupta/borrowandmutateupdate
Minor update to borrow and mutate accessors
2 parents 3c3f6e7 + 6c4dd2b commit 00e272d

Some content is hidden

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

43 files changed

+344
-193
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ private struct CollectedEffects {
255255
addEffects(.destroy, to: inst.operands[0].value, fromInitialPath: SmallProjectionPath(.anyValueFields))
256256

257257
case is ReturnInst:
258-
if inst.parentFunction.convention.hasGuaranteedAddressResult {
258+
if inst.parentFunction.convention.hasAddressResult {
259259
addEffects(.read, to: inst.operands[0].value)
260260
}
261261

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,6 @@ extension ApplySite {
303303
public var hasGuaranteedResult: Bool {
304304
functionConvention.hasGuaranteedResult
305305
}
306-
307-
public var hasGuaranteedAddressResult: Bool {
308-
functionConvention.hasGuaranteedAddressResult
309-
}
310306
}
311307

312308
extension ApplySite {

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ public enum ArgumentConvention : CustomStringConvertible {
488488
self = .directUnowned
489489
case .pack:
490490
self = .packOut
491-
case .guaranteed, .guaranteedAddress:
492-
fatalError("Result conventions @guaranteed and @guaranteed_addr are always returned directly")
491+
case .guaranteed, .guaranteedAddress, .inout:
492+
fatalError("Result conventions @guaranteed, @guaranteed_address and @inout are always returned directly")
493493
}
494494
}
495495

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,20 @@ public struct FunctionConvention : CustomStringConvertible {
102102
if results.count != 1 {
103103
return false
104104
}
105-
return results[0].convention == .guaranteed
105+
if hasLoweredAddresses {
106+
return results[0].convention == .guaranteed
107+
}
108+
return results[0].convention == .guaranteed || results[0].convention == .guaranteedAddress
106109
}
107110

108-
public var hasGuaranteedAddressResult: Bool {
111+
public var hasAddressResult: Bool {
109112
if results.count != 1 {
110113
return false
111114
}
112-
return results[0].convention == .guaranteedAddress
115+
if hasLoweredAddresses {
116+
return results[0].convention == .guaranteedAddress || results[0].convention == .inout
117+
}
118+
return results[0].convention == .inout
113119
}
114120

115121
public var description: String {
@@ -147,7 +153,7 @@ public struct ResultInfo : CustomStringConvertible {
147153
return hasLoweredAddresses || type.isExistentialArchetypeWithError()
148154
case .pack:
149155
return true
150-
case .owned, .unowned, .unownedInnerPointer, .autoreleased, .guaranteed, .guaranteedAddress:
156+
case .owned, .unowned, .unownedInnerPointer, .autoreleased, .guaranteed, .guaranteedAddress, .inout:
151157
return false
152158
}
153159
}
@@ -373,13 +379,17 @@ public enum ResultConvention : CustomStringConvertible {
373379
case owned
374380

375381
/// The caller is responsible for using the returned address within a valid
376-
/// scope. This is valid only for borrow and mutate accessors.
382+
/// scope. This is valid only for borrow accessors.
377383
case guaranteedAddress
378384

379385
/// The caller is responsible for using the returned value within a valid
380386
/// scope. This is valid only for borrow accessors.
381387
case guaranteed
382388

389+
/// The caller is responsible for mutating the returned address within a valid
390+
/// scope. This is valid only for mutate accessors.
391+
case `inout`
392+
383393
/// The caller is not responsible for destroying this return value. Its type may be trivial, or it may simply be offered unsafely. It is valid at the instant of the return, but further operations may invalidate it.
384394
case unowned
385395

@@ -423,6 +433,8 @@ public enum ResultConvention : CustomStringConvertible {
423433
return "guaranteed"
424434
case .guaranteedAddress:
425435
return "guaranteedAddress"
436+
case .inout:
437+
return "inout"
426438
}
427439
}
428440
}
@@ -456,6 +468,7 @@ extension ResultConvention {
456468
case .Pack: self = .pack
457469
case .Guaranteed: self = .guaranteed
458470
case .GuaranteedAddress: self = .guaranteedAddress
471+
case .Inout: self = .inout
459472
default:
460473
fatalError("unsupported result convention")
461474
}

docs/ABI/Mangling.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,9 @@ mangled in to disambiguate.
925925
RESULT-CONVENTION ::= 'u' // unowned inner pointer
926926
RESULT-CONVENTION ::= 'a' // auto-released
927927
RESULT-CONVENTION ::= 'k' // pack
928+
RESULT-CONVENTION ::= 'l' // guaranteed address
929+
RESULT-CONVENTION ::= 'g' // guaranteed
930+
RESULT-CONVENTION ::= 'm' // inout
928931

929932
RESULT-DIFFERENTIABILITY ::= 'w' // @noDerivative
930933

include/swift/AST/TypeAttr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ SIMPLE_SIL_TYPE_ATTR(pack_out, PackOut)
9595
SIMPLE_SIL_TYPE_ATTR(owned, Owned)
9696
SIMPLE_SIL_TYPE_ATTR(unowned_inner_pointer, UnownedInnerPointer)
9797
SIMPLE_SIL_TYPE_ATTR(guaranteed, Guaranteed)
98-
SIMPLE_SIL_TYPE_ATTR(guaranteed_addr, GuaranteedAddress)
98+
SIMPLE_SIL_TYPE_ATTR(guaranteed_address, GuaranteedAddress)
9999
SIMPLE_SIL_TYPE_ATTR(autoreleased, Autoreleased)
100100
SIMPLE_SIL_TYPE_ATTR(callee_owned, CalleeOwned)
101101
SIMPLE_SIL_TYPE_ATTR(callee_guaranteed, CalleeGuaranteed)

include/swift/AST/Types.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4814,12 +4814,16 @@ enum class ResultConvention : uint8_t {
48144814
Pack,
48154815

48164816
/// The caller is responsible for using the returned address within a valid
4817-
/// scope. This is valid only for borrow and mutate accessors.
4817+
/// scope. This is valid only for borrow accessors.
48184818
GuaranteedAddress,
48194819

48204820
/// The caller is responsible for using the returned value within a valid
48214821
/// scope. This is valid only for borrow accessors.
48224822
Guaranteed,
4823+
4824+
/// The caller is responsible for mutating the returned address within a valid
4825+
/// scope. This is valid only for mutate accessors.
4826+
Inout,
48234827
};
48244828

48254829
// Does this result require indirect storage for the purpose of reabstraction?
@@ -4976,12 +4980,20 @@ class SILResultInfo {
49764980
return getConvention() == ResultConvention::Pack;
49774981
}
49784982

4979-
bool isGuaranteedAddressResult() const {
4980-
return getConvention() == ResultConvention::GuaranteedAddress;
4983+
bool isAddressResult(bool loweredAddresses) const {
4984+
if (loweredAddresses) {
4985+
return getConvention() == ResultConvention::GuaranteedAddress ||
4986+
getConvention() == ResultConvention::Inout;
4987+
}
4988+
return getConvention() == ResultConvention::Inout;
49814989
}
49824990

4983-
bool isGuaranteedResult() const {
4984-
return getConvention() == ResultConvention::Guaranteed;
4991+
bool isGuaranteedResult(bool loweredAddresses) const {
4992+
if (loweredAddresses) {
4993+
return getConvention() == ResultConvention::Guaranteed;
4994+
}
4995+
return getConvention() == ResultConvention::Guaranteed ||
4996+
getConvention() == ResultConvention::GuaranteedAddress;
49854997
}
49864998

49874999
/// Transform this SILResultInfo by applying the user-provided
@@ -5424,18 +5436,18 @@ class SILFunctionType final
54245436
return hasErrorResult() && getErrorResult().isFormalIndirect();
54255437
}
54265438

5427-
bool hasGuaranteedResult() const {
5439+
bool hasGuaranteedResult(bool loweredAddresses) const {
54285440
if (getNumResults() != 1) {
54295441
return false;
54305442
}
5431-
return getResults()[0].isGuaranteedResult();
5443+
return getResults()[0].isGuaranteedResult(loweredAddresses);
54325444
}
54335445

5434-
bool hasGuaranteedAddressResult() const {
5446+
bool hasAddressResult(bool loweredAddresses) const {
54355447
if (getNumResults() != 1) {
54365448
return false;
54375449
}
5438-
return getResults()[0].isGuaranteedAddressResult();
5450+
return getResults()[0].isAddressResult(loweredAddresses);
54395451
}
54405452

54415453
struct IndirectFormalResultFilter {

include/swift/SIL/MemAccessUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ inline bool isGuaranteedAddressReturn(SILValue value) {
169169
if (!defInst) {
170170
return false;
171171
}
172-
return defInst->hasGuaranteedAddressResult();
172+
return defInst->hasAddressResult();
173173
}
174174

175175
/// Return the source address after stripping as many access projections as

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ enum class BridgedResultConvention {
110110
Pack,
111111
GuaranteedAddress,
112112
Guaranteed,
113+
Inout
113114
};
114115

115116
struct BridgedResultInfo {

include/swift/SIL/SILFunctionConventions.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ class SILFunctionConventions {
212212
if (silConv.loweredAddresses)
213213
return funcTy->getDirectFormalResultsType(silConv.getModule(), context);
214214

215+
if (funcTy->hasAddressResult(silConv.loweredAddresses)) {
216+
assert(funcTy->getNumDirectFormalResults() == 1);
217+
return SILType::getPrimitiveAddressType(
218+
funcTy->getSingleDirectFormalResult().getReturnValueType(
219+
silConv.getModule(), funcTy, context));
220+
}
221+
215222
return funcTy->getAllResultsSubstType(silConv.getModule(), context);
216223
}
217224

@@ -322,16 +329,24 @@ class SILFunctionConventions {
322329
if (funcTy->getNumResults() != 1) {
323330
return false;
324331
}
325-
return funcTy->getResults()[0].getConvention() ==
326-
ResultConvention::Guaranteed;
332+
auto resultConvention = funcTy->getResults()[0].getConvention();
333+
if (silConv.loweredAddresses) {
334+
return resultConvention == ResultConvention::Guaranteed;
335+
}
336+
return resultConvention == ResultConvention::Guaranteed ||
337+
resultConvention == ResultConvention::GuaranteedAddress;
327338
}
328339

329-
bool hasGuaranteedAddressResult() const {
340+
bool hasAddressResult() const {
330341
if (funcTy->getNumResults() != 1) {
331342
return false;
332343
}
333-
return funcTy->getResults()[0].getConvention() ==
334-
ResultConvention::GuaranteedAddress;
344+
auto resultConvention = funcTy->getResults()[0].getConvention();
345+
if (silConv.loweredAddresses) {
346+
return resultConvention == ResultConvention::GuaranteedAddress ||
347+
resultConvention == ResultConvention::Inout;
348+
}
349+
return resultConvention == ResultConvention::Inout;
335350
}
336351

337352
struct SILResultTypeFunc;
@@ -675,6 +690,7 @@ inline bool SILModuleConventions::isIndirectSILResult(SILResultInfo result,
675690
case ResultConvention::Autoreleased:
676691
case ResultConvention::GuaranteedAddress:
677692
case ResultConvention::Guaranteed:
693+
case ResultConvention::Inout:
678694
return false;
679695
}
680696

@@ -699,7 +715,7 @@ inline SILType
699715
SILModuleConventions::getSILResultInterfaceType(SILResultInfo result,
700716
bool loweredAddresses) {
701717
return SILModuleConventions::isIndirectSILResult(result, loweredAddresses) ||
702-
result.isGuaranteedAddressResult()
718+
result.isAddressResult(loweredAddresses)
703719
? SILType::getPrimitiveAddressType(result.getInterfaceType())
704720
: SILType::getPrimitiveObjectType(result.getInterfaceType());
705721
}

0 commit comments

Comments
 (0)