Skip to content

Commit 06f0913

Browse files
authored
Merge pull request #81865 from eeckstein/eeckstein-fix-argument-convention-6.2
[6.2] Swift SIL: Fix argument conventions for functions which have both, a direct and indirect result.
2 parents 71b7de4 + 7eb123f commit 06f0913

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,15 @@ public struct ArgumentConventions : Collection, CustomStringConvertible {
285285
if let paramIdx = parameterIndex(for: argumentIndex) {
286286
return convention.parameters[paramIdx].convention
287287
}
288-
let resultInfo = convention.indirectSILResults[argumentIndex]
288+
let resultInfo = convention.indirectSILResult(at: argumentIndex)
289289
return ArgumentConvention(result: resultInfo.convention)
290290
}
291291

292292
public subscript(result argumentIndex: Int) -> ResultInfo? {
293293
if parameterIndex(for: argumentIndex) != nil {
294294
return nil
295295
}
296-
return convention.indirectSILResults[argumentIndex]
296+
return convention.indirectSILResult(at: argumentIndex)
297297
}
298298

299299
public subscript(parameter argumentIndex: Int) -> ParameterInfo? {

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ public struct FunctionConvention : CustomStringConvertible {
5353
: SILFunctionType_getNumPackResults(functionType.bridged)
5454
}
5555

56-
/// Indirect results including the error.
57-
public var indirectSILResults: LazyFilterSequence<Results> {
58-
hasLoweredAddresses
59-
? results.lazy.filter { $0.isSILIndirect }
60-
: results.lazy.filter { $0.convention == .pack }
56+
/// Returns the indirect result - including the error - at `index`.
57+
public func indirectSILResult(at index: Int) -> ResultInfo {
58+
let indirectResults = results.lazy.filter {
59+
hasLoweredAddresses ? $0.isSILIndirect : $0.convention == .pack
60+
}
61+
// Note that subscripting a LazyFilterCollection (with the base index, e.g. `Int`) does not work
62+
// as expected, because it returns the nth element of the base collection!
63+
// Therefore we need to implement the subscript "manually".
64+
return indirectResults.enumerated().first{ $0.offset == index }!.element
6165
}
6266

6367
public var parameters: Parameters {

test/SILOptimizer/mem-behavior.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,3 +1941,19 @@ bb0(%0 : $*C, %1 : $*C, %2 : @guaranteed $C):
19411941
%99 = tuple ()
19421942
return %99 : $()
19431943
}
1944+
1945+
sil @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)
1946+
1947+
// CHECK-LABEL: @aliasing_apply_with_direct_and_indirect_result
1948+
// CHECK: PAIR #0.
1949+
// CHECK-NEXT: %2 = apply %1(%0) : $@convention(thin) () -> (Int64, @out UInt64)
1950+
// CHECK-NEXT: %0 = argument of bb0 : $*UInt64
1951+
// CHECK-NEXT: r=0,w=1
1952+
sil [ossa] @aliasing_apply_with_direct_and_indirect_result : $@convention(thin) () -> @out UInt64 {
1953+
bb0(%0 : $*UInt64):
1954+
%1 = function_ref @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)
1955+
%2 = apply %1(%0) : $@convention(thin) () -> (Int64, @out UInt64)
1956+
%99 = tuple ()
1957+
return %99 : $()
1958+
}
1959+

test/SILOptimizer/redundant_load_elim_ossa.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,24 @@ bb0(%0 : $*ExistentialIntPair, %1 : $*ExistentialIntPair):
17081708
return %4
17091709
}
17101710

1711+
sil @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)
1712+
1713+
// CHECK-LABEL: sil [ossa] @aliasing_apply_with_direct_and_indirect_result :
1714+
// CHECK: apply
1715+
// CHECK: [[L:%.*]] = load
1716+
// CHECK: return [[L]]
1717+
// CHECK-LABEL: } // end sil function 'aliasing_apply_with_direct_and_indirect_result'
1718+
sil [ossa] @aliasing_apply_with_direct_and_indirect_result : $@convention(thin) (UInt64) -> UInt64 {
1719+
bb0(%0 : $UInt64):
1720+
%4 = alloc_stack $UInt64
1721+
store %0 to [trivial] %4
1722+
%11 = function_ref @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)
1723+
%12 = apply %11(%4) : $@convention(thin) () -> (Int64, @out UInt64)
1724+
%13 = load [trivial] %4
1725+
dealloc_stack %4
1726+
return %13
1727+
}
1728+
17111729
// CHECK-LABEL: sil [ossa] @struct_of_optional_none :
17121730
// CHECK: [[E:%.*]] = enum
17131731
// CHECK: [[S:%.*]] = struct $S ([[E]] : $Optional<String>)

0 commit comments

Comments
 (0)