Created
July 2, 2019 07:18
-
-
Save omochi/5d30b141270b9f1eb4936c4a56d8697a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a.swift | |
public protocol P { | |
func f() -> Int | |
} | |
public struct PublicStruct : P { | |
public func f() -> Int { | |
return 11 | |
} | |
} | |
internal struct InternalStruct : P { | |
public func f() -> Int { | |
return 12 | |
} | |
} | |
@usableFromInline | |
internal struct ABIPublicStruct : P { | |
public func f() -> Int { | |
return 13 | |
} | |
} | |
public func getP1() -> some P { | |
return PublicStruct() | |
} | |
public func getP2() -> some P { | |
return InternalStruct() | |
} | |
public func getP3() -> some P { | |
return ABIPublicStruct() | |
} | |
// b.swift | |
import a | |
public protocol P { | |
func f() -> Int | |
} | |
internal struct SameModuleStruct : P { | |
public func f() -> Int { | |
return 14 | |
} | |
} | |
func getP4() -> some P { | |
return SameModuleStruct() | |
} | |
func main() { | |
let a = getP1() | |
print(a.f()) | |
let b = getP2() | |
print(b.f()) | |
let c = getP3() | |
print(c.f()) | |
let d = getP4() | |
print(d.f()) | |
} | |
main() | |
/* | |
# ヘッダー生成 | |
$ swiftc -emit-module a.swift | |
# dylib生成 | |
$ swiftc -emit-library a.swift | |
# 実行ファイル生成 | |
$ swiftc -emit-executable b.swift -I . -L . -la | |
# 実行の確認 | |
$ ./b | |
# SIL生成(最適化あり) | |
# swiftc -emit-sil -O b.swift -I . | |
# SIL生成(最適化なし) | |
# swiftc -emit-sil -Onone b.swift -I . | |
*/ |
最適化された場合unchecked_addr_cast
が生成される。
下記でトレースが見れる。
$ swiftc -emit-sil -O b.swift -I . -Xllvm -sil-print-around=opaque-archetype-specializer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ORTは例えABIPublicでも同じモジュールの場合しか最適化しないっぽい
コンパイラの該当箇所
https://github.com/apple/swift/blob/d939e786c4e63d99565391a3eaa3b401ceef6320/lib/AST/Type.cpp#L2550-L2556