From c87c63770f06c6b56382858f893977c47683b65f Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Tue, 9 Mar 2021 11:37:18 -0500 Subject: [PATCH 3/9] llvmGen: Accept range of LLVM versions Previously we would support only one LLVM major version. Here we generalize this to accept a range, taking this range to be LLVM 10 to 11, as 11 is necessary for Apple M1 support. We also accept 12, as that is what apple ships with BigSur on the M1. (cherry picked from commit 84927818ee68c6826327abc26d4647fb56053fb7) --- aclocal.m4 | 35 ++++--- compiler/GHC/CmmToLlvm.hs | 11 +- compiler/GHC/CmmToLlvm/Base.hs | 12 ++- compiler/GHC/SysTools/Tasks.hs | 9 +- configure.ac | 12 ++- distrib/configure.ac.in | 7 +- ghc.mk | 9 +- hadrian/src/Rules/BinaryDist.hs | 3 + hadrian/src/Rules/SourceDist.hs | 3 +- m4/ax_compare_version.m4 | 177 ++++++++++++++++++++++++++++++++ 10 files changed, 243 insertions(+), 35 deletions(-) create mode 100644 m4/ax_compare_version.m4 diff --git a/aclocal.m4 b/aclocal.m4 index 6f18a9662d..a296dbc243 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -3,6 +3,8 @@ # To be a good autoconf citizen, names of local macros have prefixed with FP_ to # ensure we don't clash with any pre-supplied autoconf ones. +m4_include([m4/ax_compare_version.m4]) + # FPTOOLS_WRITE_FILE # ------------------ # Write $2 to the file named $1. @@ -2231,22 +2233,29 @@ AC_DEFUN([XCODE_VERSION],[ # # $1 = the variable to set # $2 = the command to look for -# $3 = the version of the command to look for +# $3 = the lower bound version of the command to look for +# $4 = the upper bound version of the command to look for. # AC_DEFUN([FIND_LLVM_PROG],[ # Test for program with and without version name. - AC_CHECK_TOOLS([$1], [$2-$3 $2-$3.0 $2], [:]) - if test "$$1" != ":"; then - AC_MSG_CHECKING([$$1 is version $3]) - if test `$$1 --version | grep -c "version $3"` -gt 0 ; then - AC_MSG_RESULT(yes) - else - AC_MSG_RESULT(no) - $1="" - fi - else - $1="" - fi + PROG_VERSION_CANDIDATES=$(for llvmVersion in `seq $4 -1 $3`; do echo "$2-$llvmVersion $2-$llvmVersion.0"; done) + AC_CHECK_TOOLS([$1], [$PROG_VERSION_CANDIDATES $2], []) + AS_IF([test x"$$1" != x],[ + PROG_VERSION=`$$1 --version | awk '/.*version [[0-9\.]]+/{for(i=1;i<=NF;i++){ if(\$i ~ /^[[0-9\.]]+$/){print \$i}}}'` + AS_IF([test x"$PROG_VERSION" == x], + [AC_MSG_RESULT(no) + $1="" + AC_MSG_NOTICE([We only support llvm $3 to $4 (no version found).])], + [AC_MSG_CHECKING([$$1 version ($PROG_VERSION) is between $3 and $4]) + AX_COMPARE_VERSION([$PROG_VERSION], [lt], [$3], + [AC_MSG_RESULT(no) + $1="" + AC_MSG_NOTICE([We only support llvm $3 to $4 (found $PROG_VERSION).])], + [AX_COMPARE_VERSION([$PROG_VERSION], [gt], [$4], + [AC_MSG_RESULT(no) + $1="" + AC_MSG_NOTICE([We only support llvm $3 to $4 (found $PROG_VERSION).])], + [AC_MSG_RESULT(yes)])])])]) ]) # CHECK_LD_COPY_BUG() diff --git a/compiler/GHC/CmmToLlvm.hs b/compiler/GHC/CmmToLlvm.hs index ac8e9718e4..0d2ecb16be 100644 --- a/compiler/GHC/CmmToLlvm.hs +++ b/compiler/GHC/CmmToLlvm.hs @@ -64,7 +64,8 @@ llvmCodeGen dflags h cmm_stream let doWarn = wopt Opt_WarnUnsupportedLlvmVersion dflags when (not (llvmVersionSupported ver) && doWarn) $ putMsg dflags $ "You are using an unsupported version of LLVM!" $$ - "Currently only " <> text (llvmVersionStr supportedLlvmVersion) <> " is supported." <+> + "Currently only" <+> text (llvmVersionStr supportedLlvmVersionMin) <+> + "to" <+> text (llvmVersionStr supportedLlvmVersionMax) <+> "is supported." <+> "System LLVM version: " <> text (llvmVersionStr ver) $$ "We will try though..." let isS390X = platformArch (targetPlatform dflags) == ArchS390X @@ -73,8 +74,14 @@ llvmCodeGen dflags h cmm_stream "Warning: For s390x the GHC calling convention is only supported since LLVM version 10." <+> "You are using LLVM version: " <> text (llvmVersionStr ver) + -- HACK: the Nothing case here is potentially wrong here but we + -- currently don't use the LLVM version to guide code generation + -- so this is okay. + let llvm_ver :: LlvmVersion + llvm_ver = fromMaybe supportedLlvmVersionMin mb_ver + -- run code generation - a <- runLlvm dflags (fromMaybe supportedLlvmVersion mb_ver) bufh $ + a <- runLlvm dflags llvm_ver bufh $ llvmCodeGen' dflags (liftStream cmm_stream) bFlush bufh diff --git a/compiler/GHC/CmmToLlvm/Base.hs b/compiler/GHC/CmmToLlvm/Base.hs index ead3572a79..a47bfd3baa 100644 --- a/compiler/GHC/CmmToLlvm/Base.hs +++ b/compiler/GHC/CmmToLlvm/Base.hs @@ -15,7 +15,8 @@ module GHC.CmmToLlvm.Base ( LiveGlobalRegs, LlvmUnresData, LlvmData, UnresLabel, UnresStatic, - LlvmVersion, supportedLlvmVersion, llvmVersionSupported, parseLlvmVersion, + LlvmVersion, supportedLlvmVersionMin, supportedLlvmVersionMax, + llvmVersionSupported, parseLlvmVersion, llvmVersionStr, llvmVersionList, LlvmM, @@ -266,6 +267,7 @@ llvmPtrBits platform = widthInBits $ typeWidth $ gcWord platform -- Newtype to avoid using the Eq instance! newtype LlvmVersion = LlvmVersion { llvmVersionNE :: NE.NonEmpty Int } + deriving (Eq, Ord) parseLlvmVersion :: String -> Maybe LlvmVersion parseLlvmVersion = @@ -282,11 +284,13 @@ parseLlvmVersion = (ver_str, rest) = span isDigit s -- | The LLVM Version that is currently supported. -supportedLlvmVersion :: LlvmVersion -supportedLlvmVersion = LlvmVersion (sUPPORTED_LLVM_VERSION NE.:| []) +supportedLlvmVersionMin, supportedLlvmVersionMax :: LlvmVersion +supportedLlvmVersionMin = LlvmVersion (sUPPORTED_LLVM_VERSION_MIN NE.:| []) +supportedLlvmVersionMax = LlvmVersion (sUPPORTED_LLVM_VERSION_MAX NE.:| []) llvmVersionSupported :: LlvmVersion -> Bool -llvmVersionSupported (LlvmVersion v) = NE.head v == sUPPORTED_LLVM_VERSION +llvmVersionSupported v = + v > supportedLlvmVersionMin && v <= supportedLlvmVersionMax llvmVersionStr :: LlvmVersion -> String llvmVersionStr = intercalate "." . map show . llvmVersionList diff --git a/compiler/GHC/SysTools/Tasks.hs b/compiler/GHC/SysTools/Tasks.hs index 7dc40cef04..1ab3a0a425 100644 --- a/compiler/GHC/SysTools/Tasks.hs +++ b/compiler/GHC/SysTools/Tasks.hs @@ -11,6 +11,7 @@ module GHC.SysTools.Tasks where import GHC.Utils.Exception as Exception import GHC.Utils.Error +import GHC.CmmToLlvm.Base (LlvmVersion, llvmVersionStr, supportedLlvmVersionMin, supportedLlvmVersionMax, llvmVersionStr, parseLlvmVersion) import GHC.Driver.Types import GHC.Driver.Session import GHC.Utils.Outputable @@ -23,8 +24,6 @@ import System.IO import System.Process import GHC.Prelude -import GHC.CmmToLlvm.Base (LlvmVersion, llvmVersionStr, supportedLlvmVersion, parseLlvmVersion) - import GHC.SysTools.Process import GHC.SysTools.Info @@ -236,8 +235,10 @@ figureLlvmVersion dflags = traceToolCommand dflags "llc" $ do errorMsg dflags $ vcat [ text "Warning:", nest 9 $ text "Couldn't figure out LLVM version!" $$ - text ("Make sure you have installed LLVM " ++ - llvmVersionStr supportedLlvmVersion) ] + text ("Make sure you have installed LLVM between " + ++ llvmVersionStr supportedLlvmVersionMin + ++ " and " + ++ llvmVersionStr supportedLlvmVersionMax) ] return Nothing) diff --git a/configure.ac b/configure.ac index d967d90e70..c6a1c96d02 100644 --- a/configure.ac +++ b/configure.ac @@ -716,10 +716,14 @@ AC_SUBST(InstallNameToolCmd) # tools we are looking for. In the past, GHC supported a number of # versions of LLVM simultaneously, but that stopped working around # 3.5/3.6 release of LLVM. -LlvmVersion=9 -AC_SUBST([LlvmVersion]) -sUPPORTED_LLVM_VERSION=$(echo \($LlvmVersion\) | sed 's/\./,/') -AC_DEFINE_UNQUOTED([sUPPORTED_LLVM_VERSION], ${sUPPORTED_LLVM_VERSION}, [The supported LLVM version number]) +LlvmMinVersion=10 +LlvmMaxVersion=12 # inclusive +AC_SUBST([LlvmMinVersion]) +AC_SUBST([LlvmMaxVersion]) +sUPPORTED_LLVM_VERSION_MIN=$(echo \($LlvmMinVersion\) | sed 's/\./,/') +sUPPORTED_LLVM_VERSION_MAX=$(echo \($LlvmMaxVersion\) | sed 's/\./,/') +AC_DEFINE_UNQUOTED([sUPPORTED_LLVM_VERSION_MIN], ${sUPPORTED_LLVM_VERSION_MIN}, [The minimum supported LLVM version number]) +AC_DEFINE_UNQUOTED([sUPPORTED_LLVM_VERSION_MAX], ${sUPPORTED_LLVM_VERSION_MAX}, [The maximum supported LLVM version number]) dnl ** Which LLVM clang to use? dnl -------------------------------------------------------------- diff --git a/distrib/configure.ac.in b/distrib/configure.ac.in index 4de89941df..c287c3368d 100644 --- a/distrib/configure.ac.in +++ b/distrib/configure.ac.in @@ -118,19 +118,20 @@ AC_SUBST([StripCmd]) # tools we are looking for. In the past, GHC supported a number of # versions of LLVM simultaneously, but that stopped working around # 3.5/3.6 release of LLVM. -LlvmVersion=@LlvmVersion@ +LlvmMinVersion=@LlvmMinVersion@ +LlvmMaxVersion=@LlvmMaxVersion@ dnl ** Which LLVM llc to use? dnl -------------------------------------------------------------- AC_ARG_VAR(LLC,[Use as the path to LLVM's llc [default=autodetect]]) -FIND_LLVM_PROG([LLC], [llc], [$LlvmVersion]) +FIND_LLVM_PROG([LLC], [llc], [$LlvmMinVersion], [$LlvmMaxVersion]) LlcCmd="$LLC" AC_SUBST([LlcCmd]) dnl ** Which LLVM opt to use? dnl -------------------------------------------------------------- AC_ARG_VAR(OPT,[Use as the path to LLVM's opt [default=autodetect]]) -FIND_LLVM_PROG([OPT], [opt], [$LlvmVersion]) +FIND_LLVM_PROG([OPT], [opt], [$LlvmMinVersion], [$LlvmMaxVersion]) OptCmd="$OPT" AC_SUBST([OptCmd]) diff --git a/ghc.mk b/ghc.mk index 8434bd1d6e..0623e3eb5d 100644 --- a/ghc.mk +++ b/ghc.mk @@ -1074,7 +1074,7 @@ BIN_DIST_MK = $(BIN_DIST_PREP_DIR)/bindist.mk unix-binary-dist-prep: $(includes_1_H_CONFIG) $(includes_1_H_PLATFORM) $(includes_1_H_VERSION) $(call removeTrees,bindistprep/) "$(MKDIRHIER)" $(BIN_DIST_PREP_DIR) - set -e; for i in packages LICENSE compiler ghc rts libraries utils docs libffi includes driver mk rules Makefile aclocal.m4 config.sub config.guess install-sh llvm-targets llvm-passes ghc.mk inplace distrib/configure.ac distrib/README distrib/INSTALL; do ln -s ../../$$i $(BIN_DIST_PREP_DIR)/; done + set -e; for i in packages LICENSE compiler ghc rts libraries utils docs libffi includes driver mk rules Makefile m4 aclocal.m4 config.sub config.guess install-sh llvm-targets llvm-passes ghc.mk inplace distrib/configure.ac distrib/README distrib/INSTALL; do ln -s ../../$$i $(BIN_DIST_PREP_DIR)/; done echo "HADDOCK_DOCS = $(HADDOCK_DOCS)" >> $(BIN_DIST_MK) echo "BUILD_SPHINX_HTML = $(BUILD_SPHINX_HTML)" >> $(BIN_DIST_MK) echo "BUILD_SPHINX_PDF = $(BUILD_SPHINX_PDF)" >> $(BIN_DIST_MK) @@ -1167,12 +1167,13 @@ SRC_DIST_TESTSUITE_TARBALL = $(SRC_DIST_ROOT)/$(SRC_DIST_TESTSUITE_NAME). # # Files to include in source distributions # -SRC_DIST_GHC_DIRS = mk rules docs distrib bindisttest libffi includes \ +SRC_DIST_GHC_DIRS = mk m4 rules docs distrib bindisttest libffi includes \ utils docs rts compiler ghc driver libraries libffi-tarballs \ - hadrian + hadrian SRC_DIST_GHC_FILES += \ configure.ac config.guess config.sub configure \ - aclocal.m4 README.md ANNOUNCE HACKING.md INSTALL.md LICENSE Makefile \ + aclocal.m4 m4/ax_compare_version.m4 \ + README.md ANNOUNCE HACKING.md INSTALL.md LICENSE Makefile \ install-sh llvm-targets llvm-passes VERSION GIT_COMMIT_ID \ boot packages ghc.mk MAKEHELP.md diff --git a/hadrian/src/Rules/BinaryDist.hs b/hadrian/src/Rules/BinaryDist.hs index 8709de6b26..a527664b23 100644 --- a/hadrian/src/Rules/BinaryDist.hs +++ b/hadrian/src/Rules/BinaryDist.hs @@ -203,11 +203,14 @@ bindistRules = do root -/- "bindist" -/- "ghc-*" -/- "configure" %> \configurePath -> do ghcRoot <- topDirectory copyFile (ghcRoot -/- "aclocal.m4") (ghcRoot -/- "distrib" -/- "aclocal.m4") + copyDirectory (ghcRoot -/- "m4") (ghcRoot -/- "distrib") buildWithCmdOptions [] $ target (vanillaContext Stage1 ghc) (Autoreconf $ ghcRoot -/- "distrib") [] [] -- We clean after ourselves, moving the configure script we generated in -- our bindist dir removeFile (ghcRoot -/- "distrib" -/- "aclocal.m4") + removeDirectory (ghcRoot -/- "distrib" -/- "m4") + moveFile (ghcRoot -/- "distrib" -/- "configure") configurePath -- Generate the Makefile that enables the "make install" part diff --git a/hadrian/src/Rules/SourceDist.hs b/hadrian/src/Rules/SourceDist.hs index 78c1539b3d..de35922ae1 100644 --- a/hadrian/src/Rules/SourceDist.hs +++ b/hadrian/src/Rules/SourceDist.hs @@ -113,7 +113,8 @@ prepareTree dest = do , "mk" , "rts" , "rules" - , "utils" ] + , "utils" + , "m4" ] srcFiles = [ "GIT_COMMIT_ID" , "HACKING.md" diff --git a/m4/ax_compare_version.m4 b/m4/ax_compare_version.m4 new file mode 100644 index 0000000000..ffb4997e8b --- /dev/null +++ b/m4/ax_compare_version.m4 @@ -0,0 +1,177 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_compare_version.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE]) +# +# DESCRIPTION +# +# This macro compares two version strings. Due to the various number of +# minor-version numbers that can exist, and the fact that string +# comparisons are not compatible with numeric comparisons, this is not +# necessarily trivial to do in a autoconf script. This macro makes doing +# these comparisons easy. +# +# The six basic comparisons are available, as well as checking equality +# limited to a certain number of minor-version levels. +# +# The operator OP determines what type of comparison to do, and can be one +# of: +# +# eq - equal (test A == B) +# ne - not equal (test A != B) +# le - less than or equal (test A <= B) +# ge - greater than or equal (test A >= B) +# lt - less than (test A < B) +# gt - greater than (test A > B) +# +# Additionally, the eq and ne operator can have a number after it to limit +# the test to that number of minor versions. +# +# eq0 - equal up to the length of the shorter version +# ne0 - not equal up to the length of the shorter version +# eqN - equal up to N sub-version levels +# neN - not equal up to N sub-version levels +# +# When the condition is true, shell commands ACTION-IF-TRUE are run, +# otherwise shell commands ACTION-IF-FALSE are run. The environment +# variable 'ax_compare_version' is always set to either 'true' or 'false' +# as well. +# +# Examples: +# +# AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8]) +# AX_COMPARE_VERSION([3.15],[lt],[3.15.8]) +# +# would both be true. +# +# AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8]) +# AX_COMPARE_VERSION([3.15],[gt],[3.15.8]) +# +# would both be false. +# +# AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8]) +# +# would be true because it is only comparing two minor versions. +# +# AX_COMPARE_VERSION([3.15.7],[eq0],[3.15]) +# +# would be true because it is only comparing the lesser number of minor +# versions of the two values. +# +# Note: The characters that separate the version numbers do not matter. An +# empty string is the same as version 0. OP is evaluated by autoconf, not +# configure, so must be a string, not a variable. +# +# The author would like to acknowledge Guido Draheim whose advice about +# the m4_case and m4_ifvaln functions make this macro only include the +# portions necessary to perform the specific comparison specified by the +# OP argument in the final configure script. +# +# LICENSE +# +# Copyright (c) 2008 Tim Toolan +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 13 + +dnl ######################################################################### +AC_DEFUN([AX_COMPARE_VERSION], [ + AC_REQUIRE([AC_PROG_AWK]) + + # Used to indicate true or false condition + ax_compare_version=false + + # Convert the two version strings to be compared into a format that + # allows a simple string comparison. The end result is that a version + # string of the form 1.12.5-r617 will be converted to the form + # 0001001200050617. In other words, each number is zero padded to four + # digits, and non digits are removed. + AS_VAR_PUSHDEF([A],[ax_compare_version_A]) + A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \ + -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \ + -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \ + -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \ + -e 's/[[^0-9]]//g'` + + AS_VAR_PUSHDEF([B],[ax_compare_version_B]) + B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \ + -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \ + -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \ + -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \ + -e 's/[[^0-9]]//g'` + + dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary + dnl # then the first line is used to determine if the condition is true. + dnl # The sed right after the echo is to remove any indented white space. + m4_case(m4_tolower($2), + [lt],[ + ax_compare_version=`echo "x$A +x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"` + ], + [gt],[ + ax_compare_version=`echo "x$A +x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"` + ], + [le],[ + ax_compare_version=`echo "x$A +x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"` + ], + [ge],[ + ax_compare_version=`echo "x$A +x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"` + ],[ + dnl Split the operator from the subversion count if present. + m4_bmatch(m4_substr($2,2), + [0],[ + # A count of zero means use the length of the shorter version. + # Determine the number of characters in A and B. + ax_compare_version_len_A=`echo "$A" | $AWK '{print(length)}'` + ax_compare_version_len_B=`echo "$B" | $AWK '{print(length)}'` + + # Set A to no more than B's length and B to no more than A's length. + A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"` + B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"` + ], + [[0-9]+],[ + # A count greater than zero means use only that many subversions + A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"` + B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"` + ], + [.+],[ + AC_WARNING( + [invalid OP numeric parameter: $2]) + ],[]) + + # Pad zeros at end of numbers to make same length. + ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`" + B="$B`echo $A | sed 's/./0/g'`" + A="$ax_compare_version_tmp_A" + + # Check for equality or inequality as necessary. + m4_case(m4_tolower(m4_substr($2,0,2)), + [eq],[ + test "x$A" = "x$B" && ax_compare_version=true + ], + [ne],[ + test "x$A" != "x$B" && ax_compare_version=true + ],[ + AC_WARNING([invalid OP parameter: $2]) + ]) + ]) + + AS_VAR_POPDEF([A])dnl + AS_VAR_POPDEF([B])dnl + + dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE. + if test "$ax_compare_version" = "true" ; then + m4_ifvaln([$4],[$4],[:])dnl + m4_ifvaln([$5],[else $5])dnl + fi +]) dnl AX_COMPARE_VERSION -- 2.33.0