From 0300c81bb7ce6fe905c39f781db80dc6c1e0b54b Mon Sep 17 00:00:00 2001 From: lyken Date: Fri, 12 Jul 2024 13:18:09 +0800 Subject: [PATCH] core: add llvm.lifetime.{start.end} --- nac3core/src/codegen/llvm_intrinsics.rs | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/nac3core/src/codegen/llvm_intrinsics.rs b/nac3core/src/codegen/llvm_intrinsics.rs index 58dc3487..7c860a6a 100644 --- a/nac3core/src/codegen/llvm_intrinsics.rs +++ b/nac3core/src/codegen/llvm_intrinsics.rs @@ -35,6 +35,54 @@ fn get_float_intrinsic_repr(ctx: &Context, ft: FloatType) -> &'static str { unreachable!() } +/// Invokes the [`llvm.lifetime.start`](https://releases.llvm.org/14.0.0/docs/LangRef.html#llvm-lifetime-start-intrinsic) +/// intrinsic. +pub fn call_lifetime_start<'ctx>( + ctx: &CodeGenContext<'ctx, '_>, + size: IntValue<'ctx>, + ptr: PointerValue<'ctx>, +) { + const FN_NAME: &str = "llvm.lifetime.start"; + // NOTE: inkwell temporary workaround, see [`call_stackrestore`] for details + let intrinsic_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| { + let llvm_void = ctx.ctx.void_type(); + let llvm_i64 = ctx.ctx.i64_type(); + let llvm_p0i8 = ctx.ctx.i8_type().ptr_type(AddressSpace::default()); + let fn_type = llvm_void.fn_type(&[llvm_i64.into(), llvm_p0i8.into()], false); + + ctx.module.add_function(FN_NAME, fn_type, None) + }); + + ctx.builder + .build_call(intrinsic_fn, &[size.into(), ptr.into()], "") + .map(CallSiteValue::try_as_basic_value) + .unwrap(); +} + +/// Invokes the [`llvm.lifetime.end`](https://releases.llvm.org/14.0.0/docs/LangRef.html#llvm-lifetime-end-intrinsic) +/// intrinsic. +pub fn call_lifetime_end<'ctx>( + ctx: &CodeGenContext<'ctx, '_>, + size: IntValue<'ctx>, + ptr: PointerValue<'ctx>, +) { + const FN_NAME: &str = "llvm.lifetime.end"; + // NOTE: inkwell temporary workaround, see [`call_stackrestore`] for details + let intrinsic_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| { + let llvm_void = ctx.ctx.void_type(); + let llvm_i64 = ctx.ctx.i64_type(); + let llvm_p0i8 = ctx.ctx.i8_type().ptr_type(AddressSpace::default()); + let fn_type = llvm_void.fn_type(&[llvm_i64.into(), llvm_p0i8.into()], false); + + ctx.module.add_function(FN_NAME, fn_type, None) + }); + + ctx.builder + .build_call(intrinsic_fn, &[size.into(), ptr.into()], "") + .map(CallSiteValue::try_as_basic_value) + .unwrap(); +} + /// Invokes the [`llvm.stacksave`](https://llvm.org/docs/LangRef.html#llvm-stacksave-intrinsic) /// intrinsic. pub fn call_stacksave<'ctx>(