-
Notifications
You must be signed in to change notification settings - Fork 274
Reduce usage of libdevice, relying more on LLVM
#3149
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
+378
−275
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0527507
Drop fdiv/sqrt/inv device overrides handled by GPUCompiler.
maleadt 5f69f39
Drop more device overrides handled by Julia + NVPTX directly.
maleadt c9391fc
Test math intrinsics lower without libdevice.
maleadt 30a9b7e
Simplify.
maleadt f518896
Improve tests.
maleadt 7ab5b8c
Use FileCheck more widely.
maleadt bc47367
Fix CI.
maleadt 0b0dc66
Address review comments.
maleadt dffd57f
Simplify rsqrt, fix min/max PTX test for sm<80.
maleadt 986ca42
Define rsqrt via direct NVPTX intrinsic, not @fastmath.
maleadt 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| # math functionality | ||
|
|
||
| # we only use libdevice where needed. if possible, we go through LLVM instead, | ||
| # ideally relying on Julia's existing definitions. | ||
|
|
||
| @public fma, rsqrt, saturate, byte_perm, assume | ||
| @public add_rn, add_rz, add_rm, add_rp | ||
| @public sub_rn, sub_rz, sub_rm, sub_rp | ||
|
|
@@ -286,46 +289,20 @@ end | |
|
|
||
| ## floating-point handling | ||
|
|
||
| @device_override Base.isfinite(x::Float32) = (ccall("extern __nv_finitef", llvmcall, Int32, (Cfloat,), x)) != 0 | ||
| @device_override Base.isfinite(x::Float64) = (ccall("extern __nv_isfinited", llvmcall, Int32, (Cdouble,), x)) != 0 | ||
|
|
||
| @device_override Base.isinf(x::Float64) = (ccall("extern __nv_isinfd", llvmcall, Int32, (Cdouble,), x)) != 0 | ||
| @device_override Base.isinf(x::Float32) = (ccall("extern __nv_isinff", llvmcall, Int32, (Cfloat,), x)) != 0 | ||
|
|
||
| @device_override Base.isnan(x::Float64) = (ccall("extern __nv_isnand", llvmcall, Int32, (Cdouble,), x)) != 0 | ||
| @device_override Base.isnan(x::Float32) = (ccall("extern __nv_isnanf", llvmcall, Int32, (Cfloat,), x)) != 0 | ||
| # isnan(::Float16) inherits from Julia (x != x), which compiles to a single setp.neu.f16. | ||
|
|
||
| @device_function nearbyint(x::Float64) = ccall("extern __nv_nearbyint", llvmcall, Cdouble, (Cdouble,), x) | ||
| @device_function nearbyint(x::Float32) = ccall("extern __nv_nearbyintf", llvmcall, Cfloat, (Cfloat,), x) | ||
|
|
||
| @device_function nextafter(x::Float64, y::Float64) = ccall("extern __nv_nextafter", llvmcall, Cdouble, (Cdouble, Cdouble), x, y) | ||
| @device_function nextafter(x::Float32, y::Float32) = ccall("extern __nv_nextafterf", llvmcall, Cfloat, (Cfloat, Cfloat), x, y) | ||
|
|
||
|
|
||
| ## sign handling | ||
|
|
||
| @device_override Base.signbit(x::Float64) = (ccall("extern __nv_signbitd", llvmcall, Int32, (Cdouble,), x)) != 0 | ||
| @device_override Base.signbit(x::Float32) = (ccall("extern __nv_signbitf", llvmcall, Int32, (Cfloat,), x)) != 0 | ||
|
|
||
| @device_override Base.copysign(x::Float64, y::Float64) = ccall("extern __nv_copysign", llvmcall, Cdouble, (Cdouble, Cdouble), x, y) | ||
| @device_override Base.copysign(x::Float32, y::Float32) = ccall("extern __nv_copysignf", llvmcall, Cfloat, (Cfloat, Cfloat), x, y) | ||
|
|
||
| @device_override Base.abs(x::Int32) = ccall("extern __nv_abs", llvmcall, Int32, (Int32,), x) | ||
| @device_override Base.abs(f::Float64) = ccall("extern __nv_fabs", llvmcall, Cdouble, (Cdouble,), f) | ||
| @device_override Base.abs(f::Float32) = ccall("extern __nv_fabsf", llvmcall, Cfloat, (Cfloat,), f) | ||
| # abs(::Float16) inherits from Julia (abs_float intrinsic), lowering to and.b16. | ||
| @device_override Base.abs(x::Int64) = ccall("extern __nv_llabs", llvmcall, Int64, (Int64,), x) | ||
|
|
||
| ## roots and powers | ||
|
|
||
| @device_override Base.sqrt(x::Float64) = ccall("extern __nv_sqrt", llvmcall, Cdouble, (Cdouble,), x) | ||
| @device_override Base.sqrt(x::Float32) = ccall("extern __nv_sqrtf", llvmcall, Cfloat, (Cfloat,), x) | ||
| # sqrt(::Float16) inherits from Julia (Float16(sqrt(Float32(x)))), routing through __nv_sqrtf. | ||
| @device_override FastMath.sqrt_fast(x::Union{Float32, Float64}) = sqrt(x) | ||
|
|
||
| @device_function rsqrt(x::Float64) = ccall("extern __nv_rsqrt", llvmcall, Cdouble, (Cdouble,), x) | ||
| @device_function rsqrt(x::Float32) = ccall("extern __nv_rsqrtf", llvmcall, Cfloat, (Cfloat,), x) | ||
| # NVPTX has native `rsqrt.approx.{f32,f64}`; call the intrinsic directly. The | ||
| # obvious alternative, `@fastmath 1/sqrt(x)`, also lowers to `rsqrt.approx` | ||
| # (via `PTXRSqrtFastPass`), but is too aggressive wrt. fast-math behavior. | ||
| @device_function rsqrt(x::Float64) = ccall("llvm.nvvm.rsqrt.approx.d", llvmcall, Cdouble, (Cdouble,), x) | ||
| @device_function rsqrt(x::Float32) = ccall("llvm.nvvm.rsqrt.approx.f", llvmcall, Cfloat, (Cfloat,), x) | ||
| @device_function rsqrt(x::Float16) = Float16(rsqrt(Float32(x))) | ||
|
|
||
| @device_override Base.cbrt(x::Float64) = ccall("extern __nv_cbrt", llvmcall, Cdouble, (Cdouble,), x) | ||
|
|
@@ -395,15 +372,6 @@ end | |
| #@device_override Base.rint(x::Float64) = ccall("extern __nv_rint", llvmcall, Cdouble, (Cdouble,), x) | ||
| #@device_override Base.rint(x::Float32) = ccall("extern __nv_rintf", llvmcall, Cfloat, (Cfloat,), x) | ||
|
|
||
| @device_override Base.trunc(x::Float64) = ccall("extern __nv_trunc", llvmcall, Cdouble, (Cdouble,), x) | ||
| @device_override Base.trunc(x::Float32) = ccall("extern __nv_truncf", llvmcall, Cfloat, (Cfloat,), x) | ||
|
|
||
| @device_override Base.ceil(x::Float64) = ccall("extern __nv_ceil", llvmcall, Cdouble, (Cdouble,), x) | ||
| @device_override Base.ceil(x::Float32) = ccall("extern __nv_ceilf", llvmcall, Cfloat, (Cfloat,), x) | ||
|
|
||
| @device_override Base.floor(f::Float64) = ccall("extern __nv_floor", llvmcall, Cdouble, (Cdouble,), f) | ||
| @device_override Base.floor(f::Float32) = ccall("extern __nv_floorf", llvmcall, Cfloat, (Cfloat,), f) | ||
|
|
||
| #@device_override Base.min(x::Int32, y::Int32) = ccall("extern __nv_min", llvmcall, Int32, (Int32, Int32), x, y) | ||
| #@device_override Base.min(x::Int64, y::Int64) = ccall("extern __nv_llmin", llvmcall, Int64, (Int64, Int64), x, y) | ||
| #@device_override Base.min(x::UInt32, y::UInt32) = convert(UInt32, ccall("extern __nv_umin", llvmcall, Int32, (Int32, Int32), x, y)) | ||
|
|
@@ -508,27 +476,11 @@ end | |
| @device_override Base.rem(x::Float32, y::Float32, ::RoundingMode{:Nearest}) = ccall("extern __nv_remainderf", llvmcall, Cfloat, (Cfloat, Cfloat), x, y) | ||
| @device_override Base.rem(x::Float16, y::Float16, ::RoundingMode{:Nearest}) = Float16(rem(Float32(x), Float32(y), RoundNearest)) | ||
|
|
||
| @device_override FastMath.div_fast(x::Float32, y::Float32) = ccall("extern __nv_fast_fdividef", llvmcall, Cfloat, (Cfloat, Cfloat), x, y) | ||
| @device_override FastMath.div_fast(x::Float64, y::Float64) = x * FastMath.inv_fast(y) | ||
|
|
||
| @device_override Base.inv(x::Float32) = ccall("extern __nv_frcp_rn", llvmcall, Cfloat, (Cfloat,), x) | ||
| @device_override Base.inv(x::Float64) = ccall("extern __nv_drcp_rn", llvmcall, Cdouble, (Cdouble,), x) | ||
|
|
||
| @device_override FastMath.inv_fast(x::Float32) = ccall("llvm.nvvm.rcp.approx.ftz.f", llvmcall, Float32, (Float32,), x) | ||
| @device_override function FastMath.inv_fast(x::Float64) | ||
| # Get the approximate reciprocal | ||
| # https://docs.nvidia.com/cuda/parallel-thread-execution/#floating-point-instructions-rcp-approx-ftz-f64 | ||
| # This instruction chops off last 32bits of mantissa and computes inverse | ||
| # while treating all subnormal numbers as 0.0 | ||
| # If reciprocal would be subnormal, underflows to 0.0 | ||
| # 32 least significant bits of the result are filled with 0s | ||
| inv_x = ccall("llvm.nvvm.rcp.approx.ftz.d", llvmcall, Float64, (Float64,), x) | ||
|
|
||
| # Approximate the missing 32bits of mantissa with a single cubic iteration | ||
| e = fma(inv_x, -x, 1.0) | ||
| e = fma(e, e, e) | ||
| inv_x = fma(e, inv_x, inv_x) | ||
| end | ||
| # `Base.FastMath.inv_fast(::AbstractFloat)` is unimplemented upstream (only | ||
| # `Complex` has a method) and the catch-all fallback drops `afn` | ||
| @device_override FastMath.inv_fast(x::Union{Float16, Float32, Float64}) = | ||
| FastMath.div_fast(one(x), x) | ||
|
|
||
|
|
||
| ## distributions | ||
|
|
||
|
|
@@ -549,13 +501,20 @@ end | |
| @device_override Base.hypot(x::Float64, y::Float64) = ccall("extern __nv_hypot", llvmcall, Cdouble, (Cdouble, Cdouble), x, y) | ||
| @device_override Base.hypot(x::Float32, y::Float32) = ccall("extern __nv_hypotf", llvmcall, Cfloat, (Cfloat, Cfloat), x, y) | ||
|
|
||
| @device_override Base.fma(x::Float64, y::Float64, z::Float64) = ccall("llvm.fma.f64", llvmcall, Cdouble, (Cdouble, Cdouble, Cdouble), x, y, z) | ||
| @device_override Base.fma(x::Float32, y::Float32, z::Float32) = ccall("llvm.fma.f32", llvmcall, Cfloat, (Cfloat, Cfloat, Cfloat), x, y, z) | ||
| @device_override Base.fma(x::Float16, y::Float16, z::Float16) = ccall("llvm.fma.f16", llvmcall, Float16, (Float16, Float16, Float16), x, y, z) | ||
|
|
||
| @device_override Base.muladd(x::Float64, y::Float64, z::Float64) = ccall("llvm.fmuladd.f64", llvmcall, Cdouble, (Cdouble, Cdouble, Cdouble), x, y, z) | ||
| @device_override Base.muladd(x::Float32, y::Float32, z::Float32) = ccall("llvm.fmuladd.f32", llvmcall, Cfloat, (Cfloat, Cfloat, Cfloat), x, y, z) | ||
| @device_override Base.muladd(x::Float16, y::Float16, z::Float16) = ccall("llvm.fmuladd.f16", llvmcall, Float16, (Float16, Float16, Float16), x, y, z) | ||
|
maleadt marked this conversation as resolved.
|
||
| # `Base.fma(::Float16,...)` branches on `jl_have_fma` | ||
| @device_override Base.fma(x::Float16, y::Float16, z::Float16) = | ||
| ccall("llvm.fma.f16", llvmcall, Float16, (Float16, Float16, Float16), x, y, z) | ||
|
|
||
| # `Base.muladd(x, y, z) = fma(x, y, z)` is the natural choice on GPU: NVPTX | ||
| # always lowers `llvm.fmuladd.fXX` to `fma.rn`, and routing through | ||
| # `llvm.fmuladd` (rather than Julia's default `fmul contract + fadd contract`) | ||
| # keeps the fusion robust under vectorization (per JuliaGPU/CUDA.jl#3149). | ||
| @device_override Base.muladd(x::Float64, y::Float64, z::Float64) = | ||
| ccall("llvm.fmuladd.f64", llvmcall, Cdouble, (Cdouble, Cdouble, Cdouble), x, y, z) | ||
|
Member
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. We should really do this upstream as well. Our mulladd could technically leak in the way we emit it. |
||
| @device_override Base.muladd(x::Float32, y::Float32, z::Float32) = | ||
| ccall("llvm.fmuladd.f32", llvmcall, Cfloat, (Cfloat, Cfloat, Cfloat), x, y, z) | ||
| @device_override Base.muladd(x::Float16, y::Float16, z::Float16) = | ||
| ccall("llvm.fmuladd.f16", llvmcall, Float16, (Float16, Float16, Float16), x, y, z) | ||
|
|
||
| # Directed rounding for binary arithmetic and fma. NVPTX exposes | ||
| # `{add,mul,div,fma}.{rn,rz,rm,rp}.{f32,f64}` directly; there is no `sub` | ||
|
|
||
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.