Skip to content

Commit

Permalink
Add support for maximumnum/minimumnum (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcxzyw authored Nov 30, 2024
1 parent 00c09c6 commit 4eef8dd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
17 changes: 17 additions & 0 deletions ir/instr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ void FpBinOp::print(ostream &os) const {
case FMin: str = "fmin "; break;
case FMaximum: str = "fmaximum "; break;
case FMinimum: str = "fminimum "; break;
case FMaximumnum: str = "fmaximumnum "; break;
case FMinimumnum: str = "fminimumnum "; break;
case CopySign: str = "copysign "; break;
}
os << getName() << " = " << str << fmath << *lhs << ", " << rhs->getName();
Expand Down Expand Up @@ -906,6 +908,21 @@ StateValue FpBinOp::toSMT(State &s) const {
};
break;

case FMinimumnum:
case FMaximumnum:
fn = [&](const expr &a, const expr &b, const expr &rm) {
expr zpos = expr::mkNumber("0", a), zneg = expr::mkNumber("-0", a);
expr cmp = óp == FMinimumnum ? a.fole(b) : a.foge(b);
expr neg_cond = op == FMinimumnum ? (a.isFPNegative() || b.isFPNegative())
: (a.isFPNegative() && b.isFPNegative());
expr e = expr::mkIf(a.isFPZero() && b.isFPZero(),
expr::mkIf(neg_cond, zneg, zpos),
expr::mkIf(cmp, a, b));

return expr::mkIf(a.isNaN(), b, expr::mkIf(b.isNaN(), a, e));
};
break;

case CopySign:
bitwise = true;
fn = [](const expr &a, const expr &b, const expr &rm) {
Expand Down
2 changes: 1 addition & 1 deletion ir/instr.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class BinOp final : public Instr {
class FpBinOp final : public Instr {
public:
enum Op { FAdd, FSub, FMul, FDiv, FRem, FMax, FMin, FMaximum, FMinimum,
CopySign };
FMaximumnum, FMinimumnum, CopySign };

private:
Value *lhs, *rhs;
Expand Down
4 changes: 4 additions & 0 deletions llvm_util/llvm2alive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
case llvm::Intrinsic::maxnum:
case llvm::Intrinsic::minimum:
case llvm::Intrinsic::maximum:
case llvm::Intrinsic::minimumnum:
case llvm::Intrinsic::maximumnum:
case llvm::Intrinsic::experimental_constrained_fadd:
case llvm::Intrinsic::experimental_constrained_fsub:
case llvm::Intrinsic::experimental_constrained_fmul:
Expand All @@ -1038,6 +1040,8 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
case llvm::Intrinsic::experimental_constrained_minimum: op = FpBinOp::FMinimum; break;
case llvm::Intrinsic::maximum:
case llvm::Intrinsic::experimental_constrained_maximum: op = FpBinOp::FMaximum; break;
case llvm::Intrinsic::minimumnum: op = FpBinOp::FMinimumnum; break;
case llvm::Intrinsic::maximumnum: op = FpBinOp::FMaximumnum; break;
case llvm::Intrinsic::experimental_constrained_fadd: op = FpBinOp::FAdd; break;
case llvm::Intrinsic::experimental_constrained_fsub: op = FpBinOp::FSub; break;
case llvm::Intrinsic::experimental_constrained_fmul: op = FpBinOp::FMul; break;
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/fp/fmaxmin.opt
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,71 @@ Name: fminimum, -0, +0
%t = fminimum float -0.0, float +0.0
=>
%t = float -0.0

Name: fmaximumnum, constant prop.
%t = fmaximumnum float 1.0, float 3.0
=>
%t = float 3.0

Name: fminimumnum, constant prop.
%t = fminimumnum float 1.0, float 3.0
=>
%t = float 1.0

Name: fmaximumnum, LHS NaN
%v = fdiv float 0.0, 0.0
%t = fmaximumnum float %v, float 3.0
=>
%t = float 3.0

Name: fmaximumnum, RHS NaN
%v = fdiv float 0.0, 0.0
%t = fmaximumnum float 3.0, float %v
=>
%t = float 3.0

Name: fmaximumnum, BOTH NaN
%v = fdiv float 0.0, 0.0
%t = fmaximumnum float %v, float %v
=>
%v = fdiv float 0.0, 0.0
%t = float %v

Name: fminimumnum, LHS NaN
%v = fdiv float 0.0, 0.0
%t = fminimumnum float %v, float 3.0
=>
%t = float 3.0

Name: fminimumnum, RHS NaN
%v = fdiv float 0.0, 0.0
%t = fminimumnum float 3.0, float %v
=>
%t = float 3.0

Name: fminimumnum, BOTH NaN
%v = fdiv float 0.0, 0.0
%t = fminimumnum float %v, float %v
=>
%v = fdiv float 0.0, 0.0
%t = float %v

Name: fmaximumnum, +0, -0
%t = fmaximumnum float +0.0, float -0.0
=>
%t = float +0.0

Name: fmaximumnum, -0, +0
%t = fmaximumnum float -0.0, float +0.0
=>
%t = float +0.0

Name: fminimumnum, +0, -0
%t = fminimumnum float +0.0, float -0.0
=>
%t = float -0.0

Name: fminimumnum, -0, +0
%t = fminimumnum float -0.0, float +0.0
=>
%t = float -0.0
2 changes: 2 additions & 0 deletions tools/alive_lexer.re
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ space+ {
"fmin" { return FMIN; }
"fmaximum" { return FMAXIMUM; }
"fminimum" { return FMINIMUM; }
"fmaximumnum" { return FMAXIMUMNUM; }
"fminimumnum" { return FMINIMUMNUM; }
"extractelement" { return EXTRACTELEMENT; }
"insertelement" { return INSERTELEMENT; }
"shufflevector" { return SHUFFLEVECTOR; }
Expand Down
6 changes: 6 additions & 0 deletions tools/alive_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,8 @@ static unsigned parse_binop_flags(token op_token) {
case FMIN:
case FMAXIMUM:
case FMINIMUM:
case FMAXIMUMNUM:
case FMINIMUMNUM:
case SREM:
case UREM:
case UADD_SAT:
Expand Down Expand Up @@ -787,6 +789,8 @@ static unique_ptr<Instr> parse_fp_binop(string_view name, token op_token) {
case FMIN: op = FpBinOp::FMin; break;
case FMAXIMUM: op = FpBinOp::FMaximum; break;
case FMINIMUM: op = FpBinOp::FMinimum; break;
case FMAXIMUMNUM: op = FpBinOp::FMaximumnum; break;
case FMINIMUMNUM: op = FpBinOp::FMinimumnum; break;
default:
UNREACHABLE();
}
Expand Down Expand Up @@ -1220,6 +1224,8 @@ static unique_ptr<Instr> parse_instr(string_view name) {
case FMIN:
case FMAXIMUM:
case FMINIMUM:
case FMAXIMUMNUM:
case FMINIMUMNUM:
return parse_fp_binop(name, t);
case BITREVERSE:
case BSWAP:
Expand Down
2 changes: 2 additions & 0 deletions tools/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ TOKEN(UMULFIXSAT)
TOKEN(FMA)
TOKEN(FMAX)
TOKEN(FMAXIMUM)
TOKEN(FMAXIMUMNUM)
TOKEN(FMIN)
TOKEN(FMINIMUM)
TOKEN(FMINIMUMNUM)
TOKEN(FSUB)
TOKEN(GLOBAL_NAME)
TOKEN(HALF)
Expand Down

0 comments on commit 4eef8dd

Please sign in to comment.