Skip to content

Commit 5e0267b

Browse files
Merge pull request #64553 from nate-chandler/eagermove_collections
[stdlib] Collection types are eagerMove.
2 parents 9039a29 + ff3ec19 commit 5e0267b

22 files changed

+123
-99
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,14 @@ private func removeBridgingCodeInPredecessors(of block: BasicBlock, _ context: F
246246
}
247247

248248
private func lookThroughOwnershipInsts(_ value: Value) -> Value {
249-
// Looks like it's sufficient to support begin_borrow for now.
250-
// TODO: add copy_value if needed.
249+
// Looks like it's sufficient to support begin_borrow and copy_value for now.
250+
// TODO: add move_value if needed.
251251
if let bbi = value as? BeginBorrowInst {
252252
return bbi.borrowedValue
253253
}
254+
if let cvi = value as? CopyValueInst {
255+
return cvi.fromValue
256+
}
254257
return value
255258
}
256259

lib/SIL/IR/TypeLowering.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,16 +2758,16 @@ bool TypeConverter::visitAggregateLeaves(
27582758
packIndex);
27592759
}
27602760
} else if (auto tupleTy = ty->getAs<TupleType>()) {
2761-
for (unsigned tupleIndex = 0, num = tupleTy->getNumElements();
2762-
tupleIndex < num; ++tupleIndex) {
2763-
auto origElementTy = origTy.getTupleElementType(tupleIndex);
2764-
auto substElementTy =
2765-
tupleTy->getElementType(tupleIndex)->getCanonicalType();
2766-
substElementTy =
2767-
computeLoweredRValueType(context, origElementTy, substElementTy);
2768-
insertIntoWorklist(substElementTy, origElementTy, nullptr,
2769-
tupleIndex);
2770-
}
2761+
unsigned tupleIndex = 0;
2762+
origTy.forEachExpandedTupleElement(
2763+
CanTupleType(tupleTy),
2764+
[&](auto origElementTy, auto substElementTy, auto element) {
2765+
substElementTy =
2766+
substOpaqueTypesWithUnderlyingTypes(substElementTy, context);
2767+
insertIntoWorklist(substElementTy, origElementTy, nullptr,
2768+
tupleIndex);
2769+
++tupleIndex;
2770+
});
27712771
} else if (auto *decl = ty->getStructOrBoundGenericStruct()) {
27722772
for (auto *structField : decl->getStoredProperties()) {
27732773
auto subMap = ty->getContextSubstitutionMap(&M, decl);

stdlib/public/core/Array.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@
297297
/// instances of those types always have a contiguous block of memory as
298298
/// their storage.
299299
@frozen
300+
@_eagerMove
300301
public struct Array<Element>: _DestructorSafeContainer {
301302
#if _runtime(_ObjC)
302303
@usableFromInline

stdlib/public/core/ContiguousArray.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
/// For more information about using arrays, see `Array` and `ArraySlice`, with
3535
/// which `ContiguousArray` shares most properties and methods.
3636
@frozen
37+
@_eagerMove
3738
public struct ContiguousArray<Element>: _DestructorSafeContainer {
3839
@usableFromInline
3940
internal typealias _Buffer = _ContiguousArrayBuffer<Element>

stdlib/public/core/Dictionary.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386
/// optimization that is used when two instances of `Dictionary` share
387387
/// buffer.
388388
@frozen
389+
@_eagerMove
389390
public struct Dictionary<Key: Hashable, Value> {
390391
/// The element type of a dictionary: a tuple containing an individual
391392
/// key-value pair.

stdlib/public/core/Set.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
/// same copy-on-write optimization that is used when two instances of `Set`
145145
/// share buffer.
146146
@frozen
147+
@_eagerMove
147148
public struct Set<Element: Hashable> {
148149
@usableFromInline
149150
internal var _variant: _Variant

stdlib/public/core/String.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ internal func unimplemented_utf8_32bit(
349349
/// [scalars]: http://www.unicode.org/glossary/#unicode_scalar_value
350350
/// [equivalence]: http://www.unicode.org/glossary/#canonical_equivalent
351351
@frozen
352+
@_eagerMove
352353
public struct String {
353354
public // @SPI(Foundation)
354355
var _guts: _StringGuts

test/AutoDiff/SILOptimizer/activity_analysis.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,26 +350,26 @@ func testArrayUninitializedIntrinsicNested(_ x: Float, _ y: Float) -> [Float] {
350350
// CHECK: [ACTIVE] [[ARRAY:%.*]] = apply %14<Float>(%7) : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0>
351351
// CHECK: [USEFUL] [[INT_LIT:%.*]] = integer_literal $Builtin.Word, 2
352352
// CHECK: [NONE] // function_ref _allocateUninitializedArray<A>(_:)
353-
// CHECK: [ACTIVE] [[TUP:%.*]] = apply %19<Float>([[INT_LIT]]) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)
353+
// CHECK: [ACTIVE] [[TUP:%.*]] = apply %18<Float>([[INT_LIT]]) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)
354354
// CHECK: [ACTIVE] (**[[LHS:%.*]]**, [[RHS:%.*]]) = destructure_tuple [[TUP]] : $(Array<Float>, Builtin.RawPointer)
355355
// CHECK: [VARIED] ([[LHS]], **[[RHS]]**) = destructure_tuple [[TUP]] : $(Array<Float>, Builtin.RawPointer)
356356
// CHECK: [ACTIVE] [[FLOAT_PTR:%.*]] = pointer_to_address [[RHS]] : $Builtin.RawPointer to [strict] $*Float
357357
// CHECK: [USEFUL] [[ZERO_LITERAL:%.*]] = integer_literal $Builtin.IntLiteral, 0
358358
// CHECK: [USEFUL] [[META:%.*]] = metatype $@thin Int.Type
359359
// CHECK: [NONE] // function_ref Int.init(_builtinIntegerLiteral:)
360-
// CHECK: [USEFUL] [[RESULT_2:%.*]] = apply %26([[ZERO_LITERAL]], [[META]]) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int
360+
// CHECK: [USEFUL] [[RESULT_2:%.*]] = apply %25([[ZERO_LITERAL]], [[META]]) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int
361361
// CHECK: [NONE] // function_ref Array.subscript.getter
362-
// CHECK: [NONE] %29 = apply %28<Float>([[FLOAT_PTR]], [[RESULT_2]], %16) : $@convention(method) <τ_0_0> (Int, @guaranteed Array<τ_0_0>) -> @out τ_0_0
362+
// CHECK: [NONE] %28 = apply %27<Float>([[FLOAT_PTR]], [[RESULT_2]], %15) : $@convention(method) <τ_0_0> (Int, @guaranteed Array<τ_0_0>) -> @out τ_0_0
363363
// CHECK: [VARIED] [[ONE_LITERAL:%.*]] = integer_literal $Builtin.Word, 1
364364
// CHECK: [ACTIVE] [[INDEX_ADDR:%.*]] = index_addr [[FLOAT_PTR]] : $*Float, [[ONE_LITERAL]] : $Builtin.Word
365365
// CHECK: [USEFUL] [[ONE_LITERAL_AGAIN:%.*]] = integer_literal $Builtin.IntLiteral, 1
366366
// CHECK: [USEFUL] [[META_AGAIN:%.*]] = metatype $@thin Int.Type
367367
// CHECK: [NONE] // function_ref Int.init(_builtinIntegerLiteral:)
368-
// CHECK: [USEFUL] %35 = apply %34([[ONE_LITERAL_AGAIN]], [[META_AGAIN]]) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int
368+
// CHECK: [USEFUL] %34 = apply %33([[ONE_LITERAL_AGAIN]], [[META_AGAIN]]) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int
369369
// CHECK: [NONE] // function_ref Array.subscript.getter
370-
// CHECK: [NONE] %37 = apply %36<Float>(%31, %35, %16) : $@convention(method) <τ_0_0> (Int, @guaranteed Array<τ_0_0>) -> @out τ_0_0
370+
// CHECK: [NONE] %36 = apply %35<Float>(%30, %34, %15) : $@convention(method) <τ_0_0> (Int, @guaranteed Array<τ_0_0>) -> @out τ_0_0
371371
// CHECK: [NONE] // function_ref _finalizeUninitializedArray<A>(_:)
372-
// CHECK: [ACTIVE] %39 = apply %38<Float>(%21) : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0>
372+
// CHECK: [ACTIVE] %38 = apply %37<Float>(%20) : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0>
373373

374374
// TF-978: Test array literal initialized with `apply` indirect results.
375375
struct Wrapper<T: Differentiable>: Differentiable {
@@ -617,7 +617,7 @@ func testBeginApplyActiveInoutArgument(array: [Float], x: Float) -> Float {
617617
// CHECK-LABEL: [AD] Activity info for ${{.*}}testBeginApplyActiveInoutArgument{{.*}} at parameter indices (0, 1) and result indices (0)
618618
// CHECK: [ACTIVE] %0 = argument of bb0 : $Array<Float>
619619
// CHECK: [ACTIVE] %1 = argument of bb0 : $Float
620-
// CHECK: [ACTIVE] %4 = alloc_stack [lexical] $Array<Float>, var, name "array"
620+
// CHECK: [ACTIVE] %4 = alloc_stack $Array<Float>, var, name "array"
621621
// CHECK: [ACTIVE] %5 = copy_value %0 : $Array<Float>
622622
// CHECK: [USEFUL] %7 = integer_literal $Builtin.IntLiteral, 0
623623
// CHECK: [USEFUL] %8 = metatype $@thin Int.Type
@@ -654,7 +654,7 @@ func testBeginApplyActiveButInitiallyNonactiveInoutArgument(x: Float) -> Float {
654654

655655
// CHECK-LABEL: [AD] Activity info for ${{.*}}testBeginApplyActiveButInitiallyNonactiveInoutArgument{{.*}} at parameter indices (0) and result indices (0)
656656
// CHECK: [ACTIVE] %0 = argument of bb0 : $Float
657-
// CHECK: [ACTIVE] %2 = alloc_stack [lexical] $Array<Float>, var, name "array"
657+
// CHECK: [ACTIVE] %2 = alloc_stack $Array<Float>, var, name "array"
658658
// CHECK: [USEFUL] %3 = integer_literal $Builtin.Word, 1
659659
// CHECK: [NONE] // function_ref _allocateUninitializedArray<A>(_:)
660660
// CHECK: [USEFUL] %5 = apply %4<Float>(%3) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)

test/AutoDiff/SILOptimizer/differentiation_diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ func modify(_ s: Struct, _ x: Float) -> Float {
716716
func tupleArrayLiteralInitialization(_ x: Float, _ y: Float) -> Float {
717717
// `Array<(Float, Float)>` does not conform to `Differentiable`.
718718
let array = [(x * y, x * y)]
719-
// expected-note @-1 {{cannot differentiate through a non-differentiable argument; do you want to use 'withoutDerivative(at:)'?}} {{7-7=withoutDerivative(at: }} {{12-12=)}}
719+
// expected-note @-1 {{cannot differentiate through a non-differentiable argument; do you want to use 'withoutDerivative(at:)'?}} {{15-15=withoutDerivative(at: }} {{31-31=)}}
720720
return array[0].0
721721
}
722722

test/SILGen/consuming_parameter.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ func bar(_: String) {}
66
func foo(y: consuming String, z: String) -> () -> String {
77
// CHECK: bb0(%0 : @owned $String, %1 : @guaranteed $String):
88
// CHECK: [[BOX:%.*]] = alloc_box ${ var String }
9-
// CHECK: [[BOX1:%.*]] = begin_borrow [lexical] [[BOX]]
10-
// CHECK: [[Y:%.*]] = project_box [[BOX1]]
9+
// CHECK: [[Y:%.*]] = project_box [[BOX]]
1110
// CHECK: store %0 to [init] [[Y]]
1211

13-
// CHECK: [[YCAPTURE:%.*]] = copy_value [[BOX1]]
12+
// CHECK: [[YCAPTURE:%.*]] = copy_value [[BOX]]
1413
// CHECK: partial_apply {{.*}} {{%.*}}([[YCAPTURE]])
1514
let r = { y }
1615

@@ -36,11 +35,10 @@ struct Butt {
3635
consuming func merged(with other: Butt) -> () -> Butt {
3736
// CHECK: bb0(%0 : @guaranteed $Butt, %1 : @owned $Butt):
3837
// CHECK: [[BOX:%.*]] = alloc_box ${ var Butt }
39-
// CHECK: [[BOX1:%.*]] = begin_borrow [lexical] [[BOX]]
40-
// CHECK: [[SELF:%.*]] = project_box [[BOX1]]
38+
// CHECK: [[SELF:%.*]] = project_box [[BOX]]
4139
// CHECK: store %1 to [init] [[SELF]]
4240

43-
// CHECK: [[SELFCAPTURE:%.*]] = copy_value [[BOX1]]
41+
// CHECK: [[SELFCAPTURE:%.*]] = copy_value [[BOX]]
4442
// CHECK: partial_apply {{.*}} {{%.*}}([[SELFCAPTURE]])
4543
let r = { self }
4644

0 commit comments

Comments
 (0)