From 9373418aaa711c8b29361d025fc03af91da2925e Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Tue, 10 Dec 2024 10:38:44 -0300 Subject: [PATCH] Add PassManager from/to raw methods --- melior/src/pass/manager.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/melior/src/pass/manager.rs b/melior/src/pass/manager.rs index 7f06623ab5..09a2c037ad 100644 --- a/melior/src/pass/manager.rs +++ b/melior/src/pass/manager.rs @@ -9,7 +9,7 @@ use mlir_sys::{ mlirPassManagerGetAsOpPassManager, mlirPassManagerGetNestedUnder, mlirPassManagerRunOnOp, MlirPassManager, }; -use std::marker::PhantomData; +use std::{marker::PhantomData, mem::forget}; /// A pass manager. pub struct PassManager<'c> { @@ -68,6 +68,29 @@ impl PassManager<'_> { pub fn as_operation_pass_manager(&self) -> OperationPassManager { unsafe { OperationPassManager::from_raw(mlirPassManagerGetAsOpPassManager(self.raw)) } } + + /// Creates a PassManager from the given raw pointer. + /// + /// # Safety + /// Caller must ensure this is a valid PassManager pointer. + pub unsafe fn from_raw(raw: MlirPassManager) -> Self { + Self { + raw, + _context: Default::default(), + } + } + + /// Gets the raw object of this pass manager. + pub const fn to_raw(&self) -> MlirPassManager { + self.raw + } + + /// Converts a PassManager into an owned raw object. + pub const fn into_raw(self) -> MlirPassManager { + let raw = self.raw; + forget(self); + raw + } } impl Drop for PassManager<'_> {