Pavel P
Pavel P

Reputation: 16877

Why a basic unreferenced c++ function does not get optimized away?

Consider this simple code:

#include <stdio.h>

extern "C"
{
    void p4nenc256v32();
    void p4ndec256v32();
}

void bigFunctionTest()
{
    p4nenc256v32();
    p4ndec256v32();
}

int main()
{
    printf("hello\n");
}

Code size of those p4nenc256v32/p4ndec256v32 functions is significant, roughly 1.5MB. This binary size when compiled with latest VS2022 with optimizations enabled is 1.5MB. If I comment out that unused bigFunctionTest function then resulting binary is smaller by 1.4MB. Any ideas why would this clearly unused function wouldn't be eliminated by compiler and/or linker in release builds? By default, VS2022 in release uses /Gy and /OPT:REF. I also tried mingw64 (gcc 12.2) with -fdata-sections -ffunction-sections -Wl,--gc-sections and results were much worse: when compiled with that dummy function exe grew by 5.2MB. Seem like ms and gcc compilers agree that for some reason these functions cannot be removed.

I created a working sample project that shows the issue: https://github.com/pps83/TestLinker.git (make sure to pull submodules as well) and filled an issue with VS issue tracker: Linker doesn't eliminate correctly dead code, however, I think I might get better explanation from SO users explaining what might be the reason for the problem.

Upvotes: 1

Views: 167

Answers (0)

Related Questions