Minor style changes
This commit is contained in:
parent
f83bf0a1fd
commit
5abbccce76
|
@ -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<Expr<Option<Type>>>,
|
||||
|
||||
/// Variable for tracking the end of a `with parallel` block.
|
||||
end: Option<Expr<Option<Type>>>,
|
||||
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 {
|
||||
|
|
|
@ -20,23 +20,23 @@ 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"),
|
||||
) {
|
||||
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!();
|
||||
}
|
||||
|
@ -51,11 +51,11 @@ impl TimeFns for NowPinningTimeFns64 {
|
|||
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 +63,11 @@ 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,10 +97,10 @@ 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),
|
||||
|
@ -111,27 +111,27 @@ impl TimeFns for NowPinningTimeFns64 {
|
|||
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_val");
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue