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
extern fn test(very_unlikely1: bool, very_unlikely2: bool, a: int, b: int) -> int {
let d = if very_unlikely1 {
a / b
} else {
a
};
let e = if very_unlikely2 {
a / b
} else {
a
};
d + e
}
It has two branches with independent conditions, but the then-blocks and the else-blocks are calculating the same values, which means that Thorin will merge the primops:
Here it can already be seen that the scheduler schedules the division into the block that dominates all uses (in this case, the entry block of test_242).
The problem is that for very_unlikely1 = false, very_unlikely2 = false, b = 0, the original code is defined, while the resulting LLVM IR exhibits undefined behavior (according to http://llvm.org/docs/LangRef.html#id106).
The correctness issue can be solved by letting the div primop consume and yield a value of the mem type. This will serialize divisions w.r.t. other side-effecting operations, which can be intended or not.
However, the problem is more general. If the division is replaced by a side-effect-free computation, the scheduler will still let it execute speculatively. If the computation is very large, this slows down the code, even if the result doesn't end up being used.
The text was updated successfully, but these errors were encountered:
See for example this piece of Impala code:
It has two branches with independent conditions, but the then-blocks and the else-blocks are calculating the same values, which means that Thorin will merge the primops:
Here it can already be seen that the scheduler schedules the division into the block that dominates all uses (in this case, the entry block of
test_242
).Here's the LLVM IR:
The problem is that for
very_unlikely1 = false
,very_unlikely2 = false
,b = 0
, the original code is defined, while the resulting LLVM IR exhibits undefined behavior (according to http://llvm.org/docs/LangRef.html#id106).The correctness issue can be solved by letting the
div
primop consume and yield a value of the mem type. This will serialize divisions w.r.t. other side-effecting operations, which can be intended or not.However, the problem is more general. If the division is replaced by a side-effect-free computation, the scheduler will still let it execute speculatively. If the computation is very large, this slows down the code, even if the result doesn't end up being used.
The text was updated successfully, but these errors were encountered: