Skip to content

Swift SIL: Fix argument conventions for functions which have both, a direct and indirect result #81806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions SwiftCompilerSources/Sources/SIL/Argument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ public struct ArgumentConventions : Collection, CustomStringConvertible {
if let paramIdx = parameterIndex(for: argumentIndex) {
return convention.parameters[paramIdx].convention
}
let resultInfo = convention.indirectSILResults[argumentIndex]
let resultInfo = convention.indirectSILResult(at: argumentIndex)
return ArgumentConvention(result: resultInfo.convention)
}

public subscript(result argumentIndex: Int) -> ResultInfo? {
if parameterIndex(for: argumentIndex) != nil {
return nil
}
return convention.indirectSILResults[argumentIndex]
return convention.indirectSILResult(at: argumentIndex)
}

public subscript(parameter argumentIndex: Int) -> ParameterInfo? {
Expand Down
14 changes: 9 additions & 5 deletions SwiftCompilerSources/Sources/SIL/FunctionConvention.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ public struct FunctionConvention : CustomStringConvertible {
: SILFunctionType_getNumPackResults(functionType.bridged)
}

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

public var parameters: Parameters {
Expand Down
16 changes: 16 additions & 0 deletions test/SILOptimizer/mem-behavior.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1941,3 +1941,19 @@ bb0(%0 : $*C, %1 : $*C, %2 : @guaranteed $C):
%99 = tuple ()
return %99 : $()
}

sil @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)

// CHECK-LABEL: @aliasing_apply_with_direct_and_indirect_result
// CHECK: PAIR #0.
// CHECK-NEXT: %2 = apply %1(%0) : $@convention(thin) () -> (Int64, @out UInt64)
// CHECK-NEXT: %0 = argument of bb0 : $*UInt64
// CHECK-NEXT: r=0,w=1
sil [ossa] @aliasing_apply_with_direct_and_indirect_result : $@convention(thin) () -> @out UInt64 {
bb0(%0 : $*UInt64):
%1 = function_ref @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)
%2 = apply %1(%0) : $@convention(thin) () -> (Int64, @out UInt64)
%99 = tuple ()
return %99 : $()
}

18 changes: 18 additions & 0 deletions test/SILOptimizer/redundant_load_elim_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,24 @@ bb0(%0 : $*ExistentialIntPair, %1 : $*ExistentialIntPair):
return %4
}

sil @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)

// CHECK-LABEL: sil [ossa] @aliasing_apply_with_direct_and_indirect_result :
// CHECK: apply
// CHECK: [[L:%.*]] = load
// CHECK: return [[L]]
// CHECK-LABEL: } // end sil function 'aliasing_apply_with_direct_and_indirect_result'
sil [ossa] @aliasing_apply_with_direct_and_indirect_result : $@convention(thin) (UInt64) -> UInt64 {
bb0(%0 : $UInt64):
%4 = alloc_stack $UInt64
store %0 to [trivial] %4
%11 = function_ref @returns_tuple : $@convention(thin) () -> (Int64, @out UInt64)
%12 = apply %11(%4) : $@convention(thin) () -> (Int64, @out UInt64)
%13 = load [trivial] %4
dealloc_stack %4
return %13
}

// CHECK-LABEL: sil [ossa] @struct_of_optional_none :
// CHECK: [[E:%.*]] = enum
// CHECK: [[S:%.*]] = struct $S ([[E]] : $Optional<String>)
Expand Down