This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[jit] New test case: test-nested-loops.c


Committed to branch dmalcolm/jit:

gcc/testsuite/ChangeLog.jit:
	* jit.dg/test-nested-loops.c: New test case.
	* jit.dg/all-non-failing-tests.h: Add test-nested-loops.c.
	* jit.dg/test-combination.c (create_code): Likewise.
	(verify_code): Likewise.
	* jit.dg/test-threads.c (const): Likewise.
---
 gcc/testsuite/jit.dg/all-non-failing-tests.h |   7 ++
 gcc/testsuite/jit.dg/test-combination.c      |   2 +
 gcc/testsuite/jit.dg/test-nested-loops.c     | 179 +++++++++++++++++++++++++++
 gcc/testsuite/jit.dg/test-threads.c          |   3 +
 4 files changed, 191 insertions(+)
 create mode 100644 gcc/testsuite/jit.dg/test-nested-loops.c

diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index 54dacb7..5f7b2ec 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -109,6 +109,13 @@
 #undef create_code
 #undef verify_code
 
+/* test-nested-loops.c */
+#define create_code create_code_nested_loop
+#define verify_code verify_code_nested_loop
+#include "test-nested-loops.c"
+#undef create_code
+#undef verify_code
+
 /* test-reading-struct.c */
 #define create_code create_code_reading_struct
 #define verify_code verify_code_reading_struct
diff --git a/gcc/testsuite/jit.dg/test-combination.c b/gcc/testsuite/jit.dg/test-combination.c
index 1b9432b..9d3a535 100644
--- a/gcc/testsuite/jit.dg/test-combination.c
+++ b/gcc/testsuite/jit.dg/test-combination.c
@@ -29,6 +29,7 @@ create_code (gcc_jit_context *ctxt, void * user_data)
   create_code_hello_world (ctxt, user_data);
   create_code_linked_list (ctxt, user_data);
   create_code_quadratic (ctxt, user_data);
+  create_code_nested_loop (ctxt, user_data);
   create_code_reading_struct  (ctxt, user_data);
   create_code_string_literal (ctxt, user_data);
   create_code_sum_of_squares (ctxt, user_data);
@@ -54,6 +55,7 @@ verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
   verify_code_hello_world (ctxt, result);
   verify_code_linked_list (ctxt, result);
   verify_code_quadratic (ctxt, result);
+  verify_code_nested_loop (ctxt, result);
   verify_code_reading_struct (ctxt, result);
   verify_code_string_literal (ctxt, result);
   verify_code_sum_of_squares (ctxt, result);
diff --git a/gcc/testsuite/jit.dg/test-nested-loops.c b/gcc/testsuite/jit.dg/test-nested-loops.c
new file mode 100644
index 0000000..a4cda68
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-nested-loops.c
@@ -0,0 +1,179 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libgccjit.h"
+
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Let's try to inject the equivalent of:
+
+	double
+	test_nested_loops (int n, double *a, double *b)
+	{
+	  double result = 0.;
+	  for (int i = 0; i < n; i++)
+	    for (int j = 0; j < n; j++)
+	      result += a[i] * b[j];
+	  return result
+	}
+  */
+  gcc_jit_type *val_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);
+  gcc_jit_type *ptr_type = gcc_jit_type_get_pointer (val_type);
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+
+  gcc_jit_type *return_type = val_type;
+  gcc_jit_param *param_n =
+    gcc_jit_context_new_param (ctxt, NULL, int_type, "n");
+  gcc_jit_param *param_a =
+    gcc_jit_context_new_param (ctxt, NULL, ptr_type, "a");
+  gcc_jit_param *param_b =
+    gcc_jit_context_new_param (ctxt, NULL, ptr_type, "b");
+  gcc_jit_param *params[3] = {param_n, param_a, param_b};
+  gcc_jit_function *func =
+    gcc_jit_context_new_function (ctxt, NULL,
+				  GCC_JIT_FUNCTION_EXPORTED,
+				  return_type,
+				  "test_nested_loops",
+				  3, params, 0);
+
+  /* Create locals. */
+  gcc_jit_lvalue *result =
+    gcc_jit_function_new_local (func, NULL, val_type, "result");
+  gcc_jit_lvalue *i =
+    gcc_jit_function_new_local (func, NULL, int_type, "i");
+  gcc_jit_lvalue *j =
+    gcc_jit_function_new_local (func, NULL, int_type, "j");
+
+  /* Create basic blocks. */
+  gcc_jit_block *b_entry =
+    gcc_jit_function_new_block (func, "b_entry");
+  gcc_jit_block *b_outer_loop_cond =
+    gcc_jit_function_new_block (func, "b_outer_loop_cond");
+  gcc_jit_block *b_outer_loop_head =
+    gcc_jit_function_new_block (func, "b_outer_loop_head");
+  gcc_jit_block *b_outer_loop_tail =
+    gcc_jit_function_new_block (func, "b_outer_loop_tail");
+  gcc_jit_block *b_inner_loop_cond =
+    gcc_jit_function_new_block (func, "b_inner_loop_cond");
+  gcc_jit_block *b_inner_loop_body =
+    gcc_jit_function_new_block (func, "b_inner_loop_body");
+  gcc_jit_block *b_exit =
+    gcc_jit_function_new_block (func, "b_exit");
+
+
+  /* Populate b_entry. */
+
+  /* "result = 0.;" */
+  gcc_jit_block_add_assignment (
+    b_entry, NULL,
+    result,
+    gcc_jit_context_zero (ctxt, val_type));
+  /* "i = 0;" */
+  gcc_jit_block_add_assignment (
+    b_entry, NULL,
+    i,
+    gcc_jit_context_zero (ctxt, int_type));
+  gcc_jit_block_end_with_jump (b_entry, NULL, b_outer_loop_cond);
+
+  /* Populate b_outer_loop_cond. */
+  gcc_jit_block_end_with_conditional (
+    b_outer_loop_cond,
+    NULL,
+    /* (i < n) */
+    gcc_jit_context_new_comparison (
+      ctxt, NULL,
+      GCC_JIT_COMPARISON_LT,
+      gcc_jit_lvalue_as_rvalue (i),
+      gcc_jit_param_as_rvalue (param_n)),
+    b_outer_loop_head,
+    b_exit);
+
+  /* Populate b_outer_loop_head. */
+  /* j = 0; */
+  gcc_jit_block_add_assignment (
+    b_outer_loop_head, NULL,
+    j,
+    gcc_jit_context_zero (ctxt, int_type));
+  gcc_jit_block_end_with_jump (b_outer_loop_head, NULL, b_inner_loop_cond);
+
+  /* Populate b_inner_loop_cond. */
+  gcc_jit_block_end_with_conditional (
+    b_inner_loop_cond,
+    NULL,
+    /* (j < n) */
+    gcc_jit_context_new_comparison (
+      ctxt, NULL,
+      GCC_JIT_COMPARISON_LT,
+      gcc_jit_lvalue_as_rvalue (j),
+      gcc_jit_param_as_rvalue (param_n)),
+    b_inner_loop_body,
+    b_outer_loop_tail);
+
+  /* Populate b_inner_loop_body. */
+  /* "result += a[i] * b[j];" */
+  gcc_jit_block_add_assignment_op (
+    b_inner_loop_body, NULL,
+    result,
+    GCC_JIT_BINARY_OP_PLUS,
+    gcc_jit_context_new_binary_op (
+      ctxt, NULL,
+      GCC_JIT_BINARY_OP_MULT,
+      val_type,
+      gcc_jit_lvalue_as_rvalue (
+        gcc_jit_context_new_array_access(
+          ctxt, NULL,
+          gcc_jit_param_as_rvalue (param_a),
+          gcc_jit_lvalue_as_rvalue (i))),
+      gcc_jit_lvalue_as_rvalue (
+        gcc_jit_context_new_array_access(
+          ctxt, NULL,
+          gcc_jit_param_as_rvalue (param_b),
+          gcc_jit_lvalue_as_rvalue (j)))));
+  /* "j++" */
+  gcc_jit_block_add_assignment_op (
+    b_inner_loop_body, NULL,
+    j,
+    GCC_JIT_BINARY_OP_PLUS,
+    gcc_jit_context_one (ctxt, int_type));
+
+  gcc_jit_block_end_with_jump (b_inner_loop_body, NULL, b_inner_loop_cond);
+
+  /* Populate b_outer_loop_tail. */
+  /* "i++" */
+  gcc_jit_block_add_assignment_op (
+    b_outer_loop_tail, NULL,
+    i,
+    GCC_JIT_BINARY_OP_PLUS,
+    gcc_jit_context_one (ctxt, int_type));
+  gcc_jit_block_end_with_jump (b_outer_loop_tail, NULL, b_outer_loop_cond);
+
+  /* Populate b_exit. */
+  /* "return result;" */
+  gcc_jit_block_end_with_return (
+    b_exit,
+    NULL,
+    gcc_jit_lvalue_as_rvalue (result));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  typedef double (*test_nested_loops_fn_type) (int n, double *a, double *b);
+  CHECK_NON_NULL (result);
+
+  test_nested_loops_fn_type test_nested_loops =
+    (test_nested_loops_fn_type)gcc_jit_result_get_code (result,
+						     "test_nested_loops");
+  CHECK_NON_NULL (test_nested_loops);
+  double test_a[] = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
+  double test_b[] = {5., 6., 7., 8., 9., 10., 1., 2., 3., 4.};
+  double val = test_nested_loops (10, test_a, test_b);
+  printf("test_nested_loops returned: %f\n", val);
+  CHECK_VALUE (val, 3025.0);
+}
diff --git a/gcc/testsuite/jit.dg/test-threads.c b/gcc/testsuite/jit.dg/test-threads.c
index f31d094..e3dd69f 100644
--- a/gcc/testsuite/jit.dg/test-threads.c
+++ b/gcc/testsuite/jit.dg/test-threads.c
@@ -127,6 +127,9 @@ const struct testcase testcases[] = {
   {"quadratic",
    create_code_quadratic,
    verify_code_quadratic},
+  {"nested_loop",
+   create_code_nested_loop,
+   verify_code_nested_loop},
   {"reading_struct ",
    create_code_reading_struct ,
    verify_code_reading_struct },
-- 
1.8.5.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]