MINOR: compiler: add a new DO_NOT_FOLD() macro to prevent code folding
authorWilly Tarreau <w@1wt.eu>
Fri, 2 Feb 2024 16:00:01 +0000 (17:00 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 7 Feb 2024 10:42:04 +0000 (11:42 +0100)
commit7d2ee73d02f6383d1d68fe338e3089ad08913776
treeaa0e7953e4dfedc844ce66f0cb0e7c7897d89ecf
parent6bd7a36c1af6470ade9f2ba5207e2ebdfabca3c5
MINOR: compiler: add a new DO_NOT_FOLD() macro to prevent code folding

Modern compilers sometimes perform function tail merging and identical
code folding, which consist in merging identical occurrences of same
code paths, generally final ones (e.g. before a return, a jump or an
unreachable statement). In the case of ABORT_NOW(), it can happen that
the compiler merges all of them into a single one in a function,
defeating the purpose of the check which initially was to figure where
the bug occurred.

Here we're creating a DO_NO_FOLD() macro which makes use of the line
number and passes it as an integer argument to an empty asm() statement.
The effect is a code position dependency which prevents the compiler
from merging the code till that point (though it may still merge the
following code). In practice it's efficient at stopping the compilers
from merging calls to ha_crash_now(), which was the initial purpose.

It may also be used to force certain optimization constructs since it
gives more control to the developer.

(cherry picked from commit e06e8a2390c42260e2998c50887a09e1a78f7252)
Signed-off-by: Willy Tarreau <w@1wt.eu>
include/haproxy/compiler.h