Borph
Borph

Reputation: 933

How to ensure some code is optimized away?

tl;dr: Can it be ensured somehow (e.g. by writing a unit test) that some things are optimized away, e.g. whole loops?

The usual approach to be sure that something is not included in the production build is wrapping it with #if...#endif. But I prefer to stay with C++ mechanics instead. Even there, instead of complicated template specializations I like to keep implementations simple and argue "hey, the compiler will optimize this out anyway".

Context is embedded SW in automotive (binary size matters) with often poor compilers. They are certified in the sense of safety, but usually not good in optimizations.

Example 1: In a container the destruction of elements is typically a loop:

for(size_t i = 0; i<elements; i++)
    buffer[i].~T();

This works also for build-in types such as int, as the standard allows the explicit call of the destructor also for any scalar types (C++11 12.4-15). In such case the loop does nothing and is optimized out. In GCC it is, but in another (Aurix) not, I saw a literally empty loop in the disassembly! So that needed a template specialization to fix it.

Example 2: Code, which is intended for debugging, profiling or fault-injection etc. only:

constexpr bool isDebugging = false; // somehow a global flag
void foo(int arg) {
    if( isDebugging ) {
        // Albeit 'dead' section, it may not appear in production binary!
        // (size, security, safety...)
        // 'if constexpr..' not an option (C++11)
        std::cout << "Arg was " << arg << std::endl;
    }
    // normal code here...
}

I can look at the disassembly, sure. But being an upstream platform software it's hard to control all targets, compilers and their options one might use. The fear is big that due to any reason a downstream project has a code bloat or performance issue.

Bottom line: Is it possible to write the software in a way, that certain code is known to be optimized away in a safe manner as a #if would do? Or a unit tests, which give a fail if optimization is not as expected?

[Timing tests come to my mind for the first problem, but being bare-metal I don't have convenient tools yet.]

Upvotes: 6

Views: 1412

Answers (7)

kisch
kisch

Reputation: 414

Here's another nice solution using inline assembly. This uses assembler directives only, so it might even be kind of portable (checked with clang).

constexpr bool isDebugging = false; // somehow a global flag
void foo(int arg) {
    if( isDebugging ) {
        asm(".globl _marker\n_marker:\n");
        std::cout << "Arg was " << arg << std::endl; // may not appear in production binary!
    }
    // normal code here...
}

This would leave an exported linker symbol in the compiled executable, if the code isn't optimised away. You can check for this symbol using nm(1).

clang can even stop the compilation right away:

constexpr bool isDebugging = false; // somehow a global flag
void foo(int arg) {
    if( isDebugging ) {
        asm("_marker=1\n");
        std::cout << "Arg was " << arg << std::endl; // may not appear in production binary!
    }
    asm volatile (
        ".ifdef _marker\n"
        ".err \"code not optimised away\"\n"
        ".endif\n"
    );
    // normal code here...
}

Upvotes: 1

kisch
kisch

Reputation: 414

Here's another different way that also covers the first example. You can verify (at runtime) that the code has been eliminated, by comparing two labels placed around it.

This relies on the GCC extension "Labels as Values" https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

before:
    for(size_t i = 0; i<elements; i++)
        buffer[i].~T();
behind:
    if (intptr_t(&&behind) != intptr_t(&&before)) abort();

It would be nice if you could check this in a static_assert(), but sadly the difference of &&label expressions is not accepted as compile-time constant.

GCC insists on inserting a runtime comparison, even though both labels are in fact at the same address.

Interestingly, if you compare the addresses (type void*) directly, without casting them to intptr_t, GCC falsely optimises away the if() as "always true", whereas clang correctly optimises away the complete if() as "always false", even at -O1.

Upvotes: 0

kisch
kisch

Reputation: 414

Insert a reference to external data or function into the block that should be verified to be optimised away. Like this:

extern void nop();
constexpr bool isDebugging = false; // somehow a global flag
void foo(int arg) {
    if( isDebugging ) {
        nop();
        std::cout << "Arg was " << arg << std::endl; // may not appear in production binary!
    }
    // normal code here...
}

In Debug-Builds, link with an implementation of nop() in a extra compilation unit nop.cpp:

void nop() {}

In Release-Builds, don't provide an implementation. Release builds will only link if the optimisable code is eliminated.

`- kisch

Upvotes: 1

Dirk Herrmann
Dirk Herrmann

Reputation: 5939

This is not an answer to "How to ensure some code is optimized away?" but to your summary line "Can a unit test be written that e.g. whole loops are optimized away?".

First, the answer depends on how far you see the scope of unit-testing - so if you put in performance tests, you might have a chance.

If in contrast you understand unit-testing as a way to test the functional behaviour of the code, you don't. For one thing, optimizations (if the compiler works correctly) shall not change the behaviour of standard-conforming code.

With incorrect code (code that has undefined behaviour) optimizers can do what they want. (Well, for code with undefined behaviour the compiler can do it also in the non-optimizing case, but sometimes only the deeper analyses peformed during optimization make it possible for the compiler to detect that some code has undefined behaviour.) Thus, if you write unit-tests for some piece of code with undefined behaviour, the test results may differ when you run the tests with and without optimization. But, strictly speaking, this only tells you that the compiler translated the code both times in a different way - it does not guarantee you that the code is optimized in the way you want it to be.

Upvotes: 0

JVApen
JVApen

Reputation: 11317

Looking at your question, I see several (sub-)questions in it that require an answer. Not all answers might be possible with your bare-metal compilers as hardware vendors don't care that much about C++.

The first question is: How do I write code in a way that I'm sure it gets optimized. The obvious answer here is to put everything in a single compilation unit so the caller can see the implementation.

The second question is: How can I force a compiler to optimize. Here constexpr is a bless. Depending on whether you have support for C++11, C++14, C++17 or even the upcoming C++20, you'll get different feature sets of what you can do in a constexpr function. For the usage:

constexpr char c = std::string_view{"my_very_long_string"}[7];

With the code above, c is defined as a constexpr variable. Because you apply it to the variable, you require some things:

  • Your compiler should optimize the code so the value of c is known at compile time. This even holds true for -O0 builds!
  • All functions used for calculate c are constexpr and available. (and by result, enforce the behaviour of the first question)
  • No undefined behaviour is allowed to be triggered in the calculation of c. (For the given value)

The negative about this is: Your input needs to be known at compile time.

C++17 also provides if constexpr which has similar requirements: condition needs to be calculated at compile time. The result is that 1 branch of the code ain't allowed to be compiled (as it even can contain elements that don't work on the type you are using).

Which than brings us to the question: How do I ensure sufficient optimizations for my program to run fast enough, even if my compiler ain't well behaving. Here the only relevant answer is: create benchmarks and compare the results. Take the effort to setup a CI job that automates this for you. (And yes, you can even use external hardware although not being that easy) In the end, you have some requirements: handling A should take less than X seconds. Do A several times and time it. Even if they don't handle everything, as long as it's within the requirements, its fine.

Note: As this is about debug, you most likely can track the size of an executable as well. As soon as you start using streams, a lot of conversions to string ... your exe size will grow. (And you'll find it a bless as you will immediately find commits which add 10% to the image size)

And than the final question: You have a buggy compiler, it doesn't meet my requirements. Here the only answer is: Replace it. In the end, you can use any compiler to compiler your code to bare metal, as long as the linker scripts work. If you need a start, C++Now 2018: Michael Caisse “Modern C++ in Embedded Systems” gives you a very good idea of what you need to use a different compiler. (Like a recent Clang or GCC, on which you even can log bugs if the optimization ain't good enough)

Upvotes: 3

Xirema
Xirema

Reputation: 20396

if constexpr is the canonical C++ expression (since C++17) for this kind of test.

constexpr bool DEBUG = /*...*/;

int main() {
    if constexpr(DEBUG) {
        std::cerr << "We are in debugging mode!" << std::endl;
    }
}

If DEBUG is false, then the code to print to the console won't generate at all. So if you have things like log statements that you need for checking the behavior of your code, but which you don't want to interact with in production code, you can hide them inside if constexpr expressions to eliminate the code entirely once the code is moved to production.

Upvotes: 5

Brian
Brian

Reputation: 645

There may be a more elegant way, and it's not a unit test, but if you're just looking for that particular string, and you can make it unique,

strings $COMPILED_BINARY | grep "Arg was"

should show you if the string is being included

Upvotes: 4

Related Questions