forked from M-Labs/nac3
1
0
Fork 0

Revert "nac3artiq/codegen: fixed smax problem"

We have LLVM 12 now and can use the intrinsic.

This reverts commit 98d9f73afb.
This commit is contained in:
Sebastien Bourdeauducq 2021-11-02 14:00:28 +08:00
parent 624dfe8cd1
commit bc0f82cad8
1 changed files with 29 additions and 18 deletions

View File

@ -6,7 +6,7 @@ use nac3core::{
use rustpython_parser::ast::{Expr, ExprKind, Located, Stmt, StmtKind, StrRef}; use rustpython_parser::ast::{Expr, ExprKind, Located, Stmt, StmtKind, StrRef};
use inkwell::values::{BasicValueEnum, IntValue}; use inkwell::values::BasicValueEnum;
use crate::timeline::TimeFns; use crate::timeline::TimeFns;
@ -30,17 +30,6 @@ impl<'a> ArtiqCodeGenerator<'a> {
} }
} }
fn max_val<'ctx, 'a>(
ctx: &mut CodeGenContext<'ctx, 'a>,
a: IntValue<'ctx>,
b: IntValue<'ctx>,
) -> IntValue<'ctx> {
let cmp = ctx
.builder
.build_int_compare(inkwell::IntPredicate::SGT, a, b, "gt");
ctx.builder.build_select(cmp, a, b, "max").into_int_value()
}
impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> {
fn get_name(&self) -> &str { fn get_name(&self) -> &str {
&self.name &self.name
@ -57,7 +46,20 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> {
if let Some(end) = self.end.clone() { if let Some(end) = self.end.clone() {
let old_end = self.gen_expr(ctx, &end).unwrap(); let old_end = self.gen_expr(ctx, &end).unwrap();
let now = self.timeline.emit_now_mu(ctx); let now = self.timeline.emit_now_mu(ctx);
let max = max_val(ctx, old_end.into_int_value(), now.into_int_value()); 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, now], "smax")
.try_as_basic_value()
.left()
.unwrap();
let end_store = self.gen_store_target(ctx, &end); let end_store = self.gen_store_target(ctx, &end);
ctx.builder.build_store(end_store, max); ctx.builder.build_store(end_store, max);
} }
@ -152,11 +154,20 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> {
// inside a parallel block, should update the outer max now_mu // inside a parallel block, should update the outer max now_mu
if let Some(old_end) = &old_end { if let Some(old_end) = &old_end {
let outer_end_val = self.gen_expr(ctx, old_end).unwrap(); let outer_end_val = self.gen_expr(ctx, old_end).unwrap();
let max = max_val( let smax = ctx.module.get_function("llvm.smax.i64").unwrap_or_else(|| {
ctx, let i64 = ctx.ctx.i64_type();
end_val.into_int_value(), ctx.module.add_function(
outer_end_val.into_int_value(), "llvm.smax.i64",
); i64.fn_type(&[i64.into(), i64.into()], false),
None,
)
});
let max = ctx
.builder
.build_call(smax, &[end_val, outer_end_val], "smax")
.try_as_basic_value()
.left()
.unwrap();
let outer_end = self.gen_store_target(ctx, old_end); let outer_end = self.gen_store_target(ctx, old_end);
ctx.builder.build_store(outer_end, max); ctx.builder.build_store(outer_end, max);
} }