public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: Basile Starynkevitch <basile@starynkevitch.net>
Cc: jit@gcc.gnu.org
Subject: Re: switches in GCC JIT?
Date: Thu, 01 Jan 2015 00:00:00 -0000	[thread overview]
Message-ID: <1435019126.13727.21.camel@surprise> (raw)
In-Reply-To: <558885E4.20706@starynkevitch.net>

On Tue, 2015-06-23 at 00:02 +0200, Basile Starynkevitch wrote:
> On 06/22/2015 11:42 PM, David Malcolm wrote:
> > On Mon, 2015-06-22 at 23:40 +0200, Basile Starynkevitch wrote:
> >> Hello David & all
> >>
> >> I'm guessing that GCCJIT is able to emit switch like statements, e.g.
> >> using GIMPLE_SWITCH statements
> >> internally.
> > No, it doesn't.
> >
> >> But I don't understand how is it possible. It looks like
> >> gimple_build_switch does not occur in gcc/jit/
> > Currently gcc/jit builds functions at the tree level and hands them off
> > to the gimplifier, so you wouldn't see that in any case.  (in theory it
> > could be ported to directly generate gimple).
> >
> >> Are switch statements omitted from GCCJIT?
> > Yes.
> >
> >> If yes, why???
> > I intentionally didn't implement them, to keep the API simpler.
> >
> > I've never run into a need for them when implementing jit-compilation,
> > and in theory they could be implemented using conditionals (albeit
> > without the nice optimizations that we have for lowering GIMPLE_SWITCH).
> >
> > There was some discussion about this here:
> >   https://gcc.gnu.org/ml/jit/2014-q4/msg00116.html
> >
> >> David, do you intend to improve that?
> > Do you have a use-case for them?  We can add them if we need them.
> 
> Any language (MELT, Ocaml, Haskell, ....) having some pattern matching 
> would use a lot of switches.
> Or most efficient implementations of Rete algorithm, or similar stuff 
> when compiling Prolog-like or CLIPS-like rules.
> 
> Also, translation of most finite state automatons is done by a switch 
> (often a quite big one, with one case per each state).
> 
> At last, any kind of "byte-code" interpreter uses switches.
> 
> And many languages have a switch like construct, that would be trivial 
> to translate to a GIMPLE_SWITCH, but painful to translate otherwise.
> 
> 
> All the bytecodes I know (e.g. JVM & Ocaml) have switch-like constructs, 
> and it would be easy to translate them to a GIMPLE_SWITCH, and painful 
> otherwise.

As it happens, none of the bytecode languages I've implemented so far
have switch-like constructs.

But I see now that the JVM has opcodes "lookupswitch" and "tableswitch";
those alone make a compelling case for libgccjit supporting switches.

I'm working on it now; the API I'm thinking of looks like this:

extern void
gcc_jit_block_end_with_switch (gcc_jit_block *block,
			          gcc_jit_location *loc,
			          gcc_jit_rvalue *expr,
			          gcc_jit_block *default_block,
			          int num_cases,
			          gcc_jit_rvalue **case_min_values,
			          gcc_jit_rvalue **case_max_values,
			          gcc_jit_block **case_blocks);


thus supporting ranged cases, whilst also allowing individual values, by
simply passing in the same table for both case_min_values and
case_max_values.

> BTW, if we don't have switches, we should at least have indirect jumps, 
> and the ability to retrieve, as a label, the starting address of any 
> basic block. (i.e. the equivalent of goto *ptr; and of &&label in C). If 
> that is possible today, we need more documentation about that (at least 
> saying that switch statements could be translated that way)
> 
> And of course, leveraging on all the important optimizations done by GCC 
> on GIMPLE_SWITCH is essential...
> 
> Actually, I'm surprised you are asking what is the use case for 
> switches. I feel they are obvious and numerous... I would be annoyed, 
> e.g. if I could not use switches in my C++ or C code. Replacing a switch 
> with a sequence of if is an annoyance, and is probably a major 
> performance loss (unless GCC optimizations are clever enough to replace 
> them with a switch; which might sometimes be true, but not always).


  reply	other threads:[~2015-06-23  0:33 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-01  0:00 Basile Starynkevitch
2015-01-01  0:00 ` David Malcolm
2015-01-01  0:00   ` Basile Starynkevitch
2015-01-01  0:00     ` David Malcolm [this message]
2015-01-01  0:00       ` David Malcolm
2015-01-01  0:00       ` Basile Starynkevitch
2015-01-01  0:00       ` Dibyendu Majumdar
2015-01-01  0:00         ` David Malcolm
2015-01-01  0:00           ` Basile Starynkevitch
2015-01-01  0:00             ` [PATCH 1/2] Add gcc/typed-splay-tree.h David Malcolm
2015-01-01  0:00               ` [PATCH 2/2] jit: add switch statements David Malcolm
2015-01-01  0:00                 ` Basile Starynkevitch
2015-01-01  0:00                   ` David Malcolm
2015-01-01  0:00                 ` [PATCH, committed] " David Malcolm
2015-01-01  0:00                 ` [PATCH 2/2] " Jeff Law
2015-01-01  0:00                   ` Managing ABI compatibility (was Re: [PATCH 2/2] jit: add switch statements) David Malcolm
2015-01-01  0:00                     ` David Malcolm
2015-01-01  0:00                 ` [PATCH 2/2] jit: add switch statements Basile Starynkevitch
2015-01-01  0:00                   ` David Malcolm
2015-01-01  0:00               ` [PATCH 1/2] Add gcc/typed-splay-tree.h Jeff Law
2015-01-01  0:00                 ` David Malcolm
2015-01-01  0:00                   ` Jeff Law
2015-01-01  0:00           ` switches in GCC JIT? Dibyendu Majumdar

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=1435019126.13727.21.camel@surprise \
    --to=dmalcolm@redhat.com \
    --cc=basile@starynkevitch.net \
    --cc=jit@gcc.gnu.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.
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).