diff mbox series

libgccjit: Add missing builtins needed by optimizations

Message ID d4e0f36d10599dcbda88502b9631c3aae1119644.camel@zoho.com
State New
Headers show
Series libgccjit: Add missing builtins needed by optimizations | expand

Commit Message

Antoni Boucher Dec. 22, 2023, 2:39 p.m. UTC
Hi.
This patch adds missing builtins needed by optimizations.
Thanks for the review.

Comments

David Malcolm Jan. 9, 2024, 4:35 p.m. UTC | #1
On Fri, 2023-12-22 at 09:39 -0500, Antoni Boucher wrote:
> Hi.
> This patch adds missing builtins needed by optimizations.
> Thanks for the review.

The patch looks good to me.

Thanks!
Dave
Antoni Boucher Jan. 10, 2024, 10:02 p.m. UTC | #2
Just to make sure since we are in stage 4.
Does that mean I can merge it?

In general, how would I know if it's OK to merge?
If the patch is in the state Accepted on patchwork, does that mean it's
always OK to merge no matter the stage we're in?

On Tue, 2024-01-09 at 11:35 -0500, David Malcolm wrote:
> On Fri, 2023-12-22 at 09:39 -0500, Antoni Boucher wrote:
> > Hi.
> > This patch adds missing builtins needed by optimizations.
> > Thanks for the review.
> 
> The patch looks good to me.
> 
> Thanks!
> Dave
>
David Malcolm Jan. 10, 2024, 11:31 p.m. UTC | #3
On Wed, 2024-01-10 at 17:02 -0500, Antoni Boucher wrote:
> Just to make sure since we are in stage 4.

Are we?  I haven't seen an announcement, and it looked from
https://gcc.gnu.org/pipermail/gcc/2024-January/243117.html
that we might be pushing back the date for it.


> Does that mean I can merge it?

This one is very low risk, so please go ahead and merge it.

> 
> In general, how would I know if it's OK to merge?
> If the patch is in the state Accepted on patchwork, does that mean
> it's
> always OK to merge no matter the stage we're in?

AIUI, the status in patchwork is purely to help keep track of patch
reviews; whether or not a patch is suitable to push at a given time is
probably on a case-by-case basis (the key question being "how likely is
merging the patch going to cause problems")

Dave


> 
> On Tue, 2024-01-09 at 11:35 -0500, David Malcolm wrote:
> > On Fri, 2023-12-22 at 09:39 -0500, Antoni Boucher wrote:
> > > Hi.
> > > This patch adds missing builtins needed by optimizations.
> > > Thanks for the review.
> > 
> > The patch looks good to me.
> > 
> > Thanks!
> > Dave
> > 
>
diff mbox series

Patch

From 5ef20748a140d3384294a4218e6db7420cef692d Mon Sep 17 00:00:00 2001
From: Antoni Boucher <bouanto@zoho.com>
Date: Tue, 3 Jan 2023 15:04:41 -0500
Subject: [PATCH] libgccjit: Add missing builtins needed by optimizations

gcc/jit/ChangeLog:

	* jit-builtins.cc (ensure_optimization_builtins_exist): Add
	popcount builtins.

gcc/testsuite/ChangeLog:

	* jit.dg/all-non-failing-tests.h: New test.
	* jit.dg/test-popcount.c: New test.
---
 gcc/jit/jit-builtins.cc                      |  3 +
 gcc/testsuite/jit.dg/all-non-failing-tests.h | 10 +++
 gcc/testsuite/jit.dg/test-popcount.c         | 84 ++++++++++++++++++++
 3 files changed, 97 insertions(+)
 create mode 100644 gcc/testsuite/jit.dg/test-popcount.c

diff --git a/gcc/jit/jit-builtins.cc b/gcc/jit/jit-builtins.cc
index fdd0739789d..c84f2613f6a 100644
--- a/gcc/jit/jit-builtins.cc
+++ b/gcc/jit/jit-builtins.cc
@@ -609,6 +609,9 @@  builtins_manager::ensure_optimization_builtins_exist ()
      We can't loop through all of the builtin_data array, we don't
      support all types yet.  */
   (void)get_builtin_function_by_id (BUILT_IN_TRAP);
+  (void)get_builtin_function_by_id (BUILT_IN_POPCOUNT);
+  (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTL);
+  (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTLL);
 }
 
 /* Playback support.  */
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index e762563f9bd..b768c8977f0 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -268,6 +268,13 @@ 
 #undef create_code
 #undef verify_code
 
+/* test-popcount.c */
+#define create_code create_code_popcount
+#define verify_code verify_code_popcount
+#include "test-popcount.c"
+#undef create_code
+#undef verify_code
+
 /* test-pr103562.c: We don't add this one, since it touches
    the optimization level of the context as a whole.  */
 
@@ -488,6 +495,9 @@  const struct testcase testcases[] = {
   {"nested_loop",
    create_code_nested_loop,
    verify_code_nested_loop},
+  {"popcount",
+   create_code_popcount,
+   verify_code_popcount},
   {"pr66700_observing_write_through_ptr",
    create_code_pr66700_observing_write_through_ptr,
    verify_code_pr66700_observing_write_through_ptr},
diff --git a/gcc/testsuite/jit.dg/test-popcount.c b/gcc/testsuite/jit.dg/test-popcount.c
new file mode 100644
index 00000000000..6ad241fd2de
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-popcount.c
@@ -0,0 +1,84 @@ 
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.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:
+int
+popcount (unsigned int x)
+{
+  int i = 0;
+  while (x)
+    {
+      x &= x - 1;
+      ++i;
+    }
+  return i;
+}
+   */
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+  gcc_jit_type *uint_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT);
+
+  gcc_jit_param *param_x =
+    gcc_jit_context_new_param (
+      ctxt,
+      NULL,
+      uint_type, "x");
+  gcc_jit_param *params[1] = {param_x};
+  gcc_jit_function *func =
+    gcc_jit_context_new_function (ctxt,
+				  NULL,
+				  GCC_JIT_FUNCTION_EXPORTED,
+				  int_type,
+				  "popcount",
+				  1, params, 0);
+
+  gcc_jit_lvalue *x = gcc_jit_param_as_lvalue (param_x);
+  gcc_jit_rvalue *x_rvalue = gcc_jit_lvalue_as_rvalue (x);
+  gcc_jit_lvalue *i =
+    gcc_jit_function_new_local (func, NULL, int_type, "i");
+  gcc_jit_rvalue *zero = gcc_jit_context_zero (ctxt, int_type);
+
+  gcc_jit_block *initial =
+    gcc_jit_function_new_block (func, "initial");
+  gcc_jit_block *while_block =
+    gcc_jit_function_new_block (func, "while");
+
+  gcc_jit_block_add_assignment (initial, NULL, i, zero);
+  gcc_jit_block_end_with_jump (initial, NULL, while_block);
+
+  gcc_jit_block *after =
+    gcc_jit_function_new_block (func, "after");
+
+  gcc_jit_block *while_body =
+    gcc_jit_function_new_block (func, "while_body");
+  gcc_jit_rvalue *uzero = gcc_jit_context_zero (ctxt, uint_type);
+  gcc_jit_rvalue *cmp =
+    gcc_jit_context_new_comparison (ctxt, NULL, GCC_JIT_COMPARISON_NE, x_rvalue, uzero);
+  gcc_jit_block_end_with_conditional (while_block, NULL, cmp, while_body, after);
+
+  gcc_jit_rvalue *uone = gcc_jit_context_one (ctxt, uint_type);
+  gcc_jit_rvalue *sub = gcc_jit_context_new_binary_op (ctxt, NULL, GCC_JIT_BINARY_OP_MINUS, uint_type, x_rvalue, uone);
+  gcc_jit_block_add_assignment_op (while_body, NULL, x, GCC_JIT_BINARY_OP_BITWISE_AND, sub);
+
+  gcc_jit_rvalue *one = gcc_jit_context_one (ctxt, int_type);
+  gcc_jit_block_add_assignment_op (while_body, NULL, i, GCC_JIT_BINARY_OP_PLUS, one);
+  gcc_jit_block_end_with_jump (while_body, NULL, while_block);
+
+  gcc_jit_block_end_with_return(after, NULL, gcc_jit_lvalue_as_rvalue (i));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_NON_NULL (result);
+}
-- 
2.43.0