Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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 .
*/
@omochi

This comment has been minimized.

Copy link
Owner Author

commented Jul 2, 2019

ORTは例えABIPublicでも同じモジュールの場合しか最適化しないっぽい
コンパイラの該当箇所
https://github.com/apple/swift/blob/d939e786c4e63d99565391a3eaa3b401ceef6320/lib/AST/Type.cpp#L2550-L2556

@omochi

This comment has been minimized.

Copy link
Owner Author

commented Jul 2, 2019

最適化された場合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
You can’t perform that action at this time.