ocaml-flambda / flambda-backend

The Flambda backend project for OCaml
103 stars 73 forks source link

Generate branch free code for some simple variant matches #2460

Open mshinwell opened 5 months ago

mshinwell commented 5 months ago

It would be nice to compile all of the following so they don't involve branches:

type t1 = A of int | B of int

let test1 t =
  match t with
  | A _ -> true
  | B _ -> false

type t2 = C of int | D

let test2 t =
  match t with
  | C _ -> true
  | D -> false

type t3 = E | F

let test3 t =
  match t with
  | E -> true
  | F -> false
lthls commented 4 months ago

@chambart suggests introducing a Select primitive, that could be used instead of a switch where all branches go to the same single-argument continuation. This primitive could then be compiled into branch-less code if it has the right shape (either in Simplify or in To_cmm).