Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial code to support llvm-20 #684

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ freebsd_task:
name: FreeBSD
freebsd_instance:
matrix:
image_family: freebsd-14-0
image_family: freebsd-14-1
env:
matrix:
LLVM_VERSION: 11
Expand All @@ -13,6 +13,7 @@ freebsd_task:
LLVM_VERSION: 16
LLVM_VERSION: 17
LLVM_VERSION: 18
LLVM_VERSION: 19
install_script: pkg install -y bash coreutils cmake gmake llvm$LLVM_VERSION
script: |
export CC=cc
Expand Down
57 changes: 43 additions & 14 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ jobs:
matrix:
# Note: macOS 13 runs on x86 hardware, and 14 runs on M1 hardware
os: ['macos-13', 'macos-14', 'windows-2022']
llvm: ['11', '12', '13', '14', '15', '16', '17', '18']
llvm: ['11', '12', '13', '14', '15', '16', '17', '18', '19']
cuda: ['0', '1']
lua: ['luajit', 'moonjit']
slib: ['0', '1']
exclude:
# macOS: exclude cuda
- os: 'macos-13'
Expand All @@ -47,7 +48,7 @@ jobs:
- os: 'macos-14'
llvm: '15'

# Windows: exclude LLVM 12-18
# Windows: exclude LLVM 12-19
- os: 'windows-2022'
llvm: '12'
- os: 'windows-2022'
Expand All @@ -62,6 +63,8 @@ jobs:
llvm: '17'
- os: 'windows-2022'
llvm: '18'
- os: 'windows-2022'
llvm: '19'

# CUDA: only LLVM 11
- llvm: '12'
Expand All @@ -78,6 +81,8 @@ jobs:
cuda: '1'
- llvm: '18'
cuda: '1'
- llvm: '19'
cuda: '1'

# Moonjit: only LLVM 12
- llvm: '11'
Expand All @@ -94,6 +99,28 @@ jobs:
lua: 'moonjit'
- llvm: '18'
lua: 'moonjit'
- llvm: '19'
lua: 'moonjit'

# LLVM 19 is incompatible with SLIB_INCLUDE_LLVM
- llvm: '11'
slib: '0'
- llvm: '12'
slib: '0'
- llvm: '13'
slib: '0'
- llvm: '14'
slib: '0'
- llvm: '15'
slib: '0'
- llvm: '16'
slib: '0'
- llvm: '17'
slib: '0'
- llvm: '18'
slib: '0'
- llvm: '19'
slib: '1'
steps:
- uses: actions/checkout@v4
- run: ./travis.sh
Expand All @@ -103,7 +130,7 @@ jobs:
USE_CUDA: ${{ matrix.cuda }}
STATIC_LLVM: 1
STATIC_LUAJIT: 1
SLIB_INCLUDE_LLVM: 1
SLIB_INCLUDE_LLVM: ${{ matrix.slib }}
SLIB_INCLUDE_LUAJIT: 1
TERRA_LUA: ${{ matrix.lua }}
- uses: actions/upload-artifact@v4
Expand All @@ -121,7 +148,6 @@ jobs:
llvm: ['11', '12.0.1', '13.0.1', '14.0.6', '15.0.2', '16.0.3', '17.0.5', '18.1.7']
lua: ['luajit', 'moonjit']
cuda: ['0', '1']
test: ['1']
exclude:
# CUDA with LLVM 13 only:
- llvm: '11'
Expand Down Expand Up @@ -167,6 +193,15 @@ jobs:

# Some additional configurations:

# LLVM 19
- distro: 'ubuntu-20.04'
llvm: '19.1.7'
lua: 'luajit'
cuda: '0'
variant: 'prebuilt'
slib: '0'
static: '1'

# LLVM 12, 13, 14 on Ubuntu 22.04
- distro: 'ubuntu-22.04'
llvm: '12'
Expand All @@ -175,23 +210,20 @@ jobs:
variant: 'package'
slib: '1'
static: '1'
test: '1'
- distro: 'ubuntu-22.04'
llvm: '13'
lua: 'luajit'
cuda: '0'
variant: 'package'
slib: '1'
static: '1'
test: '1'
- distro: 'ubuntu-22.04'
llvm: '14'
lua: 'luajit'
cuda: '0'
variant: 'package'
slib: '1'
static: '1'
test: '1'

# LLVM 14, no-slib/no-static, Ubuntu 22.04
- distro: 'ubuntu-22.04'
Expand All @@ -201,15 +233,13 @@ jobs:
variant: 'package'
slib: '0'
static: '1'
test: '1'
# - distro: 'ubuntu-22.04'
# llvm: '14'
# lua: 'luajit'
# cuda: '0'
# variant: 'package'
# slib: '0'
# static: '0'
# test: '1'
steps:
- uses: actions/checkout@v4
- run: ./travis.sh
Expand All @@ -221,7 +251,7 @@ jobs:
DOCKER_SLIB: ${{ matrix.slib }}
DOCKER_CUDA: ${{ matrix.cuda }}
DOCKER_VARIANT: ${{ matrix.variant }}
DOCKER_TEST: ${{ matrix.test }}
DOCKER_TEST: 1
multiarch:
name: Multiarch (${{ matrix.distro }}, ${{ matrix.arch }}, llvm-${{ matrix.llvm }}, cuda=${{ matrix.cuda }}, ${{ matrix.variant }})
runs-on: ubuntu-latest
Expand All @@ -232,7 +262,6 @@ jobs:
llvm: ['11.1.0', '13.0.0']
variant: ['prebuilt']
cuda: ['0']
test: ['0']
exclude:
- arch: 'arm64'
llvm: '13.0.0'
Expand All @@ -247,7 +276,7 @@ jobs:
DOCKER_LLVM: ${{ matrix.llvm }}
DOCKER_CUDA: ${{ matrix.cuda }}
DOCKER_VARIANT: ${{ matrix.variant }}
DOCKER_TEST: ${{ matrix.test }}
DOCKER_TEST: 0
- uses: actions/upload-artifact@v4
with:
name: docker-${{ matrix.distro }}-${{ matrix.arch }}-llvm-${{ matrix.llvm }}
Expand All @@ -259,10 +288,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./docker/compatibility_test.sh ubuntu 18.04 "18.04 20.04 22.04 24.04" "" 18.1.7 luajit prebuilt 2
- run: ./docker/compatibility_test.sh ubuntu 20.04 "20.04 22.04 24.04" "" 19.1.7 luajit prebuilt 2
- uses: actions/upload-artifact@v4
with:
name: docker-ubuntu-18.04-x86_64-llvm-18
name: docker-ubuntu-20.04-x86_64-llvm-19
path: |
terra-*.tar.xz
terra-*.7z
Expand Down
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set(TERRA_ENABLE_CUDA ${CUDA_FOUND} CACHE BOOL "Build Terra with support for CUD
if(DEFINED TERRA_STATIC_LINK_LLVM)
set(DEFAULT_TERRA_SLIB_INCLUDE_LLVM ${TERRA_STATIC_LINK_LLVM})
else()
if(WIN32)
if(WIN32 OR LLVM_VERSION_MAJOR GREATER 18)
set(DEFAULT_TERRA_SLIB_INCLUDE_LLVM OFF)
else()
set(DEFAULT_TERRA_SLIB_INCLUDE_LLVM ON)
Expand All @@ -26,6 +26,13 @@ endif()
set(TERRA_SLIB_INCLUDE_LLVM ${DEFAULT_TERRA_SLIB_INCLUDE_LLVM} CACHE BOOL "Include LLVM in Terra static libraries")
set(TERRA_STATIC_LINK_LLVM ON CACHE BOOL "Statically link Terra against LLVM")

if(LLVM_VERSION_MAJOR GREATER 18)
set(DEFAULT_TERRA_WHOLE_ARCHIVE_LLVM OFF)
else()
set(DEFAULT_TERRA_WHOLE_ARCHIVE_LLVM ON)
endif()
set(TERRA_WHOLE_ARCHIVE_LLVM ${DEFAULT_TERRA_WHOLE_ARCHIVE_LLVM} CACHE BOOL "When statically linking LLVM, force inclusion of all objects")

if(TERRA_SLIB_INCLUDE_LLVM AND NOT TERRA_STATIC_LINK_LLVM)
message(FATAL_ERROR "TERRA_SLIB_INCLUDE_LLVM requires TERRA_STATIC_LINK_LLVM to be set")
endif()
Expand Down
6 changes: 3 additions & 3 deletions cmake/Modules/ExtractLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ elseif(TERRA_STATIC_LINK_LLVM)
endif()
endforeach()

if(UNIX AND NOT APPLE)
if(UNIX AND NOT APPLE AND TERRA_WHOLE_ARCHIVE_LLVM)
list(APPEND ALL_LLVM_LIBRARIES
-Wl,-export-dynamic
-Wl,--whole-archive
)
endif()

foreach(LLVM_LIB_PATH ${LLVM_LIBRARIES} ${CLANG_LIBRARIES})
if(APPLE)
if(APPLE AND TERRA_WHOLE_ARCHIVE_LLVM)
list(APPEND ALL_LLVM_LIBRARIES "-Wl,-force_load,${LLVM_LIB_PATH}")
else()
list(APPEND ALL_LLVM_LIBRARIES "${LLVM_LIB_PATH}")
endif()
endforeach()

if(UNIX AND NOT APPLE)
if(UNIX AND NOT APPLE AND TERRA_WHOLE_ARCHIVE_LLVM)
list(APPEND ALL_LLVM_LIBRARIES
-Wl,--no-whole-archive
)
Expand Down
8 changes: 6 additions & 2 deletions src/llvmheaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@
#include "llvmheaders_170.h"
#elif LLVM_VERSION < 190
#include "llvmheaders_180.h"
#elif LLVM_VERSION < 200
#include "llvmheaders_190.h"
#elif LLVM_VERSION < 210
#include "llvmheaders_200.h"
#else
#error "unsupported LLVM version"
// for OSX code completion
#define LLVM_VERSION 180
#include "llvmheaders_180.h"
#define LLVM_VERSION 200
#include "llvmheaders_200.h"
#endif

#define UNIQUEIFY(T, x) (std::unique_ptr<T>(x))
Expand Down
34 changes: 34 additions & 0 deletions src/llvmheaders_190.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Mangler.h"
//#include "llvm/ExecutionEngine/ObjectImage.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Linker/Linker.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"

#include "llvm/Support/VirtualFileSystem.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Object/SymbolSize.h"

#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Support/Error.h"

#define LLVM_PATH_TYPE std::string
#define RAW_FD_OSTREAM_NONE sys::fs::OF_None
#define RAW_FD_OSTREAM_BINARY sys::fs::OF_None
34 changes: 34 additions & 0 deletions src/llvmheaders_200.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Mangler.h"
//#include "llvm/ExecutionEngine/ObjectImage.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Linker/Linker.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"

#include "llvm/Support/VirtualFileSystem.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Object/SymbolSize.h"

#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Support/Error.h"

#define LLVM_PATH_TYPE std::string
#define RAW_FD_OSTREAM_NONE sys::fs::OF_None
#define RAW_FD_OSTREAM_BINARY sys::fs::OF_None
18 changes: 18 additions & 0 deletions src/tcompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,22 @@ bool OneTimeInit(struct terra_State *T) {

// LLVM 3.1 doesn't enable avx even if it is present, we detect and force it here
bool HostHasAVX() {
#if LLVM_VERSION < 190
StringMap<bool> Features;
sys::getHostCPUFeatures(Features);
#else
StringMap<bool> Features = sys::getHostCPUFeatures();
#endif
return Features["avx"];
}

bool HostHasAVX512() {
#if LLVM_VERSION < 190
StringMap<bool> Features;
sys::getHostCPUFeatures(Features);
#else
StringMap<bool> Features = sys::getHostCPUFeatures();
#endif
// The following instructions sets are supported by Intel CPUs starting 2017
// (Skylake-SP and beyond) and AMD CPUs starting 2022 (Zen4 and beyond)
const char *instruction_set[] = {
Expand Down Expand Up @@ -3358,7 +3366,12 @@ struct FunctionEmitter {
ValueToValueMapTy VMap;
DebugInfoFinder DIFinder;
BasicBlock *NewBB =
#if LLVM_VERSION < 20
CloneBasicBlock(BB, VMap, "defer", fstate->func, nullptr, &DIFinder);
#else
// TODO: Check if DIFinder needs to be used in a different function.
CloneBasicBlock(BB, VMap, "defer", fstate->func, nullptr);
#endif
VMap[BB] = NewBB;

BasicBlock::iterator oldII = BB->begin();
Expand Down Expand Up @@ -3740,8 +3753,13 @@ static void *JITGlobalValue(TerraCompilationUnit *CU, GlobalValue *gv) {
ExecutionEngine *ee = CU->ee;
if (gv->isDeclaration()) {
StringRef name = gv->getName();
#if LLVM_VERSION < 180
if (name.startswith("\01")) // remove asm renaming tag before looking for symbol
name = name.substr(1);
#else
if (name.starts_with("\01")) // remove asm renaming tag before looking for symbol
name = name.substr(1);
#endif
return ee->getPointerToNamedFunction(name);
}
void *ptr = GetGlobalValueAddress(CU, gv->getName());
Expand Down
4 changes: 4 additions & 0 deletions src/tcwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,11 @@ static void AddMacro(terra_State *T, Preprocessor &PP, const IdentifierInfo *II,
double V;
if (Literal.isFloatingLiteral()) {
llvm::APFloat Result(0.0);
#if LLVM_VERSION < 190
Literal.GetFloatValue(Result);
#else
Literal.GetFloatValue(Result, llvm::RoundingMode::NearestTiesToEven);
#endif
V = Result.convertToDouble();
} else {
llvm::APInt Result(64, 0);
Expand Down
Loading
Loading