unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Pip Cet <pipcet@gmail.com>
To: Andrea Corallo <akrl@sdf.org>
Cc: 46670@debbugs.gnu.org, Mauricio Collares <mauricio@collares.org>
Subject: bug#46670: 28.0.50; [feature/native-comp] possible miscompilation affecting lsp-mode
Date: Wed, 24 Feb 2021 09:28:39 +0000	[thread overview]
Message-ID: <CAOqdjBfw0XpoDwLBQUyHbvb2m8is_q94OSWohuKeJmkVE9-A=g@mail.gmail.com> (raw)
In-Reply-To: <xjf35xlkfax.fsf@sdf.org>

[-- Attachment #1: Type: text/plain, Size: 952 bytes --]

On Wed, Feb 24, 2021 at 9:04 AM Andrea Corallo <akrl@sdf.org> wrote:
> Pip Cet <pipcet@gmail.com> writes:
>
> > On Tue, Feb 23, 2021 at 11:36 PM Andrea Corallo <akrl@sdf.org> wrote:
> >> Pip Cet <pipcet@gmail.com> writes:
> >> > Is this one of them, or am I confused?
> >>
> >> What's suspitions with that?  At present I'm admittedly quite done but
> >> it looks okay to me.
> >
> > We're emitting
> >
> > (assume ,lhs (and ,lhs ,rhs))
> >
> > even when NEGATED is t.
>
> Nope, when NEGATED is t the complete sequence we are emitting is (see
> line just following your diff hunk):
>
> (assume tmp-mvar (not rhs))

But tmp-mvar is in the same slot as RHS.

> (assume lhs (and lhs tmp-mvar))

So this is equivalent (after the next SSA rename) to

(assume lhs (and lhs rhs))

> That's indeed the reason why it's working in the 39 testcases.

No, the reason it's "working" is that we never assert our assumes.
I've got a patch here that does just that :-)

[-- Attachment #2: 0001-Assert-at-runtime-that-assume-s-assumptions-actually.patch --]
[-- Type: text/x-patch, Size: 5583 bytes --]

From 6470b5ef25007ece8fea5754bee204e6b5ba0312 Mon Sep 17 00:00:00 2001
From: Pip Cet <pipcet@gmail.com>
Date: Wed, 24 Feb 2021 09:25:44 +0000
Subject: [PATCH] Assert at runtime that assume's assumptions actually hold.

* src/comp.c (emit_limple_insn): Handle (assert) insns.
(Fcomp__assert): New function.
(syms_of_comp): New defsubr for above.

* lisp/emacs-lisp/comp.el (comp-passes): Add `comp-assert-assumes'
pass.
(comp-emit-assert, comp-assert-assumes-func, comp-assert-assumes):
New functions.
---
 lisp/emacs-lisp/comp.el | 40 ++++++++++++++++++++++++
 src/comp.c              | 69 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+)

diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el
index 60c040926e54c..d53b8876aa8ad 100644
--- a/lisp/emacs-lisp/comp.el
+++ b/lisp/emacs-lisp/comp.el
@@ -178,6 +178,7 @@ comp-passes
                         comp-tco
                         comp-fwprop
                         comp-remove-type-hints
+                        comp-assert-assumes
                         comp-final)
   "Passes to be executed in order.")
 
@@ -3385,6 +3386,45 @@ comp-remove-type-hints
            (comp-ctxt-funcs-h comp-ctxt)))
 
 \f
+;;; Assert-assumes pass specific code.
+(defun comp-emit-assert (lhs assertion)
+  (let (insns)
+    (pcase assertion
+      ((and (pred comp-mvar-p))
+       (let ((f `(lambda (x y) (equal x y))))
+         (comp-add-const-to-relocs f)
+         (push `(assert ,f ,lhs ,assertion) insns)))
+      ((and (pred comp-cstr-p)))
+      (`(and . ,operands)
+       (dolist (op operands)
+         (setq insns (nconc insns (comp-emit-assert lhs op)))))
+      (`(not ,(and (pred comp-mvar-p) operand))
+       (let ((f `(lambda (x y) (not (equal x y)))))
+         (comp-add-const-to-relocs f)
+         (push `(assert ,f ,lhs ,assertion) insns))))
+    insns))
+
+(defun comp-assert-assumes-func ()
+  (cl-loop
+   for b being each hash-value of (comp-func-blocks comp-func)
+   do (comp-loop-insn-in-block b
+        (pcase insn
+          (`(assume ,(and (pred comp-mvar-p) lhs)
+                    ,rhs)
+           (let ((f `(lambda (x y) (not (equal x y)))))
+             (comp-add-const-to-relocs f)
+             (setf (cdr insn-cell)
+                   (nconc (comp-emit-assert lhs rhs)
+                          (cdr insn-cell)))))))))
+
+(defun comp-assert-assumes (_)
+  "Turn (assume ...) insns into asserts."
+  (maphash (lambda (_ f)
+             (let ((comp-func f))
+               (comp-assert-assumes-func)
+               (comp-log-func comp-func 3)))
+           (comp-ctxt-funcs-h comp-ctxt)))
+\f
 ;;; Final pass specific code.
 
 (defun comp-args-to-lambda-list (args)
diff --git a/src/comp.c b/src/comp.c
index a8b8ef95fa14d..2a5eebee104ed 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -2037,6 +2037,58 @@ emit_limple_insn (Lisp_Object insn)
 			       n);
       emit_cond_jump (test, target1, target2);
     }
+  else if (EQ (op, Qassert))
+    {
+      Lisp_Object callee = Qcomp__assert;
+      emit_comment (SSDATA (Fprin1_to_string (arg[0], Qnil)));
+      imm_reloc_t reloc = obj_to_reloc (arg[0]);
+      gcc_jit_rvalue *assert_args[i];
+      assert_args[0] = gcc_jit_lvalue_as_rvalue (
+	gcc_jit_context_new_array_access (comp.ctxt,
+					  NULL,
+					  reloc.array.r_val,
+					  reloc.idx));
+      for (ptrdiff_t j = 1; j < i; j++)
+	assert_args[j] = emit_mvar_rval (arg[j]);
+
+      gcc_jit_lvalue *tmp_arr =
+	gcc_jit_function_new_local (
+				    comp.func,
+				    NULL,
+				    gcc_jit_context_new_array_type
+				    (comp.ctxt,
+				     NULL,
+				     comp.lisp_obj_type,
+				     i),
+				    "local_%d");
+
+      for (ptrdiff_t j = 0; j < i; j++)
+	{
+	  gcc_jit_block_add_assignment (
+					comp.block,
+					NULL,
+					gcc_jit_context_new_array_access (
+									  comp.ctxt,
+									  NULL,
+									  gcc_jit_lvalue_as_rvalue (tmp_arr),
+									  gcc_jit_context_new_rvalue_from_int (comp.ctxt,
+													       comp.int_type,
+													       j)),
+					assert_args[j]);
+	}
+
+      gcc_jit_rvalue *call = emit_call_ref (callee,
+		     i,
+		     gcc_jit_context_new_array_access (comp.ctxt,
+						       NULL,
+						       gcc_jit_lvalue_as_rvalue (tmp_arr),
+						       comp.zero),
+		     false);
+
+      gcc_jit_block_add_eval (comp.block,
+			      NULL,
+			      call);
+    }
   else if (EQ (op, Qphi) || EQ (op, Qassume))
     {
       /* Nothing to do for phis or assumes in the backend.  */
@@ -4980,6 +5032,20 @@ DEFUN ("comp--late-register-subr", Fcomp__late_register_subr,
   return Qnil;
 }
 
+DEFUN ("comp--assert", Fcomp__assert, Scomp__assert, 1, MANY, 0,
+       doc: /* */)
+  (ptrdiff_t nargs, Lisp_Object *args)
+{
+  if (NILP (Ffuncall (nargs, args)))
+    {
+      xsignal1 (Qnative_compiler_error,
+		Fcons (build_string ("assertion failed"),
+		       Flist (nargs, args)));
+    }
+
+  return Qnil;
+}
+
 static bool
 file_in_eln_sys_dir (Lisp_Object filename)
 {
@@ -5076,6 +5142,8 @@ syms_of_comp (void)
   DEFSYM (Qdirect_call, "direct-call");
   DEFSYM (Qdirect_callref, "direct-callref");
   DEFSYM (Qassume, "assume");
+  DEFSYM (Qassert, "assert");
+  DEFSYM (Qcomp__assert, "comp--assert");
   DEFSYM (Qsetimm, "setimm");
   DEFSYM (Qreturn, "return");
   DEFSYM (Qunreachable, "unreachable");
@@ -5179,6 +5247,7 @@ syms_of_comp (void)
   defsubr (&Scomp__register_lambda);
   defsubr (&Scomp__register_subr);
   defsubr (&Scomp__late_register_subr);
+  defsubr (&Scomp__assert);
   defsubr (&Snative_elisp_load);
 
   staticpro (&comp.exported_funcs_h);
-- 
2.30.0


  reply	other threads:[~2021-02-24  9:28 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-21  0:12 bug#46670: 28.0.50; [feature/native-comp] possible miscompilation affecting lsp-mode Mauricio Collares
2021-02-21 11:51 ` Pip Cet
2021-02-21 11:56   ` Pip Cet
2021-02-21 21:03     ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-21 22:46       ` Pip Cet
2021-02-22  9:37         ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-22 10:04           ` Pip Cet
2021-02-22 10:25             ` Pip Cet
2021-02-22 11:23               ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-22 12:11                 ` Pip Cet
2021-02-22 13:12                   ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-23  7:59                     ` Pip Cet
2021-02-23  9:04                       ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-23 23:26                         ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-24  2:10                           ` Mauricio Collares
2021-02-24  8:22                             ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-23 19:09                     ` Pip Cet
2021-02-23 23:36                       ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-24  4:31                         ` Pip Cet
2021-02-24  9:04                           ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-24  9:28                             ` Pip Cet [this message]
2021-02-24  9:42                               ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-24  9:46                                 ` Pip Cet
2021-02-24 10:06                                   ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-25 12:41                                     ` Pip Cet
2021-02-25 14:58                                       ` Eli Zaretskii
2021-02-25 15:14                                         ` Pip Cet
2021-02-25 15:31                                           ` Eli Zaretskii
2021-02-25 16:56                                       ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-25 20:59                                         ` Pip Cet
2021-02-26 19:33                                           ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-26 20:30                                             ` Pip Cet
2021-02-26 20:44                                               ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-26 20:11                                           ` Eli Zaretskii
2021-02-26 20:32                                             ` Pip Cet
2021-02-27  5:06                                             ` Pip Cet
2021-02-27  7:49                                               ` Eli Zaretskii
2021-02-27  9:39                                                 ` Pip Cet
2021-02-27 10:24                                                   ` Eli Zaretskii
2021-02-27 12:39                                                     ` Pip Cet
2021-02-27 13:30                                                       ` Eli Zaretskii
2021-02-27 17:15                                                         ` Pip Cet
2021-02-27 18:40                                                           ` Eli Zaretskii
2021-02-28  8:14                                                             ` Pip Cet
2021-03-01  5:24                                                               ` Richard Stallman
2021-03-01  6:40                                                                 ` Pip Cet
2021-02-22 11:16             ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-23  9:07               ` Pip Cet
2021-02-23 22:55                 ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-24  7:00                   ` Pip Cet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOqdjBfw0XpoDwLBQUyHbvb2m8is_q94OSWohuKeJmkVE9-A=g@mail.gmail.com' \
    --to=pipcet@gmail.com \
    --cc=46670@debbugs.gnu.org \
    --cc=akrl@sdf.org \
    --cc=mauricio@collares.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).