Skip to content

Instantly share code, notes, and snippets.

View jackalcooper's full-sized avatar

Shenghang Tsai jackalcooper

View GitHub Profile
@jackalcooper
jackalcooper / 1.define_a_dialect.ex
Last active September 22, 2023 14:20
first class dialect definition support in beaver
defmodule Cmath do
alias Beaver.MLIR.Type
use Beaver.Dialect, name: "cmath"
deftype complex(t = any_of(t, [Type.f32(), Type.f64()])) do
parameters(t)
end
defop norm do
operands(complex(any()))
@jackalcooper
jackalcooper / ex-comp.diff
Created June 7, 2023 03:33
add handler for passes after core
diff --git a/lib/elixir/src/elixir_erl_compiler.erl b/lib/elixir/src/elixir_erl_compiler.erl
index a299ab4c2d..98462f7148 100644
--- a/lib/elixir/src/elixir_erl_compiler.erl
+++ b/lib/elixir/src/elixir_erl_compiler.erl
@@ -50,7 +50,16 @@ compile(Forms, File, Opts) when is_list(Forms), is_list(Opts), is_binary(File) -
format_warnings(Opts, CoreWarnings),
CompileOpts = [no_spawn_compiler_process, from_core, no_core_prepare,
no_auto_import, return, {source, Source} | Opts],
-
+ case compile:noenv_forms(CoreForms, [dprecg|CompileOpts]) of
defmodule Kinda.ParserHelper do
@moduledoc false
@doc """
define a parser combinator and variable with same name
"""
defmacro defcv(name, expr) do
quote do
defcombinatorp(unquote(name), unquote(expr))
Kernel.var!(unquote({name, [], nil})) = parsec(unquote(name))
_ = Kernel.var!(unquote({name, [], nil}))
defmodule Kinda.Parser do
import NimbleParsec
# *** Tokens ***
container_doc_comment =
string("//!") |> repeat(ascii_char([?^, ?\n])) |> repeat([?\s, ?\n]) |> times(min: 1)
doc_comment =
string("///") |> repeat(ascii_char([?^, ?\n])) |> repeat([?\s, ?\n]) |> times(min: 1)
defmodule Manx.Lowering do
alias Beaver.MLIR
import MLIR.Sigils
import MLIR.{Transforms, Conversion}
alias Beaver.MLIR.Dialect.GPU
def tosa_vulkan(op) do
op
|> MLIR.Operation.verify!(dump_if_fail: true)
|> canonicalize
@jackalcooper
jackalcooper / cf_test.exs
Created August 7, 2022 07:06
Some examples
defmodule CfTest do
use ExUnit.Case
use Beaver
alias Beaver.MLIR
alias Beaver.MLIR.Type
alias Beaver.MLIR.Attribute
alias Beaver.MLIR.Dialect.{CF, Arith}
@moduletag :smoke
defmodule MutCompiler do
@jackalcooper
jackalcooper / all_close_jit.mlir
Created August 7, 2022 04:56
mlir generated for Nx.all_close
module {
func.func @"Elixir.Manx.Assert.-all_close_jit/3-fun-0-.126722613"(%arg0: tensor<3xf32>, %arg1: tensor<3xf32>) -> tensor<i8> {
%0 = "tosa.const"() {value = dense<9.99999974E-5> : tensor<f32>} : () -> tensor<f32>
%1 = "tosa.const"() {value = dense<0x7F800000> : tensor<3xf32>} : () -> tensor<3xf32>
%2 = "tosa.abs"(%arg0) : (tensor<3xf32>) -> tensor<3xf32>
%3 = "tosa.equal"(%2, %1) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xi1>
%4 = "tosa.cast"(%3) : (tensor<3xi1>) -> tensor<3xi8>
%5 = "tosa.abs"(%arg1) : (tensor<3xf32>) -> tensor<3xf32>
%6 = "tosa.equal"(%5, %1) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xi1>
%7 = "tosa.cast"(%6) : (tensor<3xi1>) -> tensor<3xi8>
@jackalcooper
jackalcooper / resource_kind.zig
Created July 27, 2022 01:11
Zig managed NIF resource
pub fn ResourceKind(comptime ElementType: type, module_name: anytype) type {
return struct {
pub const T = ElementType;
pub const module_name = module_name;
pub const resource = struct {
pub var t: resource_type = undefined;
pub const name = @typeName(ElementType);
pub fn make(environment: env, value: T) !term {
return make_resource(environment, value, t);
}
@jackalcooper
jackalcooper / waker.ex
Created July 8, 2022 14:17
Hookup C functions with Enumerable protocol
alias Beaver.MLIR
alias Beaver.MLIR.CAPI.{
MlirModule,
MlirOperation,
MlirRegion,
MlirAttribute,
MlirBlock,
MlirValue,
MlirNamedAttribute,
@jackalcooper
jackalcooper / pattern-gen.ex
Last active July 3, 2022 01:25
Question regarding compiling Elixir pattern to MLIR (PDL dialect)
# I’m working on compiling Elixir pattern to MLIR (the PDL dialect),
# so that it could be used to manipulate another piece of IR.
# If we want to rewrite
res = TOSA.add(a, b) >>> Type.ranked_tensor([2, 3], Type.f32())
res1 = TOSA.add(res, b) >>> Type.ranked_tensor([2, 3], Type.f32())
# to
res1 = TOSA.sub(res, b) >>> Type.ranked_tensor([2, 3], Type.f32())
# We can declare a pattern like this