Skip to content

Commit 8597ee7

Browse files
authored
Merge pull request #206 from jpsamaroo/jps/conststuff
Added methods for ConstantArray
2 parents fd6ab4e + fb1dc03 commit 8597ee7

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

src/core/value/constant.jl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ end
6262

6363
# NOTE: fixed set where sizeof(T) does match the numerical width
6464
const SizeableInteger = Union{Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128}
65-
function ConstantInt(val::T, ctx::Context=GlobalContext()) where T<:SizeableInteger
65+
function ConstantInt(val::T, ctx::Context) where T<:SizeableInteger
6666
typ = IntType(sizeof(T)*8, ctx)
6767
return ConstantInt(typ, val, T<:Signed)
6868
end
@@ -79,11 +79,18 @@ Base.convert(::Type{T}, val::ConstantInt) where {T<:Signed} =
7979
end
8080
identify(::Type{Value}, ::Val{API.LLVMConstantFPValueKind}) = ConstantFP
8181

82-
ConstantFP(typ::LLVMDouble, val::Real) =
82+
ConstantFP(typ::FloatingPointType, val::Real) =
8383
ConstantFP(API.LLVMConstReal(typ, Cdouble(val)))
8484

85-
Base.convert(::Type{Float64}, val::ConstantFP) =
86-
API.LLVMConstRealGetDouble(val, Ref{API.LLVMBool}())
85+
ConstantFP(val::Float16, ctx::Context) =
86+
ConstantFP(HalfType(ctx), val)
87+
ConstantFP(val::Float32, ctx::Context) =
88+
ConstantFP(FloatType(ctx), val)
89+
ConstantFP(val::Float64, ctx::Context) =
90+
ConstantFP(DoubleType(ctx), val)
91+
92+
Base.convert(::Type{T}, val::ConstantFP) where {T<:AbstractFloat} =
93+
convert(T, API.LLVMConstRealGetDouble(val, Ref{API.LLVMBool}()))
8794

8895

8996
## aggregate
@@ -114,6 +121,23 @@ abstract type ConstantAggregate <: Constant end
114121
ref::API.LLVMValueRef
115122
end
116123
identify(::Type{Value}, ::Val{API.LLVMConstantArrayValueKind}) = ConstantArray
124+
identify(::Type{Value}, ::Val{API.LLVMConstantDataArrayValueKind}) = ConstantArray
125+
126+
ConstantArray(typ::LLVMType, data::Vector{T}) where {T<:Constant} =
127+
ConstantArray(API.LLVMConstArray(typ, data, length(data)))
128+
ConstantArray(typ::IntegerType, data::Vector{T}) where {T<:Integer} =
129+
ConstantArray(typ, map(x->ConstantInt(convert(T,x),context(typ)), data))
130+
ConstantArray(typ::FloatingPointType, data::Vector{T}) where {T<:AbstractFloat} =
131+
ConstantArray(typ, map(x->ConstantFP(convert(T,x),context(typ)), data))
132+
133+
Base.getindex(ca::ConstantArray, idx::Integer) =
134+
API.LLVMGetElementAsConstant(ca, idx-1)
135+
Base.length(ca::ConstantArray) = length(llvmtype(ca))
136+
Base.eltype(ca::ConstantArray) = eltype(llvmtype(ca))
137+
Base.convert(::Type{Array{T,1}}, ca::ConstantArray) where {T<:Integer} =
138+
[convert(T,ConstantInt(ca[i])) for i in 1:length(ca)]
139+
Base.convert(::Type{Array{T,1}}, ca::ConstantArray) where {T<:AbstractFloat} =
140+
[convert(T,ConstantFP(ca[i])) for i in 1:length(ca)]
117141

118142
@checked struct ConstantStruct <: ConstantAggregate
119143
ref::API.LLVMValueRef

test/core.jl

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,13 @@ Context() do ctx
338338

339339
# automatic construction
340340
let
341-
constval = ConstantInt(UInt32(1))
341+
constval = ConstantInt(UInt32(1), ctx)
342342
@test convert(UInt, constval) == 1
343343
end
344344

345345
# issue #81
346346
for T in [Int32, UInt32, Int64, UInt64]
347-
constval = ConstantInt(typemax(T))
347+
constval = ConstantInt(typemax(T), ctx)
348348
@test convert(T, constval) == typemax(T)
349349
end
350350

@@ -353,9 +353,45 @@ Context() do ctx
353353

354354
@testset "floating point constants" begin
355355

356-
t2 = LLVM.DoubleType(ctx)
357-
c = ConstantFP(t2, 1.1)
358-
@test convert(Float64, c) == 1.1
356+
let
357+
typ = LLVM.HalfType(ctx)
358+
c = ConstantFP(typ, Float16(1.1f0))
359+
@test convert(Float16, c) == Float16(1.1f0)
360+
end
361+
let
362+
typ = LLVM.FloatType(ctx)
363+
c = ConstantFP(typ, 1.1f0)
364+
@test convert(Float32, c) == 1.1f0
365+
end
366+
let
367+
typ = LLVM.DoubleType(ctx)
368+
c = ConstantFP(typ, 1.1)
369+
@test convert(Float64, c) == 1.1
370+
end
371+
for T in [Float16, Float32, Float64]
372+
c = ConstantFP(typemax(T), ctx)
373+
@test convert(T, c) == typemax(T)
374+
end
375+
376+
end
377+
378+
379+
@testset "array constants" begin
380+
381+
let
382+
typ = LLVM.Int32Type(ctx)
383+
vec = Int32[1,2,3,4]
384+
ca = ConstantArray(typ, vec)
385+
@test convert(Int32, ConstantInt(ca[1]))::Int32 == Int32(1)
386+
@test convert(Vector{Int32}, ca) == vec
387+
end
388+
let
389+
typ = LLVM.FloatType(ctx)
390+
vec = Float32[1.1f0,2.2f0,3.3f0,4.4f0]
391+
ca = ConstantArray(typ, vec)
392+
@test convert(Float32, ConstantFP(ca[1]))::Float32 == 1.1f0
393+
@test convert(Vector{Float32}, ca) == vec
394+
end
359395

360396
end
361397
end

0 commit comments

Comments
 (0)