Skip to content

Add a vm.check.nearly_eq + supporting pseudo ops for inexact floating-point comparisons. #5854

Closed

Description

@benvanik

In an effort to replace the check module with built-in support we need to be able to perform inexact comparisons on floating-point numbers. Adding some pseudo ops to the VM would make this fairly easy; for example a vm.cmp.eq.f32.near that implemented a ULP-based nearly-equal by expanding out to other VM ops (as in https://www.gamasutra.com/view/news/162368/Indepth_Comparing_floating_point_numbers_2012_edition.php) would allow a trivial vm.check.nearly_eq.

There's some other ops that would be nice for fully replacing the check module - like string formatting to print actual values - but having the ability to compare two numbers is a good first step.

Activity

  1. added
    help wanted
    runtime/vm
    quality of life 😊
    on May 12, 2021
  2. marbre commented on May 17, 2021

    @marbre
    Member

    Maybe vm.check.expect_near and vm.check.expect_eq similar to googletest would be nice.

  3. self-assigned this
    on Sep 15, 2021
  4. ScottTodd commented on Sep 16, 2021

    @ScottTodd
    Member

    I made some progress on this at https://github.com/ScottTodd/iree/tree/vm-nearly-eq-progress. I think I have the bulk of the ULP comparison working, I just need to figure out the edge case handling

        // this condition and the branch are not implemented yet
        if (sign(lhs) != sign(rhs)) {  // no VM sign() op, could do `(lhs > 0) == (rhs > 0)` instead?
          return lhs == rhs;
        } else {
          // these are implemented
          lhsInt = bitcastf32i32(lhs);
          rhsInt = bitcastf32i32(rhs);
          signedUlpsDiff = lhsInt - rhsInt;
          absUlpsDiff = abs(signedUlpsDiff); // this needed a new VM op for abs()
          return absUlpsDiff < maxUlpsDiff;
        }
  5. benvanik commented on Sep 16, 2021

    @benvanik
    CollaboratorAuthor

    could just bitshift to get the high bit and xor them ((lhs >> 31) ^ (rhs >> 31) will be 1 if they differ) - could add a vm.sign.i32 pseudo-op that expanded to this

  6. ScottTodd commented on Sep 16, 2021

    @ScottTodd
    Member

    could just bitshift to get the high bit and xor them ((lhs >> 31) ^ (rhs >> 31) will be 1 if they differ) - could add a vm.sign.i32 pseudo-op that expanded to this

    lhs and rhs here are f32/f64 values (for floating point comparison), so do you mean bitcast to int then shift, and vm.sign.f32?

    I'm also generally wondering where we draw the line for complexity in the VM (with both adding new ops for things like sign() and abs() vs implementing as pseudo ops that use existing primitives

  7. benvanik commented on Sep 16, 2021

    @benvanik
    CollaboratorAuthor

    yeah sorry - high bit in f32/f64 is still the sign bit, so bitcast and shift should work

    In general (but not in reality today) we have the smallest possible set of ops at runtime that balance that total op count with total impact on binary size/performance - if for example 95% of our programs used vm.sign.f32 in tight loops all over the place it's probably worth having an op for - but if it's rarely used and can be decomposed into one or two simpler ops then that's better (for example, bitcasting is free in the VM and emits no ops - so our emulated sign is just one op for shifting and there'd be no benefit of adding a runtime sign op).

  8. benvanik commented on Sep 16, 2021

    @benvanik
    CollaboratorAuthor

    Also, adding pseudo ops is cheap (compiler-only, doesn't change vmfb binaries), so can err on the side of starting with those and then promoting them to runtime ops on-demand. Before we finalize the v1 of the format we'll do a big scrub and probably shuffle around a lot of what's in there today, so having them expressed in the IR will make it easy to just switch some between VM_Op and VM_PseudoOp vs having to go fix up all the places we are constructing the instruction sequences internally. One place I regret not doing this for is the i1 casting - today there's a lot of places that xor 1 that are annoying ;(

  9. ScottTodd commented on Sep 17, 2021

    @ScottTodd
    Member

    I'm trying to wrap my head around how branches are represented in MLIR and constructed using the pattern rewriter. Is the IR in this gist here generally the structure we'd be aiming for?

  10. benvanik commented on Sep 17, 2021

    @benvanik
    CollaboratorAuthor

    yep! (I think - at the end your cond_br %1 should go to 4/5 not 1/2, but I suspect that's copypasta :)
    You can access any ssa value in a dominating block without capturing (like using %0 in ^bb1/2/3/4/5), and only need the branch args when you have divergent sources (like you have with the res_compare values). Canonicalization will take care of cleaning that up, though, as well as doing things like removing duplicated blocks, so it's (generally) ok to do whatever is simplest to construct and let the rest shake out.

    That xor is the i1 casting I was mentioning would be so much cleaner as a pseudo op :P

  11. moved this to Done in IREEon Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

help wantedquality of life 😊runtime/vm

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions