caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Reed Wilson <cedilla@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] flambda seemingly missing easy optimization
Date: Tue, 29 Mar 2016 16:06:59 -0700	[thread overview]
Message-ID: <CALLFq5RC_=7EnyRap=P8GVwpQJVO1DnTZUh23Gf05NPeiCVKrw@mail.gmail.com> (raw)

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

Hi list,

I've been playing around with the flambda build of 4.03.0+dev16, and I
found some seemingly useless code in the generated assembly and cmm tree
that is not returned without flambda.

I made a small function to demonstrate it:
let compare_str_sub p s s_off len =
if s_off < 0 || s_off + len > String.length s
then invalid_arg "Don't do that!";
else p = s

The non-flambda cmm is:
(function camlTest__compare_str_sub_1199
     (p/1200: val s/1201: val s_off/1202: val len/1203: val)
 (catch
   (if (< s_off/1202 1) (exit 2)
     (if
       (> (+ (+ s_off/1202 len/1203) -1)
         (+
           (<<
             (let tmp/1206 (- (<< (>>u (load int (+a s/1201 -8)) 10) 3) 1)
               (- tmp/1206 (load unsigned int8 (+a s/1201 tmp/1206))))
             1)
           1))
       (exit 2) (extcall "caml_string_equal" p/1200 s/1201 val)))
 with(2) (app "camlPervasives__invalid_arg_1007" "camlTest__1" val)))

flambda results in:
(function camlTest__compare_str_sub_4
     (p_9/1210: val s_8/1209: val s_off_7/1208: val len_6/1207: val)
 (catch
   (catch
     (if (< s_off_7/1208 1) (if (!= 3 1) (exit 2) (exit 3))
       (if
         (!=
           (let
             Pintcomp_arg_15/1217
               (+
                 (<<
                   (let
                     tmp/1221
                       (- (<< (>>u (load int (+a s_8/1209 -8)) 10) 3) 1)
                     (- tmp/1221 (load unsigned int8 (+a s_8/1209
tmp/1221))))
                   1)
                 1)
             (+
               (<<
                 (> (+ (+ s_off_7/1208 len_6/1207) -1) Pintcomp_arg_15/1217)
                 1)
               1))
           1)
         (exit 2) (exit 3)))
   with(3) (extcall "caml_string_equal" p_9/1210 s_8/1209 val))
 with(2)
   (app{test.ml:18,52-90} "camlPervasives__invalid_arg_279"
     "camlTest__apply_arg_31" val)))

The odd code is toward the beginning: (if (!= 3 1) (exit 2) (exit 3))
I don't know a lot about cmm code, but it looks like something the compiler
should be able to optimize better. Fiddling with the flambda optimization
options doesn't seem to remove it.

Is this just due to how new flambda is, or is there some other reason that
code makes it through?

Thanks,
Reed Wlison

-- 
ç

[-- Attachment #2: Type: text/html, Size: 3553 bytes --]

             reply	other threads:[~2016-03-29 23:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-29 23:06 Reed Wilson [this message]
2016-03-30  9:51 ` [Caml-list] <DKIM> " Pierre Chambart
2016-03-31 15:57   ` Reed Wilson
2016-03-31 19:29     ` Gabriel Scherer

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

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

  git send-email \
    --in-reply-to='CALLFq5RC_=7EnyRap=P8GVwpQJVO1DnTZUh23Gf05NPeiCVKrw@mail.gmail.com' \
    --to=cedilla@gmail.com \
    --cc=caml-list@inria.fr \
    /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.
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 NNTP newsgroup(s).