Skip to content
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

Support for BFloat16s. #368

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
LLVMExtra_jll = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[weakdeps]
BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b"

[extensions]
BFloat16sExt = "BFloat16s"

[compat]
CEnum = "0.2, 0.3, 0.4"
LLVMExtra_jll = "=0.0.26"
julia = "1.8"

[extras]
BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b"
18 changes: 18 additions & 0 deletions ext/BFloat16sExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module BFloat16sExt

using LLVM
using LLVM: API

isdefined(Base, :get_extension) ? (using BFloat16s) : (using ..BFloat16s)

## constant values

LLVM.ConstantFP(val::BFloat16) = ConstantFP(BFloatType(), val)

Base.convert(::Type{BFloat16}, val::ConstantFP) =
convert(BFloat16, API.LLVMConstRealGetDouble(val, Ref{API.LLVMBool}()))

ConstantDataArray(data::AbstractVector{BFloat16}) =
ConstantDataArray(BFloatType(), data)

end
10 changes: 10 additions & 0 deletions src/LLVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ using Unicode
using Printf
using Libdl

if !isdefined(Base, :get_extension)
using Requires: @require
end


## source code includes

Expand Down Expand Up @@ -106,6 +110,12 @@ function __init__()
Please re-compile Julia and LLVM.jl (but note that USE_SYSTEM_LLVM is not a supported configuration)."""
end

@static if !isdefined(Base, :get_extension)
@require BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b" begin
include("../ext/BFloat16sExt.jl")
end
end

_install_handlers()
_install_handlers(GlobalContext())
end
Expand Down
2 changes: 1 addition & 1 deletion src/core/type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ width(inttyp::IntegerType) = API.LLVMGetIntTypeWidth(inttyp)
# we add it for convenience of typechecking generic values (see execution.jl)
abstract type FloatingPointType <: LLVMType end

for T in [:Half, :Float, :Double, :FP128, :X86_FP80, :PPC_FP128]
for T in [:Half, :Float, :Double, :BFloat, :FP128, :X86_FP80, :PPC_FP128]
CleanT = Symbol(replace(String(T), "_"=>"")) # only the type kind retains the underscore
jl_fname = Symbol(CleanT, :Type)
api_typename = Symbol(:LLVM, CleanT)
Expand Down
17 changes: 7 additions & 10 deletions src/core/value/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,9 @@ register(ConstantFP, API.LLVMConstantFPValueKind)
ConstantFP(typ::FloatingPointType, val::Real) =
ConstantFP(API.LLVMConstReal(typ, Cdouble(val)))

ConstantFP(val::Float16) =
ConstantFP(HalfType(), val)
ConstantFP(val::Float32) =
ConstantFP(FloatType(), val)
ConstantFP(val::Float64) =
ConstantFP(DoubleType(), val)
ConstantFP(val::Float64) = ConstantFP(DoubleType(), val)
ConstantFP(val::Float32) = ConstantFP(FloatType(), val)
ConstantFP(val::Float16) = ConstantFP(HalfType(), val)

Base.convert(::Type{T}, val::ConstantFP) where {T<:AbstractFloat} =
convert(T, API.LLVMConstRealGetDouble(val, Ref{API.LLVMBool}()))
Expand Down Expand Up @@ -166,12 +163,12 @@ ConstantDataArray(data::AbstractVector{T}) where {T<:Integer} =
ConstantDataArray(IntType(sizeof(T)*8), data)
ConstantDataArray(data::AbstractVector{Core.Bool}) =
ConstantDataArray(Int1Type(), data)
ConstantDataArray(data::AbstractVector{Float16}) =
ConstantDataArray(HalfType(), data)
ConstantDataArray(data::AbstractVector{Float32}) =
ConstantDataArray(FloatType(), data)
ConstantDataArray(data::AbstractVector{Float64}) =
ConstantDataArray(DoubleType(), data)
ConstantDataArray(data::AbstractVector{Float32}) =
ConstantDataArray(FloatType(), data)
ConstantDataArray(data::AbstractVector{Float16}) =
ConstantDataArray(HalfType(), data)

@checked struct ConstantDataVector <: ConstantDataSequential
ref::API.LLVMValueRef
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LLVMExtra_jll = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"
Expand Down
7 changes: 7 additions & 0 deletions test/core_tests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@testitem "core" setup=[TestHelpers] begin

using BFloat16s

struct TestStruct
x::Bool
y::Int64
Expand Down Expand Up @@ -410,6 +412,11 @@ end
c = ConstantFP(typ, 1.1)
@test convert(Float64, c) == 1.1
end
let
typ = LLVM.BFloatType()
c = ConstantFP(typ, BFloat16(1.1))
@test convert(BFloat16, c) == BFloat16(1.1)
end
let
typ = LLVM.X86FP80Type()
# TODO: how to construct full-width constants?
Expand Down
Loading