Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove string cache #364

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions melior/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
logical_result::LogicalResult,
string_ref::StringRef,
};
use dashmap::DashMap;
use mlir_sys::{
mlirContextAppendDialectRegistry, mlirContextAttachDiagnosticHandler, mlirContextCreate,
mlirContextDestroy, mlirContextDetachDiagnosticHandler, mlirContextEnableMultithreading,
Expand All @@ -13,7 +12,7 @@ use mlir_sys::{
mlirContextIsRegisteredOperation, mlirContextLoadAllAvailableDialects,
mlirContextSetAllowUnregisteredDialects, MlirContext, MlirDiagnostic, MlirLogicalResult,
};
use std::{ffi::c_void, marker::PhantomData, pin::Pin};
use std::{ffi::c_void, marker::PhantomData};

/// A context of IR, dialects, and passes.
///
Expand All @@ -22,17 +21,13 @@ use std::{ffi::c_void, marker::PhantomData, pin::Pin};
#[derive(Debug)]
pub struct Context {
raw: MlirContext,
// We need to pass null-terminated strings to functions in the MLIR API although
// Rust's strings are not.
string_cache: DashMap<Pin<String>, ()>,
}

impl Context {
/// Creates a context.
pub fn new() -> Self {
Self {
raw: unsafe { mlirContextCreate() },
string_cache: Default::default(),
}
}

Expand Down Expand Up @@ -124,10 +119,6 @@ impl Context {
pub(crate) fn to_ref(&self) -> ContextRef {
unsafe { ContextRef::from_raw(self.to_raw()) }
}

pub(crate) fn string_cache(&self) -> &DashMap<Pin<String>, ()> {
&self.string_cache
}
}

impl Drop for Context {
Expand Down
17 changes: 0 additions & 17 deletions melior/src/string_ref.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::Context;
use mlir_sys::{mlirStringRefEqual, MlirStringRef};
use std::{
ffi::CStr,
marker::PhantomData,
pin::Pin,
slice,
str::{self, Utf8Error},
};
Expand Down Expand Up @@ -40,21 +38,6 @@ impl<'a> StringRef<'a> {
unsafe { Self::from_raw(string) }
}

/// Converts a string into a null-terminated string reference.
#[deprecated]
pub fn from_str(context: &'a Context, string: &str) -> Self {
let entry = context
.string_cache()
.entry(Pin::new(string.into()))
.or_default();
let string = MlirStringRef {
data: entry.key().as_bytes().as_ptr() as *const i8,
length: entry.key().len(),
};

unsafe { Self::from_raw(string) }
}

/// Converts a string reference into a `str`.
pub fn as_str(&self) -> Result<&'a str, Utf8Error> {
unsafe {
Expand Down
Loading