Skip to content

Commit

Permalink
Refactor implementation of conversion functions. Fixes xxxx
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Dec 22, 2024
1 parent d2edb4d commit 61e9bd5
Show file tree
Hide file tree
Showing 20 changed files with 482 additions and 310 deletions.
45 changes: 0 additions & 45 deletions src/jit/jit-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,51 +776,6 @@ bool jit_vfastcall(jit_t *j, jit_handle_t handle, jit_scalar_t *result,
return jit_try_vcall(j, f, result, args, tlab);
}

bool jit_try_call_packed(jit_t *j, jit_handle_t handle, jit_scalar_t context,
void *input, size_t insz, void *output, size_t outsz)
{
jit_func_t *f = jit_get_func(j, handle);

jit_fill_irbuf(f); // Ensure FFI spec is set
assert(ffi_spec_valid(f->spec));

ffi_type_t atype = ffi_spec_get(f->spec, 2);
ffi_type_t rtype = ffi_spec_get(f->spec, 0);

jit_scalar_t args[JIT_MAX_ARGS];
args[0] = context;

if (ffi_is_integral(atype))
args[1].integer = ffi_widen_int(atype, input);
else if (atype == FFI_FLOAT) {
assert(insz == sizeof(double));
args[1].real = *(double *)input;
}
else if (atype == FFI_POINTER)
args[1].pointer = input;
else
fatal_trace("unhandled FFI argument type %x", atype);

tlab_t tlab = jit_null_tlab(j);

jit_scalar_t result;
if (!jit_try_vcall(j, f, &result, args, &tlab))
return false;

if (ffi_is_integral(rtype))
ffi_store_int(rtype, result.integer, output);
else if (rtype == FFI_FLOAT) {
assert(outsz == sizeof(double));
*(double *)output = result.real;
}
else if (rtype == FFI_POINTER)
memcpy(output, result.pointer, outsz);
else
fatal_trace("unhandled FFI result type %x", rtype);

return true;
}

void *jit_call_thunk(jit_t *j, vcode_unit_t unit, void *context,
thunk_result_fn_t fn, void *arg)
{
Expand Down
2 changes: 1 addition & 1 deletion src/jit/jit-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const char *jit_exit_name(jit_exit_t exit)
"FUNCTION_TRIGGER", "ADD_TRIGGER", "TRANSFER_SIGNAL",
"PORT_CONVERSION", "CONVERT_IN", "CONVERT_OUT", "BIND_FOREIGN",
"OR_TRIGGER", "CMP_TRIGGER", "INSTANCE_NAME", "DEPOSIT_SIGNAL",
"MAP_IMPLICIT", "BIND_EXTERNAL", "SYSCALL",
"MAP_IMPLICIT", "BIND_EXTERNAL", "SYSCALL", "PUT_CONVERSION",
};
assert(exit < ARRAY_LEN(names));
return names[exit];
Expand Down
16 changes: 16 additions & 0 deletions src/jit/jit-exits.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,22 @@ void __nvc_do_exit(jit_exit_t which, jit_anchor_t *anchor, jit_scalar_t *args,
}
break;

case JIT_EXIT_PUT_CONVERSION:
{
rt_conv_func_t *cf = args[0].pointer;
sig_shared_t *shared = args[1].pointer;
int32_t offset = args[2].integer;
int32_t count = args[3].integer;
jit_scalar_t value = { .integer = args[4].integer };
bool scalar = args[5].integer;

if (scalar)
x_put_conversion(cf, shared, offset, count, &value.integer);
else
x_put_conversion(cf, shared, offset, count, value.pointer);
}
break;

case JIT_EXIT_PUSH_SCOPE:
{
if (!jit_has_runtime(thread->jit))
Expand Down
2 changes: 2 additions & 0 deletions src/jit/jit-exits.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ void x_force(sig_shared_t *ss, uint32_t offset, int32_t count, void *values);
void x_release(sig_shared_t *ss, uint32_t offset, int32_t count);
void x_deposit_signal(sig_shared_t *ss, uint32_t offset, int32_t count,
void *values);
void x_put_conversion(rt_conv_func_t *cf, sig_shared_t *ss, uint32_t offset,
int32_t count, void *values);
void x_resolve_signal(sig_shared_t *ss, jit_handle_t handle, void *context,
int64_t ileft, int32_t nlits, int32_t flags);
void x_unreachable(tree_t where);
Expand Down
30 changes: 0 additions & 30 deletions src/jit/jit-ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,6 @@ bool ffi_is_integral(ffi_type_t type)
|| type == FFI_UINT32;
}

int64_t ffi_widen_int(ffi_type_t type, const void *input)
{
switch (type) {
case FFI_INT8: return *((int8_t *)input);
case FFI_INT16: return *((int16_t *)input);
case FFI_INT32: return *((int32_t *)input);
case FFI_INT64: return *((int64_t *)input);
case FFI_UINT8: return *((uint8_t *)input);
case FFI_UINT16: return *((uint16_t *)input);
case FFI_UINT32: return *((uint32_t *)input);
default:
fatal_trace("invalid integer type in ffi_widen_int");
}
}

void ffi_store_int(ffi_type_t type, uint64_t value, void *output)
{
switch (type) {
case FFI_UINT8:
case FFI_INT8: *(uint8_t *)output = (uint8_t)value; break;
case FFI_UINT16:
case FFI_INT16: *(uint16_t *)output = (uint16_t)value; break;
case FFI_UINT32:
case FFI_INT32: *(uint32_t *)output = (uint32_t)value; break;
case FFI_INT64: *(uint64_t *)output = value; break;
default:
fatal_trace("invalid integer type in ffi_store_int");
}
}

static jit_dll_t *ffi_load_exe(void)
{
if (dlls != NULL)
Expand Down
2 changes: 0 additions & 2 deletions src/jit/jit-ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ ffi_spec_t ffi_spec_new(const ffi_type_t *types, size_t count);
ffi_uarray_t ffi_wrap(void *ptr, int64_t left, int64_t right);
void ffi_return_string(const char *str, jit_scalar_t *args, tlab_t *tlab);
bool ffi_is_integral(ffi_type_t type);
int64_t ffi_widen_int(ffi_type_t type, const void *input);
void ffi_store_int(ffi_type_t type, uint64_t value, void *output);

typedef struct _jit_dll jit_dll_t;

Expand Down
2 changes: 1 addition & 1 deletion src/jit/jit-interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ static void interp_load(jit_interp_t *state, jit_ir_t *ir)

JIT_ASSERT(ir->size != JIT_SZ_UNSPEC);
JIT_ASSERT(arg1 != NULL);
JIT_ASSERT((uintptr_t)arg1 >= 4096);
JIT_ASSERT((intptr_t)arg1 >= 4096);

switch (ir->size) {
case JIT_SZ_8:
Expand Down
24 changes: 24 additions & 0 deletions src/jit/jit-irgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ static ffi_type_t irgen_ffi_type(vcode_type_t type)
case VCODE_TYPE_ACCESS:
case VCODE_TYPE_FILE:
case VCODE_TYPE_DEBUG_LOCUS:
case VCODE_TYPE_CONVERSION:
return FFI_POINTER;
case VCODE_TYPE_UARRAY:
return FFI_UARRAY;
Expand Down Expand Up @@ -3217,6 +3218,26 @@ static void irgen_op_deposit_signal(jit_irgen_t *g, int op)
macro_exit(g, JIT_EXIT_DEPOSIT_SIGNAL);
}

static void irgen_op_put_conversion(jit_irgen_t *g, int op)
{
jit_value_t cf = irgen_get_arg(g, op, 0);
jit_value_t shared = irgen_get_arg(g, op, 1);
jit_value_t offset = jit_value_from_reg(jit_value_as_reg(shared) + 1);
jit_value_t count = irgen_get_arg(g, op, 2);
jit_value_t value = irgen_get_arg(g, op, 3);

jit_value_t scalar = irgen_is_scalar(g, op, 3);

j_send(g, 0, cf);
j_send(g, 1, shared);
j_send(g, 2, offset);
j_send(g, 3, count);
j_send(g, 4, value);
j_send(g, 5, scalar);

macro_exit(g, JIT_EXIT_PUT_CONVERSION);
}

static void irgen_op_sched_event(jit_irgen_t *g, int op)
{
jit_value_t shared = irgen_get_arg(g, op, 0);
Expand Down Expand Up @@ -3928,6 +3949,9 @@ static void irgen_block(jit_irgen_t *g, vcode_block_t block)
case VCODE_OP_DEPOSIT_SIGNAL:
irgen_op_deposit_signal(g, i);
break;
case VCODE_OP_PUT_CONVERSION:
irgen_op_put_conversion(g, i);
break;
case VCODE_OP_EVENT:
irgen_op_event(g, i);
break;
Expand Down
1 change: 1 addition & 0 deletions src/jit/jit-priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ typedef enum {
JIT_EXIT_MAP_IMPLICIT,
JIT_EXIT_BIND_EXTERNAL,
JIT_EXIT_SYSCALL,
JIT_EXIT_PUT_CONVERSION,
} jit_exit_t;

typedef uint16_t jit_reg_t;
Expand Down
2 changes: 0 additions & 2 deletions src/jit/jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ jit_t *jit_for_thread(void);
typedef void *(*thunk_result_fn_t)(jit_scalar_t *, void *);

bool jit_try_call(jit_t *j, jit_handle_t handle, jit_scalar_t *result, ...);
bool jit_try_call_packed(jit_t *j, jit_handle_t handle, jit_scalar_t context,
void *input, size_t insz, void *output, size_t outsz);
jit_scalar_t jit_call(jit_t *j, jit_handle_t handle, ...);
void *jit_call_thunk(jit_t *j, vcode_unit_t unit, void *context,
thunk_result_fn_t fn, void *arg);
Expand Down
Loading

0 comments on commit 61e9bd5

Please sign in to comment.