Closed asl closed 1 year ago
Tagging @compnerd @BradLarson @rxwei @dan-zheng
The difference could be observed inside silcombiner. At one of its iteration we're having input SIL:
%0 = float_literal $Builtin.FPIEEE32, 0x41200000 // 10 // user: %29
// function_ref float(_:)
%1 = function_ref @$s17e2e_optimizations5floatyS2fF : $@convention(thin) (Float) -> Float // users: %13, %2
%2 = thin_to_thick_function %1 : $@convention(thin) (Float) -> Float to $@callee_guaranteed (Float) -> Float // users: %8, %7
// function_ref forward-mode derivative of float(_:)
%3 = function_ref @$s17e2e_optimizations5floatyS2fFTJfSpSr : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) // users: %14, %4
%4 = thin_to_thick_function %3 : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) to $@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) // user:
%7
// function_ref reverse-mode derivative of float(_:)
%5 = function_ref @$s17e2e_optimizations5floatyS2fFTJrSpSr : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) // users: %15, %6
%6 = thin_to_thick_function %5 : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) to $@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) // user:
%7
%7 = differentiable_function [parameters 0] [results 0] %2 : $@callee_guaranteed (Float) -> Float with_derivative {%4 : $@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float), %6 : $
@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float)} // users: %56, %54, %52, %11, %9
strong_retain %2 : $@callee_guaranteed (Float) -> Float // id: %8
%9 = differentiable_function_extract [jvp] %7 : $@differentiable(reverse) @callee_guaranteed (Float) -> Float // user: %10
strong_retain %9 : $@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) // id: %10
%11 = differentiable_function_extract [vjp] %7 : $@differentiable(reverse) @callee_guaranteed (Float) -> Float // user: %12
strong_retain %11 : $@callee_guaranteed (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) // id: %12
Here strong_retain %2 : $@callee_guaranteed (Float) -> Float // id: %8
is removed on Mac (cross-compiling to Windows) and retained on native Windows.
The difference is caused by Swift optimization passes. In particular, strong_retain
is deleted by simplifyStrongRetainPass
from SwiftCompilerSources/Sources/Optimizer/InstructionPasses/SimplifyStrongRetainRelease.swift
Optimization passes written in Swift are not run by default on Windows native build. The behaviour could be reproduced on Mac / Linux via setting BOOTSTRAPPING_MODE
to OFF
.
@compnerd What is the proper solution on Windows then? Should the test in #62012 be marked as XFAIL on Windows? For now I'm marking it as // REQUIRES: swift_in_compiler
I think that the requires is correct approach.
Here strong_retain %2 : $@callee_guaranteed (Float) -> Float // id: %8 is removed on Mac (cross-compiling to Windows) and retained on native Windows.
Does this mean we're building incorrect code on windows? Or is this only a performance difference? (The former would be much more serious.)
CC: @eeckstein
@tbkka It's a performance difference. Basically some optimizations are not run on Windows.
https://github.com/apple/swift/pull/62012 was landed. The failed test is effectively disabled on Windows native build via // REQUIRES: swift_in_compiler
, the differences are caused by some optimizations that are not available in the Windows native build.
Result of
swiftc -O test\AutoDiff\e2e_optimizations.swift
, cross compilation on Mac:Result of
swiftc -O test\AutoDiff\e2e_optimizations.swift
natively on Windows:Originally posted by @asl in https://github.com/apple/swift/issues/62012#issuecomment-1312744279