Merge pull request #2850 from jcmdln/linux-llvm-17

Support LLVM >=17.0.1 on Darwin and Linux
This commit is contained in:
gingerBill
2023-10-16 16:15:59 +01:00
committed by GitHub
4 changed files with 99 additions and 157 deletions
+70 -152
View File
@@ -1,133 +1,87 @@
#!/usr/bin/env bash #!/usr/bin/env sh
set -eu set -eu
: ${CXX=clang++}
: ${CPPFLAGS=} : ${CPPFLAGS=}
: ${CXX=clang++}
: ${CXXFLAGS=} : ${CXXFLAGS=}
: ${LDFLAGS=} : ${LDFLAGS=}
: ${ODIN_VERSION=dev-$(date +"%Y-%m")} : ${LLVM_CONFIG=}
: ${GIT_SHA=}
CPPFLAGS="$CPPFLAGS -DODIN_VERSION_RAW=\"dev-$(date +"%Y-%m")\""
CXXFLAGS="$CXXFLAGS -std=c++14" CXXFLAGS="$CXXFLAGS -std=c++14"
DISABLED_WARNINGS="-Wno-switch -Wno-macro-redefined -Wno-unused-value"
LDFLAGS="$LDFLAGS -pthread -lm -lstdc++" LDFLAGS="$LDFLAGS -pthread -lm -lstdc++"
OS_ARCH="$(uname -m)"
OS_NAME="$(uname -s)"
if [ -d ".git" ] && [ $(which git) ]; then if [ -d ".git" ] && [ -n "$(command -v git)" ]; then
versionTag=( $(git show --pretty='%cd %h' --date=format:%Y-%m --no-patch --no-notes HEAD) ) GIT_SHA=$(git show --pretty='%h' --no-patch --no-notes HEAD)
if [ $? -eq 0 ]; then CPPFLAGS="$CPPFLAGS -DGIT_SHA=\"$GIT_SHA\""
ODIN_VERSION="dev-${versionTag[0]}"
GIT_SHA="${versionTag[1]}"
CPPFLAGS="$CPPFLAGS -DGIT_SHA=\"$GIT_SHA\""
fi
fi fi
CPPFLAGS="$CPPFLAGS -DODIN_VERSION_RAW=\"$ODIN_VERSION\"" error() {
printf "ERROR: %s\n" "$1"
DISABLED_WARNINGS="-Wno-switch -Wno-macro-redefined -Wno-unused-value"
OS=$(uname)
panic() {
printf "%s\n" "$1"
exit 1 exit 1
} }
version() { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; } if [ -z "$LLVM_CONFIG" ]; then
# darwin, linux, openbsd
config_darwin() { if [ -n "$(command -v llvm-config-17)" ]; then LLVM_CONFIG="llvm-config-17"
local ARCH=$(uname -m) elif [ -n "$(command -v llvm-config-13)" ]; then LLVM_CONFIG="llvm-config-13"
: ${LLVM_CONFIG=llvm-config} elif [ -n "$(command -v llvm-config-12)" ]; then LLVM_CONFIG="llvm-config-12"
elif [ -n "$(command -v llvm-config-11)" ]; then LLVM_CONFIG="llvm-config-11"
# allow for arm only llvm's with version 13 # freebsd
if [ "${ARCH}" == "arm64" ]; then elif [ -n "$(command -v llvm-config17)" ]; then LLVM_CONFIG="llvm-config-17"
MIN_LLVM_VERSION=("13.0.0") elif [ -n "$(command -v llvm-config13)" ]; then LLVM_CONFIG="llvm-config-13"
elif [ -n "$(command -v llvm-config12)" ]; then LLVM_CONFIG="llvm-config-12"
elif [ -n "$(command -v llvm-config11)" ]; then LLVM_CONFIG="llvm-config-11"
# fallback
elif [ -n "$(command -v llvm-config)" ]; then LLVM_CONFIG="llvm-config"
else else
# allow for x86 / amd64 all llvm versions beginning from 11 error "No llvm-config command found. Set LLVM_CONFIG to proceed."
MIN_LLVM_VERSION=("11.1.0")
fi fi
fi
if [ $(version $($LLVM_CONFIG --version)) -lt $(version $MIN_LLVM_VERSION) ]; then LLVM_VERSION="$($LLVM_CONFIG --version)"
if [ "${ARCH}" == "arm64" ]; then LLVM_VERSION_MAJOR="$(echo $LLVM_VERSION | awk -F. '{print $1}')"
panic "Requirement: llvm-config must be base version 13 for arm64" LLVM_VERSION_MINOR="$(echo $LLVM_VERSION | awk -F. '{print $2}')"
else LLVM_VERSION_PATCH="$(echo $LLVM_VERSION | awk -F. '{print $3}')"
panic "Requirement: llvm-config must be base version greater than 11 for amd64/x86"
if [ $LLVM_VERSION_MAJOR -lt 11 ] ||
([ $LLVM_VERSION_MAJOR -gt 14 ] && [ $LLVM_VERSION_MAJOR -lt 17 ]); then
error "Invalid LLVM version $LLVM_VERSION: must be 11, 12, 13, 14 or 17"
fi
case "$OS_NAME" in
Darwin)
if [ "$OS_ARCH" == "arm64" ]; then
if [ $LLVM_VERSION_MAJOR -lt 13 ] || [ $LLVM_VERSION_MAJOR -gt 17 ]; then
error "Darwin Arm64 requires LLVM 13, 14 or 17"
fi fi
fi fi
MAX_LLVM_VERSION=("14.999.999") CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
if [ $(version $($LLVM_CONFIG --version)) -gt $(version $MAX_LLVM_VERSION) ]; then
echo "Tried to use " $(which $LLVM_CONFIG) "version" $($LLVM_CONFIG --version)
panic "Requirement: llvm-config must be base version smaller than 15"
fi
LDFLAGS="$LDFLAGS -liconv -ldl -framework System" LDFLAGS="$LDFLAGS -liconv -ldl -framework System"
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
LDFLAGS="$LDFLAGS -lLLVM-C" LDFLAGS="$LDFLAGS -lLLVM-C"
} ;;
FreeBSD)
config_freebsd() {
: ${LLVM_CONFIG=}
if [ ! "$LLVM_CONFIG" ]; then
if [ -x "$(command -v llvm-config11)" ]; then
LLVM_CONFIG=llvm-config11
elif [ -x "$(command -v llvm-config12)" ]; then
LLVM_CONFIG=llvm-config12
elif [ -x "$(command -v llvm-config13)" ]; then
LLVM_CONFIG=llvm-config13
else
panic "Unable to find LLVM-config"
fi
fi
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)" LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)"
} ;;
Linux)
config_openbsd() { CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
: ${LLVM_CONFIG=/usr/local/bin/llvm-config} LDFLAGS="$LDFLAGS -ldl -Wl,-rpath=$($LLVM_CONFIG --libdir)"
LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs --libfiles)"
;;
OpenBSD)
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
LDFLAGS="$LDFLAGS -liconv" LDFLAGS="$LDFLAGS -liconv"
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)" LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)"
} ;;
*)
config_linux() { error "Platform \"$OS_NAME\" unsupported"
: ${LLVM_CONFIG=} ;;
esac
if [ ! "$LLVM_CONFIG" ]; then
if [ -x "$(command -v llvm-config)" ]; then
LLVM_CONFIG=llvm-config
elif [ -x "$(command -v llvm-config-11)" ]; then
LLVM_CONFIG=llvm-config-11
elif [ -x "$(command -v llvm-config-11-64)" ]; then
LLVM_CONFIG=llvm-config-11-64
elif [ -x "$(command -v llvm-config-14)" ]; then
LLVM_CONFIG=llvm-config-14
else
panic "Unable to find LLVM-config"
fi
fi
MIN_LLVM_VERSION=("11.0.0")
if [ $(version $($LLVM_CONFIG --version)) -lt $(version $MIN_LLVM_VERSION) ]; then
echo "Tried to use " $(which $LLVM_CONFIG) "version" $($LLVM_CONFIG --version)
panic "Requirement: llvm-config must be base version greater than 11"
fi
MAX_LLVM_VERSION=("14.999.999")
if [ $(version $($LLVM_CONFIG --version)) -gt $(version $MAX_LLVM_VERSION) ]; then
echo "Tried to use " $(which $LLVM_CONFIG) "version" $($LLVM_CONFIG --version)
panic "Requirement: llvm-config must be base version smaller than 15"
fi
LDFLAGS="$LDFLAGS -ldl"
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"
LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs --libfiles) -Wl,-rpath=\$ORIGIN"
# Creates a copy of the llvm library in the build dir, this is meant to support compiler explorer.
# The annoyance is that this copy can be cluttering the development folder. TODO: split staging folders
# for development and compiler explorer builds
cp $(readlink -f $($LLVM_CONFIG --libfiles)) ./
}
build_odin() { build_odin() {
case $1 in case $1 in
@@ -138,20 +92,19 @@ build_odin() {
EXTRAFLAGS="-O3" EXTRAFLAGS="-O3"
;; ;;
release-native) release-native)
local ARCH=$(uname -m) if [ "$OS_ARCH" == "arm64" ]; then
if [ "${ARCH}" == "arm64" ]; then # Use preferred flag for Arm (ie arm64 / aarch64 / etc)
# Use preferred flag for Arm (ie arm64 / aarch64 / etc) EXTRAFLAGS="-O3 -mcpu=native"
EXTRAFLAGS="-O3 -mcpu=native" else
else # Use preferred flag for x86 / amd64
# Use preferred flag for x86 / amd64 EXTRAFLAGS="-O3 -march=native"
EXTRAFLAGS="-O3 -march=native" fi
fi
;; ;;
nightly) nightly)
EXTRAFLAGS="-DNIGHTLY -O3" EXTRAFLAGS="-DNIGHTLY -O3"
;; ;;
*) *)
panic "Build mode unsupported!" error "Build mode \"$1\" unsupported!"
;; ;;
esac esac
@@ -164,55 +117,20 @@ run_demo() {
./odin run examples/demo/demo.odin -file ./odin run examples/demo/demo.odin -file
} }
have_which() { if [ $# -eq 0 ]; then
if ! command -v which > /dev/null 2>&1 ; then
panic "Could not find \`which\`"
fi
}
have_which
case $OS in
Linux)
config_linux
;;
Darwin)
config_darwin
;;
OpenBSD)
config_openbsd
;;
FreeBSD)
config_freebsd
;;
*)
panic "Platform unsupported!"
;;
esac
if [[ $# -eq 0 ]]; then
build_odin debug build_odin debug
run_demo run_demo
exit 0 elif [ $# -eq 1 ]; then
fi
if [[ $# -eq 1 ]]; then
case $1 in case $1 in
report) report)
if [[ ! -f "./odin" ]]; then [ ! -f "./odin" ] && build_odin debug
build_odin debug
fi
./odin report ./odin report
exit 0
;; ;;
*) *)
build_odin $1 build_odin $1
;; ;;
esac esac
run_demo run_demo
exit 0
else else
panic "Too many arguments!" error "Too many arguments!"
fi fi
+23 -3
View File
@@ -2602,17 +2602,37 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
if (build_context.sanitizer_flags & SanitizerFlag_Address) { if (build_context.sanitizer_flags & SanitizerFlag_Address) {
auto paths = array_make<String>(heap_allocator(), 0, 1);
if (build_context.metrics.os == TargetOs_windows) { if (build_context.metrics.os == TargetOs_windows) {
auto paths = array_make<String>(heap_allocator(), 0, 1);
String path = concatenate_strings(permanent_allocator(), build_context.ODIN_ROOT, str_lit("\\bin\\llvm\\windows\\clang_rt.asan-x86_64.lib")); String path = concatenate_strings(permanent_allocator(), build_context.ODIN_ROOT, str_lit("\\bin\\llvm\\windows\\clang_rt.asan-x86_64.lib"));
array_add(&paths, path); array_add(&paths, path);
Entity *lib = alloc_entity_library_name(nullptr, make_token_ident("asan_lib"), nullptr, slice_from_array(paths), str_lit("asan_lib"));
array_add(&gen->foreign_libraries, lib);
} else if (build_context.metrics.os == TargetOs_darwin || build_context.metrics.os == TargetOs_linux) {
if (!build_context.extra_linker_flags.text) {
build_context.extra_linker_flags = str_lit("-fsanitize=address");
} else {
build_context.extra_linker_flags = concatenate_strings(permanent_allocator(), build_context.extra_linker_flags, str_lit(" -fsanitize=address"));
}
} }
Entity *lib = alloc_entity_library_name(nullptr, make_token_ident("asan_lib"), nullptr, slice_from_array(paths), str_lit("asan_lib"));
array_add(&gen->foreign_libraries, lib);
} }
if (build_context.sanitizer_flags & SanitizerFlag_Memory) { if (build_context.sanitizer_flags & SanitizerFlag_Memory) {
if (build_context.metrics.os == TargetOs_darwin || build_context.metrics.os == TargetOs_linux) {
if (!build_context.extra_linker_flags.text) {
build_context.extra_linker_flags = str_lit("-fsanitize=memory");
} else {
build_context.extra_linker_flags = concatenate_strings(permanent_allocator(), build_context.extra_linker_flags, str_lit(" -fsanitize=memory"));
}
}
} }
if (build_context.sanitizer_flags & SanitizerFlag_Thread) { if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
if (build_context.metrics.os == TargetOs_darwin || build_context.metrics.os == TargetOs_linux) {
if (!build_context.extra_linker_flags.text) {
build_context.extra_linker_flags = str_lit("-fsanitize=thread");
} else {
build_context.extra_linker_flags = concatenate_strings(permanent_allocator(), build_context.extra_linker_flags, str_lit(" -fsanitize=thread"));
}
}
} }
gb_sort_array(gen->foreign_libraries.data, gen->foreign_libraries.count, foreign_library_cmp); gb_sort_array(gen->foreign_libraries.data, gen->foreign_libraries.count, foreign_library_cmp);
+4
View File
@@ -15,6 +15,9 @@
#include <llvm-c/Object.h> #include <llvm-c/Object.h>
#include <llvm-c/BitWriter.h> #include <llvm-c/BitWriter.h>
#include <llvm-c/DebugInfo.h> #include <llvm-c/DebugInfo.h>
#if LLVM_VERSION_MAJOR >= 17
#include <llvm-c/Transforms/PassBuilder.h>
#else
#include <llvm-c/Transforms/AggressiveInstCombine.h> #include <llvm-c/Transforms/AggressiveInstCombine.h>
#include <llvm-c/Transforms/InstCombine.h> #include <llvm-c/Transforms/InstCombine.h>
#include <llvm-c/Transforms/IPO.h> #include <llvm-c/Transforms/IPO.h>
@@ -23,6 +26,7 @@
#include <llvm-c/Transforms/Utils.h> #include <llvm-c/Transforms/Utils.h>
#include <llvm-c/Transforms/Vectorize.h> #include <llvm-c/Transforms/Vectorize.h>
#endif #endif
#endif
#if LLVM_VERSION_MAJOR < 11 #if LLVM_VERSION_MAJOR < 11
#error "LLVM Version 11 is the minimum required" #error "LLVM Version 11 is the minimum required"
+2 -2
View File
@@ -89,8 +89,8 @@ gb_global Timings global_timings = {0};
#if LLVM_VERSION_MAJOR < 11 #if LLVM_VERSION_MAJOR < 11
#error LLVM Version 11+ is required => "brew install llvm@11" #error LLVM Version 11+ is required => "brew install llvm@11"
#endif #endif
#if LLVM_VERSION_MAJOR > 14 #if (LLVM_VERSION_MAJOR > 14 && LLVM_VERSION_MAJOR < 17) || LLVM_VERSION_MAJOR > 17
#error LLVM Version 11..=14 is required => "brew install llvm@14" #error LLVM Version 11..=14 or =17 is required => "brew install llvm@14"
#endif #endif
#endif #endif