From 9e0601837a60988c56f6355cf06f7781d695bcb3 Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 12 Jul 2024 14:00:46 +0800 Subject: [PATCH] core: Add compile-time feature to disable escape analysis --- nac3artiq/Cargo.toml | 1 + nac3core/Cargo.toml | 3 +++ nac3core/src/typecheck/function_check.rs | 30 ++++++++++++++---------- nac3standalone/Cargo.toml | 3 +++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/nac3artiq/Cargo.toml b/nac3artiq/Cargo.toml index 10d7b0b3..e0803319 100644 --- a/nac3artiq/Cargo.toml +++ b/nac3artiq/Cargo.toml @@ -24,3 +24,4 @@ features = ["llvm14-0", "target-x86", "target-arm", "target-riscv", "no-libffi-l [features] init-llvm-profile = [] +no-escape-analysis = ["nac3core/no-escape-analysis"] diff --git a/nac3core/Cargo.toml b/nac3core/Cargo.toml index 724e0c8c..a25763bd 100644 --- a/nac3core/Cargo.toml +++ b/nac3core/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["M-Labs"] edition = "2021" +[features] +no-escape-analysis = [] + [dependencies] itertools = "0.13" crossbeam = "0.8" diff --git a/nac3core/src/typecheck/function_check.rs b/nac3core/src/typecheck/function_check.rs index b3790994..be2d1dce 100644 --- a/nac3core/src/typecheck/function_check.rs +++ b/nac3core/src/typecheck/function_check.rs @@ -212,19 +212,23 @@ impl<'a> Inferencer<'a> { /// This is a workaround preventing the caller from using a variable `alloca`-ed in the body, which /// is freed when the function returns. fn check_return_value_ty(&mut self, ret_ty: Type) -> bool { - match &*self.unifier.get_ty_immutable(ret_ty) { - TypeEnum::TObj { .. } => [ - self.primitives.int32, - self.primitives.int64, - self.primitives.uint32, - self.primitives.uint64, - self.primitives.float, - self.primitives.bool, - ] - .iter() - .any(|allowed_ty| self.unifier.unioned(ret_ty, *allowed_ty)), - TypeEnum::TTuple { ty, .. } => ty.iter().all(|t| self.check_return_value_ty(*t)), - _ => false, + if cfg!(feature = "no-escape-analysis") { + true + } else { + match &*self.unifier.get_ty_immutable(ret_ty) { + TypeEnum::TObj { .. } => [ + self.primitives.int32, + self.primitives.int64, + self.primitives.uint32, + self.primitives.uint64, + self.primitives.float, + self.primitives.bool, + ] + .iter() + .any(|allowed_ty| self.unifier.unioned(ret_ty, *allowed_ty)), + TypeEnum::TTuple { ty, .. } => ty.iter().all(|t| self.check_return_value_ty(*t)), + _ => false, + } } } diff --git a/nac3standalone/Cargo.toml b/nac3standalone/Cargo.toml index a55a26b9..4d43a763 100644 --- a/nac3standalone/Cargo.toml +++ b/nac3standalone/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["M-Labs"] edition = "2021" +[features] +no-escape-analysis = ["nac3core/no-escape-analysis"] + [dependencies] parking_lot = "0.12" nac3parser = { path = "../nac3parser" }