Skip to content

Commit

Permalink
Revert "Merge pull request #329 from maleadt/tb/sdk_version"
Browse files Browse the repository at this point in the history
This reverts commit 00e61c8, reversing
changes made to 363a66f.
  • Loading branch information
maleadt committed Mar 21, 2023
1 parent 17d2732 commit e3bbfdb
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 179 deletions.
3 changes: 0 additions & 3 deletions deps/LLVMExtra/include/LLVMExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,5 @@ void LLVMReplaceMDNodeOperandWith(LLVMMetadataRef MD, unsigned I, LLVMMetadataRe
LLVMBool LLVMContextSupportsTypedPointers(LLVMContextRef C);
#endif

// constant data
LLVMValueRef LLVMConstDataArray(LLVMTypeRef ElementTy, const void *Data, unsigned NumElements);

LLVM_C_EXTERN_C_END
#endif
5 changes: 0 additions & 5 deletions deps/LLVMExtra/lib/llvm-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,3 @@ LLVMBool LLVMContextSupportsTypedPointers(LLVMContextRef C) {
return unwrap(C)->supportsTypedPointers();
}
#endif

LLVMValueRef LLVMConstDataArray(LLVMTypeRef ElementTy, const void *Data, unsigned NumElements) {
StringRef S((const char *)Data, NumElements * unwrap(ElementTy)->getPrimitiveSizeInBits() / 8);
return wrap(ConstantDataArray::getRaw(S, NumElements, unwrap(ElementTy)));
}
1 change: 0 additions & 1 deletion deps/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ CMake_jll = "3f4e10e2-61f2-5801-8945-23b9d642d0e6"
Git = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2"
LLVMExtra_jll = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Ninja_jll = "76642167-d241-5cee-8c94-7a494e8cb7b7"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
46 changes: 18 additions & 28 deletions deps/build_local.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,57 @@ if haskey(ENV, "GITHUB_ACTIONS")
println("::warning ::Using a locally-built LLVMExtra; A bump of LLVMExtra_jll will be required before releasing LLVM.jl.")
end

using Pkg, Scratch, Preferences, Libdl, CMake_jll, Ninja_jll
using Pkg, Scratch, Preferences, Libdl, CMake_jll

LLVM = Base.UUID("929cbde3-209d-540e-8aea-75f648917ca0")

# get scratch directories
scratch_dir = get_scratch!(LLVM, "build")
isdir(scratch_dir) && rm(scratch_dir; recursive=true)
source_dir = joinpath(@__DIR__, "LLVMExtra")

# get build directory
build_dir = if isempty(ARGS)
mktempdir()
else
ARGS[1]
end
mkpath(build_dir)

# download LLVM
# 1. Ensure that an appropriate LLVM_full_jll is installed
Pkg.activate(; temp=true)
llvm_assertions = try
cglobal((:_ZN4llvm24DisableABIBreakingChecksE, Base.libllvm_path()), Cvoid)
false
catch
true
end
llvm_pkg_version = "$(Base.libllvm_version.major).$(Base.libllvm_version.minor)"
LLVM = if llvm_assertions
Pkg.add(name="LLVM_full_assert_jll", version=llvm_pkg_version)
Pkg.add(name="LLVM_full_assert_jll", version=Base.libllvm_version)
using LLVM_full_assert_jll
LLVM_full_assert_jll
else
Pkg.add(name="LLVM_full_jll", version=llvm_pkg_version)
Pkg.add(name="LLVM_full_jll", version=Base.libllvm_version)
using LLVM_full_jll
LLVM_full_jll
end
LLVM_DIR = joinpath(LLVM.artifact_dir, "lib", "cmake", "llvm")

# build and install
@info "Building" source_dir scratch_dir build_dir LLVM_DIR
cmake() do cmake_path
ninja() do ninja_path
run(`$cmake_path -GNinja -DLLVM_DIR=$(LLVM_DIR) -DCMAKE_INSTALL_PREFIX=$(scratch_dir) -B$(build_dir) -S$(source_dir)`)
run(`$ninja_path -C $(build_dir) install`)
end
# 2. Get a scratch directory
scratch_dir = get_scratch!(LLVM, "build")
isdir(scratch_dir) && rm(scratch_dir; recursive=true)
source_dir = joinpath(@__DIR__, "LLVMExtra")

# Build!
mktempdir() do build_dir
@info "Building" source_dir scratch_dir build_dir LLVM_DIR
run(`$(cmake()) -DLLVM_DIR=$(LLVM_DIR) -DCMAKE_INSTALL_PREFIX=$(scratch_dir) -B$(build_dir) -S$(source_dir)`)
run(`$(cmake()) --build $(build_dir)`)
run(`$(cmake()) --install $(build_dir)`)
end

# discover built libraries
# Discover built libraries
built_libs = filter(readdir(joinpath(scratch_dir, "lib"))) do file
endswith(file, ".$(Libdl.dlext)")
end
lib_path = joinpath(scratch_dir, "lib", only(built_libs))
isfile(lib_path) || error("Could not find library $lib_path in build directory")

# tell LLVMExtra_jll to load our library instead of the default artifact one
# Tell LLVMExtra_jll to load our library instead of the default artifact one
set_preferences!(
joinpath(dirname(@__DIR__), "LocalPreferences.toml"),
"LLVMExtra_jll",
"libLLVMExtra_path" => lib_path;
force=true,
)

# copy the preferences to `test/` as well to work around Pkg.jl#2500
# Copy the preferences to `test/` as well to work around Pkg.jl#2500
cp(joinpath(dirname(@__DIR__), "LocalPreferences.toml"),
joinpath(dirname(@__DIR__), "test", "LocalPreferences.toml"); force=true)
4 changes: 0 additions & 4 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,3 @@ function LLVMContextSupportsTypedPointers(Ctx)
ccall((:LLVMContextSupportsTypedPointers, libLLVMExtra), LLVMBool, (LLVMContextRef,), Ctx)
end
end

function LLVMConstDataArray(ElementTy, Data, NumElements)
ccall((:LLVMConstDataArray, libLLVMExtra), LLVMValueRef, (LLVMTypeRef, Ptr{Cvoid}, Cuint), ElementTy, Data, NumElements)
end
27 changes: 0 additions & 27 deletions src/core/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,30 +267,3 @@ function Base.setindex!(iter::ModuleFlagDict, val::Metadata,
(name, behavior)::Tuple{String, API.LLVMModuleFlagBehavior})
API.LLVMAddModuleFlag(iter.mod, behavior, name, length(name), val)
end


## sdk version

export sdk_version, sdk_version!

function sdk_version!(mod::Module, version::VersionNumber)
entries = Int32[version.major]
if version.minor != 0 || version.patch != 0
push!(entries, version.minor)
if version.patch != 0
push!(entries, version.patch)
end
# cannot represent prerelease or build metadata
end
md = Metadata(ConstantDataArray(entries; ctx=context(mod)))

flags(mod)["SDK Version", LLVM.API.LLVMModuleFlagBehaviorWarning] = md
end

function sdk_version(mod::Module)
haskey(flags(mod), "SDK Version") || return nothing
md = flags(mod)["SDK Version"]
c = Value(md; ctx=context(mod))
entries = collect(c)
VersionNumber(map(val->convert(Int, val), entries)...)
end
2 changes: 1 addition & 1 deletion src/core/type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function ArrayType(eltyp::LLVMType, count)
return ArrayType(API.LLVMArrayType(eltyp, count))
end

Base.length(arrtyp::ArrayType) = Int(API.LLVMGetArrayLength(arrtyp))
Base.length(arrtyp::ArrayType) = API.LLVMGetArrayLength(arrtyp)

Base.isempty(@nospecialize(T::ArrayType)) = length(T) == 0 || isempty(eltype(T))

Expand Down
72 changes: 11 additions & 61 deletions src/core/value/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,67 +112,17 @@ Base.convert(::Type{T}, val::ConstantFP) where {T<:AbstractFloat} =
convert(T, API.LLVMConstRealGetDouble(val, Ref{API.LLVMBool}()))


# sequential data
# sequential

export ConstantDataSequential, ConstantDataArray, ConstantDataVector

abstract type ConstantDataSequential <: Constant end

# ConstantData can only contain primitive types (1/2/4/8 byte integers, float/half),
# as opposed to ConstantAggregate which can contain arbitrary LLVM values.
#
# however, LLVM seems to use both array types interchangeably, e.g., constructing
# a ConstArray through LLVMConstArray may return a ConstantDataArray (presumably as an
# optimization, when the data can be represented as densely packed primitive values).
# because of that, ConstantDataArray and ConstantArray need to behave the same way,
# concretely, indexing a ConstantDataArray has to return LLVM constant values...
#
# XXX: maybe we should just not expose ConstantDataArray then?
# one advantage of keeping them separate is that creating a ConstantDataArray
# is much cheaper (we should also be able to iterate much more efficiently,
# but cannot support that as explained above).

# array interface
Base.eltype(cda::ConstantDataSequential) = llvmeltype(cda)
Base.length(cda::ConstantDataSequential) = length(llvmtype(cda))
Base.size(cda::ConstantDataSequential) = (length(cda),)
function Base.getindex(cda::ConstantDataSequential, idx::Integer)
@boundscheck 1 <= idx <= length(cda) || throw(BoundsError(cda, idx))
Value(API.LLVMGetElementAsConstant(cda, idx-1))
end
function Base.collect(cda::ConstantDataSequential)
constants = Array{Value}(undef, length(cda))
for i in 1:length(cda)
@inbounds constants[i] = cda[i]
end
return constants
end

@checked struct ConstantDataArray <: ConstantDataSequential
ref::API.LLVMValueRef
end
register(ConstantDataArray, API.LLVMConstantDataArrayValueKind)

function ConstantDataArray(typ::LLVMType, data::Array{T}) where {T <: Union{Integer, AbstractFloat}}
# TODO: can we look up the primitive size of the LLVM type?
# use that to assert it matches the Julia element type.
return ConstantDataArray(API.LLVMConstDataArray(typ, data, length(data)))
end

# shorthands with arrays of plain Julia data
# FIXME: duplicates the ConstantInt/ConstantFP conversion rules
# XXX: X[X(...)] instead of X.(...) because of empty-container inference
ConstantDataArray(data::AbstractVector{T}; ctx::Context) where {T<:Integer} =
ConstantDataArray(IntType(sizeof(T)*8; ctx), data)
ConstantDataArray(data::AbstractVector{Core.Bool}; ctx::Context) =
ConstantDataArray(Int1Type(ctx), data)
ConstantDataArray(data::AbstractVector{Float16}; ctx::Context) =
ConstantDataArray(HalfType(ctx), data)
ConstantDataArray(data::AbstractVector{Float32}; ctx::Context) =
ConstantDataArray(FloatType(ctx), data)
ConstantDataArray(data::AbstractVector{Float64}; ctx::Context) =
ConstantDataArray(DoubleType(ctx), data)

@checked struct ConstantDataVector <: ConstantDataSequential
ref::API.LLVMValueRef
end
Expand Down Expand Up @@ -212,14 +162,16 @@ export ConstantArray
ref::API.LLVMValueRef
end
register(ConstantArray, API.LLVMConstantArrayValueKind)
register(ConstantArray, API.LLVMConstantDataArrayValueKind)

ConstantArrayOrAggregateZero(value) = Value(value)::Union{ConstantArray,ConstantAggregateZero}

# generic constructor taking an array of constants
function ConstantArray(typ::LLVMType, data::AbstractArray{T,N}=T[]) where {T<:Constant,N}
@assert all(x->x==typ, llvmtype.(data))

if N == 1
# XXX: this can return a ConstDataArray (presumably as an optimization?)
return Value(API.LLVMConstArray(typ, Array(data), length(data)))
return ConstantArrayOrAggregateZero(API.LLVMConstArray(typ, Array(data), length(data)))
end

ca_vec = map(x->ConstantArray(typ, x), eachslice(data, dims=1))
Expand All @@ -231,15 +183,15 @@ end
# shorthands with arrays of plain Julia data
# FIXME: duplicates the ConstantInt/ConstantFP conversion rules
# XXX: X[X(...)] instead of X.(...) because of empty-container inference
ConstantArray(data::AbstractArray{T}; ctx::Context) where {T<:Integer} =
ConstantArray(data::AbstractArray{T,N}; ctx::Context) where {T<:Integer,N} =
ConstantArray(IntType(sizeof(T)*8; ctx), ConstantInt[ConstantInt(x; ctx) for x in data])
ConstantArray(data::AbstractArray{Core.Bool}; ctx::Context) =
ConstantArray(data::AbstractArray{Core.Bool,N}; ctx::Context) where {N} =
ConstantArray(Int1Type(ctx), ConstantInt[ConstantInt(x; ctx) for x in data])
ConstantArray(data::AbstractArray{Float16}; ctx::Context) =
ConstantArray(data::AbstractArray{Float16,N}; ctx::Context) where {N} =
ConstantArray(HalfType(ctx), ConstantFP[ConstantFP(x; ctx) for x in data])
ConstantArray(data::AbstractArray{Float32}; ctx::Context) =
ConstantArray(data::AbstractArray{Float32,N}; ctx::Context) where {N} =
ConstantArray(FloatType(ctx), ConstantFP[ConstantFP(x; ctx) for x in data])
ConstantArray(data::AbstractArray{Float64}; ctx::Context) =
ConstantArray(data::AbstractArray{Float64,N}; ctx::Context) where {N} =
ConstantArray(DoubleType(ctx), ConstantFP[ConstantFP(x; ctx) for x in data])

# convert back to known array types
Expand Down Expand Up @@ -274,9 +226,7 @@ function Base.getindex(ca::ConstantArray, idx::Integer...)
I = CartesianIndices(size(ca))[idx...]
for i in Tuple(I)
if isempty(operands(ca))
# XXX: is this valid? LLVMGetElementAsConstant is meant to be used with
# Constant*Data*Arrays, not ConstantArrays
ca = Value(API.LLVMGetElementAsConstant(ca, i-1))
ca = LLVM.Value(API.LLVMGetElementAsConstant(ca, i-1))
else
ca = (Base.@_propagate_inbounds_meta; operands(ca)[i])
end
Expand Down
50 changes: 11 additions & 39 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,23 +419,29 @@ end
end


@testset "array aggregate constants" begin
@testset "array constants" begin

# from Julia values
let
vec = Int128[1,2,3,4]
vec = Int32[1,2,3,4]
ca = ConstantArray(vec; ctx)
@test ca isa ConstantArray
@test size(vec) == size(ca)
@test length(vec) == length(ca)
@test ca[1] == ConstantInt(vec[1]; ctx)
@test collect(ca) == ConstantInt.(vec; ctx)
end
let
vec = Float32[1.1f0,2.2f0,3.3f0,4.4f0]
ca = ConstantArray(vec; ctx)
@test size(vec) == size(ca)
@test length(vec) == length(ca)
@test ca[1] == ConstantFP(vec[1]; ctx)
@test collect(ca) == ConstantFP.(vec; ctx)
end
let
# tests for ConstantAggregateZero, constructed indirectly.
# should behave similarly to ConstantArray since it can get returned there.
ca = ConstantArray(Int[]; ctx)
@test ca isa ConstantAggregateZero
@test size(ca) == (0,)
@test length(ca) == 0
@test isempty(collect(ca))
Expand All @@ -452,7 +458,7 @@ end

end

@testset "struct aggregate constants" begin
@testset "struct constants" begin

# from Julia values
let
Expand Down Expand Up @@ -507,36 +513,6 @@ end
end

end


@testset "array data constants" begin

let
vec = Int32[1,2,3,4]
eltyp = LLVM.Int32Type(ctx)
cda = ConstantDataArray(eltyp, vec)
@test cda isa ConstantDataArray
@test llvmtype(cda) == LLVM.ArrayType(eltyp, 4)
@test collect(cda) == ConstantInt.(vec; ctx)
end

# from Julia values
for T in [Int8, Int16, Int32, Int64]
vec = T[1,2,3,4]
cda = ConstantDataArray(vec; ctx)
@test cda isa ConstantDataArray
@test size(vec) == size(cda)
@test collect(cda) == ConstantInt.(vec; ctx)
end
for T in [Float32, Float64]
vec = T[1,2,3,4]
cda = ConstantDataArray(vec; ctx)
@test cda isa ConstantDataArray
@test size(vec) == size(cda)
@test collect(cda) == ConstantFP.(vec; ctx)
end

end
end

# constant expressions
Expand Down Expand Up @@ -1015,10 +991,6 @@ end

@test mod_flags["foobar"] == md
@test_throws KeyError mod_flags["foobaz"]

@test sdk_version(mod) === nothing
sdk_version!(mod, v"1.2.3")
@test sdk_version(mod) == v"1.2.3"
end

# metadata iteration
Expand Down
10 changes: 0 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
# Work around JuliaLang/Pkg.jl#2500
if VERSION < v"1.8-"
test_project = first(Base.load_path())
preferences_file = "LocalPreferences.toml"
test_preferences_file = joinpath(dirname(test_project), "LocalPreferences.toml")
if isfile(preferences_file) && !isfile(test_preferences_file)
cp(preferences_file, test_preferences_file)
end
end

using LLVM
using Test

Expand Down

0 comments on commit e3bbfdb

Please sign in to comment.