rust-lang / rustc_codegen_gcc

libgccjit AOT codegen for rustc
Apache License 2.0
921 stars 60 forks source link

libgccjit bug when creating call to function with multiple arguments #356

Closed GuillaumeGomez closed 1 year ago

GuillaumeGomez commented 1 year ago

The following code:

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
  /* Let's try to inject the equivalent of:
__attribute__((target("sse3")))
void foo () {
  __builtin_ia32_mwait(0, 1);
}
  */
  gcc_jit_type *int_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT);
  gcc_jit_type *void_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);

  /* Creating the `foo` function. */
  gcc_jit_function *foo_func =
    gcc_jit_context_new_function (ctxt, NULL,
                  GCC_JIT_FUNCTION_EXPORTED,
                  void_type,
                  "foo",
                  0, NULL,
                  0);

  /* __attribute__((target("sse3"))) */
  gcc_jit_function_add_string_attribute (
    foo_func,
    GCC_JIT_FN_ATTRIBUTE_TARGET,
    "sse3");

  gcc_jit_block *foo_block = gcc_jit_function_new_block (foo_func, NULL);

  gcc_jit_function *builtin = gcc_jit_context_get_target_builtin_function (
    ctxt, "__builtin_ia32_mwait");
  CHECK_NON_NULL (builtin);

  gcc_jit_rvalue *args[2];
  args[0] = gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0);
  args[1] = gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 1);

  gcc_jit_block_add_eval (foo_block, NULL,
    gcc_jit_context_new_call (ctxt, NULL, builtin, 2, args));

  gcc_jit_block_end_with_void_return (foo_block, NULL);
}

Fails when this code:

gcc_jit_block_add_eval (foo_block, NULL,
    gcc_jit_context_new_call (ctxt, NULL, builtin, 2, args));

is not commented.

antoyo commented 1 year ago

Just to make sure, it requires running this code twice, right?

GuillaumeGomez commented 1 year ago

Just realized the bug: it's actually when setting values to args, it's a pointer of pointer and I should have used args[0][0] and args[0][1]...