3

Let's say I've defined a class like so:

#include <vector>

enum class cpu { sse2, sse4, avx, avx4 };

template <cpu architecture = current_cpu>
class foo // current_cpu defined based on compiler's target architecture
{
    std::vector<int> data_;

public:
    // methods...
};

I compile this 4 times for each of the target architectures (sse2, sse4, avx, avx2) for x86_64.

This generates 4 different classes foo<cpu::sse2>, foo<cpu::sse4>, etc

I then use some runtime dispatching to create the correct version at runtime and use some pointer trickery to invoke the methods on the instance in memory.

The question is related to the std::vector<int> _data. Can the compiler decide not to inline all of std::vector so that the linker is left with several versions of, say, a std::vector method compiled for multiple architectures and then link to the wrong one because they all have the same mangled name?

keith
  • 5,122
  • 3
  • 21
  • 50
  • I wonder how the compiler would even compile `std::vector` for several architectures. I believe it will only compile it for the architecture you compile the code for. – NathanOliver Dec 09 '19 at 19:56
  • Neat question! This is pedantic, but consider adding a `template <...>` declaration above your struct before a bunch of people come running 'round telling you that your code isn't valid C++. – parktomatomi Dec 09 '19 at 20:03
  • You may be interested in this: https://gcc.gnu.org/wiki/FunctionMultiVersioning – Jesper Juhl Dec 09 '19 at 20:04
  • @parktomatomi - well spotted thanks :-) – keith Dec 09 '19 at 20:09
  • @NathanOliver- Reinstate Monica - the class will be compiled four times for different architectures. – keith Dec 09 '19 at 20:10
  • @Jesper Juhl - is there an equivalent for MSVC? (p.s. not sure it answers the question, but it's an interesting point :-)) – keith Dec 09 '19 at 20:12
  • This document mentions that when CPU dispatching is used, there is a suffix added to mangled function names to distinguish them on the Itanium ABI used by g++ and clang: https://www.agner.org/optimize/calling_conventions.pdf . This other SO answer mentions that MSVC only supports limited CPU dispatch: https://stackoverflow.com/questions/49846979/does-msvc-2017-support-automatic-cpu-dispatch – parktomatomi Dec 09 '19 at 21:16
  • @keith I don't know about MSVC, but I'd look that up if I were you :) – Jesper Juhl Dec 10 '19 at 05:49

1 Answers1

0

On Clang, -fvisibility-inlines-hidden in theory could be used, however it doesn't seem to be working in practice. The alternative is to avoid classes/functions that do not make themselves unique to different architectures.

keith
  • 5,122
  • 3
  • 21
  • 50