-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add Builtin.isConcrete<T>(T.Type) -> Int1
#26466
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ba0612f
Add `Builtin.isConcrete<T>(T.Type) -> Int1`
nvzqz da14b83
Never inline `isConcrete_false` test
nvzqz c4cd641
Fix variable reference in ConstantFolding phase
nvzqz 890409e
Elaborate in docs for `Swift._isConcrete`
nvzqz b35559e
Remove `Builtin.isConcrete` IRGenPrepare SIL test
nvzqz 6fd7db8
Add IRGen SIL test for `Builtin.isConcrete`
nvzqz 34eccf5
Add visibility specifiers to isConcrete IRGen test
nvzqz 6e542bf
Annotate `Swift._isConcrete` with `@_transparent`
nvzqz a1f3cb9
Add comment on required isConcrete specialization
nvzqz ff7542a
Add SIL combine test for `Builtin.isConcrete`
nvzqz e6ca03d
Generalize constant_propagation_stdlib SIL tests
nvzqz 931ab46
Remove unused call to `constantFoldIsConcrete`
nvzqz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -691,6 +691,19 @@ func _isPOD<T>(_ type: T.Type) -> Bool { | |
return Bool(Builtin.ispod(type)) | ||
} | ||
|
||
/// Returns `true` if `type` is known to refer to a concrete type once all | ||
/// optimizations and constant folding has occurred at the call site. Otherwise, | ||
/// this returns `false` if the check has failed. | ||
/// | ||
/// Note that there may be cases in which, despite `T` being concrete at some | ||
/// point in the caller chain, this function will return `false`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add that it will never return a false positive. |
||
@_alwaysEmitIntoClient | ||
nvzqz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@_transparent | ||
public // @testable | ||
func _isConcrete<T>(_ type: T.Type) -> Bool { | ||
return Bool(Builtin.isConcrete(type)) | ||
} | ||
|
||
/// Returns `true` if type is a bitwise takable. A bitwise takable type can | ||
/// just be moved to a different address in memory. | ||
@_transparent | ||
|
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// RUN: %target-swift-frontend -emit-ir -parse-sil %s -module-name Swift -parse-stdlib | %FileCheck %s | ||
|
||
import Builtin | ||
|
||
struct MyInt { | ||
var value: Builtin.Int32 | ||
} | ||
|
||
// CHECK-LABEL: define{{( protected| dllexport)?}} swiftcc i1 @isConcrete_true() {{.*}} { | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: ret i1 true | ||
// CHECK-NEXT: } | ||
sil @isConcrete_true : $@convention(thin) (@thin MyInt.Type) -> Builtin.Int1 { | ||
bb0(%0 : $@thin MyInt.Type): | ||
%1 = builtin "isConcrete"(%0 : $@thin MyInt.Type) : $Builtin.Int1 | ||
return %1 : $Builtin.Int1 | ||
} | ||
|
||
// CHECK-LABEL: define{{( protected| dllexport)?}} swiftcc i1 @isConcrete_false(%swift.type* %T) {{.*}} { | ||
// CHECK-NEXT: entry: | ||
// CHECK: ret i1 false | ||
// CHECK-NEXT: } | ||
sil @isConcrete_false : $@convention(thin) <T> (@thin T.Type) -> Builtin.Int1 { | ||
bb0(%0 : $@thin T.Type): | ||
// FIXME: Explicit specialization is required here when it shouldn't be | ||
%1 = builtin "isConcrete"<T>(%0 : $@thin T.Type) : $Builtin.Int1 | ||
return %1 : $Builtin.Int1 | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// RUN: %target-swift-frontend -module-name constant_propagation_stdlib %s -parse-stdlib -emit-sil -o - | %FileCheck --check-prefix=CHECK-ONONE %s | ||
// RUN: %target-swift-frontend -module-name constant_propagation_stdlib %s -parse-stdlib -emit-sil -o - -O | %FileCheck --check-prefix=CHECK-O %s | ||
|
||
public struct MyInt { | ||
var v: Builtin.Int32 | ||
} | ||
|
||
// CHECK-ONONE-LABEL: sil @$s27constant_propagation_stdlib15isConcrete_trueyBi1_AA5MyIntVF : $@convention(thin) (MyInt) -> Builtin.Int1 { | ||
// CHECK-ONONE: bb0( | ||
// CHECK-ONONE: [[RESULT:%.*]] = integer_literal $Builtin.Int1, -1 | ||
// CHECK-ONONE: return [[RESULT]] | ||
// CHECK-ONONE: } // end sil function '$s27constant_propagation_stdlib15isConcrete_trueyBi1_AA5MyIntVF' | ||
// CHECK-O-LABEL: sil @$s27constant_propagation_stdlib15isConcrete_trueyBi1_AA5MyIntVF : $@convention(thin) (MyInt) -> Builtin.Int1 { | ||
// CHECK-O-NEXT: bb0( | ||
// CHECK-O-NEXT: [[RESULT:%.*]] = integer_literal $Builtin.Int1, -1 | ||
// CHECK-O-NEXT: return [[RESULT]] | ||
// CHECK-O-NEXT: } // end sil function '$s27constant_propagation_stdlib15isConcrete_trueyBi1_AA5MyIntVF' | ||
public func isConcrete_true(_ x: MyInt) -> Builtin.Int1 { | ||
return Builtin.isConcrete(MyInt.self) | ||
} | ||
|
||
// CHECK-ONONE-LABEL: sil @$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlF : $@convention(thin) <T> (@in_guaranteed T) -> Builtin.Int1 { | ||
// CHECK-ONONE: bb0( | ||
// CHECK-ONONE: [[METATYPE:%.*]] = metatype $@thick T.Type | ||
// CHECK-ONONE: [[RESULT:%.*]] = builtin "isConcrete"<T>([[METATYPE]] : $@thick T.Type) : $Builtin.Int1 | ||
// CHECK-ONONE: return [[RESULT]] | ||
// CHECK-ONONE: } // end sil function '$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlF' | ||
// CHECK-O-LABEL: sil [signature_optimized_thunk] [always_inline] @$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlF : $@convention(thin) <T> (@in_guaranteed T) -> Builtin.Int1 { | ||
// CHECK-O: bb0( | ||
// CHECK-O: [[GEN_FUNC:%.*]] = function_ref @$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlFTf4d_n : $@convention(thin) <τ_0_0> () -> Builtin.Int1 | ||
// CHECK-O: [[RESULT:%.*]] = apply [[GEN_FUNC]]<T>() : $@convention(thin) <τ_0_0> () -> Builtin.Int1 | ||
// CHECK-O: return [[RESULT]] | ||
// CHECK-O: } // end sil function '$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlF' | ||
public func isConcrete_false<T>(_ x: T) -> Builtin.Int1 { | ||
return Builtin.isConcrete(T.self) | ||
} | ||
|
||
// CHECK-ONONE-LABEL: sil @$s27constant_propagation_stdlib25isConcrete_generic_calleryBi1_xlF : $@convention(thin) <T> (@in_guaranteed T) -> Builtin.Int1 { | ||
// CHECK-ONONE: bb0( | ||
// CHECK-ONONE: [[GEN_FUNC:%.*]] = function_ref @$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlF : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> Builtin.Int1 | ||
// CHECK-ONONE: [[RESULT:%.*]] = apply [[GEN_FUNC]]<T>(%0) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> Builtin.Int1 | ||
// CHECK-ONONE: return [[RESULT]] | ||
// CHECK-ONONE: } // end sil function '$s27constant_propagation_stdlib25isConcrete_generic_calleryBi1_xlF' | ||
// CHECK-O-LABEL: sil @$s27constant_propagation_stdlib25isConcrete_generic_calleryBi1_xlF : $@convention(thin) <T> (@in_guaranteed T) -> Builtin.Int1 { | ||
// CHECK-O: bb0( | ||
// CHECK-O: [[GEN_FUNC:%.*]] = function_ref @$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlFTf4d_n : $@convention(thin) <τ_0_0> () -> Builtin.Int1 | ||
// CHECK-O: [[RESULT:%.*]] = apply [[GEN_FUNC]]<T>() : $@convention(thin) <τ_0_0> () -> Builtin.Int1 | ||
// CHECK-O: return [[RESULT]] | ||
// CHECK-O: } // end sil function '$s27constant_propagation_stdlib25isConcrete_generic_calleryBi1_xlF' | ||
public func isConcrete_generic_caller<T>(_ x: T) -> Builtin.Int1 { | ||
return isConcrete_false(x) | ||
} | ||
|
||
// CHECK-ONONE-LABEL: sil @$s27constant_propagation_stdlib26isConcrete_concrete_calleryBi1_AA5MyIntVF : $@convention(thin) (MyInt) -> Builtin.Int1 { | ||
// CHECK-ONONE: bb0( | ||
// CHECK-ONONE: [[STACK_ARG:%.*]] = alloc_stack $MyInt | ||
// CHECK-ONONE: store %0 to [[STACK_ARG]] : $*MyInt | ||
// CHECK-ONONE: [[GEN_FUNC:%.*]] = function_ref @$s27constant_propagation_stdlib25isConcrete_generic_calleryBi1_xlF : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> Builtin.Int1 | ||
// CHECK-ONONE: [[RESULT:%.*]] = apply [[GEN_FUNC]]<MyInt>([[STACK_ARG]]) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> Builtin.Int1 | ||
// CHECK-ONONE: dealloc_stack [[STACK_ARG]] : $*MyInt | ||
// CHECK-ONONE: return [[RESULT]] | ||
// CHECK-ONONE: } // end sil function '$s27constant_propagation_stdlib26isConcrete_concrete_calleryBi1_AA5MyIntVF' | ||
// CHECK-O-LABEL: sil @$s27constant_propagation_stdlib26isConcrete_concrete_calleryBi1_AA5MyIntVF : $@convention(thin) (MyInt) -> Builtin.Int1 { | ||
// CHECK-O: bb0( | ||
// CHECK-O: [[RESULT:%.*]] = integer_literal $Builtin.Int1, -1 | ||
// CHECK-O: return [[RESULT]] | ||
// CHECK-O: } // end sil function '$s27constant_propagation_stdlib26isConcrete_concrete_calleryBi1_AA5MyIntVF' | ||
nvzqz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public func isConcrete_concrete_caller(_ x: MyInt) -> Builtin.Int1 { | ||
return isConcrete_generic_caller(x) | ||
} | ||
|
||
// CHECK-ONONE-LABEL: sil @$s27constant_propagation_stdlib4main1xBi1__Bi1_Bi1_tAA5MyIntV_tF : $@convention(thin) (MyInt) -> (Builtin.Int1, Builtin.Int1, Builtin.Int1) { | ||
// CHECK-ONONE: bb0( | ||
// CHECK-ONONE: [[IS_CONCRETE_TRUE_FUNC:%.*]] = function_ref @$s27constant_propagation_stdlib15isConcrete_trueyBi1_AA5MyIntVF : $@convention(thin) (MyInt) -> Builtin.Int1 | ||
// CHECK-ONONE: [[IS_CONCRETE_TRUE:%.*]] = apply [[IS_CONCRETE_TRUE_FUNC]](%0) : $@convention(thin) (MyInt) -> Builtin.Int1 | ||
// CHECK-ONONE: [[STACK_ARG:%.*]] = alloc_stack $MyInt | ||
// CHECK-ONONE: store %0 to [[STACK_ARG]] : $*MyInt | ||
// CHECK-ONONE: [[IS_CONCRETE_FALSE_FUNC:%.*]] = function_ref @$s27constant_propagation_stdlib16isConcrete_falseyBi1_xlF : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> Builtin.Int1 | ||
// CHECK-ONONE: [[IS_CONCRETE_FALSE:%.*]] = apply [[IS_CONCRETE_FALSE_FUNC]]<MyInt>([[STACK_ARG]]) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> Builtin.Int1 | ||
// CHECK-ONONE: dealloc_stack [[STACK_ARG]] : $*MyInt | ||
// CHECK-ONONE: [[IS_CONCRETE_CONCRETE_CALLER_FUNC:%.*]] = function_ref @$s27constant_propagation_stdlib26isConcrete_concrete_calleryBi1_AA5MyIntVF : $@convention(thin) (MyInt) -> Builtin.Int1 | ||
// CHECK-ONONE: [[IS_CONCRETE_CONCRETE_CALLER:%.*]] = apply [[IS_CONCRETE_CONCRETE_CALLER_FUNC]](%0) : $@convention(thin) (MyInt) -> Builtin.Int1 | ||
// CHECK-ONONE: [[RESULT:%.*]] = tuple ([[IS_CONCRETE_TRUE]] : $Builtin.Int1, [[IS_CONCRETE_FALSE]] : $Builtin.Int1, [[IS_CONCRETE_CONCRETE_CALLER]] : $Builtin.Int1) | ||
// CHECK-ONONE: return [[RESULT]] | ||
// CHECK-ONONE: } // end sil function '$s27constant_propagation_stdlib4main1xBi1__Bi1_Bi1_tAA5MyIntV_tF' | ||
// CHECK-O-LABEL: sil @$s27constant_propagation_stdlib4main1xBi1__Bi1_Bi1_tAA5MyIntV_tF : $@convention(thin) (MyInt) -> (Builtin.Int1, Builtin.Int1, Builtin.Int1) { | ||
// CHECK-O-NEXT: bb0( | ||
// CHECK-O-NEXT: [[VALUE:%.*]] = integer_literal $Builtin.Int1, -1 | ||
// CHECK-O-NEXT: [[RESULT:%.*]] = tuple ([[VALUE]] : $Builtin.Int1, [[VALUE]] : $Builtin.Int1, [[VALUE]] : $Builtin.Int1) | ||
// CHECK-O-NEXT: return [[RESULT]] | ||
// CHECK-O-NEXT: } // end sil function '$s27constant_propagation_stdlib4main1xBi1__Bi1_Bi1_tAA5MyIntV_tF' | ||
public func main(x: MyInt) -> (Builtin.Int1, Builtin.Int1, Builtin.Int1) { | ||
return (isConcrete_true(x), isConcrete_false(x), isConcrete_concrete_caller(x)) | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimizing this builtin works by:
So, this extra optimization can just be removed. If I'm right, your tests should still pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I ran my tests against this bit being removed and it starts to fail. So apparently this is a necessary component. 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atrick gentle ping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno, but it definitely doesn't make sense to do the same optimization in two places. To debug it take a minimal .sil test case and run
sil-opt -diagnostic-constant-propagation t.sil
. My reading of the code is that your change toinitalizeWorklist
would push the builtin apply instruction on the worklist, andconstantFoldBuiltin
should get a chance to run on each worklist item.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other call to
constantFoldIsConcrete
has no effect, so I removed it in 931ab46. I found out that this call happens beforeconstantFoldInstruction
gets called.