test-dot-product.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "libgccjit.h"
  5. #include "harness.h"
  6. void
  7. create_code (gcc_jit_context *ctxt, void *user_data)
  8. {
  9. /* Let's try to inject the equivalent of:
  10. double
  11. my_dot_product (int n, double *a, double *b)
  12. {
  13. double result = 0.;
  14. for (int i = 0; i < n; i++)
  15. result += a[i] * b[i];
  16. return result
  17. }
  18. and see what the optimizer can do. */
  19. gcc_jit_type *val_type =
  20. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);
  21. gcc_jit_type *ptr_type = gcc_jit_type_get_pointer (val_type);
  22. gcc_jit_type *int_type =
  23. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  24. gcc_jit_type *return_type = val_type;
  25. gcc_jit_param *param_n =
  26. gcc_jit_context_new_param (ctxt, NULL, int_type, "n");
  27. gcc_jit_param *param_a =
  28. gcc_jit_context_new_param (ctxt, NULL, ptr_type, "a");
  29. gcc_jit_param *param_b =
  30. gcc_jit_context_new_param (ctxt, NULL, ptr_type, "b");
  31. gcc_jit_param *params[3] = {param_n, param_a, param_b};
  32. gcc_jit_function *func =
  33. gcc_jit_context_new_function (ctxt, NULL,
  34. GCC_JIT_FUNCTION_EXPORTED,
  35. return_type,
  36. "my_dot_product",
  37. 3, params, 0);
  38. gcc_jit_block *initial = gcc_jit_function_new_block (func, "initial");
  39. gcc_jit_block *loop_test = gcc_jit_function_new_block (func, "loop_test");
  40. gcc_jit_block *loop_body = gcc_jit_function_new_block (func, "loop_body");
  41. gcc_jit_block *final = gcc_jit_function_new_block (func, "final");
  42. /* Build: "double result = 0.;" */
  43. gcc_jit_lvalue *result =
  44. gcc_jit_function_new_local (func, NULL, val_type, "result");
  45. gcc_jit_block_add_assignment (initial, NULL,
  46. result, gcc_jit_context_zero (ctxt, val_type));
  47. /* Build: "for (int i = 0; i < n; i++)" */
  48. gcc_jit_lvalue *i =
  49. gcc_jit_function_new_local (func, NULL, int_type, "i");
  50. gcc_jit_block_add_assignment (initial, NULL,
  51. i, gcc_jit_context_zero (ctxt, int_type));
  52. gcc_jit_block_end_with_jump (initial, NULL, loop_test);
  53. gcc_jit_block_end_with_conditional (
  54. loop_test, NULL,
  55. /* (i < n) */
  56. gcc_jit_context_new_comparison (
  57. ctxt, NULL,
  58. GCC_JIT_COMPARISON_LT,
  59. gcc_jit_lvalue_as_rvalue (i),
  60. gcc_jit_param_as_rvalue (param_n)),
  61. loop_body,
  62. final);
  63. /* Build: "result += a[i] * b[i];" */
  64. gcc_jit_block_add_assignment_op (
  65. loop_body, NULL,
  66. result,
  67. GCC_JIT_BINARY_OP_PLUS,
  68. gcc_jit_context_new_binary_op (
  69. ctxt, NULL,
  70. GCC_JIT_BINARY_OP_MULT,
  71. val_type,
  72. gcc_jit_lvalue_as_rvalue (
  73. gcc_jit_context_new_array_access (
  74. ctxt, NULL,
  75. gcc_jit_param_as_rvalue (param_a),
  76. gcc_jit_lvalue_as_rvalue (i))),
  77. gcc_jit_lvalue_as_rvalue (
  78. gcc_jit_context_new_array_access (
  79. ctxt, NULL,
  80. gcc_jit_param_as_rvalue (param_b),
  81. gcc_jit_lvalue_as_rvalue (i)))));
  82. /* Build: "i++" */
  83. gcc_jit_block_add_assignment_op (
  84. loop_body, NULL,
  85. i,
  86. GCC_JIT_BINARY_OP_PLUS,
  87. gcc_jit_context_one (ctxt, int_type));
  88. gcc_jit_block_end_with_jump (loop_body, NULL, loop_test);
  89. /* Build: "return result;" */
  90. gcc_jit_block_end_with_return (
  91. final,
  92. NULL,
  93. gcc_jit_lvalue_as_rvalue (result));
  94. }
  95. void
  96. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  97. {
  98. typedef double (*my_dot_product_fn_type) (int n, double *a, double *b);
  99. CHECK_NON_NULL (result);
  100. my_dot_product_fn_type my_dot_product =
  101. (my_dot_product_fn_type)gcc_jit_result_get_code (result,
  102. "my_dot_product");
  103. CHECK_NON_NULL (my_dot_product);
  104. double test_array[] = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
  105. double val = my_dot_product (10, test_array, test_array);
  106. note ("my_dot_product returned: %f", val);
  107. CHECK_VALUE (val, 385.0);
  108. }