Repository navigation
Add experimental _hasTargetFeature predicate for CPU/ISA feature query #92189
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
Open
hjyamauchi
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
hjyamauchi:has-target-feature-predicate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,31 @@ | ||
| // RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -target arm64-apple-macosx13.0 -target-cpu apple-a14 -enable-experimental-feature TargetFeaturePredicate -module-name has_target_feature | ||
| // RUN: %FileCheck %s < %t.swiftinterface | ||
| // RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name has_target_feature | ||
|
|
||
| // REQUIRES: swift_feature_TargetFeaturePredicate | ||
| // REQUIRES: OS=macosx && CPU=arm64 | ||
|
|
||
| // Check that hasTargetFeature is retained rather than evaluated away in inlinable function bodies. | ||
|
|
||
| // CHECK-LABEL: func hasDotProd | ||
| // CHECK: #if $TargetFeaturePredicate | ||
| // CHECK-NEXT: #if _hasTargetFeature("dotprod") | ||
| // CHECK-NEXT: return true | ||
| // CHECK-NEXT: #else | ||
| // CHECK-NEXT: return false | ||
| // CHECK-NEXT: #endif | ||
| // CHECK-NEXT: #else | ||
| // CHECK-NEXT: return false | ||
| // CHECK-NEXT: #endif | ||
| @inlinable | ||
| public func hasDotProd() -> Bool { | ||
| #if $TargetFeaturePredicate | ||
| #if _hasTargetFeature("dotprod") | ||
| return true | ||
| #else | ||
| return false | ||
| #endif | ||
| #else | ||
| return false | ||
| #endif | ||
| } |
46 changes: 46 additions & 0 deletions
46
test/Parse/ConditionalCompilation/has_target_feature.swift
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,46 @@ | ||
| // RUN: %swift -typecheck %s -target x86_64-apple-macosx13.0 -target-cpu haswell -enable-experimental-feature TargetFeaturePredicate -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-HASWELL %s | ||
| // RUN: %swift -typecheck %s -target x86_64-apple-macosx13.0 -target-cpu core2 -enable-experimental-feature TargetFeaturePredicate -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-CORE2 %s | ||
| // RUN: %swift -typecheck %s -target arm64-apple-macosx13.0 -target-cpu apple-a14 -enable-experimental-feature TargetFeaturePredicate -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-A14 %s | ||
| // RUN: %swift -typecheck %s -target arm64-apple-macosx13.0 -target-cpu generic -enable-experimental-feature TargetFeaturePredicate -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-GENERIC-ARM %s | ||
| // RUN: %swift -typecheck %s -target x86_64-apple-macosx13.0 -target-cpu core2 -enable-experimental-feature TargetFeaturePredicate -parse-stdlib -Xcc -Xclang -Xcc -target-feature -Xcc -Xclang -Xcc +avx2 2>&1 | %FileCheck -check-prefix CHECK-CORE2-XCC-OVERRIDE %s | ||
|
|
||
| // REQUIRES: swift_feature_TargetFeaturePredicate | ||
|
|
||
| #if _hasTargetFeature("avx2") | ||
| #warning("has avx2") | ||
| #else | ||
| #warning("no avx2") | ||
| #endif | ||
|
|
||
| // A misspelled/unknown feature name is not a real feature | ||
| #if _hasTargetFeature("avx3000") | ||
| #warning("has avx3000") | ||
| #else | ||
| #warning("no avx3000") | ||
| #endif | ||
|
|
||
| #if arch(aarch64) && _hasTargetFeature("dotprod") | ||
| #warning("has dotprod") | ||
| #else | ||
| #warning("no dotprod") | ||
| #endif | ||
|
|
||
| // CHECK-HASWELL: has avx2 | ||
| // CHECK-HASWELL: no avx3000 | ||
| // CHECK-HASWELL: no dotprod | ||
|
|
||
| // CHECK-CORE2: no avx2 | ||
| // CHECK-CORE2: no avx3000 | ||
| // CHECK-CORE2: no dotprod | ||
|
|
||
| // CHECK-A14: no avx2 | ||
| // CHECK-A14: no avx3000 | ||
| // CHECK-A14: has dotprod | ||
|
|
||
| // CHECK-GENERIC-ARM: no avx2 | ||
| // CHECK-GENERIC-ARM: no avx3000 | ||
| // CHECK-GENERIC-ARM: no dotprod | ||
|
|
||
| // CHECK-CORE2-XCC-OVERRIDE: has avx2 | ||
| // CHECK-CORE2-XCC-OVERRIDE: no avx3000 | ||
| // CHECK-CORE2-XCC-OVERRIDE: no dotprod |
7 changes: 7 additions & 0 deletions
7
test/Parse/ConditionalCompilation/has_target_feature_disabled.swift
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,7 @@ | ||
| // RUN: %target-typecheck-verify-swift | ||
|
|
||
| #if _hasTargetFeature("avx2") // expected-error {{invalid conditional compilation expression}} | ||
| #warning("has avx2") | ||
| #else | ||
| #warning("no avx2") // expected-warning {{no avx2}} | ||
| #endif |
15 changes: 15 additions & 0 deletions
15
test/Parse/ConditionalCompilation/has_target_feature_invalid.swift
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,15 @@ | ||
| // RUN: %target-typecheck-verify-swift -enable-experimental-feature TargetFeaturePredicate | ||
|
|
||
| // REQUIRES: swift_feature_TargetFeaturePredicate | ||
|
|
||
| #if _hasTargetFeature(avx2) // expected-error {{'_hasTargetFeature' requires a single unlabeled argument for the target feature name}} | ||
| #endif | ||
|
|
||
| #if _hasTargetFeature("") // expected-error {{'_hasTargetFeature' requires a single unlabeled argument for the target feature name}} | ||
| #endif | ||
|
|
||
| #if _hasTargetFeature() // expected-error {{'_hasTargetFeature' requires a single unlabeled argument for the target feature name}} | ||
| #endif | ||
|
|
||
| #if _hasTargetFeature("avx2", "avx512f") // expected-error {{'_hasTargetFeature' requires a single unlabeled argument for the target feature name}} | ||
| #endif |
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.
I'm not sure about this implementation. Doesn't the
@targetattribute change what target features are available within a particular function?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.
Yes, but this is orthogonal. This is meant to support the following idiom:
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes and this allows us to choose among different versions at build time. What I'm aiming for here is the combination of @_target and this so that we can call separate code optimized for different ISA features based on the build-time baseline target CPU, something like:
@_target("sve2")
func foo_sve2() { ... }
@_target("sve")
func foo_sve() { ... }
// default
func foo_default() { ... }
#if _hasTargetFeature("sve2")
foo_sve2()
#elseif _hasTargetFeature("sve")
foo_sve()
#else
foo_default()
#endif
where foo_sve2() is taken when it's built with sve2 and foo_sve() is taken when it's built with sve
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.
Okay, now put that into a function that uses
@target:When
arch(particular processor)holds, and the target provided on the command line doesn't imply_hasTargetFeature(particular feature), what should I expect this to do?Uh oh!
There was an error while loading. Please reload this page.
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.
_hasTargetFeature()refers to the current processor architecture being targeted by the current compilation context. This may be affected by compiler command-line options and by surrounding@target()annotations.In your example here, the presence of
@target(particular feature)indicates that this function is being compiled for a processor with that particular feature, so I would expect_hasTargetFeature(particular feature)to evaluatetrue.To be honest, this seems like a pointless combination in practice. For an MVP, I'd be happy for
_hasTargetFeature()to diagnose when put inside a function with@target()on it.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 still think
#ifis the right design here. The whole point is being able to choose different implementations at compile time and that's exactly what#ifexpresses.As I said, there's not much point to combining these two. Is it feasible to diagnose? I'd have to think about whether I would be willing to just silently ignore
@target()for this purpose. Maybe @stephentyrone has some insight?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.
Some variation of function multi-versioning could conceivably be an alternative: clang supports a feature where you provide overloads of a function and annotate each one with the particular processor/target feature that it should be used for. This should allow us to defer final function selection until IRGen (or even later).
Uh oh!
There was an error while loading. Please reload this page.
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.
If we have multi-versioning, do we need this
#if _hasTargetFeature?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 think
#ifallows purely compile-time conditional code while multi-versioning involves runtime/link-time checks/indirection. An expected use case is when we are willing to raise baseline target cpu and have multiple builds optimized for each CPU generation. Maybe not be a perfect example but writing code sort of like this would be enabled.Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, we absolutely need (a good version of) this in the presence of multi-versioning (if anything, it becomes more important, not less). Consider something like a two-register lookup table in SIMD. This is going to get inlined into a multi-versioned function. On arm64, it should become TBL. On baseline x86 it has to be an explicit memory gather. But on x86 with SSSE3 (whether via command line flags, target, or multi-versioning), it should become two PSHUFBs and a blend. On x86 with the right AVX512 extensions, it should be a single instruction. But there's no way to write that in a target-independent manner*. We actually need to have different implementations of the two-register lookup table depending on what features are available in the context we get inlined into.
(*) LLVM has been working on this, so it might be soon, but lots of other things still won't be.
Even if one isn't writing explicit SIMD, I frequently want to use a different algorithm depending on what instructions are available. For instance, in something like
Augmented.productfrom Numerics, we would like to use an FMA-based algorithm when targeting x86 with FMA, but Dekker's hi-lo split algorithm when we do not have FMA available, rather than going through a soft float FMA from the host libm.