You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a vm.check.nearly_eq + supporting pseudo ops for inexact floating-point comparisons. #5854
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.
// this condition and the branch are not implemented yetif (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;
}
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
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
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).
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 ;(
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?
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
Activity
marbre commentedon May 17, 2021
Maybe
vm.check.expect_nearandvm.check.expect_eqsimilar to googletest would be nice.ScottTodd commentedon Sep 16, 2021
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
benvanik commentedon Sep 16, 2021
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
ScottTodd commentedon Sep 16, 2021
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()andabs()vs implementing as pseudo ops that use existing primitivesbenvanik commentedon Sep 16, 2021
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).
benvanik commentedon Sep 16, 2021
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 ;(
ScottTodd commentedon Sep 17, 2021
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?
benvanik commentedon Sep 17, 2021
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
Add vm.abs.i32 and vm.abs.i64 ops. (#7080)
Add vm.check.nearly_eq and vm.cmp.eq.f*.near pseudo ops. (#7081)