1 /* Testcase for gcc_jit_context_add_command_line_option (PR jit/66628). */
10 #ifndef LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option
11 #error LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option was not defined
15 create_code (gcc_jit_context
*ctxt
, void *user_data
)
17 gcc_jit_context_add_command_line_option (ctxt
, "-ffast-math");
18 gcc_jit_context_add_command_line_option (ctxt
, "-fverbose-asm");
20 /* Let's try to inject the equivalent of:
23 my_dot_product (int n, double *a, double *b)
26 for (int i = 0; i < n; i++)
27 result += a[i] * b[i];
31 and see what the optimizer can do. */
32 gcc_jit_type
*val_type
=
33 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_DOUBLE
);
34 gcc_jit_type
*ptr_type
= gcc_jit_type_get_pointer (val_type
);
35 gcc_jit_type
*int_type
=
36 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
38 gcc_jit_type
*return_type
= val_type
;
39 gcc_jit_param
*param_n
=
40 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "n");
41 gcc_jit_param
*param_a
=
42 gcc_jit_context_new_param (ctxt
, NULL
, ptr_type
, "a");
43 gcc_jit_param
*param_b
=
44 gcc_jit_context_new_param (ctxt
, NULL
, ptr_type
, "b");
45 gcc_jit_param
*params
[3] = {param_n
, param_a
, param_b
};
46 gcc_jit_function
*func
=
47 gcc_jit_context_new_function (ctxt
, NULL
,
48 GCC_JIT_FUNCTION_EXPORTED
,
53 gcc_jit_block
*initial
= gcc_jit_function_new_block (func
, "initial");
54 gcc_jit_block
*loop_test
= gcc_jit_function_new_block (func
, "loop_test");
55 gcc_jit_block
*loop_body
= gcc_jit_function_new_block (func
, "loop_body");
56 gcc_jit_block
*final
= gcc_jit_function_new_block (func
, "final");
58 /* Build: "double result = 0.;" */
59 gcc_jit_lvalue
*result
=
60 gcc_jit_function_new_local (func
, NULL
, val_type
, "result");
62 gcc_jit_block_add_assignment (initial
, NULL
,
63 result
, gcc_jit_context_zero (ctxt
, val_type
));
65 /* Build: "for (int i = 0; i < n; i++)" */
67 gcc_jit_function_new_local (func
, NULL
, int_type
, "i");
68 gcc_jit_block_add_assignment (initial
, NULL
,
69 i
, gcc_jit_context_zero (ctxt
, int_type
));
71 gcc_jit_block_end_with_jump (initial
, NULL
, loop_test
);
73 gcc_jit_block_end_with_conditional (
77 gcc_jit_context_new_comparison (
79 GCC_JIT_COMPARISON_LT
,
80 gcc_jit_lvalue_as_rvalue (i
),
81 gcc_jit_param_as_rvalue (param_n
)),
86 /* Build: "result += a[i] * b[i];" */
87 gcc_jit_block_add_assignment_op (
90 GCC_JIT_BINARY_OP_PLUS
,
91 gcc_jit_context_new_binary_op (
93 GCC_JIT_BINARY_OP_MULT
,
95 gcc_jit_lvalue_as_rvalue (
96 gcc_jit_context_new_array_access (
98 gcc_jit_param_as_rvalue (param_a
),
99 gcc_jit_lvalue_as_rvalue (i
))),
100 gcc_jit_lvalue_as_rvalue (
101 gcc_jit_context_new_array_access (
103 gcc_jit_param_as_rvalue (param_b
),
104 gcc_jit_lvalue_as_rvalue (i
)))));
107 gcc_jit_block_add_assignment_op (
110 GCC_JIT_BINARY_OP_PLUS
,
111 gcc_jit_context_one (ctxt
, int_type
));
113 gcc_jit_block_end_with_jump (loop_body
, NULL
, loop_test
);
115 /* Build: "return result;" */
116 gcc_jit_block_end_with_return (
119 gcc_jit_lvalue_as_rvalue (result
));
123 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
125 typedef double (*my_dot_product_fn_type
) (int n
, double *a
, double *b
);
126 CHECK_NON_NULL (result
);
128 my_dot_product_fn_type my_dot_product
=
129 (my_dot_product_fn_type
)gcc_jit_result_get_code (result
,
131 CHECK_NON_NULL (my_dot_product
);
132 double test_array
[] = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
133 double val
= my_dot_product (10, test_array
, test_array
);
134 note ("my_dot_product returned: %f", val
);
135 CHECK_VALUE (val
, 385.0);