Skip to content

Commit

Permalink
add defintrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
jackalcooper committed Oct 6, 2024
1 parent 7b72023 commit a168eb1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ locals_without_parens = [
op: 1,
value: 1,
call: 1,
const: 1
const: 1,
defintrinsic: 1
]

[
Expand Down
2 changes: 1 addition & 1 deletion lib/charms/defm/expander.ex
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ defmodule Charms.Defm.Expander do
Code.ensure_loaded(module)

cond do
function_exported?(module, :handle_intrinsic, 3) ->
function_exported?(module, :__intrinsics__, 0) and fun in module.__intrinsics__() ->
{args, state, env} = expand(args, state, env)

{module.handle_intrinsic(fun, args, ctx: state.mlir.ctx, block: state.mlir.blk),
Expand Down
2 changes: 2 additions & 0 deletions lib/charms/env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ defmodule Charms.Env do
def handle_intrinsic(:t, [], opts) do
Beaver.ENIF.Type.env(opts)
end

defintrinsic [:t]
end
27 changes: 27 additions & 0 deletions lib/charms/intrinsic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,38 @@ defmodule Charms.Intrinsic do
@type opt :: {:ctx, MLIR.Context.t()} | {:block, MLIR.Block.t()}
@type opts :: [opt | {atom(), term()}]
@callback handle_intrinsic(atom(), [term()], opts()) :: term()
Module.register_attribute(__MODULE__, :defintrinsic, accumulate: true)

@doc false
def collect_intrinsics(nil) do
raise ArgumentError, "no intrinsic functions defined"
end

def collect_intrinsics(attr_list) when length(attr_list) > 0 do
attr_list |> Enum.reverse() |> List.flatten() |> Enum.uniq()
end

defmacro __using__(_) do
quote do
@behaviour Charms.Intrinsic
use Beaver
@before_compile Charms.Intrinsic
import Charms.Intrinsic, only: :macros
end
end

defmacro defintrinsic(intrinsic_list) do
quote do
@defintrinsic unquote(intrinsic_list)
end
end

defmacro __before_compile__(_env) do
quote do
@defintrinsic_list @defintrinsic |> Charms.Intrinsic.collect_intrinsics()
def __intrinsics__() do
@defintrinsic_list
end
end
end
end
2 changes: 2 additions & 0 deletions lib/charms/pointer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ defmodule Charms.Pointer do
def handle_intrinsic(:t, [], opts) do
Beaver.Deferred.from_opts(opts, ~t{!llvm.ptr})
end

defintrinsic [:t, :allocate, :load, :store, :element_ptr]
end
2 changes: 2 additions & 0 deletions lib/charms/prelude.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ defmodule Charms.Prelude do
def handle_intrinsic(_name, _args, _opts) do
:not_handled
end

defintrinsic [:result_at, :!=, :-, :+, :<, :>, :<=, :>=, :==, :&&, :*]
end
2 changes: 2 additions & 0 deletions lib/charms/simd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ defmodule Charms.SIMD do
def handle_intrinsic(:t, [type, width], _opts) do
Type.vector([width], type)
end

defintrinsic [:new, :t]
end
2 changes: 2 additions & 0 deletions lib/charms/term.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ defmodule Charms.Term do
def handle_intrinsic(:t, [], opts) do
Beaver.ENIF.Type.term(opts)
end

defintrinsic [:t]
end

0 comments on commit a168eb1

Please sign in to comment.