Compare commits
1 Commits
master
...
misc/impl-
Author | SHA1 | Date |
---|---|---|
David Mak | 675a0bb7ab |
10
flake.nix
10
flake.nix
|
@ -107,18 +107,18 @@
|
|||
(pkgs.fetchFromGitHub {
|
||||
owner = "m-labs";
|
||||
repo = "sipyco";
|
||||
rev = "094a6cd63ffa980ef63698920170e50dc9ba77fd";
|
||||
sha256 = "sha256-PPnAyDedUQ7Og/Cby9x5OT9wMkNGTP8GS53V6N/dk4w=";
|
||||
rev = "939f84f9b5eef7efbf7423c735d1834783b6140e";
|
||||
sha256 = "sha256-15Nun4EY35j+6SPZkjzZtyH/ncxLS60KuGJjFh5kSTc=";
|
||||
})
|
||||
(pkgs.fetchFromGitHub {
|
||||
owner = "m-labs";
|
||||
repo = "artiq";
|
||||
rev = "28c9de3e251daa89a8c9fd79d5ab64a3ec03bac6";
|
||||
sha256 = "sha256-vAvpbHc5B+1wtG8zqN7j9dQE1ON+i22v+uqA+tw6Gak=";
|
||||
rev = "923ca3377d42c815f979983134ec549dc39d3ca0";
|
||||
sha256 = "sha256-oJoEeNEeNFSUyh6jXG8Tzp6qHVikeHS0CzfE+mODPgw=";
|
||||
})
|
||||
];
|
||||
buildInputs = [
|
||||
(python3-mimalloc.withPackages(ps: [ ps.numpy ps.scipy ps.jsonschema ps.lmdb ps.platformdirs nac3artiq-instrumented ]))
|
||||
(python3-mimalloc.withPackages(ps: [ ps.numpy ps.scipy ps.jsonschema ps.lmdb nac3artiq-instrumented ]))
|
||||
pkgs.llvmPackages_14.llvm.out
|
||||
];
|
||||
phases = [ "buildPhase" "installPhase" ];
|
||||
|
|
|
@ -206,7 +206,7 @@ class Core:
|
|||
embedding = EmbeddingMap()
|
||||
|
||||
if allow_registration:
|
||||
compiler.analyze(registered_functions, registered_classes, set())
|
||||
compiler.analyze(registered_functions, registered_classes)
|
||||
allow_registration = False
|
||||
|
||||
if hasattr(method, "__self__"):
|
||||
|
|
|
@ -24,14 +24,15 @@ use parking_lot::{Mutex, RwLock};
|
|||
use pyo3::{
|
||||
create_exception, exceptions,
|
||||
prelude::*,
|
||||
types::{PyBytes, PyDict, PyNone, PySet},
|
||||
types::{PyBytes, PyDict, PySet},
|
||||
};
|
||||
use tempfile::{self, TempDir};
|
||||
|
||||
use nac3core::{
|
||||
codegen::{
|
||||
concrete_type::ConcreteTypeStore, gen_func_impl, irrt::load_irrt, CodeGenLLVMOptions,
|
||||
CodeGenTargetMachineOptions, CodeGenTask, CodeGenerator, WithCall, WorkerRegistry,
|
||||
concrete_type::ConcreteTypeStore, gen_func_impl, irrt::load_irrt,
|
||||
tracert::TraceRuntimeConfig, CodeGenLLVMOptions, CodeGenTargetMachineOptions, CodeGenTask,
|
||||
CodeGenerator, WithCall, WorkerRegistry,
|
||||
},
|
||||
inkwell::{
|
||||
context::Context,
|
||||
|
@ -142,32 +143,14 @@ impl Nac3 {
|
|||
module: &PyObject,
|
||||
registered_class_ids: &HashSet<u64>,
|
||||
) -> PyResult<()> {
|
||||
let (module_name, source_file, source) =
|
||||
Python::with_gil(|py| -> PyResult<(String, String, String)> {
|
||||
let module: &PyAny = module.extract(py)?;
|
||||
let source_file = module.getattr("__file__");
|
||||
let (source_file, source) = if let Ok(source_file) = source_file {
|
||||
let source_file = source_file.extract()?;
|
||||
(
|
||||
source_file,
|
||||
fs::read_to_string(&source_file).map_err(|e| {
|
||||
exceptions::PyIOError::new_err(format!(
|
||||
"failed to read input file: {e}"
|
||||
))
|
||||
})?,
|
||||
)
|
||||
} else {
|
||||
// kernels submitted by content have no file
|
||||
// but still can provide source by StringLoader
|
||||
let get_src_fn = module
|
||||
.getattr("__loader__")?
|
||||
.extract::<PyObject>()?
|
||||
.getattr(py, "get_source")?;
|
||||
("<expcontent>", get_src_fn.call1(py, (PyNone::get(py),))?.extract(py)?)
|
||||
};
|
||||
Ok((module.getattr("__name__")?.extract()?, source_file.to_string(), source))
|
||||
})?;
|
||||
let (module_name, source_file) = Python::with_gil(|py| -> PyResult<(String, String)> {
|
||||
let module: &PyAny = module.extract(py)?;
|
||||
Ok((module.getattr("__name__")?.extract()?, module.getattr("__file__")?.extract()?))
|
||||
})?;
|
||||
|
||||
let source = fs::read_to_string(&source_file).map_err(|e| {
|
||||
exceptions::PyIOError::new_err(format!("failed to read input file: {e}"))
|
||||
})?;
|
||||
let parser_result = parse_program(&source, source_file.into())
|
||||
.map_err(|e| exceptions::PySyntaxError::new_err(format!("parse error: {e}")))?;
|
||||
|
||||
|
@ -716,8 +699,13 @@ impl Nac3 {
|
|||
let membuffer = membuffers.clone();
|
||||
let mut has_return = false;
|
||||
py.allow_threads(|| {
|
||||
let (registry, handles) =
|
||||
WorkerRegistry::create_workers(threads, top_level.clone(), &self.llvm_options, &f);
|
||||
let (registry, handles) = WorkerRegistry::create_workers(
|
||||
threads,
|
||||
top_level.clone(),
|
||||
&self.llvm_options,
|
||||
&TraceRuntimeConfig::default(),
|
||||
&f,
|
||||
);
|
||||
|
||||
let mut generator = ArtiqCodeGenerator::new("main".to_string(), size_t, self.time_fns);
|
||||
let context = Context::create();
|
||||
|
@ -1090,12 +1078,7 @@ impl Nac3 {
|
|||
})
|
||||
}
|
||||
|
||||
fn analyze(
|
||||
&mut self,
|
||||
functions: &PySet,
|
||||
classes: &PySet,
|
||||
content_modules: &PySet,
|
||||
) -> PyResult<()> {
|
||||
fn analyze(&mut self, functions: &PySet, classes: &PySet) -> PyResult<()> {
|
||||
let (modules, class_ids) =
|
||||
Python::with_gil(|py| -> PyResult<(HashMap<u64, PyObject>, HashSet<u64>)> {
|
||||
let mut modules: HashMap<u64, PyObject> = HashMap::new();
|
||||
|
@ -1105,22 +1088,14 @@ impl Nac3 {
|
|||
let getmodule_fn = PyModule::import(py, "inspect")?.getattr("getmodule")?;
|
||||
|
||||
for function in functions {
|
||||
let module: PyObject = getmodule_fn.call1((function,))?.extract()?;
|
||||
if !module.is_none(py) {
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
}
|
||||
let module = getmodule_fn.call1((function,))?.extract()?;
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
}
|
||||
for class in classes {
|
||||
let module: PyObject = getmodule_fn.call1((class,))?.extract()?;
|
||||
if !module.is_none(py) {
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
}
|
||||
let module = getmodule_fn.call1((class,))?.extract()?;
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
class_ids.insert(id_fn.call1((class,))?.extract()?);
|
||||
}
|
||||
for module in content_modules {
|
||||
let module: PyObject = module.extract()?;
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module.into());
|
||||
}
|
||||
Ok((modules, class_ids))
|
||||
})?;
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||
default = ["derive"]
|
||||
derive = ["dep:nac3core_derive"]
|
||||
no-escape-analysis = []
|
||||
tracing = []
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.13"
|
||||
|
@ -31,4 +32,5 @@ indoc = "2.0"
|
|||
insta = "=1.11.0"
|
||||
|
||||
[build-dependencies]
|
||||
itertools = "0.13"
|
||||
regex = "1.10"
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use std::ffi::OsStr;
|
||||
use std::{
|
||||
env,
|
||||
fs::File,
|
||||
|
@ -6,14 +7,28 @@ use std::{
|
|||
process::{Command, Stdio},
|
||||
};
|
||||
|
||||
use itertools::Itertools;
|
||||
use regex::Regex;
|
||||
|
||||
fn main() {
|
||||
struct IRRTCompilation<'a> {
|
||||
pub file: &'a str,
|
||||
pub gcc_options: Vec<&'a str>,
|
||||
pub cargo_instructions: Vec<&'a str>,
|
||||
}
|
||||
|
||||
/// Extracts the extension-less filename from a [`Path`].
|
||||
fn path_to_extless_filename(path: &Path) -> &str {
|
||||
path.file_name().map(Path::new).and_then(Path::file_stem).and_then(OsStr::to_str).unwrap()
|
||||
}
|
||||
|
||||
/// Compiles a source C file into LLVM bitcode.
|
||||
fn compile_file_to_ir(compile_opts: &IRRTCompilation) {
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
||||
let out_dir = Path::new(&out_dir);
|
||||
let out_path = Path::new(&out_dir);
|
||||
let irrt_dir = Path::new("irrt");
|
||||
|
||||
let irrt_cpp_path = irrt_dir.join("irrt.cpp");
|
||||
let path = Path::new(compile_opts.file);
|
||||
let filename_without_ext = path_to_extless_filename(path);
|
||||
|
||||
/*
|
||||
* HACK: Sadly, clang doesn't let us emit generic LLVM bitcode.
|
||||
|
@ -35,22 +50,22 @@ fn main() {
|
|||
"-",
|
||||
"-I",
|
||||
irrt_dir.to_str().unwrap(),
|
||||
irrt_cpp_path.to_str().unwrap(),
|
||||
];
|
||||
|
||||
// Apply custom flags from IRRTCompilation
|
||||
flags.extend_from_slice(&compile_opts.gcc_options);
|
||||
|
||||
match env::var("PROFILE").as_deref() {
|
||||
Ok("debug") => {
|
||||
flags.push("-O0");
|
||||
flags.push("-DIRRT_DEBUG_ASSERT");
|
||||
}
|
||||
Ok("release") => {
|
||||
flags.push("-O3");
|
||||
}
|
||||
Ok("debug") => flags.extend_from_slice(&["-O0", "-DIRRT_DEBUG_ASSERT"]),
|
||||
Ok("release") => flags.push("-O3"),
|
||||
flavor => panic!("Unknown or missing build flavor {flavor:?}"),
|
||||
}
|
||||
|
||||
// Tell Cargo to rerun if any file under `irrt_dir` (recursive) changes
|
||||
println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap());
|
||||
flags.push(path.to_str().unwrap());
|
||||
|
||||
// Tell Cargo to rerun if the main IRRT source is changed
|
||||
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
|
||||
compile_opts.cargo_instructions.iter().for_each(|inst| println!("cargo::{inst}"));
|
||||
|
||||
// Compile IRRT and capture the LLVM IR output
|
||||
let output = Command::new("clang-irrt")
|
||||
|
@ -61,8 +76,7 @@ fn main() {
|
|||
})
|
||||
.unwrap();
|
||||
|
||||
// https://github.com/rust-lang/regex/issues/244
|
||||
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
|
||||
let output = std::str::from_utf8(&output.stdout).unwrap();
|
||||
let mut filtered_output = String::with_capacity(output.len());
|
||||
|
||||
// Filter out irrelevant IR
|
||||
|
@ -76,7 +90,7 @@ fn main() {
|
|||
r"(?ms:^define.*?\}$)|(?m:^declare.*?$)|(?m:^%.+?=\s*type\s*\{.+?\}$)|(?m:^@.+?=.+$)",
|
||||
)
|
||||
.unwrap();
|
||||
for f in regex_filter.captures_iter(&output) {
|
||||
for f in regex_filter.captures_iter(output) {
|
||||
assert_eq!(f.len(), 1);
|
||||
filtered_output.push_str(&f[0]);
|
||||
filtered_output.push('\n');
|
||||
|
@ -90,20 +104,47 @@ fn main() {
|
|||
// Doing `DEBUG_DUMP_IRRT=1 cargo build -p nac3core` dumps the LLVM IR generated
|
||||
const DEBUG_DUMP_IRRT: &str = "DEBUG_DUMP_IRRT";
|
||||
println!("cargo:rerun-if-env-changed={DEBUG_DUMP_IRRT}");
|
||||
if env::var(DEBUG_DUMP_IRRT).is_ok() {
|
||||
let mut file = File::create(out_dir.join("irrt.ll")).unwrap();
|
||||
if env::var("DEBUG_DUMP_IRRT").is_ok() {
|
||||
let mut file = File::create(out_path.join(format!("{filename_without_ext}.ll"))).unwrap();
|
||||
file.write_all(output.as_bytes()).unwrap();
|
||||
|
||||
let mut file = File::create(out_dir.join("irrt-filtered.ll")).unwrap();
|
||||
let mut file =
|
||||
File::create(out_path.join(format!("{filename_without_ext}-filtered.ll"))).unwrap();
|
||||
file.write_all(filtered_output.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
let mut llvm_as = Command::new("llvm-as-irrt")
|
||||
.stdin(Stdio::piped())
|
||||
.arg("-o")
|
||||
.arg(out_dir.join("irrt.bc"))
|
||||
.arg(out_path.join(format!("{filename_without_ext}.bc")))
|
||||
.spawn()
|
||||
.unwrap();
|
||||
llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap();
|
||||
assert!(llvm_as.wait().unwrap().success());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let irrt_compilations: &[IRRTCompilation] = &[
|
||||
IRRTCompilation {
|
||||
file: "irrt/irrt.cpp",
|
||||
gcc_options: Vec::default(),
|
||||
cargo_instructions: vec!["rerun-if-changed=irrt/irrt"],
|
||||
},
|
||||
IRRTCompilation {
|
||||
file: "irrt/tracert.cpp",
|
||||
gcc_options: Vec::default(),
|
||||
cargo_instructions: Vec::default(),
|
||||
},
|
||||
];
|
||||
|
||||
assert!(irrt_compilations
|
||||
.iter()
|
||||
.map(|comp| comp.file)
|
||||
.map(Path::new)
|
||||
.map(path_to_extless_filename)
|
||||
.all_unique());
|
||||
|
||||
for path in irrt_compilations {
|
||||
compile_file_to_ir(path)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
extern "C" {
|
||||
// stdio.h
|
||||
int printf(const char *format, ...);
|
||||
} // extern "C"
|
|
@ -1,8 +1,11 @@
|
|||
use std::iter::once;
|
||||
|
||||
use inkwell::{
|
||||
attributes::{Attribute, AttributeLoc},
|
||||
values::{BasicValueEnum, CallSiteValue, FloatValue, IntValue},
|
||||
values::{BasicValue, BasicValueEnum, CallSiteValue, FloatValue, IntValue},
|
||||
AddressSpace,
|
||||
};
|
||||
use itertools::Either;
|
||||
use itertools::{Either, Itertools};
|
||||
|
||||
use super::CodeGenContext;
|
||||
|
||||
|
@ -20,7 +23,7 @@ use super::CodeGenContext;
|
|||
/// These will be used unless other attributes are specified
|
||||
/// * `$(,$args:ident)*`: Operands of the extern function
|
||||
/// The data type of these operands will be set to `FloatValue`
|
||||
///
|
||||
///
|
||||
macro_rules! generate_extern_fn {
|
||||
("unary", $fn_name:ident, $extern_fn:literal) => {
|
||||
generate_extern_fn!($fn_name, $extern_fn, arg, "mustprogress", "nofree", "nounwind", "willreturn", "writeonly");
|
||||
|
@ -191,3 +194,49 @@ generate_linalg_extern_fn!(call_np_linalg_det, "np_linalg_det", 2);
|
|||
generate_linalg_extern_fn!(call_sp_linalg_lu, "sp_linalg_lu", 3);
|
||||
generate_linalg_extern_fn!(call_sp_linalg_schur, "sp_linalg_schur", 3);
|
||||
generate_linalg_extern_fn!(call_sp_linalg_hessenberg, "sp_linalg_hessenberg", 3);
|
||||
|
||||
/// Invokes the `printf` function.
|
||||
pub fn call_printf<'ctx>(
|
||||
ctx: &CodeGenContext<'ctx, '_>,
|
||||
format: &str,
|
||||
args: &[BasicValueEnum<'ctx>],
|
||||
) -> IntValue<'ctx> {
|
||||
const FN_NAME: &str = "printf";
|
||||
|
||||
let llvm_i8 = ctx.ctx.i8_type();
|
||||
let llvm_i32 = ctx.ctx.i32_type();
|
||||
let llvm_pi8 = llvm_i8.ptr_type(AddressSpace::default());
|
||||
|
||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
||||
let fn_type = llvm_i32.fn_type(&[llvm_pi8.into()], true);
|
||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
||||
for attr in ["nofree", "nounwind"] {
|
||||
func.add_attribute(
|
||||
AttributeLoc::Function,
|
||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
||||
);
|
||||
}
|
||||
|
||||
func
|
||||
});
|
||||
|
||||
let pformat = ctx
|
||||
.builder
|
||||
.build_global_string_ptr(&format!("{format}\0"), "")
|
||||
.map(|v| v.as_basic_value_enum())
|
||||
.map(BasicValueEnum::into_pointer_value)
|
||||
.unwrap();
|
||||
|
||||
let fn_args = once(&pformat.as_basic_value_enum())
|
||||
.chain(args)
|
||||
.copied()
|
||||
.map(BasicValueEnum::into)
|
||||
.collect_vec();
|
||||
|
||||
ctx.builder
|
||||
.build_call(extern_fn, fn_args.as_slice(), "")
|
||||
.map(CallSiteValue::try_as_basic_value)
|
||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
||||
.map(Either::unwrap_left)
|
||||
.unwrap()
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ use crate::{
|
|||
};
|
||||
use concrete_type::{ConcreteType, ConcreteTypeEnum, ConcreteTypeStore};
|
||||
pub use generator::{CodeGenerator, DefaultCodeGenerator};
|
||||
use tracert::{TraceRuntimeConfig, TraceRuntimeState};
|
||||
use types::{ListType, NDArrayType, ProxyType, RangeType};
|
||||
|
||||
pub mod builtin_fns;
|
||||
|
@ -49,6 +50,7 @@ pub mod irrt;
|
|||
pub mod llvm_intrinsics;
|
||||
pub mod numpy;
|
||||
pub mod stmt;
|
||||
pub mod tracert;
|
||||
pub mod types;
|
||||
pub mod values;
|
||||
|
||||
|
@ -220,6 +222,8 @@ pub struct CodeGenContext<'ctx, 'a> {
|
|||
/// See [`need_sret`].
|
||||
pub need_sret: bool,
|
||||
|
||||
pub tracert_state: Option<TraceRuntimeState>,
|
||||
|
||||
/// The current source location.
|
||||
pub current_loc: Location,
|
||||
}
|
||||
|
@ -267,6 +271,8 @@ pub struct WorkerRegistry {
|
|||
|
||||
/// LLVM-related options for code generation.
|
||||
pub llvm_options: CodeGenLLVMOptions,
|
||||
|
||||
tracert_config: TraceRuntimeConfig,
|
||||
}
|
||||
|
||||
impl WorkerRegistry {
|
||||
|
@ -276,6 +282,7 @@ impl WorkerRegistry {
|
|||
generators: Vec<Box<G>>,
|
||||
top_level_ctx: Arc<TopLevelContext>,
|
||||
llvm_options: &CodeGenLLVMOptions,
|
||||
tracert_config: &TraceRuntimeConfig,
|
||||
f: &Arc<WithCall>,
|
||||
) -> (Arc<WorkerRegistry>, Vec<thread::JoinHandle<()>>) {
|
||||
let (sender, receiver) = unbounded();
|
||||
|
@ -297,6 +304,7 @@ impl WorkerRegistry {
|
|||
wait_condvar,
|
||||
top_level_ctx,
|
||||
llvm_options: llvm_options.clone(),
|
||||
tracert_config: tracert_config.clone(),
|
||||
});
|
||||
|
||||
let mut handles = Vec::new();
|
||||
|
@ -980,6 +988,13 @@ pub fn gen_func_impl<
|
|||
unifier,
|
||||
static_value_store,
|
||||
need_sret: has_sret,
|
||||
tracert_state: if cfg!(feature = "tracing")
|
||||
|| registry.tracert_config.enabled_tags.is_empty()
|
||||
{
|
||||
None
|
||||
} else {
|
||||
Some(TraceRuntimeState::create(registry.tracert_config.clone()))
|
||||
},
|
||||
current_loc: Location::default(),
|
||||
debug_info: (dibuilder, compile_unit, func_scope.as_debug_info_scope()),
|
||||
};
|
||||
|
|
|
@ -17,6 +17,7 @@ use parking_lot::RwLock;
|
|||
|
||||
use super::{
|
||||
concrete_type::ConcreteTypeStore,
|
||||
tracert::TraceRuntimeConfig,
|
||||
types::{ListType, NDArrayType, ProxyType, RangeType},
|
||||
CodeGenContext, CodeGenLLVMOptions, CodeGenTargetMachineOptions, CodeGenTask, CodeGenerator,
|
||||
DefaultCodeGenerator, WithCall, WorkerRegistry,
|
||||
|
@ -245,7 +246,13 @@ fn test_primitives() {
|
|||
opt_level: OptimizationLevel::Default,
|
||||
target: CodeGenTargetMachineOptions::from_host_triple(),
|
||||
};
|
||||
let (registry, handles) = WorkerRegistry::create_workers(threads, top_level, &llvm_options, &f);
|
||||
let (registry, handles) = WorkerRegistry::create_workers(
|
||||
threads,
|
||||
top_level,
|
||||
&llvm_options,
|
||||
&TraceRuntimeConfig::default(),
|
||||
&f,
|
||||
);
|
||||
registry.add_task(task);
|
||||
registry.wait_tasks_complete(handles);
|
||||
}
|
||||
|
@ -438,7 +445,13 @@ fn test_simple_call() {
|
|||
opt_level: OptimizationLevel::Default,
|
||||
target: CodeGenTargetMachineOptions::from_host_triple(),
|
||||
};
|
||||
let (registry, handles) = WorkerRegistry::create_workers(threads, top_level, &llvm_options, &f);
|
||||
let (registry, handles) = WorkerRegistry::create_workers(
|
||||
threads,
|
||||
top_level,
|
||||
&llvm_options,
|
||||
&TraceRuntimeConfig::default(),
|
||||
&f,
|
||||
);
|
||||
registry.add_task(task);
|
||||
registry.wait_tasks_complete(handles);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
use std::panic::Location;
|
||||
|
||||
use inkwell::context::Context;
|
||||
use inkwell::memory_buffer::MemoryBuffer;
|
||||
use inkwell::module::Module;
|
||||
use inkwell::values::BasicValueEnum;
|
||||
use inkwell::AtomicOrdering;
|
||||
|
||||
use crate::codegen::{extern_fns, CodeGenContext};
|
||||
|
||||
#[derive(Clone, Default, Eq, PartialEq)]
|
||||
pub struct TraceRuntimeConfig {
|
||||
pub enabled_tags: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Eq, Clone, PartialEq)]
|
||||
pub struct TraceRuntimeState {
|
||||
config: TraceRuntimeConfig,
|
||||
indent: usize,
|
||||
}
|
||||
|
||||
impl TraceRuntimeState {
|
||||
#[must_use]
|
||||
pub fn create(config: TraceRuntimeConfig) -> TraceRuntimeState {
|
||||
TraceRuntimeState { config, indent: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn load_tracert<'ctx>(ctx: &'ctx Context, config: &TraceRuntimeConfig) -> Option<Module<'ctx>> {
|
||||
if cfg!(feature = "tracing") && !config.enabled_tags.is_empty() {
|
||||
let bitcode_buf = MemoryBuffer::create_from_memory_range(
|
||||
include_bytes!(concat!(env!("OUT_DIR"), "/tracert.bc")),
|
||||
"tracert_bitcode_buffer",
|
||||
);
|
||||
let module = Module::parse_bitcode_from_buffer(&bitcode_buf, ctx).unwrap();
|
||||
|
||||
return Some(module);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
// TODO: Might need to redesign how trace logging should be done
|
||||
|
||||
pub fn trace_log<'ctx>(
|
||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||
tag: &'static str,
|
||||
format: &'static str,
|
||||
args: &[BasicValueEnum<'ctx>],
|
||||
) {
|
||||
if ctx.tracert_state.is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Add indentation
|
||||
let str = format!("[TRACING] {tag} - {format}\n\0");
|
||||
extern_fns::call_printf(ctx, &str, args);
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub fn trace_log_with_location<'ctx>(
|
||||
ctx: &CodeGenContext<'ctx, '_>,
|
||||
tag: &'static str,
|
||||
format: &str,
|
||||
args: &[BasicValueEnum<'ctx>],
|
||||
) {
|
||||
if ctx.tracert_state.is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Add indentation
|
||||
let caller_loc = Location::caller();
|
||||
let str = format!(
|
||||
"[TRACING] {}:{}:{}: {tag} - {format}\n\0",
|
||||
caller_loc.file(),
|
||||
caller_loc.line(),
|
||||
caller_loc.column()
|
||||
);
|
||||
extern_fns::call_printf(ctx, &str, args);
|
||||
}
|
||||
|
||||
pub fn trace_push_level(ctx: &mut CodeGenContext<'_, '_>) {
|
||||
let Some(tracert_state) = &mut ctx.tracert_state else {
|
||||
return;
|
||||
};
|
||||
|
||||
debug_assert!(tracert_state.indent < usize::MAX);
|
||||
if tracert_state.indent < usize::MAX {
|
||||
tracert_state.indent = tracert_state.indent.saturating_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trace_pop_level(ctx: &mut CodeGenContext<'_, '_>) {
|
||||
let Some(tracert_state) = &mut ctx.tracert_state else {
|
||||
return;
|
||||
};
|
||||
|
||||
debug_assert!(tracert_state.indent > 0);
|
||||
if tracert_state.indent > 0 {
|
||||
tracert_state.indent = tracert_state.indent.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn mfence(ctx: &mut CodeGenContext<'_, '_>) {
|
||||
if ctx.tracert_state.is_some() {
|
||||
ctx.builder.build_fence(AtomicOrdering::SequentiallyConsistent, 0, "").unwrap();
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
|
||||
[features]
|
||||
no-escape-analysis = ["nac3core/no-escape-analysis"]
|
||||
tracing = ["nac3core/tracing"]
|
||||
|
||||
[dependencies]
|
||||
parking_lot = "0.12"
|
||||
|
|
|
@ -65,7 +65,7 @@ struct cslice {
|
|||
size_t len;
|
||||
};
|
||||
|
||||
void output_int32_list(struct cslice* slice) {
|
||||
void output_int32_list(const struct cslice* slice) {
|
||||
const int32_t* data = (int32_t*)slice->data;
|
||||
|
||||
putchar('[');
|
||||
|
@ -80,7 +80,7 @@ void output_int32_list(struct cslice* slice) {
|
|||
putchar('\n');
|
||||
}
|
||||
|
||||
void output_str(struct cslice* slice) {
|
||||
void output_str(const struct cslice* slice) {
|
||||
const char* data = (const char*)slice->data;
|
||||
|
||||
for (size_t i = 0; i < slice->len; ++i) {
|
||||
|
@ -88,12 +88,12 @@ void output_str(struct cslice* slice) {
|
|||
}
|
||||
}
|
||||
|
||||
void output_strln(struct cslice* slice) {
|
||||
void output_strln(const struct cslice* slice) {
|
||||
output_str(slice);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
uint64_t dbg_stack_address(__attribute__((unused)) struct cslice* slice) {
|
||||
uint64_t dbg_stack_address(__attribute__((unused)) const struct cslice* slice) {
|
||||
int i;
|
||||
void* ptr = (void*)&i;
|
||||
return (uintptr_t)ptr;
|
||||
|
|
|
@ -15,8 +15,11 @@ use parking_lot::{Mutex, RwLock};
|
|||
|
||||
use nac3core::{
|
||||
codegen::{
|
||||
concrete_type::ConcreteTypeStore, irrt::load_irrt, CodeGenLLVMOptions,
|
||||
CodeGenTargetMachineOptions, CodeGenTask, DefaultCodeGenerator, WithCall, WorkerRegistry,
|
||||
concrete_type::ConcreteTypeStore,
|
||||
irrt::load_irrt,
|
||||
tracert::{load_tracert, TraceRuntimeConfig},
|
||||
CodeGenLLVMOptions, CodeGenTargetMachineOptions, CodeGenTask, DefaultCodeGenerator,
|
||||
WithCall, WorkerRegistry,
|
||||
},
|
||||
inkwell::{
|
||||
memory_buffer::MemoryBuffer, module::Linkage, passes::PassBuilderOptions,
|
||||
|
@ -76,6 +79,9 @@ struct CommandLineArgs {
|
|||
/// Additional target features to enable/disable, specified using the `+`/`-` prefixes.
|
||||
#[arg(long)]
|
||||
target_features: Option<String>,
|
||||
|
||||
#[arg(long)]
|
||||
trace: Vec<String>,
|
||||
}
|
||||
|
||||
fn handle_typevar_definition(
|
||||
|
@ -276,8 +282,16 @@ fn handle_global_var(
|
|||
|
||||
fn main() {
|
||||
let cli = CommandLineArgs::parse();
|
||||
let CommandLineArgs { file_name, threads, opt_level, emit_llvm, triple, mcpu, target_features } =
|
||||
cli;
|
||||
let CommandLineArgs {
|
||||
file_name,
|
||||
threads,
|
||||
opt_level,
|
||||
emit_llvm,
|
||||
triple,
|
||||
mcpu,
|
||||
target_features,
|
||||
trace,
|
||||
} = cli;
|
||||
|
||||
Target::initialize_all(&InitializationConfig::default());
|
||||
|
||||
|
@ -306,6 +320,7 @@ fn main() {
|
|||
// The default behavior for -O<n> where n>3 defaults to O3 for both Clang and GCC
|
||||
_ => OptimizationLevel::Aggressive,
|
||||
};
|
||||
let tracert_config = TraceRuntimeConfig { enabled_tags: trace };
|
||||
|
||||
let target_machine_options = CodeGenTargetMachineOptions {
|
||||
triple,
|
||||
|
@ -350,6 +365,14 @@ fn main() {
|
|||
irrt.write_bitcode_to_path(Path::new("irrt.bc"));
|
||||
}
|
||||
|
||||
// Process tracert
|
||||
let tracert = load_tracert(&context, &tracert_config);
|
||||
if let Some(tracert) = &tracert {
|
||||
if emit_llvm {
|
||||
tracert.write_bitcode_to_path(Path::new("tracert.bc"));
|
||||
}
|
||||
}
|
||||
|
||||
// Process the Python script
|
||||
let parser_result = parser::parse_program(&program, file_name.into()).unwrap();
|
||||
|
||||
|
@ -458,7 +481,8 @@ fn main() {
|
|||
let threads = (0..threads)
|
||||
.map(|i| Box::new(DefaultCodeGenerator::new(format!("module{i}"), size_t)))
|
||||
.collect();
|
||||
let (registry, handles) = WorkerRegistry::create_workers(threads, top_level, &llvm_options, &f);
|
||||
let (registry, handles) =
|
||||
WorkerRegistry::create_workers(threads, top_level, &llvm_options, &tracert_config, &f);
|
||||
registry.add_task(task);
|
||||
registry.wait_tasks_complete(handles);
|
||||
|
||||
|
@ -484,6 +508,9 @@ fn main() {
|
|||
}
|
||||
|
||||
main.link_in_module(irrt).unwrap();
|
||||
if let Some(tracert) = tracert {
|
||||
main.link_in_module(tracert).unwrap();
|
||||
}
|
||||
|
||||
// Private all functions except "run"
|
||||
let mut function_iter = main.get_first_function();
|
||||
|
|
Loading…
Reference in New Issue