1
#include <iostream>

int ExpensiveFunction() {
    // Do expensive calculation
    // ...

    std::cout << "You shouldn't see this!" << std::endl;

    return 42;
}

void print(int num)
{

}

int main()
{
    print(ExpensiveFunction());
    return 0;
}

I would like that the compiler recognizes that print is an empty function, thus completely remove the print call and argument evaluation of the statement:

print(ExpensiveFunction());

However no matter what kind of optimization settings I use (with MSVC) it will always perform the calculations of the expensive function.

I recognize that an optimization that removes the call to ExpensiveFunction may be a destructive optimization, but I feel that this should be possible without converting print into a macro that will be compiled out in the most optimized configuration.

Is it at all possible to direct any C++ compiler to do this?

EDIT: I've added a std::cout statement

7
  • 1
    You can experiment with various compilers and settings yourself. You can see that GCC 8.1 and Clang 8.0.0 both elided the call entirely, at least with -O3.
    – Useless
    Feb 21, 2020 at 13:56
  • 3
    The compiler can only do so if ExpensiveFunction() does not have observable side effects. To make that assertion the compiler must probably see the source code of the function, either because it is present in the translation unit or because you enable link time / whole program optimization. Feb 21, 2020 at 13:56
  • True (about side-effects), but when I try MSVC on the Godbolt site, even the trivial ExpensiveFunction is still called.
    – Useless
    Feb 21, 2020 at 13:58
  • 1
    @Useless Make sure to use the proper optimization options. The default is unoptimized; MSVC removes the calls both with /O2 (speed) and /Ox (space). -O2 does not have any effect. Feb 21, 2020 at 14:11
  • Also, if I declare ExpensiveFunction() extern, without providing the implementation, the compiler simply calls ExpensiveFunction() directly (because it cannot know it does not have side effects) and omits the empty print() wrapper entirely. When we tell gcc via __attribute__ ((pure)) that there are no side-effects gcc dutifully removes the call even without source. Feb 21, 2020 at 14:15

3 Answers 3

3

Is it at all possible to direct any C++ compiler to do this?

Probably not directly - it's a quality-of-implementation issue.

Both Clang and GCC (for the recent versions tested), do this with the standard -O3 flag. Actually, they even do it with -O1 and -O2 ... you have to disable optimization completely for them to emit the call to ExpensiveFunction.

A quick attempt with x64 MSVC 19.24 shows it will optimize out the call as well, at least with /O2.

MSVC command-line options are listed here.

GCC command-line options are in the manual, optimization-specific ones are here.

In general you can see what several different compilers do to the same code simultaneously with Matt Godbolt's compiler explorer, like so.


Note that this is only testing your sample code - if ExpensiveFunction has observable side-effects, the optimizer will be unable to remove it.


Edit so it turns out that ExpensiveFunction does have observable side-effects.

I recognize that an optimization that removes the call to ExpensiveFunction may be a destructive optimization,

There is no such thing as a "destructive optimization". Optimization is something that improves the non-functional characteristics of your program (typically performance or size) without changing the functional characteristics, which are specifically defined as the visible side-effects.

Let's consider your title, corrected to match the body of the question:

Are there C++ compiler options that allow aggressive removal of all function calls and passed in arguments to functions with non-empty bodies?

If the compiler is free to speed up your code by throwing away things that take time even if they have an externally-observable effect, life becomes very simple: it can just replace every program with

int main() {}

and call it a "destructive optimization". This is a correct solution to your corrected title, right?

... but I feel that this should be possible without converting print into a macro that will be compiled out in the most optimized configuration.

You can alternatively make the body of ExpensiveFunction conditional, if for example it is really debug output whose observable side-effect you want to disable in some builds

int ExpensiveFunction() {
    #ifndef DEBUG
    // Do expensive calculation
    // ...

    std::cout << "You shouldn't see this!" << std::endl;
    #endif
    return 42;
}

But disabling code that has observable side-effects must always be under your manual control, unless you're happy with my extra-reductive whole-program optimization above.

6
  • I can confirm that on godbolt for gcc. If I declare the functions static, i.e. no external linking, the compiler does not even emit any code or symbol for them. Feb 21, 2020 at 14:02
  • ("The compiler" above being gcc. MSVC emits code even for static functions but does also not call them in main.) Feb 21, 2020 at 14:09
  • To be fair, it wouldn't be unusual for unused functions to be kept around until link time.
    – Useless
    Feb 21, 2020 at 14:14
  • Thank you for your answer. I've clarified my example. With a std::cout statement it won't be compiled out with any optimization flags, so it's impossible that it will be removed by any compiler because it has side-effects? I'm left to defining macros for each function I want to have removed this way? Feb 21, 2020 at 20:22
  • If you have a function with a side-effect, it will always be called, yes. Of course! Otherwise cout statements could be removed so long as you don't look at the return value ... If the compiler optimizes out functions with side-effects, it's changing the behaviour of your program, and not just its performance.
    – Useless
    Feb 23, 2020 at 12:46
3

For GCC and Clang, you can use __attribute__((pure)) to reassure the compiler that ExpensiveFunction() doesn't have any side effects and can be elided if its result is not used. In some simple cases these compilers can figure out the pure-ness of a function anyway, but using the attribute allows you to get this effect in more complex cases and for functions defined in other translation units.

Unfortunately, MSVC doesn't have any equivalent facility that I'm aware of. See pure/const function attributes in different compilers for more.

1
  • Thank you for this suggestion. It's a shame MSVC doesn't have this. MSVC is also reluctant to run some constexpr functions at compile time where other compilers have no problem doing this. I always have to define a constexpr variable first to force the compiler to do this :( Feb 21, 2020 at 20:35
1

For MSVC, you'd need /O2 (optimize), /GL+/LTCG (link-time code generation) and /OPT:REF (eliminate functions and data that are never referenced).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.