I know it's a bit strange but I need a non-associative (boolean) or
operator, the basic situation I've got set up is this:
x
and x_is_uninit
x
is some arbitrary boolean that is uninitialized iff x_is_uninit
is truex_is_uninit | x
x
can be uninitialized it would be very bad if the order of operations were swappedAgain, I know it's mildly ill-advised but I'm just not sure how to approach this well
I'm a little confused by the question. In what sense is x
uninitialized, and what would be bad about using the uninitialized value this way? Cranelift doesn't have undefined behavior, so it'll happily compile a bitwise 'or' where the operands are any SSA value, even from a load from memory that you haven't previously stored anything to. And to the extent that we still have booleans at all, they're in the definition of instructions like brif
, which treats anything non-zero as "true". So if x_is_uninit
is non-zero, no matter what the value of x
is, combining them with bor
will also be non-zero. And if x_is_uninit
is zero, then the result will equal x
, but in that case it was initialized. So that is the result you wanted, I think?
Like I said, I'm mainly concerned by the associativity of or
, if x_is_uninit | x
was folded into x | x_is_uninit
then that would cause undefined/incorrect/unwanted behavior
I gather you mean commutativity rather than associativity, right? But Cranelift's bor
instruction really does produce identical results regardless of which order its operands are in, no matter where those operands came from. I still don't know what you mean by "uninitialized" in this context, since Cranelift's instruction semantics don't include any undefined behavior.
Yes my bad, I meant commutativity. I guess you're right though, I need a short-circuiting or operator in order to do this correctly
You certainly can use branches to implement short-circuiting boolean-or, and Cranelift won't re-order those. But I still would love to understand what correctness criteria you're using, because from Cranelift's perspective I don't see any way to get an "incorrect" result in the situation you've described.
Last updated: Dec 23 2024 at 12:05 UTC