From f75ae786771441705e811214974ebf98416ee69b Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 23 Oct 2023 10:22:03 +0800 Subject: [PATCH 1/5] cargo: Update dependencies --- Cargo.lock | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 028486a..5936512 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ahash" -version = "0.7.6" +version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd" dependencies = [ "getrandom", "once_cell", @@ -15,14 +15,15 @@ dependencies = [ [[package]] name = "ahash" -version = "0.8.3" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +checksum = "cd7d5a2cecb58716e47d67d5703a249964b14c7be1ec3cad3affc295b2d1c35d" dependencies = [ "cfg-if", "getrandom", "once_cell", "version_check", + "zerocopy", ] [[package]] @@ -387,7 +388,7 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" dependencies = [ - "ahash 0.7.6", + "ahash 0.7.7", ] [[package]] @@ -674,7 +675,7 @@ dependencies = [ name = "nac3parser" version = "0.1.2" dependencies = [ - "ahash 0.8.3", + "ahash 0.8.5", "insta", "lalrpop", "lalrpop-util", @@ -1547,3 +1548,23 @@ checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" dependencies = [ "linked-hash-map", ] + +[[package]] +name = "zerocopy" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c19fae0c8a9efc6a8281f2e623db8af1db9e57852e04cde3e754dd2dc29340f" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc56589e9ddd1f1c28d4b4b5c773ce232910a6bb67a70133d61c9e347585efe9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] -- 2.44.1 From 8373a6cb0f24f24c48de27fb0b9f8d00934ba21d Mon Sep 17 00:00:00 2001 From: David Mak Date: Thu, 19 Oct 2023 11:54:56 +0800 Subject: [PATCH 2/5] artiq: Use gen_block when generating "with sequential" --- nac3artiq/src/codegen.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index 1128643..939ea1c 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -215,12 +215,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { return Ok(()); } else if id == &"sequential".into() { let start = self.start.take(); - for stmt in body.iter() { - self.gen_stmt(ctx, stmt)?; - if ctx.is_terminated() { - break; - } - } + gen_block(self, ctx, body.iter())?; self.start = start; return Ok(()); } -- 2.44.1 From 65d6104d00b67566f2abee9866c738d3d95f45ac Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 23 Oct 2023 13:35:29 +0800 Subject: [PATCH 3/5] artiq: Improve IR value naming and add documentation --- nac3artiq/src/codegen.rs | 25 +++++++++- nac3artiq/src/timeline.rs | 100 ++++++++++++++++++++++---------------- 2 files changed, 83 insertions(+), 42 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index 939ea1c..dc15d56 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -28,9 +28,17 @@ use std::{ pub struct ArtiqCodeGenerator<'a> { name: String, + + /// The size of a `size_t` variable in bits. size_t: u32, + + /// Monotonic counter for naming `start`/`stop` variables used by `with parallel` blocks. name_counter: u32, + + /// Variable for tracking the start of a `with parallel` block. start: Option>>, + + /// Variable for tracking the end of a `with parallel` block. end: Option>>, timeline: &'a (dyn TimeFns + Sync), } @@ -102,6 +110,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { if let StmtKind::With { items, body, .. } = &stmt.node { if items.len() == 1 && items[0].optional_vars.is_none() { let item = &items[0]; + // Behavior of parallel and sequential: // Each function call (indirectly, can be inside a sequential block) within a parallel // block will update the end variable to the maximum now_mu in the block. @@ -119,11 +128,15 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { if id == &"parallel".into() { let old_start = self.start.take(); let old_end = self.end.take(); + let now = if let Some(old_start) = &old_start { - self.gen_expr(ctx, old_start)?.unwrap().to_basic_value_enum(ctx, self, old_start.custom.unwrap())? + self.gen_expr(ctx, old_start)? + .unwrap() + .to_basic_value_enum(ctx, self, old_start.custom.unwrap())? } else { self.timeline.emit_now_mu(ctx) }; + // Emulate variable allocation, as we need to use the CodeGenContext // HashMap to store our variable due to lifetime limitation // Note: we should be able to store variables directly if generic @@ -157,8 +170,11 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { ctx.builder.build_store(end, now); self.end = Some(end_expr); self.name_counter += 1; + gen_block(self, ctx, body.iter())?; + let current = ctx.builder.get_insert_block().unwrap(); + // if the current block is terminated, move before the terminator // we want to set the timeline before reaching the terminator // TODO: This may be unsound if there are multiple exit paths in the @@ -172,6 +188,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { } else { false }; + // set duration let end_expr = self.end.take().unwrap(); let end_val = self @@ -183,6 +200,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { if old_start.is_none() { self.timeline.emit_at_mu(ctx, end_val); } + // inside a parallel block, should update the outer max now_mu if let Some(old_end) = &old_end { let outer_end_val = self @@ -207,20 +225,25 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { let outer_end = self.gen_store_target(ctx, old_end, Some("outer_end.addr"))?; ctx.builder.build_store(outer_end, max); } + self.start = old_start; self.end = old_end; + if reset_position { ctx.builder.position_at_end(current); } + return Ok(()); } else if id == &"sequential".into() { let start = self.start.take(); gen_block(self, ctx, body.iter())?; self.start = start; + return Ok(()); } } } + // not parallel/sequential gen_with(self, ctx, stmt) } else { diff --git a/nac3artiq/src/timeline.rs b/nac3artiq/src/timeline.rs index 47f9536..e3fc124 100644 --- a/nac3artiq/src/timeline.rs +++ b/nac3artiq/src/timeline.rs @@ -1,9 +1,16 @@ use inkwell::{values::BasicValueEnum, AddressSpace, AtomicOrdering}; use nac3core::codegen::CodeGenContext; +/// Functions for manipulating the timeline. pub trait TimeFns { + + /// Emits LLVM IR for `now_mu`. fn emit_now_mu<'ctx, 'a>(&self, ctx: &mut CodeGenContext<'ctx, 'a>) -> BasicValueEnum<'ctx>; + + /// Emits LLVM IR for `at_mu`. fn emit_at_mu<'ctx, 'a>(&self, ctx: &mut CodeGenContext<'ctx, 'a>, t: BasicValueEnum<'ctx>); + + /// Emits LLVM IR for `delay_mu`. fn emit_delay_mu<'ctx, 'a>(&self, ctx: &mut CodeGenContext<'ctx, 'a>, dt: BasicValueEnum<'ctx>); } @@ -20,23 +27,25 @@ impl TimeFns for NowPinningTimeFns64 { .get_global("now") .unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now")); let now_hiptr = - ctx.builder.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now_hiptr"); + ctx.builder.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now.hi.addr"); + if let BasicValueEnum::PointerValue(now_hiptr) = now_hiptr { let now_loptr = unsafe { - ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now_gep") + ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now.lo.addr") }; + if let (BasicValueEnum::IntValue(now_hi), BasicValueEnum::IntValue(now_lo)) = ( - ctx.builder.build_load(now_hiptr, "now_hi"), - ctx.builder.build_load(now_loptr, "now_lo"), + ctx.builder.build_load(now_hiptr, "now.hi"), + ctx.builder.build_load(now_loptr, "now.lo"), ) { - let zext_hi = ctx.builder.build_int_z_extend(now_hi, i64_type, "now_zext_hi"); + let zext_hi = ctx.builder.build_int_z_extend(now_hi, i64_type, ""); let shifted_hi = ctx.builder.build_left_shift( zext_hi, i64_type.const_int(32, false), - "now_shifted_zext_hi", + "", ); - let zext_lo = ctx.builder.build_int_z_extend(now_lo, i64_type, "now_zext_lo"); - ctx.builder.build_or(shifted_hi, zext_lo, "now_or").into() + let zext_lo = ctx.builder.build_int_z_extend(now_lo, i64_type, ""); + ctx.builder.build_or(shifted_hi, zext_lo, "now_mu").into() } else { unreachable!(); } @@ -48,14 +57,15 @@ impl TimeFns for NowPinningTimeFns64 { fn emit_at_mu<'ctx, 'a>(&self, ctx: &mut CodeGenContext<'ctx, 'a>, t: BasicValueEnum<'ctx>) { let i32_type = ctx.ctx.i32_type(); let i64_type = ctx.ctx.i64_type(); + let i64_32 = i64_type.const_int(32, false); if let BasicValueEnum::IntValue(time) = t { let time_hi = ctx.builder.build_int_truncate( - ctx.builder.build_right_shift(time, i64_32, false, "now_lshr"), + ctx.builder.build_right_shift(time, i64_32, false, "time.hi"), i32_type, - "now_trunc", + "", ); - let time_lo = ctx.builder.build_int_truncate(time, i32_type, "now_trunc"); + let time_lo = ctx.builder.build_int_truncate(time, i32_type, "time.lo"); let now = ctx .module .get_global("now") @@ -63,11 +73,12 @@ impl TimeFns for NowPinningTimeFns64 { let now_hiptr = ctx.builder.build_bitcast( now, i32_type.ptr_type(AddressSpace::default()), - "now_bitcast", + "now.hi.addr", ); + if let BasicValueEnum::PointerValue(now_hiptr) = now_hiptr { let now_loptr = unsafe { - ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now_gep") + ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now.lo.addr") }; ctx.builder .build_store(now_hiptr, time_hi) @@ -97,41 +108,43 @@ impl TimeFns for NowPinningTimeFns64 { .get_global("now") .unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now")); let now_hiptr = - ctx.builder.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now_hiptr"); + ctx.builder.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now.hi.addr"); + if let BasicValueEnum::PointerValue(now_hiptr) = now_hiptr { let now_loptr = unsafe { - ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now_loptr") + ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now.lo.addr") }; + if let ( BasicValueEnum::IntValue(now_hi), BasicValueEnum::IntValue(now_lo), BasicValueEnum::IntValue(dt), ) = ( - ctx.builder.build_load(now_hiptr, "now_hi"), - ctx.builder.build_load(now_loptr, "now_lo"), + ctx.builder.build_load(now_hiptr, "now.hi"), + ctx.builder.build_load(now_loptr, "now.lo"), dt, ) { - let zext_hi = ctx.builder.build_int_z_extend(now_hi, i64_type, "now_zext_hi"); + let zext_hi = ctx.builder.build_int_z_extend(now_hi, i64_type, ""); let shifted_hi = ctx.builder.build_left_shift( zext_hi, i64_type.const_int(32, false), - "now_shifted_zext_hi", + "", ); - let zext_lo = ctx.builder.build_int_z_extend(now_lo, i64_type, "now_zext_lo"); - let now_val = ctx.builder.build_or(shifted_hi, zext_lo, "now_or"); + let zext_lo = ctx.builder.build_int_z_extend(now_lo, i64_type, ""); + let now_val = ctx.builder.build_or(shifted_hi, zext_lo, "now"); - let time = ctx.builder.build_int_add(now_val, dt, "now_add"); + let time = ctx.builder.build_int_add(now_val, dt, "time"); let time_hi = ctx.builder.build_int_truncate( ctx.builder.build_right_shift( time, i64_type.const_int(32, false), false, - "now_lshr", + "", ), i32_type, - "now_trunc", + "time.hi", ); - let time_lo = ctx.builder.build_int_truncate(time, i32_type, "now_trunc"); + let time_lo = ctx.builder.build_int_truncate(time, i32_type, "time.lo"); ctx.builder .build_store(now_hiptr, time_hi) @@ -162,11 +175,12 @@ impl TimeFns for NowPinningTimeFns { .get_global("now") .unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now")); let now_raw = ctx.builder.build_load(now.as_pointer_value(), "now"); + if let BasicValueEnum::IntValue(now_raw) = now_raw { let i64_32 = i64_type.const_int(32, false); - let now_lo = ctx.builder.build_left_shift(now_raw, i64_32, "now_shl"); - let now_hi = ctx.builder.build_right_shift(now_raw, i64_32, false, "now_lshr"); - ctx.builder.build_or(now_lo, now_hi, "now_or").into() + let now_lo = ctx.builder.build_left_shift(now_raw, i64_32, "now.lo"); + let now_hi = ctx.builder.build_right_shift(now_raw, i64_32, false, "now.hi"); + ctx.builder.build_or(now_lo, now_hi, "now_mu").into() } else { unreachable!(); } @@ -176,11 +190,12 @@ impl TimeFns for NowPinningTimeFns { let i32_type = ctx.ctx.i32_type(); let i64_type = ctx.ctx.i64_type(); let i64_32 = i64_type.const_int(32, false); + if let BasicValueEnum::IntValue(time) = t { let time_hi = ctx.builder.build_int_truncate( - ctx.builder.build_right_shift(time, i64_32, false, "now_lshr"), + ctx.builder.build_right_shift(time, i64_32, false, ""), i32_type, - "now_trunc", + "time.hi", ); let time_lo = ctx.builder.build_int_truncate(time, i32_type, "now_trunc"); let now = ctx @@ -190,11 +205,12 @@ impl TimeFns for NowPinningTimeFns { let now_hiptr = ctx.builder.build_bitcast( now, i32_type.ptr_type(AddressSpace::default()), - "now_bitcast", + "now.hi.addr", ); + if let BasicValueEnum::PointerValue(now_hiptr) = now_hiptr { let now_loptr = unsafe { - ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(1, false)], "now_gep") + ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(1, false)], "now.lo.addr") }; ctx.builder .build_store(now_hiptr, time_hi) @@ -224,26 +240,28 @@ impl TimeFns for NowPinningTimeFns { .module .get_global("now") .unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now")); - let now_raw = ctx.builder.build_load(now.as_pointer_value(), "now"); + let now_raw = ctx.builder.build_load(now.as_pointer_value(), ""); + if let (BasicValueEnum::IntValue(now_raw), BasicValueEnum::IntValue(dt)) = (now_raw, dt) { - let now_lo = ctx.builder.build_left_shift(now_raw, i64_32, "now_shl"); - let now_hi = ctx.builder.build_right_shift(now_raw, i64_32, false, "now_lshr"); - let now_val = ctx.builder.build_or(now_lo, now_hi, "now_or"); - let time = ctx.builder.build_int_add(now_val, dt, "now_add"); + let now_lo = ctx.builder.build_left_shift(now_raw, i64_32, "now.lo"); + let now_hi = ctx.builder.build_right_shift(now_raw, i64_32, false, "now.hi"); + let now_val = ctx.builder.build_or(now_lo, now_hi, "now_val"); + let time = ctx.builder.build_int_add(now_val, dt, "time"); let time_hi = ctx.builder.build_int_truncate( - ctx.builder.build_right_shift(time, i64_32, false, "now_lshr"), + ctx.builder.build_right_shift(time, i64_32, false, "time.hi"), i32_type, "now_trunc", ); - let time_lo = ctx.builder.build_int_truncate(time, i32_type, "now_trunc"); + let time_lo = ctx.builder.build_int_truncate(time, i32_type, "time.lo"); let now_hiptr = ctx.builder.build_bitcast( now, i32_type.ptr_type(AddressSpace::default()), - "now_bitcast", + "now.hi.addr", ); + if let BasicValueEnum::PointerValue(now_hiptr) = now_hiptr { let now_loptr = unsafe { - ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(1, false)], "now_gep") + ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(1, false)], "now.lo.addr") }; ctx.builder .build_store(now_hiptr, time_hi) -- 2.44.1 From cbd333ab105d8e96037f789507069ee7e1f87522 Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 24 Oct 2023 19:08:23 +0800 Subject: [PATCH 4/5] artiq: Extract `parallel` block timeline utilities --- nac3artiq/src/codegen.rs | 112 +++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 47 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index dc15d56..4867eae 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -52,6 +52,66 @@ impl<'a> ArtiqCodeGenerator<'a> { assert!(size_t == 32 || size_t == 64); ArtiqCodeGenerator { name, size_t, name_counter: 0, start: None, end: None, timeline } } + + /// If the generator is currently in a direct-`parallel` block context, emits IR that resets the + /// position of the timeline to the initial timeline position before entering the `parallel` + /// block. + /// + /// Direct-`parallel` block context refers to when the generator is generating statements whose + /// closest parent `with` statement is a `with parallel` block. + fn timeline_reset_start<'ctx, 'b>( + &mut self, + ctx: &mut CodeGenContext<'ctx, 'b> + ) -> Result<(), String> { + if let Some(start) = self.start.clone() { + let start_val = self.gen_expr(ctx, &start)? + .unwrap() + .to_basic_value_enum(ctx, self, start.custom.unwrap())?; + self.timeline.emit_at_mu(ctx, start_val); + } + + Ok(()) + } + + /// If the generator is currently in a `parallel` block context, emits IR that updates the + /// maximum end position of the `parallel` block as specified by the timeline `end` value. + /// + /// In general the `end` parameter should be set to `self.end` for updating the maximum end + /// position for the current `parallel` block. Other values can be passed in to update the + /// maximum end position for other `parallel` blocks. + /// + /// `parallel`-block context refers to when the generator is generating statements within a + /// (possibly indirect) `parallel` block. + fn timeline_update_end_max<'ctx, 'b>( + &mut self, + ctx: &mut CodeGenContext<'ctx, 'b>, + end: Option>>, + ) -> Result<(), String> { + if let Some(end) = end { + let old_end = self.gen_expr(ctx, &end)? + .unwrap() + .to_basic_value_enum(ctx, self, end.custom.unwrap())?; + let now = self.timeline.emit_now_mu(ctx); + let smax = ctx.module.get_function("llvm.smax.i64").unwrap_or_else(|| { + let i64 = ctx.ctx.i64_type(); + ctx.module.add_function( + "llvm.smax.i64", + i64.fn_type(&[i64.into(), i64.into()], false), + None, + ) + }); + let max = ctx + .builder + .build_call(smax, &[old_end.into(), now.into()], "smax") + .try_as_basic_value() + .left() + .unwrap(); + let end_store = self.gen_store_target(ctx, &end, Some(""))?; + ctx.builder.build_store(end_store, max); + } + + Ok(()) + } } impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { @@ -75,30 +135,10 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { params: Vec<(Option, ValueEnum<'ctx>)>, ) -> Result>, String> { let result = gen_call(self, ctx, obj, fun, params)?; - if let Some(end) = self.end.clone() { - let old_end = self.gen_expr(ctx, &end)?.unwrap().to_basic_value_enum(ctx, self, end.custom.unwrap())?; - let now = self.timeline.emit_now_mu(ctx); - let smax = ctx.module.get_function("llvm.smax.i64").unwrap_or_else(|| { - let i64 = ctx.ctx.i64_type(); - ctx.module.add_function( - "llvm.smax.i64", - i64.fn_type(&[i64.into(), i64.into()], false), - None, - ) - }); - let max = ctx - .builder - .build_call(smax, &[old_end.into(), now.into()], "smax") - .try_as_basic_value() - .left() - .unwrap(); - let end_store = self.gen_store_target(ctx, &end, Some("end_store.addr"))?; - ctx.builder.build_store(end_store, max); - } - if let Some(start) = self.start.clone() { - let start_val = self.gen_expr(ctx, &start)?.unwrap().to_basic_value_enum(ctx, self, start.custom.unwrap())?; - self.timeline.emit_at_mu(ctx, start_val); - } + + self.timeline_update_end_max(ctx, self.end.clone())?; + self.timeline_reset_start(ctx)?; + Ok(result) } @@ -202,29 +242,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { } // inside a parallel block, should update the outer max now_mu - if let Some(old_end) = &old_end { - let outer_end_val = self - .gen_expr(ctx, old_end)? - .unwrap() - .to_basic_value_enum(ctx, self, old_end.custom.unwrap())?; - let smax = - ctx.module.get_function("llvm.smax.i64").unwrap_or_else(|| { - let i64 = ctx.ctx.i64_type(); - ctx.module.add_function( - "llvm.smax.i64", - i64.fn_type(&[i64.into(), i64.into()], false), - None, - ) - }); - let max = ctx - .builder - .build_call(smax, &[end_val.into(), outer_end_val.into()], "smax") - .try_as_basic_value() - .left() - .unwrap(); - let outer_end = self.gen_store_target(ctx, old_end, Some("outer_end.addr"))?; - ctx.builder.build_store(outer_end, max); - } + self.timeline_update_end_max(ctx, old_end.clone())?; self.start = old_start; self.end = old_end; -- 2.44.1 From 9518d3fe1459e38d2f729f1e9e75d92dd2204f0a Mon Sep 17 00:00:00 2001 From: David Mak Date: Wed, 25 Oct 2023 15:54:27 +0800 Subject: [PATCH 5/5] artiq: Fix timeline not resetting upon exiting sequential block --- nac3artiq/src/codegen.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index 4867eae..e3a1ec5 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -50,7 +50,14 @@ impl<'a> ArtiqCodeGenerator<'a> { timeline: &'a (dyn TimeFns + Sync), ) -> ArtiqCodeGenerator<'a> { assert!(size_t == 32 || size_t == 64); - ArtiqCodeGenerator { name, size_t, name_counter: 0, start: None, end: None, timeline } + ArtiqCodeGenerator { + name, + size_t, + name_counter: 0, + start: None, + end: None, + timeline, + } } /// If the generator is currently in a direct-`parallel` block context, emits IR that resets the @@ -82,10 +89,14 @@ impl<'a> ArtiqCodeGenerator<'a> { /// /// `parallel`-block context refers to when the generator is generating statements within a /// (possibly indirect) `parallel` block. + /// + /// * `store_name` - The LLVM value name for the pointer to `end`. `.addr` will be appended to + /// the end of the provided value name. fn timeline_update_end_max<'ctx, 'b>( &mut self, ctx: &mut CodeGenContext<'ctx, 'b>, end: Option>>, + store_name: Option<&str>, ) -> Result<(), String> { if let Some(end) = end { let old_end = self.gen_expr(ctx, &end)? @@ -106,7 +117,10 @@ impl<'a> ArtiqCodeGenerator<'a> { .try_as_basic_value() .left() .unwrap(); - let end_store = self.gen_store_target(ctx, &end, Some(""))?; + let end_store = self.gen_store_target( + ctx, + &end, + store_name.map(|name| format!("{name}.addr")).as_deref())?; ctx.builder.build_store(end_store, max); } @@ -136,7 +150,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { ) -> Result>, String> { let result = gen_call(self, ctx, obj, fun, params)?; - self.timeline_update_end_max(ctx, self.end.clone())?; + self.timeline_update_end_max(ctx, self.end.clone(), Some("end"))?; self.timeline_reset_start(ctx)?; Ok(result) @@ -242,7 +256,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { } // inside a parallel block, should update the outer max now_mu - self.timeline_update_end_max(ctx, old_end.clone())?; + self.timeline_update_end_max(ctx, old_end.clone(), Some("outer.end"))?; self.start = old_start; self.end = old_end; @@ -257,6 +271,9 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { gen_block(self, ctx, body.iter())?; self.start = start; + // Reset the timeline when we are exiting the sequential block + self.timeline_reset_start(ctx)?; + return Ok(()); } } -- 2.44.1