2

I was experimenting with some template programming on the compiler explorer when I noticed that MSVC left a lot of templated function definitions in the assembly output even though they were optimized out of the main function and not called at any point; this is in contrast to GCC which does not include them. I checked using my installation of Visual Studio 2019 (16.1.3) and the output is the same as the compiler explorer's.

Are there any combinations of compiler and linker flags, or directives I can give in the source, which will remove these from the binary? I have tried combinations involving /O1 but to no avail.

The code I used is

template <unsigned int N>
constexpr unsigned int fact()
{
    return N * fact<N-1>();
}

template <>
constexpr unsigned int fact<0>()
{
    return 1;
}

int main()
{
    return fact<4>();
}

The GCC output is

main:
        mov     eax, 24
        ret

and from MSVC

unsigned int fact<1>(void) PROC                       ; fact<1>, COMDAT
        mov     eax, 1
        ret     0
unsigned int fact<1>(void) ENDP                       ; fact<1>

etc...

unsigned int fact<4>(void) PROC                       ; fact<4>, COMDAT
        mov     eax, 24
        ret     0
unsigned int fact<4>(void) ENDP                       ; fact<4>

unsigned int fact<0>(void) PROC                                ; fact<0>, COMDAT
        mov     eax, 1
        ret     0
unsigned int fact<0>(void) ENDP                                ; fact<0>

main    PROC                                            ; COMDAT
        mov     eax, 24
        ret     0
main    ENDP

whereas I am hoping to find a way for it to simply emit

main    PROC                                            ; COMDAT
        mov     eax, 24
        ret     0
main    ENDP
Dominic Price
  • 1,111
  • 8
  • 19
  • Did you try compiler:`/GL` and linker:`/LTCG`? – Robert Andrzejuk Jun 21 '19 at 08:35
  • I’ve just tried /GL but isn’t recognised, I have used /LTCG – Dominic Price Jun 21 '19 at 08:41
  • 3
    Godbolt output shows translation unit content, those unused functions won't be included into final executable. Linking is not performed at all. – user7860670 Jun 21 '19 at 08:41
  • @VTT that should be the answer, not a comment – darune Jun 21 '19 at 08:44
  • https://stackoverflow.com/questions/40554894/relation-between-msvc-compiler-linker-option-for-comdat-folding – Robert Andrzejuk Jun 21 '19 at 08:45
  • @darune This question is about MSVC not about how godbolt.org works and displays the result. – Robert Andrzejuk Jun 21 '19 at 08:46
  • I thought it might be godbolt missing a step, as I know it doesn't produce executable code (at least for MSVC) which is why I looked at the disassembly when I compiled the same code on my computer. I'm guessing the disassembly is thus free to include extra code from pdb/source files not present in the final binary? – Dominic Price Jun 21 '19 at 08:52
  • [dumpbin](https://learn.microsoft.com/en-us/cpp/build/reference/dumpbin-reference?view=vs-2019) can be used to inspect content of executable – user7860670 Jun 21 '19 at 09:08

0 Answers0