Skip to content

Commit

Permalink
rework monkey patching
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 16, 2022
1 parent aee596c commit 22cacea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 52 deletions.
59 changes: 10 additions & 49 deletions gen/src/MPIgenerator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,17 @@ module MPIgenerator
error("Unknown MPI binary: $(MPIPreferences.binary)")
end

@eval Clang.Generators translate(jlty::JuliaCpointer, options=Dict()) = begin
# @show jlty getPointeeType(jlty.ref) getNamedType(getPointeeType(jlty.ref)) propertynames(jlty)
is_jl_funcptr(jlty) && return translate(JuliaPtrCvoid(), options)
jlptree = tojulia(getPointeeType(jlty.ref))
if get(options, "always_NUL_terminated_string", false)
is_jl_char(jlptree) && return :Cstring
is_jl_wchar(jlptree) && return :Cwstring
@eval Clang.Generators _get_func_arg(cursor::CLFunctionDecl, options, dag) = begin
arg_names, args = invoke(_get_func_arg, NTuple{3,Any}, cursor, options, dag)

re = r"^type_(.*)_fn"
for (i, (name, arg)) in enumerate(zip(arg_names, args))
match(re, string(name)) nothing && continue
arg != :(Ptr{Cvoid}) && continue
args[i] = :($(Symbol(replace(string(name), re => s"MPI_Type_\1_function"))))
end
return Expr(:curly, :Ptr, translate(jlptree, options))
end

rewrite!(dag::ExprDAG) = begin
replace!(get_nodes(dag)) do node
# signature = name(node.cursor)
# @show signature node.cursor

for a in get_function_args(node.cursor)
startswith(name(a), "type_") || continue
# @show a, typeof(a)
if (ct = Clang.getCursorType(a)) isa CLPointer
# @show translate(tojulia(ct))
# if Clang.Generators.is_jl_funcptr(tojulia(ct))
if (pt = Clang.getPointeeType(ct)) isa CLTypedef
dumpobj(a)
repl = "MPI_" * replace(name(a), "type_" => "Type_")
@show repl
# FIXME: how do we replace node function arguments ?
# translate(tojulia(ct)) should replace "Ptr{Cvoid}" => repl

# the following approach doesn't work since some information is lost during julia code generation
#=
replace!(get_exprs(node)) do ex
out = Expr(ex.head)
for a in ex.args
push!(out.args, a)
end
@show out
out
end
=#
end
# end
end
end
# dump(node)

node
end
arg_names, args
end

signatures() = begin
Expand All @@ -85,9 +48,7 @@ module MPIgenerator

ctx = create_context(headers, args, options)

build!(ctx, BUILDSTAGE_NO_PRINTING)
rewrite!(ctx.dag)
build!(ctx, BUILDSTAGE_PRINTING_ONLY)
build!(ctx) # run generator

############################
# custom MPI post-processing
Expand Down
6 changes: 3 additions & 3 deletions src/api/generated_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ end
$(_doc_external(:MPI_Type_create_keyval))
"""
function MPI_Type_create_keyval(type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
@mpichk ccall((:MPI_Type_create_keyval, libmpi), Cint, (MPIPtr, MPIPtr, Ptr{Cint}, MPIPtr), type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
@mpichk ccall((:MPI_Type_create_keyval, libmpi), Cint, (MPI_Type_copy_attr_function, MPI_Type_delete_attr_function, Ptr{Cint}, MPIPtr), type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
end

"""
Expand Down Expand Up @@ -5118,7 +5118,7 @@ end
$(_doc_external(:PMPI_Type_create_keyval))
"""
function PMPI_Type_create_keyval(type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
@mpichk ccall((:PMPI_Type_create_keyval, libmpi), Cint, (MPIPtr, MPIPtr, Ptr{Cint}, MPIPtr), type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
@mpichk ccall((:PMPI_Type_create_keyval, libmpi), Cint, (MPI_Type_copy_attr_function, MPI_Type_delete_attr_function, Ptr{Cint}, MPIPtr), type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
end

"""
Expand Down Expand Up @@ -9960,7 +9960,7 @@ end
$(_doc_external(:QMPI_Type_create_keyval))
"""
function QMPI_Type_create_keyval(context, tool_id, type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
@mpichk ccall((:QMPI_Type_create_keyval, libmpi), Cint, (QMPI_Context, Cint, MPIPtr, MPIPtr, Ptr{Cint}, MPIPtr), context, tool_id, type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
@mpichk ccall((:QMPI_Type_create_keyval, libmpi), Cint, (QMPI_Context, Cint, MPI_Type_copy_attr_function, MPI_Type_delete_attr_function, Ptr{Cint}, MPIPtr), context, tool_id, type_copy_attr_fn, type_delete_attr_fn, type_keyval, extra_state)
end

"""
Expand Down

0 comments on commit 22cacea

Please sign in to comment.