mir icon indicating copy to clipboard operation
mir copied to clipboard

Memory operation

Open sletz opened this issue 5 years ago • 5 comments

I'm trying to implement a "load in array" operation, so giving an array filled with some values to the "compute" function, which reads and returns the content of array[4]. The generated code seems correct, but the returned value is not:

static void test2()
{
    MIR_context_t fContext = MIR_init();
    MIR_module_t fModule = MIR_new_module(fContext, "Faust");
    
    // Create 'compute' function
    MIR_type_t res_type = MIR_T_D;
    MIR_item_t fCompute = MIR_new_func(fContext, "compute", 1, &res_type, 1, MIR_T_P, "real_heap");
    
    // Get 'heap' argument
    MIR_reg_t HEAP = MIR_reg(fContext, "real_heap", fCompute->u.func);
    
    // Create a local
    MIR_reg_t VAR1 = MIR_new_func_reg(fContext, fCompute->u.func, MIR_T_D, "VAR1");
    
    // Create and set 'index'
    MIR_reg_t INDEX = MIR_new_func_reg(fContext, fCompute->u.func, MIR_T_I64, "INDEX");
    
    MIR_append_insn(fContext, fCompute,
                     MIR_new_insn(fContext,
                                  MIR_MOV,
                                  MIR_new_reg_op(fContext, INDEX),
                                  MIR_new_int_op(fContext, 4)));
    
    // Get the 'heap' content at 'index'
    MIR_append_insn(fContext, fCompute,
                     MIR_new_insn(fContext, MIR_DMOV,
                                   MIR_new_reg_op(fContext, VAR1),
                                   MIR_new_mem_op(fContext, MIR_T_D, 0, HEAP, INDEX, 1)));
    
    MIR_append_insn(fContext, fCompute, MIR_new_ret_insn(fContext, 1, MIR_new_reg_op(fContext, VAR1)));
    
    // Finish function
    MIR_finish_func(fContext);
    
    // Finish module
    MIR_finish_module(fContext);
    
    MIR_load_module (fContext, fModule);
    MIR_link(fContext, MIR_set_interp_interface, import_resolver);
    
    // Dump module
    MIR_output(fContext, stderr);
    
    // Preparing test heap with some values
    double heap[8];
    for (int i = 0; i < 8; i++) {
        heap[i] = (double)i;
        printf("Heap: %f\n", heap[i]);
    }
    
    MIR_val_t val;
    MIR_interp(fContext, fCompute, &val, 1, (MIR_val_t){.a = (void*)heap});
    printf("Result: %f\n", val.d);
    
    MIR_finish(fContext);
}
Faust:	module
compute:	func	d, p:real_heap
	local	d:VAR1, i64:INDEX, i64:t1
# 1 arg, 3 locals
	mov	INDEX, 4
	add	t1, real_heap, INDEX
	dmov	VAR1, d:(t1)
	ret	VAR1
	endfunc
	endmodule
Heap: 0.000000
Heap: 1.000000
Heap: 2.000000
Heap: 3.000000
Heap: 4.000000
Heap: 5.000000
Heap: 6.000000
Heap: 7.000000
Result: 0.000000

sletz avatar Feb 26 '20 16:02 sletz

mov INDEX, 4 add t1, real_heap, INDEX dmov VAR1, d:(t1)

When you use index it is scaled by the last operand of MIR_new_mem_op which is 1 in your test. Therefore your MIR code reads from half of the 0th heap double (not just 4th double). Instead of

MIR_new_mem_op(fContext, MIR_T_D, 0, HEAP, INDEX, 1) you should use MIR_new_mem_op(fContext, MIR_T_D, 0, HEAP, INDEX, 8)

MIR is low level language, it is easy to make a mistake. Some people generates C code and use c2mir although it slows down compilation a lot and make JIT much bigger.

vnmakarov avatar Feb 26 '20 21:02 vnmakarov

Working, thanks a lot ! So I understand that I can also write some C and use c2mir to better understand what has to be generated.

sletz avatar Feb 26 '20 21:02 sletz

So I understand that I can also write some C and use c2mir to better understand what has to be generated.

Yes, you can. And some people do this. Such approach makes JIT bigger and slow. Generation of code through MIR API is about 10-15 times faster than through c2mir. Although c2mir is also 10-15 times faster than gcc -O2 it was never designed to be a fast compiler.

My own plans to implement JIT for Ruby is to use MIR API directly during JIT work and c2mir for translation of C code only during building Ruby (it is an existing C code implementing standard Ruby methods). During JIT work, the translated C code can be inlined with MIR code generated by MIR API to use inlining and to optimize the result inlined code.

vnmakarov avatar Feb 26 '20 21:02 vnmakarov

I plan to directly use MIR API. My point what to say: I can write some simple C tests, compile them using c2mir to understand what has to be generated, then directly generate MIR.

sletz avatar Feb 26 '20 21:02 sletz

I plan to directly use MIR API. My point what to say: I can write some simple C tests, compile them using c2mir to understand what has to be generated, then directly generate MIR.

I like your approach. I guess it can work for you. The only trouble is that MIR code generated from C might be hard to read and there are a lot of dead code (it is mostly results of assign-stmts which are never used).

vnmakarov avatar Feb 26 '20 22:02 vnmakarov