Skip to content

Commit b48b3b0

Browse files
committed
[sil-opaque-values] Add support for createLoadBorrow.
1 parent 1c85d0c commit b48b3b0

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ class SILBuilder {
517517
}
518518

519519
LoadBorrowInst *createLoadBorrow(SILLocation Loc, SILValue LV) {
520-
assert(LV->getType().isLoadable(F.getModule()));
520+
assert(!SILModuleConventions(F.getModule()).useLoweredAddresses()
521+
|| LV->getType().isLoadable(F.getModule()));
521522
return insert(new (F.getModule())
522523
LoadBorrowInst(getSILDebugLocation(Loc), LV));
523524
}

lib/SIL/SILVerifier.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
12361236
F.hasQualifiedOwnership(),
12371237
"Inst with qualified ownership in a function that is not qualified");
12381238
require(LBI->getType().isObject(), "Result of load must be an object");
1239-
require(LBI->getType().isLoadable(LBI->getModule()),
1239+
require(!fnConv.useLoweredAddresses()
1240+
|| LBI->getType().isLoadable(LBI->getModule()),
12401241
"Load must have a loadable type");
12411242
require(LBI->getOperand()->getType().isAddress(),
12421243
"Load operand must be an address");

test/SILGen/opaque_values_silgen.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ func s460______________foo<Element>(p: UnsafePointer<Element>) -> UnsafeBufferPo
982982
return UnsafeBufferPointer(start: p, count: 1)
983983
}
984984

985-
// Test emitNativeToCBridgedNonoptionalValue
985+
// Test emitNativeToCBridgedNonoptionalValue.
986986
// ---
987987
// CHECK-objc-LABEL: sil hidden @_T020opaque_values_silgen21s470________nativeToCyXlyp7fromAny_tF : $@convention(thin) (@in Any) -> @owned AnyObject {
988988
// CHECK-objc bb0(%0 : $Any):
@@ -1002,7 +1002,7 @@ func s470________nativeToC(fromAny any: Any) -> AnyObject {
10021002
}
10031003
#endif
10041004

1005-
// Test emitOpenExistential
1005+
// Test emitOpenExistential.
10061006
// ---
10071007
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s480_________getErroryps0F0_p04someF0_tF : $@convention(thin) (@owned Error) -> @out Any {
10081008
// CHECK: bb0(%0 : $Error):
@@ -1017,6 +1017,39 @@ func s480_________getError(someError: Error) -> Any {
10171017
return someError
10181018
}
10191019

1020+
// Test SILBuilder.createLoadBorrow.
1021+
// ---
1022+
// CHECK-LABEL: sil private @_T020opaque_values_silgen21s490_______loadBorrowyyF3FooL_V3foo7ElementQzSg5IndexQz3pos_tF : $@convention(method) <Elements where Elements : Collection> (@in Elements.Index, @inout Foo<Elements>) -> @out Optional<Elements.Element> {
1023+
// CHECK: bb0(%0 : $Elements.Index, %1 : $*Foo<Elements>):
1024+
// CHECK: [[READ:%.*]] = begin_access [read] [unknown] %1 : $*Foo<Elements>
1025+
// CHECK: [[LOAD:%.*]] = load_borrow [[READ]] : $*Foo<Elements>
1026+
// CHECK: end_access [[READ]] : $*Foo<Elements>
1027+
// CHECK: [[EXTRACT:%.*]] = struct_extract [[LOAD]] : $Foo<Elements>, #<abstract function>Foo._elements
1028+
// CHECK: [[COPYELT:%.*]] = copy_value [[EXTRACT]] : $Elements
1029+
// CHECK: [[BORROW:%.*]] = begin_borrow %0 : $Elements.Index
1030+
// CHECK: [[COPYIDX:%.*]] = copy_value [[BORROW]] : $Elements.Index
1031+
// CHECK: [[WT:%.*]] = witness_method $Elements, #Collection.subscript!getter.1 : <Self where Self : Collection> (Self) -> (Self.Index) -> Self.Element : $@convention(witness_method) <τ_0_0 where τ_0_0 : Collection> (@in τ_0_0.Index, @in_guaranteed τ_0_0) -> @out τ_0_0.Element
1032+
// CHECK: %{{.*}} = apply [[WT]]<Elements>([[COPYIDX]], [[COPYELT]]) : $@convention(witness_method) <τ_0_0 where τ_0_0 : Collection> (@in τ_0_0.Index, @in_guaranteed τ_0_0) -> @out τ_0_0.Element
1033+
// CHECK: destroy_value [[COPYELT]] : $Elements
1034+
// CHECK: [[ENUM:%.*]] = enum $Optional<Elements.Element>, #Optional.some!enumelt.1, %12 : $Elements.Element
1035+
// CHECK: end_borrow [[BORROW]] from %0 : $Elements.Index, $Elements.Index
1036+
// CHECK: end_borrow [[LOAD]] from [[READ]] : $Foo<Elements>, $*Foo<Elements>
1037+
// CHECK: destroy_value %0 : $Elements.Index
1038+
// CHECK: return %14 : $Optional<Elements.Element>
1039+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s490_______loadBorrowyyF3FooL_V3foo7ElementQzSg5IndexQz3pos_tF'
1040+
1041+
func s490_______loadBorrow() {
1042+
struct Foo<Elements : Collection> {
1043+
internal let _elements: Elements
1044+
1045+
public mutating func foo(pos: Elements.Index) -> Elements.Element? {
1046+
return _elements[pos]
1047+
}
1048+
}
1049+
var foo = Foo(_elements: [])
1050+
_ = foo.foo(pos: 1)
1051+
}
1052+
10201053
// Tests conditional value casts and correspondingly generated reabstraction thunk, with <T> types
10211054
// ---
10221055
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s999_____condTFromAnyyyp_xtlF : $@convention(thin) <T> (@in Any, @in T) -> () {

0 commit comments

Comments
 (0)