-
Notifications
You must be signed in to change notification settings - Fork 172
Add AIR compatibility #2934
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
pw0908
wants to merge
1
commit into
EnzymeAD:main
Choose a base branch
from
pw0908:metal-air-math-intrinsics
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
Add AIR compatibility #2934
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| ; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -preserve-nvvm -enzyme -early-cse -instcombine -enzyme-preopt=false -S | FileCheck %s; fi | ||
| ; RUN: %opt < %s %newLoadEnzyme -passes="preserve-nvvm,enzyme,function(early-cse,instcombine)" -enzyme-preopt=false -S | FileCheck %s | ||
|
|
||
| ; Metal AIR math intrinsic (air.atan2.f32): not a real LLVM intrinsic, so | ||
| ; this exercises the new air.atan2.f32/air.atan2.f64 CallPattern entry in | ||
| ; forward mode. | ||
|
|
||
| define float @tester(float %y, float %x) { | ||
| entry: | ||
| %call = call float @air.atan2.f32(float %y, float %x) | ||
| ret float %call | ||
| } | ||
|
|
||
| define float @test_derivative(float %y, float %x) { | ||
| entry: | ||
| %0 = tail call float (...) @__enzyme_fwddiff(float (float, float)* nonnull @tester, float %y, float 1.000000e+00, float %x, float 1.000000e+00) | ||
| ret float %0 | ||
| } | ||
|
|
||
| declare float @air.atan2.f32(float, float) | ||
|
|
||
| ; Function Attrs: nounwind | ||
| declare float @__enzyme_fwddiff(...) | ||
|
|
||
| ; CHECK-LABEL: define internal float @fwddiffetester( | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-DAG: %[[a3:.+]] = fmul fast float %"y'", %x | ||
| ; CHECK-DAG: %[[a1:.+]] = fmul fast float %x, %x | ||
| ; CHECK-DAG: %[[a0:.+]] = fmul fast float %y, %y | ||
| ; CHECK-DAG: %[[a2:.+]] = fadd fast float %[[a1]], %[[a0]] | ||
| ; CHECK-DAG: %[[a4:.+]] = fmul fast float %"x'", %y | ||
| ; CHECK-DAG: %[[a5:.+]] = fsub fast float %[[a3]], %[[a4]] | ||
| ; CHECK-DAG: %[[a6:.+]] = fdiv fast float %[[a5]], %[[a2]] | ||
| ; CHECK-NEXT: ret float %[[a6]] | ||
| ; CHECK-NEXT: } |
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,37 @@ | ||
| ; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -enzyme-preopt=false -mem2reg -sroa -early-cse -instsimplify -simplifycfg -adce -S | FileCheck %s; fi | ||
| ; RUN: %opt < %s %newLoadEnzyme -passes="preserve-nvvm,enzyme,function(mem2reg,sroa,early-cse,instsimplify,%simplifycfg,adce)" -enzyme-preopt=false -S | FileCheck %s | ||
|
|
||
| ; Metal AIR math intrinsic (air.atan2.f32): not a real LLVM intrinsic, so | ||
| ; this exercises the new air.atan2.f32/air.atan2.f64 CallPattern entry. | ||
|
|
||
| define float @tester(float %y, float %x) { | ||
| entry: | ||
| %call = call float @air.atan2.f32(float %y, float %x) | ||
| ret float %call | ||
| } | ||
|
|
||
| define float @test_derivative(float %y, float %x) { | ||
| entry: | ||
| %0 = tail call float (...) @__enzyme_autodiff(float (float, float)* nonnull @tester, float %y, float %x) | ||
| ret float %0 | ||
| } | ||
|
|
||
| declare float @air.atan2.f32(float, float) | ||
|
|
||
| ; Function Attrs: nounwind | ||
| declare float @__enzyme_autodiff(...) | ||
|
|
||
| ; CHECK: define internal { float, float } @diffetester(float %y, float %x, float %differeturn) | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-DAG: %[[a0:.+]] = fmul fast float %y, %y | ||
| ; CHECK-DAG: %[[a1:.+]] = fmul fast float %x, %x | ||
| ; CHECK-DAG: %[[a2:.+]] = fadd fast float %[[a1]], %[[a0]] | ||
| ; CHECK-DAG: %[[a3:.+]] = fmul fast float %differeturn, %x | ||
| ; CHECK-DAG: %[[a4:.+]] = fdiv fast float %[[a3]], %[[a2]] | ||
| ; CHECK-DAG: %[[a5:.+]] = fmul fast float %differeturn, %y | ||
| ; CHECK-DAG: %[[a6:.+]] = fdiv fast float %[[a5]], %[[a2]] | ||
| ; CHECK-DAG: %[[a7:.+]] = {{(fneg fast float)|(fsub fast float (-)?0.000000e\+00,)}} %[[a6]] | ||
| ; CHECK-DAG: %[[a8:.+]] = insertvalue { float, float } undef, float %[[a4]], 0 | ||
| ; CHECK-DAG: %[[a9:.+]] = insertvalue { float, float } %[[a8]], float %[[a7]], 1 | ||
| ; CHECK-DAG: ret { float, float } %[[a9]] | ||
| ; CHECK-NEXT: } |
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,33 @@ | ||
| ; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -preserve-nvvm -enzyme -mem2reg -sroa -instsimplify -simplifycfg -S | FileCheck %s; fi | ||
| ; RUN: %opt < %s %newLoadEnzyme -passes="preserve-nvvm,enzyme,function(mem2reg,sroa,instsimplify,%simplifycfg)" -S | FileCheck %s | ||
|
|
||
| ; Metal AIR math intrinsic (air.cbrt.f32): not a real LLVM intrinsic, so | ||
| ; this exercises the new air.cbrt.f32/air.cbrt.f64 CallPattern entry. | ||
|
|
||
| ; Function Attrs: nounwind readnone uwtable | ||
| define float @tester(float %x) { | ||
| entry: | ||
| %call = call float @air.cbrt.f32(float %x) | ||
| ret float %call | ||
| } | ||
|
|
||
| define float @test_derivative(float %x) { | ||
| entry: | ||
| %0 = tail call float (float (float)*, ...) @__enzyme_autodiff(float (float)* nonnull @tester, float %x) | ||
| ret float %0 | ||
| } | ||
|
|
||
| declare float @air.cbrt.f32(float) | ||
|
|
||
| ; Function Attrs: nounwind | ||
| declare float @__enzyme_autodiff(float (float)*, ...) | ||
|
|
||
| ; CHECK: define internal { float } @diffetester(float %x, float %differeturn) | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-NEXT: %0 = call fast float @air.cbrt.f32(float %x) | ||
| ; CHECK-DAG: [[REG1:%[0-9]+]] = fmul fast float 3.000000e+00, %x | ||
| ; CHECK-DAG: [[REG2:%[0-9]+]] = fmul fast float %differeturn, %0 | ||
| ; CHECK-NEXT: %3 = fdiv fast float [[REG2]], [[REG1]] | ||
| ; CHECK-NEXT: %4 = insertvalue { float } undef, float %3, 0 | ||
| ; CHECK-NEXT: ret { float } %4 | ||
| ; CHECK-NEXT: } |
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,35 @@ | ||
| ; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -enzyme-preopt=false -preserve-nvvm -enzyme -mem2reg -instsimplify -simplifycfg -S | FileCheck %s; fi | ||
| ; RUN: %opt < %s %newLoadEnzyme -enzyme-preopt=false -passes="preserve-nvvm,enzyme,function(mem2reg,instsimplify,%simplifycfg)" -S | FileCheck %s | ||
|
|
||
| ; Metal AIR fast-math intrinsic (air.fast_sin.f32): exercises the "fast_" | ||
| ; infix stripping in isMemFreeLibMFunction on top of the air. prefix and | ||
| ; .f32 suffix stripping -- reduces to "sin", a real LLVM intrinsic, so it's | ||
| ; handled by the same rule as air.sin.f32 (see air-sin.ll) with no | ||
| ; dedicated CallPattern entry. | ||
|
|
||
| ; Function Attrs: nounwind readnone uwtable | ||
| define float @tester(float %x) { | ||
| entry: | ||
| %0 = tail call fast float @air.fast_sin.f32(float %x) | ||
| ret float %0 | ||
| } | ||
|
|
||
| define float @test_derivative(float %x) { | ||
| entry: | ||
| %0 = tail call float (float (float)*, ...) @__enzyme_autodiff(float (float)* nonnull @tester, float %x) | ||
| ret float %0 | ||
| } | ||
|
|
||
| ; Function Attrs: nounwind readnone speculatable | ||
| declare float @air.fast_sin.f32(float) | ||
|
|
||
| ; Function Attrs: nounwind | ||
| declare float @__enzyme_autodiff(float (float)*, ...) | ||
|
|
||
| ; CHECK: define internal { float } @diffetester(float %x, float %differeturn) | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-NEXT: %0 = call fast float @llvm.cos.f32(float %x) | ||
| ; CHECK-NEXT: %1 = fmul fast float %differeturn, %0 | ||
| ; CHECK-NEXT: %2 = insertvalue { float } undef, float %1, 0 | ||
| ; CHECK-NEXT: ret { float } %2 | ||
| ; CHECK-NEXT: } |
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 @@ | ||
| ; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -enzyme-preopt=false -preserve-nvvm -enzyme -mem2reg -instsimplify -simplifycfg -S | FileCheck %s; fi | ||
| ; RUN: %opt < %s %newLoadEnzyme -enzyme-preopt=false -passes="preserve-nvvm,enzyme,function(mem2reg,instsimplify,%simplifycfg)" -S | FileCheck %s | ||
|
|
||
| ; Metal AIR math intrinsic (air.pow.f32) resolved via isMemFreeLibMFunction's | ||
| ; air. prefix stripping in TypeAnalysis.h to Intrinsic::pow. The self- | ||
| ; referencing "d/dx x^y" term calls back the original air.pow.f32 (SameFunc | ||
| ; preserves whatever name matched), while the "d/dy x^y" term's cross- | ||
| ; function log companion becomes a genuine llvm.log.f32 intrinsic call. | ||
|
|
||
| ; Function Attrs: noinline nounwind readnone uwtable | ||
| define float @tester(float %x, float %y) { | ||
| entry: | ||
| %0 = tail call fast float @air.pow.f32(float %x, float %y) | ||
| ret float %0 | ||
| } | ||
|
|
||
| define float @test_derivative(float %x, float %y) { | ||
| entry: | ||
| %0 = tail call float (float (float, float)*, ...) @__enzyme_autodiff(float (float, float)* nonnull @tester, float %x, float %y) | ||
| ret float %0 | ||
| } | ||
|
|
||
| ; Function Attrs: nounwind readnone speculatable | ||
| declare float @air.pow.f32(float, float) | ||
|
|
||
| ; Function Attrs: nounwind | ||
| declare float @__enzyme_autodiff(float (float, float)*, ...) | ||
|
|
||
| ; CHECK: define internal {{(dso_local )?}}{ float, float } @diffetester(float %x, float %y, float %differeturn) | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-NEXT: %[[ym1:.+]] = fsub fast float %y, 1.000000e+00 | ||
| ; CHECK-NEXT: %[[newpow:.+]] = call fast float @air.pow.f32(float %x, float %[[ym1]]) | ||
| ; CHECK-NEXT: %[[newpowdret:.+]] = fmul fast float %y, %[[newpow]] | ||
| ; CHECK-NEXT: %[[dx:.+]] = fmul fast float %differeturn, %[[newpowdret]] | ||
| ; CHECK-NEXT: %[[isxzero:.+]] = fcmp fast oeq float %x, 0.000000e+00 | ||
| ; CHECK-NEXT: %[[origpow:.+]] = call fast float @air.pow.f32(float %x, float %y) | ||
| ; CHECK-NEXT: %[[logy:.+]] = call fast float @llvm.log.f32(float %x) | ||
| ; CHECK-NEXT: %[[origpowdret:.+]] = fmul fast float %[[origpow]], %[[logy]] | ||
| ; CHECK-NEXT: %[[guardeddy:.+]] = select fast i1 %[[isxzero]], float 0.000000e+00, float %[[origpowdret]] | ||
| ; CHECK-NEXT: %[[dy:.+]] = fmul fast float %differeturn, %[[guardeddy]] | ||
| ; CHECK-NEXT: %[[interres:.+]] = insertvalue { float, float } undef, float %[[dx:.+]], 0 | ||
| ; CHECK-NEXT: %[[finalres:.+]] = insertvalue { float, float } %[[interres]], float %[[dy:.+]], 1 | ||
| ; CHECK-NEXT: ret { float, float } %[[finalres]] | ||
| ; CHECK-NEXT: } |
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,33 @@ | ||
| ; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -enzyme-preopt=false -preserve-nvvm -enzyme -mem2reg -instsimplify -simplifycfg -S | FileCheck %s; fi | ||
| ; RUN: %opt < %s %newLoadEnzyme -enzyme-preopt=false -passes="preserve-nvvm,enzyme,function(mem2reg,instsimplify,%simplifycfg)" -S | FileCheck %s | ||
|
|
||
| ; Metal AIR math intrinsic (air.sin.f32) resolved via isMemFreeLibMFunction's | ||
| ; air. prefix stripping in TypeAnalysis.h to Intrinsic::sin, so its | ||
| ; derivative reuses the same rule as llvm.sin.f32 (see sin.ll). | ||
|
|
||
| ; Function Attrs: nounwind readnone uwtable | ||
| define float @tester(float %x) { | ||
| entry: | ||
| %0 = tail call fast float @air.sin.f32(float %x) | ||
| ret float %0 | ||
| } | ||
|
|
||
| define float @test_derivative(float %x) { | ||
| entry: | ||
| %0 = tail call float (float (float)*, ...) @__enzyme_autodiff(float (float)* nonnull @tester, float %x) | ||
| ret float %0 | ||
| } | ||
|
|
||
| ; Function Attrs: nounwind readnone speculatable | ||
| declare float @air.sin.f32(float) | ||
|
|
||
| ; Function Attrs: nounwind | ||
| declare float @__enzyme_autodiff(float (float)*, ...) | ||
|
|
||
| ; CHECK: define internal { float } @diffetester(float %x, float %differeturn) | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-NEXT: %0 = call fast float @llvm.cos.f32(float %x) | ||
| ; CHECK-NEXT: %1 = fmul fast float %differeturn, %0 | ||
| ; CHECK-NEXT: %2 = insertvalue { float } undef, float %1, 0 | ||
| ; CHECK-NEXT: ret { float } %2 | ||
| ; CHECK-NEXT: } |
Oops, something went wrong.
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.
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.
You are missing
air.atan2.f32.here?