beaver-lodge / beaver

MLIR Toolkit in Elixir and Zig.
MIT License
162 stars 8 forks source link

Prototype Elixir AST dialect #229

Closed jackalcooper closed 11 months ago

jackalcooper commented 11 months ago

Source

defmodule TwoFuncMod do
  def add_two_literal() do
    a = 1 + 2
    b = a + 3
    b
  end

  def main() do
    add_two_literal()
  end
end

defmodule OneFuncMod do
  def add_two_literal() do
    a = 1 + 2
    b = a + 3
    b
  end
end

IR generated

"builtin.module"() ({
  "ex.mod"() ({
    "ex.func"() ({
      %0 = "ex.lit_int"() {value = 1 : i64} : () -> i64
      %1 = "ex.lit_int"() {value = 2 : i64} : () -> i64
      %2 = "ex.add"(%0, %1) : (i64, i64) -> !ex.dyn
      %3 = "ex.var"() {name = "a"} : () -> !ex.unbound
      %4 = "ex.bind"(%3, %2) : (!ex.unbound, !ex.dyn) -> !ex.bound
      %5 = "ex.var"() {name = "a"} : () -> !ex.dyn
      %6 = "ex.lit_int"() {value = 3 : i64} : () -> i64
      %7 = "ex.add"(%5, %6) : (!ex.dyn, i64) -> !ex.dyn
      %8 = "ex.var"() {name = "b"} : () -> !ex.unbound
      %9 = "ex.bind"(%8, %7) : (!ex.unbound, !ex.dyn) -> !ex.bound
      %10 = "ex.var"() {name = "b"} : () -> !ex.dyn
    }) {name = "add_two_literal"} : () -> ()
    "ex.func"() ({
      %0 = "ex.call"() {name = "add_two_literal"} : () -> !ex.dyn
    }) {name = "main"} : () -> ()
  }) {name = "TwoFuncMod"} : () -> ()
  "ex.mod"() ({
    "ex.func"() ({
      %0 = "ex.lit_int"() {value = 1 : i64} : () -> i64
      %1 = "ex.lit_int"() {value = 2 : i64} : () -> i64
      %2 = "ex.add"(%0, %1) : (i64, i64) -> !ex.dyn
      %3 = "ex.var"() {name = "a"} : () -> !ex.unbound
      %4 = "ex.bind"(%3, %2) : (!ex.unbound, !ex.dyn) -> !ex.bound
      %5 = "ex.var"() {name = "a"} : () -> !ex.dyn
      %6 = "ex.lit_int"() {value = 3 : i64} : () -> i64
      %7 = "ex.add"(%5, %6) : (!ex.dyn, i64) -> !ex.dyn
      %8 = "ex.var"() {name = "b"} : () -> !ex.unbound
      %9 = "ex.bind"(%8, %7) : (!ex.unbound, !ex.dyn) -> !ex.bound
      %10 = "ex.var"() {name = "b"} : () -> !ex.dyn
    }) {name = "add_two_literal"} : () -> ()
  }) {name = "OneFuncMod"} : () -> ()
}) : () -> ()