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

make memorynew intrinsic #55913

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,17 @@ function tuple_tfunc(𝕃::AbstractLattice, argtypes::Vector{Any})
return anyinfo ? PartialStruct(typ, argtypes) : typ
end

@nospecs function memorynew_tfunc(𝕃::AbstractLattice, memtype, m)
hasintersect(widenconst(m), Int) || return Bottom
if isa(memtype, Const)
mem = memtype.val
mem <: GenericMemory || return Bottom
return mem
end
return GenericMemory
end
add_tfunc(Core.memorynew, 2, 2, memorynew_tfunc, 10)

@nospecs function memoryrefget_tfunc(𝕃::AbstractLattice, mem, order, boundscheck)
memoryref_builtin_common_errorcheck(mem, order, boundscheck) || return Bottom
return memoryref_elemtype(mem)
Expand Down
1 change: 1 addition & 0 deletions src/builtin_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ DECLARE_BUILTIN(is);
DECLARE_BUILTIN(isa);
DECLARE_BUILTIN(isdefined);
DECLARE_BUILTIN(issubtype);
DECLARE_BUILTIN(memory);
DECLARE_BUILTIN(memoryref);
DECLARE_BUILTIN(memoryref_isassigned);
DECLARE_BUILTIN(memoryrefget);
Expand Down
9 changes: 9 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,14 @@
}

// genericmemory ---------------------------------------------------------------------
JL_CALLABLE(jl_f_memory)
{
JL_NARGS(memoryref, 2, 2);
JL_TYPECHK(memoryref, genericmemory, args[0]);

Check warning on line 1649 in src/builtins.c

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

tab
JL_TYPECHK(memoryref, long, args[1]);

Check warning on line 1650 in src/builtins.c

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

tab
size_t nel = jl_unbox_long(args[1]) - 1;

Check warning on line 1651 in src/builtins.c

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

tab
return (jl_value_t*)jl_alloc_genericmemory(args[0], nel);

Check warning on line 1652 in src/builtins.c

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

tab
}

JL_CALLABLE(jl_f_memoryref)
{
Expand Down Expand Up @@ -2409,6 +2417,7 @@
jl_builtin_setglobalonce = add_builtin_func("setglobalonce!", jl_f_setglobalonce);

// memory primitives
jl_builtin_memory = add_builtin_func("memorynew", jl_f_memory);
jl_builtin_memoryref = add_builtin_func("memoryrefnew", jl_f_memoryref);
jl_builtin_memoryrefoffset = add_builtin_func("memoryrefoffset", jl_f_memoryrefoffset);
jl_builtin_memoryrefget = add_builtin_func("memoryrefget", jl_f_memoryrefget);
Expand Down
6 changes: 6 additions & 0 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4485,6 +4485,12 @@ static int compare_cgparams(const jl_cgparams_t *a, const jl_cgparams_t *b)
}
#endif

static jl_cgval_t emit_memorynew(jl_codectx_t &ctx, jl_datatype_t *mty_dt, jl_cgval_t len)
{
// Jameson HALP
Copy link
Member Author

Choose a reason for hiding this comment

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

@vtjnash can I get some help on writing this part?

Copy link
Member

Choose a reason for hiding this comment

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

Is this a special case where mty_dt is const?

Copy link
Member Author

Choose a reason for hiding this comment

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

no (although it might be worth special casing that)

return len;
}

static jl_cgval_t _emit_memoryref(jl_codectx_t &ctx, Value *mem, Value *data, const jl_datatype_layout_t *layout, jl_value_t *typ)
{
//jl_cgval_t argv[] = {
Expand Down
12 changes: 11 additions & 1 deletion src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,7 @@
{ jl_f_nfields_addr, new JuliaFunction<>{XSTR(jl_f_nfields), get_func_sig, get_func_attrs} },
{ jl_f__expr_addr, new JuliaFunction<>{XSTR(jl_f__expr), get_func_sig, get_func_attrs} },
{ jl_f__typevar_addr, new JuliaFunction<>{XSTR(jl_f__typevar), get_func_sig, get_func_attrs} },
{ jl_f_memory_addr, new JuliaFunction<>{XSTR(jl_f_memory), get_func_sig, get_func_attrs} },
{ jl_f_memoryref_addr, new JuliaFunction<>{XSTR(jl_f_memoryref), get_func_sig, get_func_attrs} },
{ jl_f_memoryrefoffset_addr, new JuliaFunction<>{XSTR(jl_f_memoryrefoffset), get_func_sig, get_func_attrs} },
{ jl_f_memoryrefset_addr, new JuliaFunction<>{XSTR(jl_f_memoryrefset), get_func_sig, get_func_attrs} },
Expand Down Expand Up @@ -4304,6 +4305,15 @@
return true;
}

else if (f == jl_builtin_memory && (nargs == 2)) {
const jl_cgval_t &memty = argv[1];
jl_datatype_t *mty_dt = (jl_datatype_t*)jl_unwrap_unionall(memty.typ);
if (jl_is_genericmemoryref_type(mty_dt) && jl_is_concrete_type((jl_value_t*)mty_dt)) {
*ret = emit_memorynew(ctx, mty_dt, argv[2]);
return true;
}
}

Check warning on line 4316 in src/codegen.cpp

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

trailing whitespace
else if (f == jl_builtin_memoryref && nargs == 1) {
const jl_cgval_t &mem = argv[1];
jl_datatype_t *mty_dt = (jl_datatype_t*)jl_unwrap_unionall(mem.typ);
Expand All @@ -4328,7 +4338,7 @@
return true;
}
}

Check warning on line 4341 in src/codegen.cpp

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

trailing whitespace
else if (f == jl_builtin_memoryrefoffset && nargs == 1) {
const jl_cgval_t &ref = argv[1];
jl_value_t *mty_dt = jl_unwrap_unionall(ref.typ);
Expand Down
3 changes: 2 additions & 1 deletion src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extern "C" {
// TODO: put WeakRefs on the weak_refs list during deserialization
// TODO: handle finalizers

#define NUM_TAGS 193
#define NUM_TAGS 194

// An array of references that need to be restored from the sysimg
// This is a manually constructed dual of the gvars array, which would be produced by codegen for Julia code, for C.
Expand Down Expand Up @@ -289,6 +289,7 @@ jl_value_t **const*const get_tags(void) {
INSERT_TAG(jl_builtin_replacefield);
INSERT_TAG(jl_builtin_setfieldonce);
INSERT_TAG(jl_builtin_fieldtype);
INSERT_TAG(jl_builtin_memory);
INSERT_TAG(jl_builtin_memoryref);
INSERT_TAG(jl_builtin_memoryrefoffset);
INSERT_TAG(jl_builtin_memoryrefget);
Expand Down
Loading