NBISweden / MrBayes

MrBayes is a program for Bayesian inference and model choice across a wide range of phylogenetic and evolutionary models. For documentation and downloading the program, please see the home page:
http://NBISweden.github.io/MrBayes/
GNU General Public License v3.0
234 stars 79 forks source link

AX_EXT macro wrongly hardcodes build host CPU characteristics into the built executable #261

Closed mgorny closed 1 year ago

mgorny commented 2 years ago

What is the current observed behaviour?

The configure scripts detects the extensions provided by the build host CPU and builds code utilizing them, disregarding the characteristics of the target machine specified by CFLAGS.

For example:

./configure CFLAGS='-march=x86-64'

specifies that the executable should work on a generic x86-64 machine. However, the configure script appends a number of flags incompatible with the generic x86-64 target, plus compiles code incompatible with it:

gcc -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -msse4a -msha -maes -mavx -mfma -mavx2 -mrdrnd -mbmi -mbmi2 -madx -mabm  # ...

What is the expected/wanted behaviour?

Only code compatible with the selected target should be built.

How may we reproduce this bug?

Steps to reproduce the bug:

  1. ./configure CFLAGS='-march=x86-64'
  2. make

Would you be able to compile and run MrBayes to test fixes to this bug?

What is the environment that you run MrBayes in?

Other information that may be of use to us in resolving this issue

In my opinion, there are two main solutions to the problem. Either:

  1. Use compiler macros to determine which extensions are requested via CFLAGS, e.g. #if defined(__SSE2__) and so on. You can use e.g. clang -march=... -dM -E -x c /dev/null to see what flags are available for the given CPU.
  2. Call cpuid at runtime to determine which extensions are available. Build the respective optimized functions unconditionally and use attributes to enable necessary compiler instructions locally, and then check cpuid to determine which function to call.

In either case, AX_EXT is completely broken by design and should never be used. You shouldn't forcibly append -m flags either as that breaks the ability to compile MrBayes for CPU other than the one currently used.