refactor(rust): resolve artifacts' path and better stringRef mgmnt

This commit is contained in:
youben11
2022-11-29 14:00:39 +01:00
committed by Ayoub Benaissa
parent ad988de9a3
commit fbc60097ab
3 changed files with 59 additions and 11 deletions

View File

@@ -78,6 +78,14 @@ DEFINE_NULL_PTR_CHECKER(compilationFeedbackIsNull, CompilationFeedback);
/// Cpp object referenced, and a destroy function that does free this allocated
/// memory.
/// ********** Utilities *******************************************************
/// Destroy string references created by the compiler.
///
/// This is not supposed to destroy any string ref, but only the ones we have
/// allocated memory for and know how to free.
MLIR_CAPI_EXPORTED void mlirStringRefDestroy(MlirStringRef str);
/// ********** CompilationTarget CAPI ******************************************
enum CompilationTarget {
@@ -124,13 +132,10 @@ compilerEngineCompileSetOptions(CompilerEngine engine,
/// Get a string reference holding the textual representation of the compiled
/// module. The returned `MlirStringRef` should be destroyed using
/// `compilationResultDestroyModuleString` to free memory.
/// `mlirStringRefDestroy` to free memory.
MLIR_CAPI_EXPORTED MlirStringRef
compilationResultGetModuleString(CompilationResult result);
/// Free memory allocated for the module string.
MLIR_CAPI_EXPORTED void compilationResultDestroyModuleString(MlirStringRef str);
MLIR_CAPI_EXPORTED void compilationResultDestroy(CompilationResult result);
/// ********** Library CAPI ****************************************************
@@ -176,6 +181,12 @@ MLIR_CAPI_EXPORTED PublicResult
librarySupportServerCall(LibrarySupport support, ServerLambda server,
PublicArguments args, EvaluationKeys evalKeys);
MLIR_CAPI_EXPORTED MlirStringRef
librarySupportGetSharedLibPath(LibrarySupport support);
MLIR_CAPI_EXPORTED MlirStringRef
librarySupportGetClientParametersPath(LibrarySupport support);
MLIR_CAPI_EXPORTED void librarySupportDestroy(LibrarySupport support);
/// ********** ServerLamda CAPI ************************************************

View File

@@ -43,6 +43,21 @@ fn get_error_msg_from_ctype<T: CStructErrorMsg>(c_struct: T) -> String {
}
}
/// Create string from an MlirStringRef and free its memory.
///
/// # SAFETY
///
/// This should only be used with string refs returned by the compiler.
unsafe fn mlir_string_ref_to_string(str_ref: MlirStringRef) -> String {
let result = String::from_utf8_lossy(std::slice::from_raw_parts(
str_ref.data as *const u8,
str_ref.length as usize,
))
.to_string();
mlirStringRefDestroy(str_ref);
result
}
/// Parse the MLIR code and returns it.
///
/// The function parse the provided MLIR textual representation and returns it. It would fail with
@@ -81,12 +96,7 @@ pub fn round_trip(mlir_code: &str) -> Result<String, CompilerError> {
)));
}
let module_compiled = compilationResultGetModuleString(compilation_result);
let result_str = String::from_utf8_lossy(std::slice::from_raw_parts(
module_compiled.data as *const u8,
module_compiled.length as usize,
))
.to_string();
compilationResultDestroyModuleString(module_compiled);
let result_str = mlir_string_ref_to_string(module_compiled);
compilerEngineDestroy(engine);
Ok(result_str)
}
@@ -229,6 +239,16 @@ impl LibrarySupport {
Ok(result)
}
}
/// Get path to the compiled shared library
pub fn get_shared_lib_path(&self) -> String {
unsafe { mlir_string_ref_to_string(librarySupportGetSharedLibPath(self.support)) }
}
/// Get path to the client parameters
pub fn get_client_parameters_path(&self) -> String {
unsafe { mlir_string_ref_to_string(librarySupportGetClientParametersPath(self.support)) }
}
}
/// Support for keygen, encryption, and decryption.
@@ -419,7 +439,8 @@ mod test {
assert!(!libraryCompilationResultIsNull(lib));
libraryCompilationResultDestroy(lib);
// the sharedlib should be enough as a sign that the compilation worked
assert!(temp_dir.path().join("sharedlib.so").exists());
assert!(Path::new(support.get_shared_lib_path().as_str()).exists());
assert!(Path::new(support.get_client_parameters_path().as_str()).exists());
}
}

View File

@@ -253,6 +253,22 @@ PublicResult librarySupportServerCall(LibrarySupport support,
return wrap(resultOrError.get().release());
}
MlirStringRef librarySupportGetSharedLibPath(LibrarySupport support) {
auto path = unwrap(support)->getSharedLibPath();
// allocate buffer and copy module string
char *buffer = new char[path.length() + 1];
strcpy(buffer, path.c_str());
return mlirStringRefCreate(buffer, path.length());
}
MlirStringRef librarySupportGetClientParametersPath(LibrarySupport support) {
auto path = unwrap(support)->getClientParametersPath();
// allocate buffer and copy module string
char *buffer = new char[path.length() + 1];
strcpy(buffer, path.c_str());
return mlirStringRefCreate(buffer, path.length());
}
void librarySupportDestroy(LibrarySupport support) { C_STRUCT_CLEANER(support) }
/// ********** ServerLamda CAPI ************************************************