From 0404da7ba7d22379b236c503d6a87746a848776c Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 27 Jun 2021 17:46:50 +0100 Subject: [PATCH] superenv: handle formulae with runtime CPU detection Some formulae are able to detect the features of the runtime CPU, and execute code accordingly. This typically entails 1) the detection of features of the build-time CPU in order to determine the targets that the compiler can generate code for, and 2) generating code for the targets that the compiler can support. Our filtering of optimization flags can cause misdetection of compiler features, leading to failed builds [1], and miscompilation even when the build does not fail [2]. Let's try to fix this by allowing formulae to declare `ENV.runtime_cpu_detection` which skips the filtering of `-march` and related flags. I've also skipped the filtering of the optimisation level, since it seems to me that if upstream maintainers have gone to the lengths of writing code that detects runtime hardware, they probably also know better about appropriate `-O` flags to use. This is a partial list of formulae that should make use of this feature: 1. apache-arrow 2. fftw 3. gromacs 4. open-mpi 5. openblas Partially resolves Homebrew/homebrew-core#76537. [1] open-mpi/ompi#8306 and linked issues/PRs [2] Homebrew/homebrew-core#76537 --- Library/Homebrew/extend/ENV/super.rb | 8 +++++++- Library/Homebrew/shims/super/cc | 14 ++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/extend/ENV/super.rb b/Library/Homebrew/extend/ENV/super.rb index 17334a3a24..e2ce2e6262 100644 --- a/Library/Homebrew/extend/ENV/super.rb +++ b/Library/Homebrew/extend/ENV/super.rb @@ -91,10 +91,11 @@ module Superenv # g - Enable "-stdlib=libc++" for clang. # h - Enable "-stdlib=libstdc++" for clang. # K - Don't strip -arch , -m32, or -m64 + # d - Don't strip -march=. Use only in formulae that + # have runtime detection of CPU features. # w - Pass -no_weak_imports to the linker # # These flags will also be present: - # s - apply fix for sed's Unicode support # a - apply fix for apr-1-config path end alias generic_setup_build_environment setup_build_environment @@ -314,6 +315,11 @@ module Superenv append_to_cccfg "K" end + sig { void } + def runtime_cpu_detection + append_to_cccfg "d" + end + sig { void } def cxx11 append_to_cccfg "x" diff --git a/Library/Homebrew/shims/super/cc b/Library/Homebrew/shims/super/cc index b1cc61e274..fbe556f8b0 100755 --- a/Library/Homebrew/shims/super/cc +++ b/Library/Homebrew/shims/super/cc @@ -173,10 +173,12 @@ class Cmd case arg when /^-g\d?$/, /^-gstabs\d+/, "-gstabs+", /^-ggdb\d?/, - /^-march=.+/, /^-mtune=.+/, /^-mcpu=.+/, - /^-O[0-9zs]?$/, "-fast", "-no-cpp-precomp", - "-pedantic", "-pedantic-errors", "-Wno-long-double", + /^-mtune=.+/, /^-mcpu=.+/, /^-O[0-9zs]?$/, + "-fast", "-no-cpp-precomp", "-pedantic", + "-pedantic-errors", "-Wno-long-double", "-Wno-unused-but-set-variable" + when /^-march=.+/ + args << arg if runtime_cpu_detection? when "-fopenmp", "-lgomp", "-mno-fused-madd", "-fforce-addr", "-fno-defer-pop", "-mno-dynamic-no-pic", "-fearly-inlining", /^-f(?:no-)?inline-functions-called-once/, /^-finline-limit/, /^-f(?:no-)?check-new/, "-fno-delete-null-pointer-checks", @@ -283,7 +285,7 @@ class Cmd args << "-pipe" args << "-w" unless configure? args << "-#{ENV["HOMEBREW_OPTIMIZATION_LEVEL"]}" - args.concat(optflags) + args.concat(optflags) unless runtime_cpu_detection? args.concat(archflags) args << "-std=#{@arg0}" if /c[89]9/.match?(@arg0) args @@ -385,6 +387,10 @@ class Cmd config.include?("K") end + def runtime_cpu_detection? + config.include?("d") + end + def no_weak_imports? config.include?("w") end