Skip to content

Commit

Permalink
Fix for LLVM 12
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Jun 23, 2021
1 parent 5ab417e commit 7ff650c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 110 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VectorizationBase"
uuid = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f"
authors = ["Chris Elrod <elrodc@gmail.com>"]
version = "0.18.14"
version = "0.18.15"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
115 changes: 6 additions & 109 deletions src/llvm_intrin/masks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,12 @@ end
end
@generated function _mask(::Union{Val{W},StaticInt{W}}, l::I, ::False) where {W,I<:Integer}
# Otherwise, it's probably more efficient to use a comparison, as this will probably create some type that can be used directly for masked moves/blends/etc
if Base.libllvm_version v"11"
quote
$(Expr(:meta,:inline))
mask(Val{$W}(), zero(l), ((l - one(l)) & $(I(W-1))))
end
else
M = mask_type_symbol(W)
quote
$(Expr(:meta,:inline))
rem = valrem(Val{$W}(), vsub((l % $M), one($M)))
rem MM{$W}(0)
end
end
M = mask_type_symbol(W)
quote
$(Expr(:meta,:inline))
rem = valrem(Val{$W}(), vsub((l % $M), one($M)))
rem MM{$W}(0)
end
end
@inline function mask(::Union{Val{W},StaticInt{W}}, l::I) where {W, I <: Integer}
_mask(StaticInt{W}(), l, has_opmask_registers())
Expand Down Expand Up @@ -453,99 +446,3 @@ end
@inline vmul(b::Bool, v::VecUnroll{N,W,T}) where {N,W,T} = b ? v : zero(v)



@static if Base.libllvm_version v"11"
"""
mask(::Union{StaticInt{W},Val{W}}, base, N)
mask(base::MM{W}, N)
The two arg (`base`, `N`) method takes a base (current index) and last index of a loop.
Idiomatic use for three-arg version may look like
```julia
using VectorizationBase
sp = stridedpointer(x);
for i ∈ 1:8:N
m = mask(Val(8), (MM{8}(i),), N) # if using an integer base, also needs a `Val` or `StaticInt` to indicate size.
v = vload(sp, (MM{8}(i),), m)
# do something with `v`
end
```
or, a full runnable example:
```julia
using VectorizationBase, SLEEFPirates
x = randn(117); y = similar(x);
function vexp!(y, x)
W = VectorizationBase.pick_vector_width(eltype(x));
L = length(y);
spx = stridedpointer(x); spy = stridedpointer(y);
i = MM(W, 1); # use an `MM` index.
while (m = mask(i,L); m !== VectorizationBase.zero_mask(W))
yᵢ = exp(vload(spx, (i,), m))
vstore!(spy, yᵢ, (i,), m)
i += W
end
end
vexp!(y, x)
@assert y ≈ exp.(x)
# A sum optimized for short vectors (e.g., 10-20 elements)
function simd_sum(x)
W = VectorizationBase.pick_vector_width(eltype(x));
L = length(x);
spx = stridedpointer(x);
i = MM(W, 1); # use an `MM` index.
s = VectorizationBase.vzero(W, eltype(x))
while (m = mask(i,L); m !== VectorizationBase.zero_mask(W))
s += vload(spx, (i,), m)
i += W
end
VectorizationBase.vsum(s)
end
# or
function simd_sum(x)
W = VectorizationBase.pick_vector_width(eltype(x));
L = length(x);
spx = stridedpointer(x);
i = MM(W, 1); # use an `MM` index.
s = VectorizationBase.vzero(W, eltype(x))
cond = true
m = mask(i,L)
while cond
s += vload(spx, (i,), m)
i += W
m = mask(i,L)
cond = m !== VectorizationBase.zero_mask(W)
end
VectorizationBase.vsum(s)
end
```
```julia
julia> VectorizationBase.mask(Val(8), 1, 6) # starting with `i = 1`, if vector is of length 6, 6 lanes are on
Mask{8,Bool}<1, 1, 1, 1, 1, 1, 0, 0>
julia> VectorizationBase.mask(Val(8), 81, 93) # if `i = 81` and the vector is of length 93, we want all lanes on.
Mask{8,Bool}<1, 1, 1, 1, 1, 1, 1, 1>
julia> VectorizationBase.mask(Val(8), 89, 93) # But after `i += 8`, we're at `i = 89`, and now want just 5 lanes on.
Mask{8,Bool}<1, 1, 1, 1, 1, 0, 0, 0>
```
"""
@generated function mask(::Union{Val{W},StaticInt{W}}, base::T, N::T) where {W,T <: IntegerTypesHW}
# declare <8 x i1> @llvm.get.active.lane.mask.v8i1.i64(i64 %base, i64 %n)
bits = 8sizeof(T)
typ = "i$(bits)"
decl = "declare <$W x i1> @llvm.get.active.lane.mask.v$(W)i1.$(typ)($(typ), $(typ))"
instrs = ["%m = call <$W x i1> @llvm.get.active.lane.mask.v$(W)i1.$(typ)($(typ) %0, $(typ) %1)"]
zext_mask!(instrs, 'm', W, 0)
push!(instrs, "ret i$(max(W,8)) %res.0")
# args = [:base, :(vsub(N,one($T)))]
args = [:base, :N]
call = llvmcall_expr(decl, join(instrs,"\n"), mask_type_symbol(W), :(Tuple{$T,$T}), "i$(max(W,8))", [typ, typ], args, true)
Expr(:block, Expr(:meta,:inline), :(Mask{$W}($call)))
end
@inline mask(i::MM{W}, N::T) where {W,T<:IntegerTypesHW} = mask(Val{W}(), i.i, N)
end

2 comments on commit 7ff650c

@chriselrod
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/39498

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.18.15 -m "<description of version>" 7ff650c90602f39b65cf7ecbadfeb2956b21840c
git push origin v0.18.15

Please sign in to comment.