1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
use std::ffi::c_void;
use inkwell::module::Module;
use inkwell::values::{AsValueRef, FunctionValue};
use crate::{LlvmFunctionAnalysis, LlvmModuleAnalysis};
/// Struct allowing to query the pass manager for the result of
/// analyses on function IR.
pub struct FunctionAnalysisManager {
inner: *mut c_void,
from_analysis_id: Option<crate::AnalysisKey>,
}
impl FunctionAnalysisManager {
#[doc(hidden)]
pub unsafe fn from_raw(
inner: *mut c_void,
from_analysis_id: Option<crate::AnalysisKey>,
) -> Self {
Self {
inner,
from_analysis_id,
}
}
/// Returns the result of the analysis on a given function IR.
///
/// If the result is not in cache, the pass manager will execute the
/// analysis pass. Otherwise, the result is directly returned from cache.
///
/// # Panics
///
/// Panics if the given analysis wasn't registered, or if this function was
/// called within the given analysis itself.
pub fn get_result<A>(&self, function: &FunctionValue<'_>) -> &A::Result
where
A: crate::LlvmFunctionAnalysis,
{
let id = A::id();
assert!(
!matches!(self.from_analysis_id, Some(n) if id == n),
"Analysis cannot request its own result"
);
unsafe {
let res =
crate::get_function_analysis_result(self.inner, id, function.as_value_ref().cast());
Box::leak(Box::from_raw(res.cast()))
}
}
/// Returns the result of the analysis on a given function IR.
///
/// If the result is not in cache, `None` is returned. Otherwise,
/// the result is directly returned from cache.
///
/// This function never triggers the execution of an analysis.
///
/// # Panics
///
/// Panics if the given analysis wasn't registered, or if this function was
/// called within the given analysis itself.
pub fn get_cached_result<A>(&self, function: &FunctionValue<'_>) -> Option<&A::Result>
where
A: crate::LlvmFunctionAnalysis,
{
let id = A::id();
assert!(
!matches!(self.from_analysis_id, Some(n) if id == n),
"Analysis cannot request its own result"
);
unsafe {
let res = crate::get_function_analysis_cached_result(
self.inner,
id,
function.as_value_ref().cast(),
);
(!res.is_null()).then_some(Box::leak(Box::from_raw(res.cast())))
}
}
/// Register an analysis pass to the analysis manager.
///
/// # Panics
///
/// Panics if the given analysis was already registered.
pub fn register_pass<T>(&mut self, pass: T)
where
T: LlvmFunctionAnalysis,
{
let pass = Box::new(pass);
extern "C" fn result_deleter<T>(data: *mut c_void)
where
T: LlvmFunctionAnalysis,
{
drop(unsafe { Box::<<T as LlvmFunctionAnalysis>::Result>::from_raw(data.cast()) })
}
extern "C" fn pass_deleter<T>(pass: *mut c_void) {
drop(unsafe { Box::<T>::from_raw(pass.cast()) })
}
extern "C" fn pass_entrypoint<T>(
pass: *mut c_void,
function: *mut c_void,
manager: *mut c_void,
res: *mut *mut c_void,
res_deleter: *mut extern "C" fn(*mut c_void),
) where
T: LlvmFunctionAnalysis,
{
let pass = unsafe { Box::<T>::from_raw(pass.cast()) };
let function = unsafe { FunctionValue::new(function.cast()).unwrap() };
let manager = unsafe { FunctionAnalysisManager::from_raw(manager, Some(T::id())) };
let data = pass.run_analysis(&function, &manager);
let data = Box::new(data);
unsafe {
*res = Box::<<T as LlvmFunctionAnalysis>::Result>::into_raw(data).cast();
*res_deleter = result_deleter::<T>;
}
Box::into_raw(pass);
#[allow(forgetting_copy_types)]
std::mem::forget(function);
}
let success = unsafe {
super::functionAnalysisManagerRegisterPass(
self.inner,
Box::into_raw(pass).cast(),
pass_deleter::<T>,
pass_entrypoint::<T>,
T::id(),
)
};
assert!(success, "analysis already registered");
}
}
/// Struct allowing to query the pass manager for the result of
/// analyses on module IR.
pub struct ModuleAnalysisManager {
inner: *mut c_void,
from_analysis_id: Option<crate::AnalysisKey>,
}
impl ModuleAnalysisManager {
#[doc(hidden)]
pub unsafe fn from_raw(
inner: *mut c_void,
from_analysis_id: Option<crate::AnalysisKey>,
) -> Self {
Self {
inner,
from_analysis_id,
}
}
/// Returns the result of the analysis on a given module IR.
///
/// If the result is not in cache, the pass manager will execute the
/// analysis pass. Otherwise, the result is directly returned from cache.
///
/// # Panics
///
/// Panics if the given analysis wasn't registered, or if this function was
/// called within the given analysis itself.
pub fn get_result<A>(&self, module: &Module<'_>) -> &A::Result
where
A: crate::LlvmModuleAnalysis,
{
let id = A::id();
assert!(
!matches!(self.from_analysis_id, Some(n) if id == n),
"Analysis cannot request its own result"
);
let res =
crate::get_module_analysis_result(self.inner, A::id(), module.as_mut_ptr().cast());
unsafe { Box::leak(Box::from_raw(res.cast())) }
}
/// Returns the result of the analysis on a given module IR.
///
/// If the result is not in cache, `None` is returned. Otherwise,
/// the result is directly returned from cache.
///
/// This function never triggers the execution of an analysis.
///
/// # Panics
///
/// Panics if the given analysis wasn't registered, or if this function was
/// called within the given analysis itself.
pub fn get_cached_result<A>(&self, module: &Module<'_>) -> Option<&A::Result>
where
A: crate::LlvmModuleAnalysis,
{
let id = A::id();
assert!(
!matches!(self.from_analysis_id, Some(n) if id == n),
"Analysis cannot request its own result"
);
let res = crate::get_module_analysis_cached_result(
self.inner,
A::id(),
module.as_mut_ptr().cast(),
);
unsafe { (!res.is_null()).then_some(Box::leak(Box::from_raw(res.cast()))) }
}
/// Returns a [FunctionAnalysisManagerProxy], which is essentially an interface
/// allowing management of analyses at the function level.
pub fn get_function_analysis_manager_proxy(
&self,
module: &Module<'_>,
) -> FunctionAnalysisManagerProxy {
let proxy = crate::get_function_analysis_manager_module_proxy(
self.inner,
module.as_mut_ptr().cast(),
);
FunctionAnalysisManagerProxy { inner: proxy }
}
/// Register an analysis pass to the analysis manager.
///
/// # Panics
///
/// Panics if the given analysis was already registered.
pub fn register_pass<T>(&mut self, pass: T)
where
T: LlvmModuleAnalysis,
{
let pass = Box::new(pass);
extern "C" fn result_deleter<T>(data: *mut c_void)
where
T: LlvmModuleAnalysis,
{
drop(unsafe { Box::<<T as LlvmModuleAnalysis>::Result>::from_raw(data.cast()) })
}
extern "C" fn pass_deleter<T>(pass: *mut c_void) {
drop(unsafe { Box::<T>::from_raw(pass.cast()) })
}
extern "C" fn pass_entrypoint<T>(
pass: *mut c_void,
module: *mut c_void,
manager: *mut c_void,
res: *mut *mut c_void,
res_deleter: *mut extern "C" fn(*mut c_void),
) where
T: LlvmModuleAnalysis,
{
let pass = unsafe { Box::<T>::from_raw(pass.cast()) };
let module = unsafe { Module::new(module.cast()) };
let manager = unsafe { ModuleAnalysisManager::from_raw(manager, Some(T::id())) };
let data = pass.run_analysis(&module, &manager);
let data = Box::new(data);
unsafe {
*res = Box::<<T as LlvmModuleAnalysis>::Result>::into_raw(data).cast();
*res_deleter = result_deleter::<T>;
}
Box::into_raw(pass);
std::mem::forget(module);
}
let success = unsafe {
super::moduleAnalysisManagerRegisterPass(
self.inner,
Box::into_raw(pass).cast(),
pass_deleter::<T>,
pass_entrypoint::<T>,
T::id(),
)
};
assert!(success, "analysis already registered");
}
}
/// Struct allowing to make queries to the pass manager about function-level
/// analyses.
///
/// The main use-case of such interface is to give the ability for module-level
/// passes to trigger/query function-level analyses.
///
/// # Example
///
/// ```
/// # use llvm_plugin::inkwell::module::Module;
/// # use llvm_plugin::inkwell::values::FunctionValue;
/// # use llvm_plugin::{
/// # AnalysisKey, FunctionAnalysisManager, LlvmFunctionAnalysis, LlvmModulePass,
/// # ModuleAnalysisManager, PreservedAnalyses,
/// # };
/// struct Pass;
/// impl LlvmModulePass for Pass {
/// fn run_pass(
/// &self,
/// module: &mut Module,
/// manager: &ModuleAnalysisManager,
/// ) -> PreservedAnalyses {
/// let manager = manager
/// .get_function_analysis_manager_proxy(&module)
/// .get_manager();
///
/// let function = module.get_first_function().unwrap();
/// let result = manager.get_result::<Analysis>(&function);
/// assert_eq!(result, "Some result");
///
/// PreservedAnalyses::All
/// }
/// }
///
/// struct Analysis;
/// impl LlvmFunctionAnalysis for Analysis {
/// type Result = String;
///
/// fn run_analysis(
/// &self,
/// _function: &FunctionValue,
/// _manager: &FunctionAnalysisManager,
/// ) -> Self::Result {
/// "Some result".to_owned()
/// }
///
/// fn id() -> AnalysisKey {
/// 1 as AnalysisKey
/// }
/// }
/// ```
pub struct FunctionAnalysisManagerProxy {
inner: *mut c_void,
}
impl FunctionAnalysisManagerProxy {
/// Returns the inner [FunctionAnalysisManager].
pub fn get_manager(&self) -> FunctionAnalysisManager {
let manager = crate::get_function_analysis_manager(self.inner);
FunctionAnalysisManager {
inner: manager,
from_analysis_id: None,
}
}
}