Skip to content

Commit

Permalink
Simplify example in README (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackalcooper authored Feb 14, 2024
1 parent f6932ff commit 79cad4a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
42 changes: 18 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,27 @@ Elixir could actually be a good fit as a MLIR front end. Elixir has SSA, pattern
Here is an example to build and verify a piece of IR in Beaver:

```elixir
mlir do
module ctx: ctx do
Func.func some_func(function_type: Type.function([], [Type.i(32)])) do
region do
block _bb_entry() do
v0 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
cond0 = Arith.constant(true) >>> Type.i(1)
CF.cond_br(cond0, Beaver.Env.block(bb1), {Beaver.Env.block(bb2), [v0]}) >>> []
end

block bb1() do
v1 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
_add = Arith.addi(v0, v0) >>> Type.i(32)
CF.br({Beaver.Env.block(bb2), [v1]}) >>> []
end

block bb2(arg >>> Type.i(32)) do
v2 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
add = Arith.addi(arg, v2) >>> Type.i(32)
Func.return(add) >>> []
end
end
Func.func some_func(function_type: Type.function([], [Type.i(32)])) do
region do
block _() do
v0 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
cond0 = Arith.constant(true) >>> Type.i(1)
CF.cond_br(cond0, Beaver.Env.block(bb1), {Beaver.Env.block(bb2), [v0]}) >>> []
end

block bb1() do
v1 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
_add = Arith.addi(v0, v0) >>> Type.i(32)
CF.br({Beaver.Env.block(bb2), [v1]}) >>> []
end

block bb2(arg >>> Type.i(32)) do
v2 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
add = Arith.addi(arg, v2) >>> Type.i(32)
Func.return(add) >>> []
end
|> MLIR.Operation.verify!(debug: true)
end
end
|> MLIR.Operation.verify!(debug: true)
```

And a small example to showcase what it is like to define and run a pass in Beaver (with some monad magic):
Expand Down
23 changes: 1 addition & 22 deletions test/gen_ir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,7 @@ defmodule CFTest do
ir =
mlir ctx: test_context[:ctx] do
module do
Func.func some_func(function_type: Type.function([], [Type.i(32)])) do
region do
block _bb_entry() do
v0 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
cond0 = Arith.constant(true) >>> Type.i(1)
CF.cond_br(cond0, Beaver.Env.block(bb1), {Beaver.Env.block(bb2), [v0]}) >>> []
end

block bb1() do
v1 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
_add = Arith.addi(v0, v0) >>> Type.i(32)
CF.br({Beaver.Env.block(bb2), [v1]}) >>> []
end

block bb2(arg >>> Type.i(32)) do
v2 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
add = Arith.addi(arg, v2) >>> Type.i(32)
Func.return(add) >>> []
end
end
end
|> MLIR.Operation.verify!(debug: true)
unquote(File.read!("test/readme_example.exs") |> Code.string_to_quoted!())

Func.func some_func2(function_type: Type.function([], [Type.i(32)])) do
region do
Expand Down
21 changes: 21 additions & 0 deletions test/readme_example.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Func.func some_func(function_type: Type.function([], [Type.i(32)])) do
region do
block _() do
v0 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
cond0 = Arith.constant(true) >>> Type.i(1)
CF.cond_br(cond0, Beaver.Env.block(bb1), {Beaver.Env.block(bb2), [v0]}) >>> []
end

block bb1() do
v1 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
_add = Arith.addi(v0, v0) >>> Type.i(32)
CF.br({Beaver.Env.block(bb2), [v1]}) >>> []
end

block bb2(arg >>> Type.i(32)) do
v2 = Arith.constant(value: Attribute.integer(Type.i(32), 0)) >>> Type.i(32)
add = Arith.addi(arg, v2) >>> Type.i(32)
Func.return(add) >>> []
end
end
end

0 comments on commit 79cad4a

Please sign in to comment.