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

Migrate to opaque pointers #112

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
97 changes: 49 additions & 48 deletions src/llvm_intrin/memory_addr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function offset_ptr(
if iszero(O)
push!(
instrs,
"%ptr.$(i) = inttoptr $(JULIAPOINTERTYPE) %0 to $(index_gep_typ)*"
Copy link
Member

Choose a reason for hiding this comment

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

They shouldn't be loads at all. There shouldn't be statements here at all; they should simply be deleted.

I'd suggest renaming the argument from @foo(ptr %0) to @foo(ptr %ptr.0) so that you don't need special handling here for the initial definition.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, this maneuver to get a %ptr.0 is a bit trickier than I though. I reannotated the call sites of @foo(ptr %ptr.0) but am still not finding all the ccases.

"%ptr.$(i) = load $(index_gep_typ)*, ptr %0"
)
i += 1
else # !iszero(O)
Expand All @@ -181,25 +181,25 @@ function offset_ptr(
end
push!(
instrs,
"%ptr.$(i) = inttoptr $(JULIAPOINTERTYPE) %0 to $(offset_gep_typ)*"
"%ptr.$(i) = load $(offset_gep_typ)*, ptr %0"
)
i += 1
push!(
instrs,
"%ptr.$(i) = getelementptr inbounds $(offset_gep_typ), $(offset_gep_typ)* %ptr.$(i-1), i32 $(offset)"
"%ptr.$(i) = getelementptr inbounds $(offset_gep_typ), ptr %ptr.$(i-1), i32 $(offset)"
)
i += 1
if forgep && iszero(M) && (iszero(X) || isone(X))
push!(
instrs,
"%ptr.$(i) = ptrtoint $(offset_gep_typ)* %ptr.$(i-1) to $(JULIAPOINTERTYPE)"
"%ptr.$(i) = load $(index_gep_typ)*, ptr %ptr.$(i-1)"
)
i += 1
return instrs, i
elseif offset_gep_typ != index_gep_typ
push!(
instrs,
"%ptr.$(i) = bitcast $(offset_gep_typ)* %ptr.$(i-1) to $(index_gep_typ)*"
"%ptr.$(i) = load $(index_gep_typ)*, ptr %ptr.$(i-1)"
)
i += 1
end
Expand All @@ -218,19 +218,19 @@ function offset_ptr(
end
push!(
instrs,
"%ptr.$(i) = getelementptr inbounds $(index_gep_typ), $(index_gep_typ)* %ptr.$(i-1), <$W x i$(ibits)> %$(indname)"
"%ptr.$(i) = getelementptr inbounds $(index_gep_typ), ptr %ptr.$(i-1), <$W x i$(ibits)> %$(indname)"
)
i += 1
if forgep
push!(
instrs,
"%ptr.$(i) = ptrtoint <$W x $index_gep_typ*> %ptr.$(i-1) to <$W x $JULIAPOINTERTYPE>"
"%ptr.$(i) = load <$W x ptr>, ptr %ptr.$(i-1)"
)
i += 1
elseif index_gep_typ != vtyp
push!(
instrs,
"%ptr.$(i) = bitcast <$W x $index_gep_typ*> %ptr.$(i-1) to <$W x $typ*>"
"%ptr.$(i) = load <$W x ptr>, ptr %ptr.$(i-1)"
)
i += 1
end
Expand Down Expand Up @@ -278,7 +278,7 @@ function offset_ptr(
end
push!(
instrs,
"%ptr.$(i) = getelementptr inbounds $(index_gep_typ), $(index_gep_typ)* %ptr.$(i-1), i$(ibits) %$(indname)"
"%ptr.$(i) = getelementptr inbounds $(index_gep_typ)*, ptr %ptr.$(i-1), i$(ibits) %$(indname)"
)
i += 1
end
Expand All @@ -293,19 +293,19 @@ function offset_ptr(
if typ !== index_gep_typ
push!(
instrs,
"%ptr.$(i) = bitcast $(index_gep_typ)* %ptr.$(i-1) to $(typ)*"
"%ptr.$(i) = load $(typ)*, ptr %ptr.$(i-1)"
)
i += 1
end
push!(
instrs,
"%ptr.$(i) = getelementptr inbounds $(typ), $(typ)* %ptr.$(i-1), <$W x $(vityp)> <$vityp $vi>"
"%ptr.$(i) = getelementptr inbounds $(typ)*, ptr %ptr.$(i-1), <$W x $(vityp)> <$vityp $vi>"
)
i += 1
if forgep
push!(
instrs,
"%ptr.$(i) = ptrtoint <$W x $typ*> %ptr.$(i-1) to <$W x $JULIAPOINTERTYPE>"
"%ptr.$(i) = load <$W x ptr>, ptr, %ptr.$(i-1)"
)
i += 1
end
Expand All @@ -314,13 +314,13 @@ function offset_ptr(
if forgep # if forgep, just return now
push!(
instrs,
"%ptr.$(i) = ptrtoint $(index_gep_typ)* %ptr.$(i-1) to $JULIAPOINTERTYPE"
"%ptr.$(i) = load $(vtyp)*, %ptr.$(i-1)"
Copy link
Author

Choose a reason for hiding this comment

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

This one I'm not sure how to bring back the $(vtyp)*

Copy link
Member

@chriselrod chriselrod May 12, 2024

Choose a reason for hiding this comment

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

$(vtyp)* does not exist anymore in an opaque pointer world. It should just be ptr.

Every bitcast from one pointer type to another pointer type should just be deleted without replacement.
Every ptrtoint should just be deleted without replacement.
Every inttoptr should just be deleted without replacement.

Minor fixes will be needed:

%y = bitcast sometype* %x to someothertype*
call foo(%y)

needs to become

call foo(%x)

but that should be relatively trivial when x is ptr.$(i) and y is ptr.$(i+1): just delete the i+=1 along with the bitcast/ptrtoint/intoptr statement.

But that's why I'd also simplify things by having the initial definition (as an argument to the function) be named %ptr.0 instead of %0 as it is currently.

)
i += 1
elseif index_gep_typ != vtyp
push!(
instrs,
"%ptr.$(i) = bitcast $(index_gep_typ)* %ptr.$(i-1) to $(vtyp)*"
"%ptr.$(i) = load $vtyp*, ptr %ptr.$(i-1)"
)
i += 1
end
Expand Down Expand Up @@ -373,14 +373,14 @@ function gep_quote(
# ::Type{T}, ind_type::Symbol, indargname = '1', ibytes::Int, W::Int = 1, X::Int = 1, M::Int = 1, O::Int = 0, forgep::Bool = false
instrs, i = offset_ptr(T_sym, ind_type, '1', ibits, W, X, M, O, true, rs)
ret = Expr(:curly, :Ptr, T_sym)
lret = JULIAPOINTERTYPE
lret = "ptr"
if gep_returns_vector(W, X, M, ind_type)
ret = Expr(:curly, :_Vec, W, ret)
lret = "<$W x $lret>"
end

args = Expr(:curly, :Tuple, Expr(:curly, :Ptr, T_sym))
largs = String[JULIAPOINTERTYPE]
largs = "ptr"
arg_syms = Union{Symbol,Expr}[:ptr]

if !(iszero(M) || ind_type === :StaticInt)
Expand Down Expand Up @@ -822,27 +822,27 @@ function vload_quote_llvmcall_core(
suffix(W, T_sym) *
'.' *
ptr_suffix(W, T_sym)
decl *= "declare $loadinstr(<$W x $typ*>, i32, <$W x i1>, $vtyp)"
decl *= "declare $loadinstr(<$W x ptr>, i32, <$W x i1>, $vtyp)"
m = mask ? m = "%mask.0" : llvmconst(W, "i1 1")
passthrough = mask ? "zeroinitializer" : "undef"
push!(
instrs,
"%res = call $loadinstr(<$W x $typ*> %ptr.$(i-1), i32 $alignment, <$W x i1> $m, $vtyp $passthrough)" *
"%res = call $loadinstr(<$W x ptr> %ptr.$(i-1), i32 $alignment, <$W x i1> $m, $vtyp $passthrough)" *
LOAD_SCOPE_TBAA_FLAGS
)
elseif mask
suff = suffix(W, T_sym)
loadinstr = "$vtyp @llvm.masked.load." * suff * ".p0" * suff
decl *= "declare $loadinstr($vtyp*, i32, <$W x i1>, $vtyp)"
decl *= "declare $loadinstr(ptr, i32, <$W x i1>, $vtyp)"
push!(
instrs,
"%res = call $loadinstr($vtyp* %ptr.$(i-1), i32 $alignment, <$W x i1> %mask.0, $vtyp zeroinitializer)" *
"%res = call $loadinstr(ptr %ptr.$(i-1), i32 $alignment, <$W x i1> %mask.0, $vtyp zeroinitializer)" *
LOAD_SCOPE_TBAA_FLAGS
)
else
push!(
instrs,
"%res = load $vtyp, $vtyp* %ptr.$(i-1), align $alignment" *
"%res = load $vtyp, ptr %ptr.$(i-1), align $alignment" *
LOAD_SCOPE_TBAA_FLAGS
)
end
Expand Down Expand Up @@ -886,7 +886,7 @@ function vload_quote_llvmcall_core(
end
end
args = Expr(:curly, :Tuple, Expr(:curly, :Ptr, T_sym))
largs = String[JULIAPOINTERTYPE]
largs = "ptr"
arg_syms = Union{Symbol,Expr}[:ptr]
if dynamic_index
push!(arg_syms, :(data(i)))
Expand Down Expand Up @@ -1254,33 +1254,33 @@ function vstore_quote(
suffix(W, T_sym) *
'.' *
ptr_suffix(W, T_sym)
decl *= "declare $storeinstr($vtyp, <$W x $typ*>, i32, <$W x i1>)"
decl *= "declare $storeinstr($vtyp, <$W x ptr>, i32, <$W x i1>)"
m = mask ? m = "%mask.0" : llvmconst(W, "i1 1")
push!(
instrs,
"call $storeinstr($vtyp $(argtostore), <$W x $typ*> %ptr.$(i-1), i32 $alignment, <$W x i1> $m)" *
"call $storeinstr($vtyp $(argtostore), <$W x ptr> %ptr.$(i-1), i32 $alignment, <$W x i1> $m)" *
metadata
)
# push!(instrs, "call $storeinstr($vtyp $(argtostore), <$W x $typ*> %ptr.$(i-1), i32 $alignment, <$W x i1> $m)")
elseif mask
suff = suffix(W, T_sym)
storeinstr = "void @llvm.masked.store." * suff * ".p0" * suff
decl *= "declare $storeinstr($vtyp, $vtyp*, i32, <$W x i1>)"
decl *= "declare $storeinstr($vtyp, ptr, i32, <$W x i1>)"
push!(
instrs,
"call $storeinstr($vtyp $(argtostore), $vtyp* %ptr.$(i-1), i32 $alignment, <$W x i1> %mask.0)" *
"call $storeinstr($vtyp $(argtostore), ptr %ptr.$(i-1), i32 $alignment, <$W x i1> %mask.0)" *
metadata
)
elseif nontemporal
push!(
instrs,
"store $vtyp $(argtostore), $vtyp* %ptr.$(i-1), align $alignment, !nontemporal !{i32 1}" *
"store $vtyp $(argtostore), ptr %ptr.$(i-1), align $alignment, !nontemporal !{i32 1}" *
metadata
)
else
push!(
instrs,
"store $vtyp $(argtostore), $vtyp* %ptr.$(i-1), align $alignment" *
"store $vtyp $(argtostore), ptr %ptr.$(i-1), align $alignment" *
miguelraz marked this conversation as resolved.
Show resolved Hide resolved
metadata
)
end
Expand All @@ -1298,7 +1298,7 @@ function vstore_quote(
else
Expr(:curly, :Tuple, ptrtyp, T_sym)
end
largs = String[JULIAPOINTERTYPE, vtyp]
largs = String["ptr", vtyp]
arg_syms = Union{Symbol,Expr}[:ptr, Expr(:call, :data, :v)]
if dynamic_index
push!(arg_syms, :(data(i)))
Expand Down Expand Up @@ -2170,10 +2170,10 @@ end
"Prefetch intrinsic requires a read/write argument of 0, 1, but received $R."
)
)
decl = "declare void @llvm.prefetch(i8*, i32, i32, i32)"
decl = "declare void @llvm.prefetch(ptr, i32, i32, i32)"
instrs = """
%addr = inttoptr $JULIAPOINTERTYPE %0 to i8*
call void @llvm.prefetch(i8* %addr, i32 $R, i32 $L, i32 1)
%addr = load i8*, ptr %0
call void @llvm.prefetch(ptr %addr, i32 $R, i32 $L, i32 1)
ret void
"""
llvmcall_expr(
Expand All @@ -2182,7 +2182,7 @@ end
:Cvoid,
:(Tuple{Ptr{Cvoid}}),
"void",
[JULIAPOINTERTYPE],
["ptr"],
[:ptr],
false,
true
Expand Down Expand Up @@ -2228,31 +2228,31 @@ end

@generated function lifetime_start!(ptr::Ptr{T}, ::Val{L}) where {L,T}
ptyp = LLVM_TYPES[T]
decl = "declare void @llvm.lifetime.start(i64, $ptyp* nocapture)"
instrs = "%ptr = inttoptr $JULIAPOINTERTYPE %0 to $ptyp*\ncall void @llvm.lifetime.start(i64 $L, $ptyp* %ptr)\nret void"
decl = "declare void @llvm.lifetime.start(i64, ptr nocapture)"
instrs = "%ptr = load $ptyp*, ptr %0\ncall void @llvm.lifetime.start(i64 $L, ptr %ptr)\nret void"
llvmcall_expr(
decl,
instrs,
:Cvoid,
:(Tuple{Ptr{$T}}),
"void",
[JULIAPOINTERTYPE],
["ptr"],
[:ptr],
false,
true
)
end
@generated function lifetime_end!(ptr::Ptr{T}, ::Val{L}) where {L,T}
ptyp = LLVM_TYPES[T]
decl = "declare void @llvm.lifetime.end(i64, $ptyp* nocapture)"
instrs = "%ptr = inttoptr $JULIAPOINTERTYPE %0 to $ptyp*\ncall void @llvm.lifetime.end(i64 $L, $ptyp* %ptr)\nret void"
decl = "declare void @llvm.lifetime.end(i64, ptr nocapture)"
instrs = "%ptr = load $ptyp*, ptr %0\ncall void @llvm.lifetime.end(i64 $L, ptr %ptr)\nret void"
llvmcall_expr(
decl,
instrs,
:Cvoid,
:(Tuple{Ptr{$T}}),
"void",
[JULIAPOINTERTYPE],
["ptr"],
[:ptr],
false,
true
Expand Down Expand Up @@ -2284,20 +2284,21 @@ end
vtyp = "<$W x $typ>"
mtyp_input = LLVM_TYPES[U]
mtyp_trunc = "i$W"
instrs = String["%ptr = inttoptr $JULIAPOINTERTYPE %1 to $typ*"]
instrs = String["%ptr = load $typ*, ptr %1"]
truncate_mask!(instrs, '2', W, 0)
decl = "declare void @llvm.masked.compressstore.$(suffix(W,T))($vtyp, $typ*, <$W x i1>)"
decl = "declare void @llvm.masked.compressstore.$(suffix(W,T))($vtyp, ptr, <$W x i1>)"
push!(
instrs,
"call void @llvm.masked.compressstore.$(suffix(W,T))($vtyp %0, $typ* %ptr, <$W x i1> %mask.0)\nret void"
"call void @llvm.masked.compressstore.$(suffix(W,T))($vtyp %0, ptr %ptr, <$W x i1> %mask.0)\nret void"
)

llvmcall_expr(
decl,
join(instrs, "\n"),
:Cvoid,
:(Tuple{_Vec{$W,$T},Ptr{$T},$U}),
"void",
[vtyp, JULIAPOINTERTYPE, "i$(8sizeof(U))"],
[vtyp, "ptr", "i$(8sizeof(U))"],
[:(data(v)), :ptr, :(data(mask))],
false,
true
Expand All @@ -2310,29 +2311,29 @@ end
) where {W,T<:NativeTypes,U<:Unsigned}
typ = LLVM_TYPES[T]
vtyp = "<$W x $typ>"
vptrtyp = "<$W x $typ*>"
vptrtyp = "<$W x ptr>"
mtyp_input = LLVM_TYPES[U]
mtyp_trunc = "i$W"
instrs = String[]
push!(instrs, "%ptr = inttoptr $JULIAPOINTERTYPE %0 to $typ*")
push!(instrs, "%ptr = load $typ*, ptr %0")
if mtyp_input == mtyp_trunc
push!(instrs, "%mask = bitcast $mtyp_input %1 to <$W x i1>")
else
push!(instrs, "%masktrunc = trunc $mtyp_input %1 to $mtyp_trunc")
push!(instrs, "%mask = bitcast $mtyp_trunc %masktrunc to <$W x i1>")
end
decl = "declare $vtyp @llvm.masked.expandload.$(suffix(W,T))($typ*, <$W x i1>, $vtyp)"
decl = "declare $vtyp @llvm.masked.expandload.$(suffix(W,T))(ptr, <$W x i1>, $vtyp)"
push!(
instrs,
"%res = call $vtyp @llvm.masked.expandload.$(suffix(W,T))($typ* %ptr, <$W x i1> %mask, $vtyp zeroinitializer)\nret $vtyp %res"
"%res = call $vtyp @llvm.masked.expandload.$(suffix(W,T))(ptr %ptr, <$W x i1> %mask, $vtyp zeroinitializer)\nret $vtyp %res"
)
llvmcall_expr(
decl,
join(instrs, "\n"),
:(_Vec{$W,$T}),
:(Tuple{Ptr{$T},$U}),
vtyp,
[JULIAPOINTERTYPE, "i$(8sizeof(U))"],
["ptr"],
miguelraz marked this conversation as resolved.
Show resolved Hide resolved
[:ptr, :(data(mask))],
false,
true
Expand Down
4 changes: 1 addition & 3 deletions src/llvm_intrin/vbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,10 @@ end
) where {W,T}
isone(W) && return Expr(:block, Expr(:meta, :inline), :(vload(ptr)))
typ = LLVM_TYPES[T]
ptyp = JULIAPOINTERTYPE
vtyp = "<$W x $typ>"
alignment = Base.datatype_alignment(T)
instrs = """
%ptr = inttoptr $ptyp %0 to $typ*
%res = load $typ, $typ* %ptr, align $alignment
%res = load $typ, ptr %ptr, align $alignment
%ie = insertelement $vtyp undef, $typ %res, i32 0
%v = shufflevector $vtyp %ie, $vtyp undef, <$W x i32> zeroinitializer
ret $vtyp %v
Expand Down
2 changes: 0 additions & 2 deletions src/llvm_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ function _get_alignment(W::Int, sym::Symbol)::Int
end
end

const JULIAPOINTERTYPE = 'i' * string(8sizeof(Int))

vtype(W, typ::String) = (isone(abs(W)) ? typ : "<$W x $typ>")::String
vtype(W, T::DataType) = vtype(W, LLVM_TYPES[T])::String
vtype(W, T::Symbol) = vtype(W, get(LLVM_TYPES_SYM, T, T))::String
Expand Down
Loading