Skip to content

Commit

Permalink
Upgrade to MLIR 18 (#222)
Browse files Browse the repository at this point in the history
Newer precompiled MLIR requires GCC12, so ubuntu 20.04 workflow jobs are
dropped
  • Loading branch information
jackalcooper authored Nov 15, 2023
1 parent 865b5d2 commit 0b00406
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 81 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ concurrency:
cancel-in-progress: true
jobs:
build:
name: Test OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
runs-on: ubuntu-latest
name: otp${{matrix.otp}}-ex${{matrix.elixir}} / ${{matrix.runs-on}}
runs-on: ${{matrix.runs-on}}
strategy:
fail-fast: false
matrix:
runs-on: ["ubuntu-latest"]
otp: ["24.2", "25.0"]
elixir: ["1.13.0", "1.14.0", "1.15.0"]
elixir: ["1.13.0", "1.15.0"]
steps:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ jobs:
matrix:
nif: ["2.16"]
job:
- { target: x86_64-unknown-linux-gnu, os: ubuntu-20.04 }
- { target: x86_64-unknown-linux-gnu, os: ubuntu-22.04 }
steps:
- uses: actions/checkout@v3
- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
elixir-version: "1.14.0" # Define the elixir version [required]
otp-version: "24.1" # Define the OTP version [required]
otp-version: "24.2" # Define the OTP version [required]
- name: Set up Zig
uses: goto-bus-stop/setup-zig@v1
with:
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
echo "==> before replace needed"
patchelf --print-needed $sofile
patchelf --replace-needed libMLIRBeaver.so.17 libMLIRBeaver.so $sofile
patchelf --replace-needed libMLIRBeaver.so.18 libMLIRBeaver.so $sofile
echo "==> after"
patchelf --print-needed $sofile
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mlir==17.0.0+ffbca7e9
mlir==18.0.0.2023110801+c86d35a
--find-links https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest
59 changes: 59 additions & 0 deletions lib/beaver/mlir/capi_codegen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,65 @@ defmodule Beaver.MLIR.CAPI.CodeGen do
{:ok, %KindDecl{zig_t: type, module_name: Beaver.MLIR.GenericCallback}}
end

def type_gen(
_root_module,
{:optional_type,
{:ref,
[
:*,
:const,
{:fn,
%Zig.Parser.FnOptions{
position: nil,
doc_comment: nil,
block: nil,
align: nil,
linksection: nil,
callconv: {:enum_literal, :C},
extern: false,
export: false,
pub: false,
inline: :maybe
},
[
params: [
{:_,
%Zig.Parser.ParamDeclOption{
doc_comment: nil,
noalias: false,
comptime: false
}, {:optional_type, {:ref, [:*, :anyopaque]}}},
{:_,
%Zig.Parser.ParamDeclOption{
doc_comment: nil,
noalias: false,
comptime: false
}, {:optional_type, {:ref, [:*, :const, :anyopaque]}}},
{:_,
%Zig.Parser.ParamDeclOption{
doc_comment: nil,
noalias: false,
comptime: false
}, :usize},
{:_,
%Zig.Parser.ParamDeclOption{
doc_comment: nil,
noalias: false,
comptime: false
}, :usize}
],
type: :void
]}
]}} =
type
) do
{:ok,
%KindDecl{
zig_t: type,
module_name: Beaver.MLIR.UnmanagedDenseResourceElementsAttrGetDeleteCallback
}}
end

def type_gen(_root_module, type)
when type in [
:MlirPass,
Expand Down
22 changes: 0 additions & 22 deletions lib/beaver/mlir/pattern.ex
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
defmodule Beaver.MLIR.Pattern do
alias Beaver.MLIR
import MLIR.Sigils
alias Beaver.MLIR.CAPI

@moduledoc """
Although this module is `MLIR.Pattern`, at this point it is a synonym of PDL patterns.
Pattern-matching is done by MLIR which works in a different way from Erlang pattern-matching.
The major difference is that MLIR pattern-matching will greedily match the patterns and maximize the benefit.
Compiled patterns will be saved as module attributes in MLIR assembly format.
"""

def from_string(pdl_pattern_str, opts \\ []) when is_binary(pdl_pattern_str) do
Beaver.Deferred.from_opts(
opts,
fn ctx ->
pattern_module = ~m{#{pdl_pattern_str}}.(ctx)
if MLIR.is_null(pattern_module), do: raise("fail to parse module")
MLIR.Operation.verify!(pattern_module)
pdl_pattern = CAPI.beaverPDLPatternGet(pattern_module)
pdl_pattern
end
)
end

@apply_default_opts [debug: false]
@doc """
Expand Down
7 changes: 1 addition & 6 deletions lib/beaver/mlir/pattern_set.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ defmodule Beaver.MLIR.PatternSet do
CAPI.beaverRewritePatternSetGet(ctx)
end

def insert(pattern_set, %Beaver.MLIR.Module{} = module) do
pattern_module = CAPI.beaverPDLPatternGet(module)
insert(pattern_set, pattern_module)
end

def insert(pattern_set, %Beaver.MLIR.CAPI.MlirPDLPatternModule{} = pattern_module) do
def insert(pattern_set, %Beaver.MLIR.Module{} = pattern_module) do
CAPI.beaverPatternSetAddOwnedPDLPattern(pattern_set, pattern_module)
pattern_set
end
Expand Down
6 changes: 5 additions & 1 deletion native/mlir-c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
cmake_minimum_required(VERSION 3.13.4)
set(CMAKE_BUILD_TYPE
RelWithDebInfo
Release
CACHE STRING "")
project(mlir-beaver)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

if(POLICY CMP0116)
cmake_policy(SET CMP0116 OLD)
endif()
Expand Down
2 changes: 1 addition & 1 deletion native/mlir-c/aggregate/MLIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ set(LIBS

message(STATUS "MLIR libs: ${LIBS}")

# This lib is for dlopen
# This lib is for dlopen or being linked by Zig/Rust
add_mlir_aggregate(
MLIRBeaver
SHARED
Expand Down
5 changes: 1 addition & 4 deletions native/mlir-c/include/mlir-c/Beaver/PDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ extern "C" {
}; \
typedef struct name name

DEFINE_C_API_STRUCT(MlirPDLPatternModule, void);
DEFINE_C_API_STRUCT(MlirRewritePatternSet, void);

#undef DEFINE_C_API_STRUCT

MLIR_CAPI_EXPORTED MlirPDLPatternModule beaverPDLPatternGet(MlirModule module);

MLIR_CAPI_EXPORTED MlirRewritePatternSet
beaverRewritePatternSetGet(MlirContext context);

MLIR_CAPI_EXPORTED MlirRewritePatternSet beaverPatternSetAddOwnedPDLPattern(
MlirRewritePatternSet patternList, MlirPDLPatternModule pdlPattern);
MlirRewritePatternSet patternList, MlirModule module);

MLIR_CAPI_EXPORTED MlirLogicalResult beaverApplyOwnedPatternSetOnRegion(
MlirRegion region, MlirRewritePatternSet patternList);
Expand Down
1 change: 0 additions & 1 deletion native/mlir-c/include/mlir/CAPI/Beaver.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"

DEFINE_C_API_PTR_METHODS(MlirPDLPatternModule, mlir::PDLPatternModule)
DEFINE_C_API_PTR_METHODS(MlirRewritePatternSet, mlir::RewritePatternSet)
DEFINE_C_API_PTR_METHODS(MlirOperand, mlir::Value::use_iterator)
DEFINE_C_API_METHODS(MlirRegisteredOperationName, mlir::RegisteredOperationName)
Expand Down
15 changes: 3 additions & 12 deletions native/mlir-c/lib/CAPI/Beaver.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
// include otherwise on linux it can't find the symbol
#define _GLIBCXX_USE_CXX11_ABI 0
#include "mlir/CAPI/Beaver.h"
#include "mlir/CAPI/Pass.h"
#include "mlir/CAPI/Registration.h"

#include "mlir/CAPI/Beaver.h"
#include "mlir/Dialect/Utils/ReshapeOpsUtils.h"

using namespace mlir;

MLIR_CAPI_EXPORTED MlirPDLPatternModule beaverPDLPatternGet(MlirModule module) {
// should this module be removed from parent?
auto *pdlPattern = new PDLPatternModule(unwrap(module));
return wrap(pdlPattern);
}

MLIR_CAPI_EXPORTED MlirRewritePatternSet
beaverRewritePatternSetGet(MlirContext context) {
return wrap(new RewritePatternSet(unwrap(context)));
}

MLIR_CAPI_EXPORTED MlirRewritePatternSet beaverPatternSetAddOwnedPDLPattern(
MlirRewritePatternSet patternList, MlirPDLPatternModule pdlPattern) {
auto &set = unwrap(patternList)->add(std::move(*(unwrap(pdlPattern))));
MlirRewritePatternSet patternList, MlirModule module) {
auto &set = unwrap(patternList)->add(PDLPatternModule(unwrap(module)));
return wrap(&set);
}

Expand Down
5 changes: 0 additions & 5 deletions native/mlir-c/pre-built-llvm.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)

set(CMAKE_CXX_STANDARD
17
CACHE STRING "C++ standard to conform to")

find_package(MLIR REQUIRED CONFIG)

message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
Expand Down
1 change: 1 addition & 0 deletions native/mlir-zig-proj/src/prelude.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub const DiagnosticHandlerDeleteUserData = ?*const fn (?*anyopaque) callconv(.C
pub const ExternalPassConstruct = ?*const fn (?*anyopaque) callconv(.C) ?*anyopaque;
pub const ExternalPassRun = ?*const fn (c.MlirOperation, c.MlirExternalPass, ?*anyopaque) callconv(.C) void;
pub const GenericCallback = ?*const fn (c.MlirContext, ?*anyopaque) callconv(.C) c.MlirLogicalResult;
pub const UnmanagedDenseResourceElementsAttrGetDeleteCallback = ?*const fn (?*anyopaque, ?*const anyopaque, usize, usize) callconv(.C) void;
pub const AffineMapCompressUnusedSymbolsPopulateResult = ?*const fn (?*anyopaque, isize, c.MlirAffineMap) callconv(.C) void;
pub const SymbolTableWalkSymbolTablesCallback = ?*const fn (c.MlirOperation, bool, ?*anyopaque) callconv(.C) void;
pub usingnamespace c;
5 changes: 4 additions & 1 deletion test/dialect_registry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule DialectRegistryTest do
{"amx", "AMX"},
{"arith", "Arith"},
{"arm_neon", "ArmNeon"},
{"arm_sve", "ArmSVE"},
{"arm_sme", "ArmSME"},
{"arm_sve", "ArmSVE"},
{"async", "Async"},
{"bufferization", "Bufferization"},
{"builtin", "Builtin"},
Expand All @@ -39,6 +39,7 @@ defmodule DialectRegistryTest do
{"llvm", "LLVM"},
{"math", "Math"},
{"memref", "MemRef"},
{"mesh", "Mesh"},
{"ml_program", "MLProgram"},
{"nvgpu", "NVGPU"},
{"nvvm", "NVVM"},
Expand All @@ -52,8 +53,10 @@ defmodule DialectRegistryTest do
{"sparse_tensor", "SparseTensor"},
{"spirv", "SPIRV"},
{"tensor", "Tensor"},
{"test_dyn", "TestDyn"},
{"tosa", "TOSA"},
{"transform", "Transform"},
{"ub", "Ub"},
{"vector", "Vector"},
{"x86vector", "X86Vector"}
])
Expand Down
1 change: 0 additions & 1 deletion test/memref_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ defmodule MemRefTest do
~w{finalizing-bufferize buffer-deallocation convert-linalg-to-loops}
)
|> convert_scf_to_cf
|> convert_linalg_to_llvm()
|> MLIR.Pass.Composer.append("finalize-memref-to-llvm")
|> convert_func_to_llvm
|> reconcile_unrealized_casts
Expand Down
21 changes: 2 additions & 19 deletions test/pdl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ defmodule PDLTest do
ir_module = MLIR.Module.create(ctx, @apply_rewrite_op_ir)
MLIR.Operation.verify!(pattern_module)
MLIR.Operation.verify!(ir_module)
pdl_pattern = CAPI.beaverPDLPatternGet(pattern_module)
pattern_set = CAPI.beaverRewritePatternSetGet(ctx)
pattern_set = CAPI.beaverPatternSetAddOwnedPDLPattern(pattern_set, pdl_pattern)
pattern_set = CAPI.beaverPatternSetAddOwnedPDLPattern(pattern_set, pattern_module)

region =
ir_module
Expand Down Expand Up @@ -159,9 +158,8 @@ defmodule PDLTest do
pattern_string = MLIR.to_string(pattern_module)
assert String.contains?(pattern_string, "test.op")
assert String.contains?(pattern_string, "test.success2")
pdl_pattern = CAPI.beaverPDLPatternGet(pattern_module)
pattern_set = CAPI.beaverRewritePatternSetGet(ctx)
pattern_set = CAPI.beaverPatternSetAddOwnedPDLPattern(pattern_set, pdl_pattern)
pattern_set = CAPI.beaverPatternSetAddOwnedPDLPattern(pattern_set, pattern_module)
region = ir_module |> MLIR.Operation.from_module() |> CAPI.mlirOperationGetFirstRegion()
result = CAPI.beaverApplyOwnedPatternSetOnRegion(region, pattern_set)

Expand All @@ -173,21 +171,6 @@ defmodule PDLTest do
CAPI.mlirContextDestroy(ctx)
end

test "load from string", test_context do
%MLIR.CAPI.MlirPDLPatternModule{} =
"""
module @erase {
pdl.pattern : benefit(1) {
%root = operation "foo.op"
rewrite %root {
erase %root
}
}
}
"""
|> MLIR.Pattern.from_string(ctx: test_context[:ctx])
end

test "replace tosa", test_context do
defmodule TestTOSAPatterns do
def gen_ir_module(ctx) do
Expand Down
2 changes: 1 addition & 1 deletion test/tosa_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule TosaTest do
v0 = TOSA.add(arg0, arg1) >>> Type.ranked_tensor([2, 3], Type.f32())

v0 =
TOSA.mul(v0, arg1, {:shift, ~a{0 : i32}}) >>>
TOSA.mul(v0, arg1, {:shift, ~a{0 : i8}}) >>>
Type.ranked_tensor([2, 3], Type.f32())

Func.return(v0) >>> []
Expand Down

0 comments on commit 0b00406

Please sign in to comment.