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
Missed Optimization: memcpy(a,b,sizeof(*a)) is slower than *a = *b #6846
If you run with the HEAD of the branch, the test is very slow; if you run with one commit before HEAD, its fast. The only change is to emit memcpy(a,b,sizeof(*a)) vs*a = *b.
When Kani was first written, we didn't have support for Rust intrinsics, so we used a hook mechanism to override the standard library functions that used the intrinsics. Now that we have support, we want to remove the hooks and use the actual standard library code. But, this means we end up using the copy_nonoverlapping intrinsic, which turns into memcpy, which gives us the performance issue.
Studying the example mentioned at the top, I am not entirely convinced that memcpy is what is giving us grief: The changes in model-checking/kani@92ce4729518d appear to be introducing a loop that CBMC is unable to determine an unwinding bound for. (CBMC's memcpy does not use a loop.) Running CBMC on that "slower" code with --unwind 2 --unwinding-assertions yields 1) no failing unwinding assertions and 2) a modest increase in execution time from 0.45 seconds to 2.83 seconds. The following loop unwinding reports are new, and do not appear when running on the prior commit:
Further investigation suggests that the root cause of those additional loops being triggered is constant propagation no longer taking place. This, in turn, is caused by thememcpy operation entailing byte extracts (this is expected) over a struct where only some members are constants. Therefore, at least at present, the entire expression is no longer a constant, and propagation through that byte extract operation is lost.
I will try to come up with a small example and will then try to implement additional simplification rules that make constant propagation possible again.
Activity
danielsn commentedon May 10, 2022
Branch
read-ptr-hookshows what we are actually trying to achieve, (i.e. how Rust actually does it).tautschnig commentedon May 10, 2022
A bunch of questions/comments:
PtrReadto understand "how Rust actually does it?"memcpyfor what you're trying to achieve?memcpyseems non-trivial as a lot of information is lost at that point.danielsn commentedon May 11, 2022
Sorry for the scanty feature request.
It implements the logical operation
However, under the hood, it is implemented as
memcpy(orcopy_nonoverlappingas it's called in rust). This is likely because the function is generic: making it a memcpy over a set of bytes is the easiest way to support the generic operation.https://doc.rust-lang.org/src/core/ptr/mod.rs.html#685
When Kani was first written, we didn't have support for Rust intrinsics, so we used a
hookmechanism to override the standard library functions that used the intrinsics. Now that we have support, we want to remove the hooks and use the actual standard library code. But, this means we end up using thecopy_nonoverlappingintrinsic, which turns intomemcpy, which gives us the performance issue.danielsn commentedon May 11, 2022
copy_nonoverlappingintrinsic and doing the optimization ourselves at that point. But given thatit seemed to make sense to make this a CBMC optimization
memcpy, rather than in the body ofmemcpytautschnig commentedon May 13, 2022
Studying the example mentioned at the top, I am not entirely convinced that
memcpyis what is giving us grief: The changes in model-checking/kani@92ce4729518d appear to be introducing a loop that CBMC is unable to determine an unwinding bound for. (CBMC'smemcpydoes not use a loop.) Running CBMC on that "slower" code with--unwind 2 --unwinding-assertionsyields 1) no failing unwinding assertions and 2) a modest increase in execution time from 0.45 seconds to 2.83 seconds. The following loop unwinding reports are new, and do not appear when running on the prior commit:tautschnig commentedon May 13, 2022
Further investigation suggests that the root cause of those additional loops being triggered is constant propagation no longer taking place. This, in turn, is caused by the
memcpyoperation entailing byte extracts (this is expected) over a struct where only some members are constants. Therefore, at least at present, the entire expression is no longer a constant, and propagation through that byte extract operation is lost.I will try to come up with a small example and will then try to implement additional simplification rules that make constant propagation possible again.
Constant propagation for individual fields through byte extract
tautschnig commentedon May 13, 2022
#6856 improves the constant propagation, and makes the Kani test case complete without having to specify unwinding constraints.
Constant propagation for individual fields through byte extract
Constant propagation for individual fields through byte extract
Constant propagation for individual fields through byte extract
Constant propagation for individual fields through byte extract
Constant propagation for individual fields through byte extract