From 8983ac7d149f19f1690107684d8a274c7516a177 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:24:28 +1000 Subject: [PATCH 01/16] Update MLIR to 16 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b9a926720c..478d1d155b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/raviqqe/melior" documentation = "https://raviqqe.github.io/melior/melior/" [dependencies] -mlir-sys = "0.1" +mlir-sys = "0.2" once_cell = "1" [dev-dependencies] From fd900a7ef8cf684239552c05229bca8c0e6c57b4 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:25:18 +1000 Subject: [PATCH 02/16] Update lock file --- Cargo.lock | 165 +++++++++-------------------------------------------- 1 file changed, 27 insertions(+), 138 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce94dc900c..256bf25bd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,52 +2,26 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "aho-corasick" -version = "0.7.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e" -dependencies = [ - "memchr", -] - -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - [[package]] name = "bindgen" -version = "0.60.1" +version = "0.65.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "062dddbc1ba4aca46de6338e2bf87771414c335f7b2f2036e8f3e9befebf88e6" +checksum = "cfdf7b466f9a4903edc73f95d6d2bcd5baf8ae620638762244d3f60143643cc5" dependencies = [ "bitflags", "cexpr", "clang-sys", - "clap", - "env_logger", "lazy_static", "lazycell", "log", "peeking_take_while", + "prettyplease", "proc-macro2", "quote", "regex", "rustc-hash", "shlex", + "syn 2.0.15", "which", ] @@ -83,30 +57,6 @@ dependencies = [ "libloading", ] -[[package]] -name = "clap" -version = "3.2.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" -dependencies = [ - "atty", - "bitflags", - "clap_lex", - "indexmap", - "strsim", - "termcolor", - "textwrap", -] - -[[package]] -name = "clap_lex" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" -dependencies = [ - "os_str_bytes", -] - [[package]] name = "console" version = "0.15.4" @@ -126,7 +76,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdffe87e1d521a10f9696f833fe502293ea446d7f256c06128293a4119bdf4cb" dependencies = [ "quote", - "syn", + "syn 1.0.100", ] [[package]] @@ -147,56 +97,12 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" -[[package]] -name = "env_logger" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c90bf5f19754d10198ccb95b70664fc925bd1fc090a0fd9a6ebc54acc8cd6272" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "glob" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" - -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - -[[package]] -name = "indexmap" -version = "1.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" -dependencies = [ - "autocfg", - "hashbrown", -] - [[package]] name = "indoc" version = "2.0.1" @@ -284,9 +190,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "mlir-sys" -version = "0.1.4" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "710a46e98d11b823b3d593b480558ae7bd65bb90dcb8351134a978dab4ecddf2" +checksum = "c1262be288d5f59eaa5a6367722e4fd2eb2f668229d2e3e3680530f266a193b3" dependencies = [ "bindgen", ] @@ -307,12 +213,6 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" -[[package]] -name = "os_str_bytes" -version = "6.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" - [[package]] name = "output_vt100" version = "0.1.3" @@ -340,20 +240,30 @@ dependencies = [ "yansi", ] +[[package]] +name = "prettyplease" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ceca8aaf45b5c46ec7ed39fff75f57290368c1846d33d24a122ca81416ab058" +dependencies = [ + "proc-macro2", + "syn 2.0.15", +] + [[package]] name = "proc-macro2" -version = "1.0.43" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.21" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -364,8 +274,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" dependencies = [ - "aho-corasick", - "memchr", "regex-syntax", ] @@ -393,12 +301,6 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62ac7f900db32bf3fd12e0117dd3dc4da74bc52ebaac97f39668446d89694803" -[[package]] -name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - [[package]] name = "syn" version = "1.0.100" @@ -411,20 +313,16 @@ dependencies = [ ] [[package]] -name = "termcolor" -version = "1.1.3" +name = "syn" +version = "2.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" dependencies = [ - "winapi-util", + "proc-macro2", + "quote", + "unicode-ident", ] -[[package]] -name = "textwrap" -version = "0.15.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" - [[package]] name = "unicode-ident" version = "1.0.4" @@ -458,15 +356,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" From eee337acd5140f0846e26284660daf7951eee007 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:25:34 +1000 Subject: [PATCH 03/16] Update LLVM version --- tools/setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/setup.sh b/tools/setup.sh index d80754f1d4..92a4f130bd 100755 --- a/tools/setup.sh +++ b/tools/setup.sh @@ -2,7 +2,7 @@ set -e -llvm_version=15 +llvm_version=16 brew update brew install llvm@$llvm_version From 85a51853cdd189cb3212890b15eb542a182ff882 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:26:24 +1000 Subject: [PATCH 04/16] Update documentation --- README.md | 4 ++-- src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index aa080caf3d..ba6f89ae22 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,10 @@ cargo add melior ### Dependencies -[LLVM/MLIR 15](https://llvm.org/) needs to be installed on your system. On Linux and macOS, you can install it via [Homebrew](https://brew.sh). +[LLVM/MLIR 16](https://llvm.org/) needs to be installed on your system. On Linux and macOS, you can install it via [Homebrew](https://brew.sh). ```sh -brew install llvm@15 +brew install llvm@16 ``` ## Documentation diff --git a/src/lib.rs b/src/lib.rs index dc4ea9244f..13c3483fa4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,10 +6,10 @@ //! //! # Dependencies //! -//! [LLVM/MLIR 15](https://llvm.org/) needs to be installed on your system. On Linux and macOS, you can install it via [Homebrew](https://brew.sh). +//! [LLVM/MLIR 16](https://llvm.org/) needs to be installed on your system. On Linux and macOS, you can install it via [Homebrew](https://brew.sh). //! //! ```sh -//! brew install llvm@15 +//! brew install llvm@16 //! ``` //! //! # Safety From a1efd0d6964d5707df4e8dc8ebb8a541e69b4895 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:27:24 +1000 Subject: [PATCH 05/16] Update CI --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 688b438a6b..fd6603f3be 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -18,7 +18,7 @@ jobs: matrix: os: - ubuntu-22.04 - # TODO Support macOS with LLVM 15. + # TODO Support macOS with LLVM 16. # - macos-12 runs-on: ${{ matrix.os }} steps: From 180aea5879c53abd9b72a69bf779ea6025818d85 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:41:45 +1000 Subject: [PATCH 06/16] Update API --- src/execution_engine.rs | 8 +++++++- src/ir/attribute.rs | 11 +++-------- src/pass/conversion.rs | 4 ++-- src/utility.rs | 9 ++++++++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/execution_engine.rs b/src/execution_engine.rs index e7b01dc93e..7e6cf3bcb1 100644 --- a/src/execution_engine.rs +++ b/src/execution_engine.rs @@ -12,7 +12,12 @@ pub struct ExecutionEngine { impl ExecutionEngine { /// Creates an execution engine. - pub fn new(module: &Module, optimization_level: usize, shared_library_paths: &[&str]) -> Self { + pub fn new( + module: &Module, + optimization_level: usize, + shared_library_paths: &[&str], + enable_object_dump: bool, + ) -> Self { Self { raw: unsafe { mlirExecutionEngineCreate( @@ -24,6 +29,7 @@ impl ExecutionEngine { .map(|&string| StringRef::from(string).to_raw()) .collect::>() .as_ptr(), + enable_object_dump, ) }, } diff --git a/src/ir/attribute.rs b/src/ir/attribute.rs index 1203b447b9..1e51069cc2 100644 --- a/src/ir/attribute.rs +++ b/src/ir/attribute.rs @@ -10,9 +10,9 @@ use mlir_sys::{ mlirAttributeIsABool, mlirAttributeIsADenseElements, mlirAttributeIsADenseFPElements, mlirAttributeIsADenseIntElements, mlirAttributeIsADictionary, mlirAttributeIsAElements, mlirAttributeIsAFloat, mlirAttributeIsAInteger, mlirAttributeIsAIntegerSet, - mlirAttributeIsAOpaque, mlirAttributeIsAOpaqueElements, mlirAttributeIsASparseElements, - mlirAttributeIsAString, mlirAttributeIsASymbolRef, mlirAttributeIsAType, mlirAttributeIsAUnit, - mlirAttributeParseGet, mlirAttributePrint, MlirAttribute, + mlirAttributeIsAOpaque, mlirAttributeIsASparseElements, mlirAttributeIsAString, + mlirAttributeIsASymbolRef, mlirAttributeIsAType, mlirAttributeIsAUnit, mlirAttributeParseGet, + mlirAttributePrint, MlirAttribute, }; use std::{ ffi::c_void, @@ -127,11 +127,6 @@ impl<'c> Attribute<'c> { !self.is_null() && unsafe { mlirAttributeIsAOpaque(self.raw) } } - /// Returns `true` if an attribute is opaque elements. - pub fn is_opaque_elements(&self) -> bool { - !self.is_null() && unsafe { mlirAttributeIsAOpaqueElements(self.raw) } - } - /// Returns `true` if an attribute is sparse elements. pub fn is_sparse_elements(&self) -> bool { !self.is_null() && unsafe { mlirAttributeIsASparseElements(self.raw) } diff --git a/src/pass/conversion.rs b/src/pass/conversion.rs index 4e23024d71..9b3c4a9820 100644 --- a/src/pass/conversion.rs +++ b/src/pass/conversion.rs @@ -2,7 +2,7 @@ use super::Pass; use mlir_sys::{ - mlirCreateConversionConvertArithmeticToLLVM, mlirCreateConversionConvertControlFlowToLLVM, + mlirCreateConversionArithToLLVMConversionPass, mlirCreateConversionConvertControlFlowToLLVM, mlirCreateConversionConvertControlFlowToSPIRV, mlirCreateConversionConvertFuncToLLVM, mlirCreateConversionConvertMathToLLVM, mlirCreateConversionConvertMathToLibm, mlirCreateConversionConvertMathToSPIRV, @@ -10,7 +10,7 @@ use mlir_sys::{ /// Creates a pass to convert the `arith` dialect to the `llvm` dialect. pub fn convert_arithmetic_to_llvm() -> Pass { - Pass::from_raw_fn(mlirCreateConversionConvertArithmeticToLLVM) + Pass::from_raw_fn(mlirCreateConversionArithToLLVMConversionPass) } /// Creates a pass to convert the `cf` dialect to the `llvm` dialect. diff --git a/src/utility.rs b/src/utility.rs index 9d8b731688..6a2dd7d576 100644 --- a/src/utility.rs +++ b/src/utility.rs @@ -10,6 +10,7 @@ use mlir_sys::{ use std::{ ffi::c_void, fmt::{self, Formatter}, + ptr::null_mut, sync::Once, }; @@ -33,8 +34,14 @@ pub fn register_all_passes() { /// Parses a pass pipeline. pub fn parse_pass_pipeline(manager: pass::OperationManager, source: &str) -> Result<(), Error> { + // TODO Handle parse errors. let result = LogicalResult::from_raw(unsafe { - mlirParsePassPipeline(manager.to_raw(), StringRef::from(source).to_raw()) + mlirParsePassPipeline( + manager.to_raw(), + StringRef::from(source).to_raw(), + None, + null_mut(), + ) }); if result.is_success() { From d2e755e04b65cadb0335eae26add14b1727cad55 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:42:45 +1000 Subject: [PATCH 07/16] Fix test --- src/execution_engine.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/execution_engine.rs b/src/execution_engine.rs index 7e6cf3bcb1..04b21a2000 100644 --- a/src/execution_engine.rs +++ b/src/execution_engine.rs @@ -104,7 +104,7 @@ mod tests { assert_eq!(pass_manager.run(&mut module), Ok(())); - let engine = ExecutionEngine::new(&module, 2, &[]); + let engine = ExecutionEngine::new(&module, 2, &[], false); let mut argument = 42; let mut result = -1; From 7b73ba8f1ec91532936e558d620111a951e3a3bc Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 5 May 2023 23:52:50 +1000 Subject: [PATCH 08/16] Fix linting --- src/string_ref.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/string_ref.rs b/src/string_ref.rs index e9372cf755..52aace538c 100644 --- a/src/string_ref.rs +++ b/src/string_ref.rs @@ -28,7 +28,7 @@ impl<'a> StringRef<'a> { /// Converts a string reference into a `str`. pub fn as_str(&self) -> Result<&str, Utf8Error> { unsafe { - let bytes = slice::from_raw_parts(self.raw.data as *mut u8, self.raw.length as usize); + let bytes = slice::from_raw_parts(self.raw.data as *mut u8, self.raw.length); str::from_utf8(if bytes[bytes.len() - 1] == 0 { &bytes[..bytes.len() - 1] From 74773cb6bca28a4c14aa7587f9e8e6c1d464d9b3 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 14:41:32 +1000 Subject: [PATCH 09/16] Fix LLVM array type syntax --- src/dialect/llvm/type.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dialect/llvm/type.rs b/src/dialect/llvm/type.rs index a12f031fad..e1bd6602c1 100644 --- a/src/dialect/llvm/type.rs +++ b/src/dialect/llvm/type.rs @@ -108,7 +108,7 @@ mod tests { assert_eq!( super::array(i32, 4), - Type::parse(&context, "!llvm.array<4xi32>").unwrap() + Type::parse(&context, "!llvm.array<4 x i32>").unwrap() ); } From 16bd896d27b11da98bae88bbd6a0a86b7bdaf5a8 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 14:46:31 +1000 Subject: [PATCH 10/16] Update snapshot --- src/snapshots/melior__tests__build_sum.snap | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/snapshots/melior__tests__build_sum.snap b/src/snapshots/melior__tests__build_sum.snap index fc1aa31b80..ba3510faed 100644 --- a/src/snapshots/melior__tests__build_sum.snap +++ b/src/snapshots/melior__tests__build_sum.snap @@ -5,13 +5,13 @@ expression: module.as_operation() module { func.func @sum(%arg0: memref, %arg1: memref) { %c0 = arith.constant 0 : index - %0 = memref.dim %arg0, %c0 : memref + %dim = memref.dim %arg0, %c0 : memref %c1 = arith.constant 1 : index - scf.for %arg2 = %c0 to %0 step %c1 { - %1 = memref.load %arg0[%arg2] : memref - %2 = memref.load %arg1[%arg2] : memref - %3 = arith.addf %1, %2 : f32 - memref.store %3, %arg0[%arg2] : memref + scf.for %arg2 = %c0 to %dim step %c1 { + %0 = memref.load %arg0[%arg2] : memref + %1 = memref.load %arg1[%arg2] : memref + %2 = arith.addf %0, %1 : f32 + memref.store %2, %arg0[%arg2] : memref } return } From fb774d4ff9f01d5b0bdadd058aa7c8a0610dc651 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 14:51:40 +1000 Subject: [PATCH 11/16] Update test --- src/pass/manager.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pass/manager.rs b/src/pass/manager.rs index a07b4335b9..67789f1675 100644 --- a/src/pass/manager.rs +++ b/src/pass/manager.rs @@ -204,8 +204,7 @@ mod tests { fn print_pass_pipeline() { let context = Context::new(); let manager = Manager::new(&context); - let module_manager = manager.nested_under("builtin.module"); - let function_manager = module_manager.nested_under("func.func"); + let function_manager = manager.nested_under("func.func"); function_manager.add_pass(pass::transform::print_operation_stats()); @@ -214,10 +213,9 @@ mod tests { "builtin.module(func.func(print-op-stats{json=false}))" ); assert_eq!( - module_manager.to_string(), + function_manager.to_string(), "func.func(print-op-stats{json=false})" ); - assert_eq!(function_manager.to_string(), "print-op-stats{json=false}"); } #[test] From bf4711e9bc771ef16dd24fe7b0f4631576291ebc Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 15:05:25 +1000 Subject: [PATCH 12/16] Fix --- src/error.rs | 6 ++++-- src/pass/manager.rs | 2 +- src/utility.rs | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/error.rs b/src/error.rs index e646c20f32..0fadd8cb82 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,7 +15,7 @@ pub enum Error { MemRefExpected(String), OperationResultExpected(String), OperationResultPosition(String, usize), - ParsePassPipeline, + ParsePassPipeline(String), RunPass, TupleExpected(String), TupleFieldPosition(String, usize), @@ -53,7 +53,9 @@ impl Display for Error { "operation result position {position} out of range: {operation}" ) } - Self::ParsePassPipeline => write!(formatter, "failed to parse pass pipeline"), + Self::ParsePassPipeline(message) => { + write!(formatter, "failed to parse pass pipeline: {}", message) + } Self::RunPass => write!(formatter, "failed to run pass"), Self::TupleExpected(r#type) => write!(formatter, "tuple expected: {type}"), Self::TupleFieldPosition(r#type, position) => { diff --git a/src/pass/manager.rs b/src/pass/manager.rs index 67789f1675..13c1425ab1 100644 --- a/src/pass/manager.rs +++ b/src/pass/manager.rs @@ -229,7 +229,7 @@ mod tests { "builtin.module(func.func(print-op-stats{json=false}),\ func.func(print-op-stats{json=false}))" ), - Err(Error::ParsePassPipeline) + Err(Error::ParsePassPipeline("MLIR Textual PassPipeline Parser:1:11: error: 'print-op-stats' does not refer to a registered pass or pass pipeline\nfunc.func(print-op-stats{json=false}),func.func(print-op-stats{json=false})\n ^\n".into())) ); register_print_operation_stats(); diff --git a/src/utility.rs b/src/utility.rs index 6a2dd7d576..441ff349e0 100644 --- a/src/utility.rs +++ b/src/utility.rs @@ -10,7 +10,6 @@ use mlir_sys::{ use std::{ ffi::c_void, fmt::{self, Formatter}, - ptr::null_mut, sync::Once, }; @@ -34,20 +33,34 @@ pub fn register_all_passes() { /// Parses a pass pipeline. pub fn parse_pass_pipeline(manager: pass::OperationManager, source: &str) -> Result<(), Error> { - // TODO Handle parse errors. + let mut error_message = None; + let result = LogicalResult::from_raw(unsafe { mlirParsePassPipeline( manager.to_raw(), StringRef::from(source).to_raw(), - None, - null_mut(), + Some(handle_parse_error), + &mut error_message as *mut _ as *mut _, ) }); if result.is_success() { Ok(()) } else { - Err(Error::ParsePassPipeline) + Err(Error::ParsePassPipeline(error_message.unwrap_or_else( + || "failed to parse error message in UTF-8".into(), + ))) + } +} + +unsafe extern "C" fn handle_parse_error(raw_string: MlirStringRef, data: *mut c_void) { + let string = StringRef::from_raw(raw_string); + let data = &mut *(data as *mut Option); + + if let Some(message) = data { + message.extend(string.as_str()) + } else { + *data = string.as_str().map(String::from).ok(); } } From fcb97897a3c4b4c878963d961b2e5e3c05078eef Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 15:08:10 +1000 Subject: [PATCH 13/16] Fix --- src/error.rs | 2 +- src/pass/manager.rs | 12 +++++------- ...__pass__manager__tests__parse_pass_pipeline_.snap | 9 +++++++++ 3 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 src/pass/snapshots/melior__pass__manager__tests__parse_pass_pipeline_.snap diff --git a/src/error.rs b/src/error.rs index 0fadd8cb82..9ff71121b5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -54,7 +54,7 @@ impl Display for Error { ) } Self::ParsePassPipeline(message) => { - write!(formatter, "failed to parse pass pipeline: {}", message) + write!(formatter, "failed to parse pass pipeline:\n{}", message) } Self::RunPass => write!(formatter, "failed to run pass"), Self::TupleExpected(r#type) => write!(formatter, "tuple expected: {type}"), diff --git a/src/pass/manager.rs b/src/pass/manager.rs index 13c1425ab1..fc1ec62c98 100644 --- a/src/pass/manager.rs +++ b/src/pass/manager.rs @@ -223,14 +223,12 @@ mod tests { let context = Context::new(); let manager = Manager::new(&context); - assert_eq!( - parse_pass_pipeline( - manager.as_operation_pass_manager(), - "builtin.module(func.func(print-op-stats{json=false}),\ + insta::assert_display_snapshot!(parse_pass_pipeline( + manager.as_operation_pass_manager(), + "builtin.module(func.func(print-op-stats{json=false}),\ func.func(print-op-stats{json=false}))" - ), - Err(Error::ParsePassPipeline("MLIR Textual PassPipeline Parser:1:11: error: 'print-op-stats' does not refer to a registered pass or pass pipeline\nfunc.func(print-op-stats{json=false}),func.func(print-op-stats{json=false})\n ^\n".into())) - ); + ) + .unwrap_err()); register_print_operation_stats(); diff --git a/src/pass/snapshots/melior__pass__manager__tests__parse_pass_pipeline_.snap b/src/pass/snapshots/melior__pass__manager__tests__parse_pass_pipeline_.snap new file mode 100644 index 0000000000..40ee59c816 --- /dev/null +++ b/src/pass/snapshots/melior__pass__manager__tests__parse_pass_pipeline_.snap @@ -0,0 +1,9 @@ +--- +source: src/pass/manager.rs +expression: "parse_pass_pipeline(manager.as_operation_pass_manager(),\n \"builtin.module(func.func(print-op-stats{json=false}),\\\n func.func(print-op-stats{json=false}))\").unwrap_err()" +--- +failed to parse pass pipeline: +MLIR Textual PassPipeline Parser:1:11: error: 'print-op-stats' does not refer to a registered pass or pass pipeline +func.func(print-op-stats{json=false}),func.func(print-op-stats{json=false}) + ^ + From f3625b37fb43e5aac99d671f6972a01010c27b69 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 15:08:40 +1000 Subject: [PATCH 14/16] Remove unused import --- src/pass/manager.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pass/manager.rs b/src/pass/manager.rs index fc1ec62c98..93494c6182 100644 --- a/src/pass/manager.rs +++ b/src/pass/manager.rs @@ -84,7 +84,6 @@ mod tests { ir::{Location, Module}, pass::{self, transform::register_print_operation_stats}, utility::{parse_pass_pipeline, register_all_dialects}, - Error, }; use indoc::indoc; use pretty_assertions::assert_eq; From aa01c626adaa4e7d4b2310df3299a089f11a149c Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 15:14:04 +1000 Subject: [PATCH 15/16] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 478d1d155b..6ad10036b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "melior" description = "The rustic MLIR bindings in Rust" -version = "0.3.2" +version = "0.4.0" edition = "2021" license = "Apache-2.0" repository = "https://github.com/raviqqe/melior" From 2503d7ea23a418664391aa8519b27e3c698f332f Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 6 May 2023 15:14:14 +1000 Subject: [PATCH 16/16] Update lock file --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 256bf25bd2..fcba7c9091 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,7 +167,7 @@ dependencies = [ [[package]] name = "melior" -version = "0.3.2" +version = "0.4.0" dependencies = [ "indoc", "insta",