Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lower mChckNaN magic with MIR pass #1297

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions compiler/backend/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1645,13 +1645,6 @@ proc genMagicExpr(p: BProc, e: CgNode, d: var TLoc, op: TMagic) =
typ.add "*"

linefmt(p, cpsStmts, "$1 = ($2)($3);$n", [a.r, typ, rdLoc(b)])
of mChckNaN:
var a: TLoc
initLocExpr(p, e[1], a)
# NOTE: if the value is a signaling NaN, the comparison itself results in
# a float-point exception (which might result in a trap)
linefmt(p, cpsStmts, "if ($1 != $1){ #raiseFloatInvalidOp(); $2}$n",
[rdLoc(a), raiseInstr(p, e.exit)])
of mChckIndex:
var arr, a: TLoc
initLocExpr(p, e[1], arr)
Expand Down
23 changes: 23 additions & 0 deletions compiler/mir/rtchecks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ proc emitRangeCheck(tree, call, graph, env, bu): Value =
bu.emitByVal lo
bu.emitByVal hi

proc emitNanCheck(tree; call; graph; env; bu) =
## For ``chckNaN(val)`` emit:
## def _1 = eqF64(arg val, arg val)
## def _2 = not(arg _1)
## if _2:
## raiseFloatInvalidOp()
let cmp = bu.wrapTemp BoolType:
bu.buildMagicCall mEqF64, BoolType:
bu.subTree mnkArg:
bu.emitFrom(tree, NodePosition tree.argument(call, 0))
bu.subTree mnkArg:
bu.emitFrom(tree, NodePosition tree.argument(call, 0))

# if a float value is not equal to itself, it is not a number (=NaN)
bu.buildIfNot cmp:
bu.emitCall(tree, call, env.addCompilerProc(graph, "raiseFloatInvalidOp")):
discard

proc lowerChecks*(tree; graph; env; changes: var Changeset) =
## Lowers all magic calls implementing the run-time checks.
for i, n in tree.pairs:
Expand All @@ -188,5 +206,10 @@ proc lowerChecks*(tree; graph; env; changes: var Changeset) =
changes.replaceMulti(tree, call, bu):
bu.subTree mnkConv, tree[call].typ:
bu.use tmp
of mChckNaN:
let call = tree.parent(i)
# make sure to take the ``mnkVoid`` wrapper into account
changes.replaceMulti(tree, tree.parent(call), bu):
emitNanCheck(tree, call, graph, env, bu)
else:
discard "not relevant"
Loading