From 9937d33bf9454de48d852d69d90fe05f966f8026 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sat, 5 Sep 2020 11:51:24 +0200 Subject: add support for a new instruction: OP_FMADD This will be the instruction for fused multiply-add but the motivation for it is some experimentation with the linearization of builtins. Signed-off-by: Luc Van Oostenryck --- Documentation/IR.rst | 7 +++++++ linearize.c | 2 ++ opcode.def | 1 + 3 files changed, 10 insertions(+) diff --git a/Documentation/IR.rst b/Documentation/IR.rst index 97d4b2b2..ff5af1c5 100644 --- a/Documentation/IR.rst +++ b/Documentation/IR.rst @@ -309,6 +309,13 @@ Ternary ops * .target: result of the operation * .type: type of .target +.. op:: OP_FMADD + Fused multiply-add. + + * .src1, .src2, .src3: operands (types must be compatible with .target) + * .target: result of the operation (must be a floating-point type) + * .type: type of .target + .. op:: OP_RANGE Range/bounds checking (only used for an unused sparse extension). diff --git a/linearize.c b/linearize.c index 5a8e7497..0d91d412 100644 --- a/linearize.c +++ b/linearize.c @@ -244,6 +244,7 @@ static const char *opcodes[] = { /* Special three-input */ [OP_SEL] = "select", + [OP_FMADD] = "fmadd", /* Memory */ [OP_LOAD] = "load", @@ -461,6 +462,7 @@ const char *show_instruction(struct instruction *insn) break; case OP_SEL: + case OP_FMADD: buf += sprintf(buf, "%s <- %s, %s, %s", show_pseudo(insn->target), show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3)); break; diff --git a/opcode.def b/opcode.def index 2583e2f4..7959efaf 100644 --- a/opcode.def +++ b/opcode.def @@ -91,6 +91,7 @@ OPCODE(SLICE, BADOP, BADOP, BADOP, 1, OPF_TARGET) /* Select - three input values */ OPCODE(SEL, BADOP, BADOP, BADOP, 3, OPF_TARGET) +OPCODE(FMADD, BADOP, BADOP, BADOP, 3, OPF_TARGET) /* Memory */ OPCODE(LOAD, BADOP, BADOP, BADOP, 1, OPF_TARGET) -- cgit 1.2.3-korg From 78d7bdb29a9256681219366422b95a39d1e02faf Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sat, 5 Sep 2020 11:47:53 +0200 Subject: builtin: allow linearization to fail Allow the linearization of builtins to fail and continue with the normal linearization of OP_CALLs. The motivation for this is for the linearization of target specific builtins. Signed-off-by: Luc Van Oostenryck --- linearize.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/linearize.c b/linearize.c index 0d91d412..3690f2e7 100644 --- a/linearize.c +++ b/linearize.c @@ -1513,8 +1513,11 @@ static pseudo_t linearize_call_expression(struct entrypoint *ep, struct expressi fntype = fn->ctype; // handle builtins - if (fntype->op && fntype->op->linearize) - return fntype->op->linearize(ep, expr); + if (fntype->op && fntype->op->linearize) { + retval = fntype->op->linearize(ep, expr); + if (retval) + return retval; + } ctype = &fntype->ctype; if (fntype->type == SYM_NODE) -- cgit 1.2.3-korg From 5c86c487ea5e3c6d4a56d7c98b39e33b6b4a28f5 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sat, 5 Sep 2020 11:53:38 +0200 Subject: builtin: add declaration for __builtin_fma{,f,l}() The motivation for this is to experiment with adding infrastructure for the linearization of builtins. Signed-off-by: Luc Van Oostenryck --- builtin.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/builtin.c b/builtin.c index 2e9be8be..26b612dc 100644 --- a/builtin.c +++ b/builtin.c @@ -490,6 +490,9 @@ static const struct builtin_fn builtins_common[] = { { "__builtin_ffs", &int_ctype, 0, { &int_ctype }, .op = &ffs_op }, { "__builtin_ffsl", &int_ctype, 0, { &long_ctype }, .op = &ffs_op }, { "__builtin_ffsll", &int_ctype, 0, { &llong_ctype }, .op = &ffs_op }, + { "__builtin_fma", &double_ctype, 0, { &double_ctype, &double_ctype, &double_ctype }}, + { "__builtin_fmaf", &float_ctype, 0, { &float_ctype, &float_ctype, &float_ctype }}, + { "__builtin_fmal", &ldouble_ctype, 0, { &ldouble_ctype, &ldouble_ctype, &ldouble_ctype }}, { "__builtin_frame_address", &ptr_ctype, 0, { &uint_ctype }}, { "__builtin_free", &void_ctype, 0, { &ptr_ctype }}, { "__builtin_huge_val", &double_ctype, 0 }, -- cgit 1.2.3-korg From 814cda15f647c14e0892deef3c57d73417e87b34 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sat, 5 Sep 2020 11:55:15 +0200 Subject: builtin: teach sparse to linearize __builtin_fma() The support for the linearization of builtins was already added for __builtin_unreachable() but this builtin has no arguments and no return value. So, to complete the experience of builtin linearization, add the linearization of __builtin_fma(). Signed-off-by: Luc Van Oostenryck --- linearize.c | 20 ++++++++++++++++++++ validation/linear/builtin-fma.c | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 validation/linear/builtin-fma.c diff --git a/linearize.c b/linearize.c index 3690f2e7..1081bda8 100644 --- a/linearize.c +++ b/linearize.c @@ -2585,6 +2585,23 @@ struct entrypoint *linearize_symbol(struct symbol *sym) * Builtin functions */ +static pseudo_t linearize_fma(struct entrypoint *ep, struct expression *expr) +{ + struct instruction *insn = alloc_typed_instruction(OP_FMADD, expr->ctype); + struct expression *arg; + + PREPARE_PTR_LIST(expr->args, arg); + insn->src1 = linearize_expression(ep, arg); + NEXT_PTR_LIST(arg) + insn->src2 = linearize_expression(ep, arg); + NEXT_PTR_LIST(arg) + insn->src3 = linearize_expression(ep, arg); + FINISH_PTR_LIST(arg); + + add_one_insn(ep, insn); + return insn->target = alloc_pseudo(insn); +} + static pseudo_t linearize_unreachable(struct entrypoint *ep, struct expression *exp) { add_unreachable(ep); @@ -2597,6 +2614,9 @@ static struct sym_init { struct symbol_op op; } builtins_table[] = { // must be declared in builtin.c:declare_builtins[] + { "__builtin_fma", linearize_fma }, + { "__builtin_fmaf", linearize_fma }, + { "__builtin_fmal", linearize_fma }, { "__builtin_unreachable", linearize_unreachable }, { } }; diff --git a/validation/linear/builtin-fma.c b/validation/linear/builtin-fma.c new file mode 100644 index 00000000..b1024f39 --- /dev/null +++ b/validation/linear/builtin-fma.c @@ -0,0 +1,19 @@ +double fma(double a, double x, double y) +{ + return __builtin_fma(a, x, y); +} + +/* + * check-name: builtin-fma + * check-command: test-linearize -Wno-decl $file + * + * check-output-start +fma: +.L0: + + fmadd.64 %r4 <- %r1, %r2, %r3 + ret.64 %r4 + + + * check-output-end + */ -- cgit 1.2.3-korg