(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-extra-options.c
       1  /* Testcase for gcc_jit_context_add_command_line_option (PR jit/66628).  */
       2  #include <stdlib.h>
       3  #include <stdio.h>
       4  #include <string.h>
       5  
       6  #include "libgccjit.h"
       7  
       8  #include "harness.h"
       9  
      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
      12  #endif
      13  
      14  void
      15  create_code (gcc_jit_context *ctxt, void *user_data)
      16  {
      17    gcc_jit_context_add_command_line_option (ctxt, "-ffast-math");
      18    gcc_jit_context_add_command_line_option (ctxt, "-fverbose-asm");
      19  
      20    /* Let's try to inject the equivalent of:
      21  
      22  	double
      23  	my_dot_product (int n, double *a, double *b)
      24  	{
      25  	  double result = 0.;
      26  	  for (int i = 0; i < n; i++)
      27  	    result += a[i] * b[i];
      28  	  return result
      29  	}
      30  
      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);
      37  
      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,
      49  				  return_type,
      50  				  "my_dot_product",
      51  				  3, params, 0);
      52  
      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");
      57  
      58    /* Build: "double result = 0.;" */
      59    gcc_jit_lvalue *result =
      60      gcc_jit_function_new_local (func, NULL, val_type, "result");
      61  
      62    gcc_jit_block_add_assignment (initial, NULL,
      63      result, gcc_jit_context_zero (ctxt, val_type));
      64  
      65    /* Build: "for (int i = 0; i < n; i++)" */
      66    gcc_jit_lvalue *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));
      70  
      71    gcc_jit_block_end_with_jump (initial, NULL, loop_test);
      72  
      73    gcc_jit_block_end_with_conditional (
      74      loop_test, NULL,
      75  
      76      /* (i < n) */
      77      gcc_jit_context_new_comparison (
      78        ctxt, NULL,
      79        GCC_JIT_COMPARISON_LT,
      80        gcc_jit_lvalue_as_rvalue (i),
      81        gcc_jit_param_as_rvalue (param_n)),
      82  
      83      loop_body,
      84      final);
      85  
      86    /* Build: "result += a[i] * b[i];" */
      87    gcc_jit_block_add_assignment_op (
      88      loop_body, NULL,
      89      result,
      90      GCC_JIT_BINARY_OP_PLUS,
      91      gcc_jit_context_new_binary_op (
      92        ctxt, NULL,
      93        GCC_JIT_BINARY_OP_MULT,
      94        val_type,
      95        gcc_jit_lvalue_as_rvalue (
      96  	gcc_jit_context_new_array_access (
      97            ctxt, NULL,
      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 (
     102            ctxt, NULL,
     103  	  gcc_jit_param_as_rvalue (param_b),
     104  	  gcc_jit_lvalue_as_rvalue (i)))));
     105  
     106    /* Build: "i++" */
     107    gcc_jit_block_add_assignment_op (
     108      loop_body, NULL,
     109      i,
     110      GCC_JIT_BINARY_OP_PLUS,
     111      gcc_jit_context_one (ctxt, int_type));
     112  
     113    gcc_jit_block_end_with_jump (loop_body, NULL, loop_test);
     114  
     115    /* Build: "return result;" */
     116    gcc_jit_block_end_with_return (
     117      final,
     118      NULL,
     119      gcc_jit_lvalue_as_rvalue (result));
     120  }
     121  
     122  void
     123  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
     124  {
     125    typedef double (*my_dot_product_fn_type) (int n, double *a, double *b);
     126    CHECK_NON_NULL (result);
     127  
     128    my_dot_product_fn_type my_dot_product =
     129      (my_dot_product_fn_type)gcc_jit_result_get_code (result,
     130  						     "my_dot_product");
     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);
     136  }