Implement fixed-point arithmetic intrinsics for -llvm-api backend

This commit is contained in:
gingerBill
2021-04-01 14:36:06 +01:00
parent 0fc04a939e
commit cef698afd6
3 changed files with 127 additions and 0 deletions

View File

@@ -5853,6 +5853,77 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
}
break;
case BuiltinProc_fixed_point_mul:
case BuiltinProc_fixed_point_div:
case BuiltinProc_fixed_point_mul_sat:
case BuiltinProc_fixed_point_div_sat:
{
if (!build_context.use_llvm_api) {
error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name));
return false;
}
Operand x = {};
Operand y = {};
Operand z = {};
check_expr(c, &x, ce->args[0]);
if (x.mode == Addressing_Invalid) {
return false;
}
check_expr(c, &y, ce->args[1]);
if (y.mode == Addressing_Invalid) {
return false;
}
convert_to_typed(c, &x, y.type);
if (x.mode == Addressing_Invalid) {
return false;
}
convert_to_typed(c, &y, x.type);
if (x.mode == Addressing_Invalid) {
return false;
}
if (!are_types_identical(x.type, y.type)) {
gbString xts = type_to_string(x.type);
gbString yts = type_to_string(y.type);
error(x.expr, "Mismatched types for %.*s, %s vs %s", LIT(builtin_procs[id].name), xts, yts);
gb_string_free(yts);
gb_string_free(xts);
return false;
}
if (!is_type_integer(x.type) || is_type_untyped(x.type)) {
gbString xts = type_to_string(x.type);
error(x.expr, "Expected an integer type for %.*s, got %s", LIT(builtin_procs[id].name), xts);
gb_string_free(xts);
return false;
}
check_expr(c, &z, ce->args[2]);
if (z.mode == Addressing_Invalid) {
return false;
}
if (z.mode != Addressing_Constant || !is_type_integer(z.type)) {
error(z.expr, "Expected a constant integer for the scale in %.*s", LIT(builtin_procs[id].name));
return false;
}
i64 n = exact_value_to_i64(z.value);
if (n <= 0) {
error(z.expr, "Scale parameter in %.*s must be positive, got %lld", LIT(builtin_procs[id].name), n);
return false;
}
i64 sz = 8*type_size_of(x.type);
if (n > sz) {
error(z.expr, "Scale parameter in %.*s is larger than the base integer bit width, got %lld, expected a maximum of %lld", LIT(builtin_procs[id].name), n, sz);
return false;
}
operand->type = x.type;
operand->mode = Addressing_Value;
}
break;
case BuiltinProc_type_base_type:
if (operand->mode != Addressing_Type) {
error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));

View File

@@ -117,6 +117,11 @@ enum BuiltinProcId {
BuiltinProc_atomic_cxchgweak_acq_failrelaxed,
BuiltinProc_atomic_cxchgweak_acqrel_failrelaxed,
BuiltinProc_fixed_point_mul,
BuiltinProc_fixed_point_div,
BuiltinProc_fixed_point_mul_sat,
BuiltinProc_fixed_point_div_sat,
// Constant type tests
@@ -317,6 +322,11 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
{STR_LIT("atomic_cxchgweak_acqrel_failrelaxed"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("fixed_point_mul"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("fixed_point_div"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("fixed_point_mul_sat"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("fixed_point_div_sat"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT(""), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
{STR_LIT("type_base_type"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("type_core_type"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},

View File

@@ -9192,6 +9192,52 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
case BuiltinProc_type_hasher_proc:
return lb_get_hasher_proc_for_type(p->module, ce->args[0]->tav.type);
case BuiltinProc_fixed_point_mul:
case BuiltinProc_fixed_point_div:
case BuiltinProc_fixed_point_mul_sat:
case BuiltinProc_fixed_point_div_sat:
{
lbValue x = lb_build_expr(p, ce->args[0]);
lbValue y = lb_build_expr(p, ce->args[1]);
lbValue scale = lb_build_expr(p, ce->args[2]);
x = lb_emit_conv(p, x, tv.type);
y = lb_emit_conv(p, y, tv.type);
scale = lb_emit_conv(p, scale, tv.type);
char const *name = nullptr;
if (is_type_unsigned(tv.type)) {
switch (id) {
case BuiltinProc_fixed_point_mul: name = "llvm.umul.fix"; break;
case BuiltinProc_fixed_point_div: name = "llvm.udiv.fix"; break;
case BuiltinProc_fixed_point_mul_sat: name = "llvm.umul.fix.sat"; break;
case BuiltinProc_fixed_point_div_sat: name = "llvm.udiv.fix.sat"; break;
}
} else {
switch (id) {
case BuiltinProc_fixed_point_mul: name = "llvm.imul.fix"; break;
case BuiltinProc_fixed_point_div: name = "llvm.idiv.fix"; break;
case BuiltinProc_fixed_point_mul_sat: name = "llvm.imul.fix.sat"; break;
case BuiltinProc_fixed_point_div_sat: name = "llvm.idiv.fix.sat"; break;
}
}
GB_ASSERT(name != nullptr);
unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
LLVMTypeRef types[1] = {};
types[0] = lb_type(p->module, tv.type);
LLVMValueRef args[3] = {};
args[0] = x.value;
args[1] = y.value;
args[2] = scale.value;
LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
lbValue res = {};
res.type = tv.type;
res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
return res;
}
}
GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name));