[WIP] MDEV-35770 Augment DBUG_ASSERT with __builtin_assume #3944
+256
−245
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
The DBUG_ASSERT macro tests an invariant at runtime in debug build and aborts if the check fails. Although assertions are disabled in optimized builds, code is usually written in such a manner, that failing an assertion condition would lead to undefined behavior. It therefore makes sense to introduce platform-specific analog of C++23
[[assume(cond)]]
and use it in these cases, which potentially could lead to generation of more optimized builds in production.E.g. compiler would be able to optimize
while
loop intodo-while
loop (which means less instructions in assembly) if it knows that the loop will run at least once.However, there are lots of code fragments like:
and
If we simply wrap DBUG_ASSERT in an assume, the compiler will elide both the if-guard and the default branch — removing important fallback code.
This PR:
DBUG_ASSUME is implemented for GCC >= 13.0, Clang and MSVC.
TODO: Measure actual performance improvements and document LLVM/assembly diffs.
How can this PR be tested?
No additional test cases are needed, since this patch doesn't intend to change the behaviour. We may have missed a few “special” patterns, but we’ve covered all known occurrences.
Basing the PR against the correct MariaDB version
main
branch.PR quality check