-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add @[TargetFeature] annotation
#16717
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
straight-shoota
merged 14 commits into
crystal-lang:master
from
stakach:target-annotation
Mar 26, 2026
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3b46ba9
feat: add 0020-target-annotation
stakach 6569fb3
fix: target features extraction
stakach ebea912
feat: update specs
stakach a49dfe4
check for StringLiteral
stakach e7bc9fe
fix spec
stakach 9d95d0c
fix spec
stakach 40b3aac
use .value to obtain the string literal value
stakach 3f3f379
Merge branch 'master' into target-annotation
stakach 26b9786
Merge branch 'master' into target-annotation
stakach 302ad17
Merge branch 'master' into target-annotation
stakach 6b4e327
chore: fix spec
stakach 2038f7f
chore: fix spec
stakach 76bb314
Fix: function must be called
ysbaddaden e1342a5
simplify cpu x86-64-v3 example
ysbaddaden 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| require "../spec_helper" | ||
|
|
||
| describe "Code gen: TargetFeature annotation" do | ||
| it "can target optional CPU features" do | ||
| {% if compare_versions(Crystal::LLVM_VERSION, "13.0.0") < 0 %} pending! "requires LLVM 13+" {% end %} | ||
|
|
||
| compile(<<-CRYSTAL, target: "aarch64-darwin") | ||
| @[TargetFeature("+sve,+sve2")] | ||
| def sve2_smoke_test : Nil | ||
| asm("ext z0.b, { z1.b, z2.b }, #0" :::: "volatile") | ||
| nil | ||
| end | ||
|
|
||
| sve2_smoke_test | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "can optimize code for a specific CPU" do | ||
| {% if compare_versions(Crystal::LLVM_VERSION, "13.0.0") < 0 %} pending! "requires LLVM 13+" {% end %} | ||
|
|
||
| compile(<<-CRYSTAL, prelude: "prelude", target: "x86_64-linux-gnu") | ||
| @[TargetFeature(cpu: "x86-64-v3")] | ||
| def foo | ||
| [1, 2, 3].sample | ||
| end | ||
|
|
||
| foo | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "can target optional CPU features and optimize code for a specific CPU" do | ||
| {% if compare_versions(Crystal::LLVM_VERSION, "13.0.0") < 0 %} pending! "requires LLVM 13+" {% end %} | ||
|
|
||
| compile(<<-CRYSTAL, target: "aarch64-darwin") | ||
| @[TargetFeature("+sve,+sve2", cpu: "apple-m1")] | ||
| def sve2_smoke_test : Nil | ||
| asm("ext z0.b, { z1.b, z2.b }, #0" :::: "volatile") | ||
| nil | ||
| end | ||
|
|
||
| sve2_smoke_test | ||
| CRYSTAL | ||
| end | ||
| end | ||
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,63 @@ | ||
| require "../spec_helper" | ||
|
|
||
| describe "Semantic: TargetFeature annotation" do | ||
| it "errors if invalid argument provided" do | ||
| assert_error <<-CRYSTAL, "no argument named 'invalid', expected 'cpu'" | ||
| @[TargetFeature(invalid: "lorem ipsum")] | ||
| def foo | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "errors if invalid cpu argument type provided" do | ||
| assert_error <<-CRYSTAL, "expected argument 'cpu' to be String" | ||
| @[TargetFeature(cpu: 3)] | ||
| def foo | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "errors if invalid cpu argument type provided and feature provided" do | ||
| assert_error <<-CRYSTAL, "expected argument 'cpu' to be String" | ||
| @[TargetFeature("+sve", cpu: 4)] | ||
| def foo | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "errors if invalid feature argument type provided" do | ||
| assert_error <<-CRYSTAL, "expected argument #1 to 'TargetFeature' to be String" | ||
| @[TargetFeature(3)] | ||
| def foo | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "errors if invalid feature argument type provided and cpu provided" do | ||
| assert_error <<-CRYSTAL, "expected argument #1 to 'TargetFeature' to be String" | ||
| @[TargetFeature(3, cpu: "apple-m1")] | ||
| def foo | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "errors if wrong number of arguments provided" do | ||
| assert_error <<-CRYSTAL, "wrong number of arguments for TargetFeature (given 2, expected 0..1)" | ||
| @[TargetFeature("+sve", "+sve2")] | ||
| def foo | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "can target a specific LLVM supported feature" do | ||
| assert_type(<<-CRYSTAL) { int32 } | ||
| # This feature is available on all platforms | ||
| @[TargetFeature("+strict-align")] | ||
| def strict_align(input : Int32) : Int32 | ||
| input | ||
| end | ||
|
|
||
| strict_align(1) | ||
| CRYSTAL | ||
| end | ||
| end |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.