From af046fb46ab5a4c2e4ac196f68241520aae17a1e Mon Sep 17 00:00:00 2001 From: Elliott Slaughter Date: Fri, 21 Jun 2024 20:30:23 -0700 Subject: [PATCH] Add support for LLVM 18 (#662) --- .github/workflows/main.yml | 16 +++++++++++++--- CHANGES.md | 3 ++- cmake/Modules/FindClang.cmake | 3 +++ release/share/terra/README.md | 1 + src/llvmheaders.h | 12 ++++++++++-- src/llvmheaders_180.h | 34 ++++++++++++++++++++++++++++++++++ src/tcompiler.cpp | 32 +++++++++++++++++++++++++++++--- src/tcuda.cpp | 8 +++++++- src/tllvmutil.cpp | 14 +++++++++++++- tests/constantinits.t | 5 ----- travis.sh | 8 +++++++- 11 files changed, 119 insertions(+), 17 deletions(-) create mode 100644 src/llvmheaders_180.h diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a8f0c2ed9..85ad1bf8f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ jobs: strategy: matrix: os: ['macos-12', 'windows-2022'] - llvm: ['11', '12', '13', '14', '15', '16', '17'] + llvm: ['11', '12', '13', '14', '15', '16', '17', '18'] cuda: ['0', '1'] lua: ['luajit', 'moonjit'] exclude: @@ -28,7 +28,7 @@ jobs: - os: 'macos-12' cuda: '1' - # Windows: exclude LLVM 12-17 + # Windows: exclude LLVM 12-18 - os: 'windows-2022' llvm: '12' - os: 'windows-2022' @@ -41,6 +41,8 @@ jobs: llvm: '16' - os: 'windows-2022' llvm: '17' + - os: 'windows-2022' + llvm: '18' # CUDA: only LLVM 11 - llvm: '12' @@ -55,6 +57,8 @@ jobs: cuda: '1' - llvm: '17' cuda: '1' + - llvm: '18' + cuda: '1' # Moonjit: only LLVM 12 - llvm: '11' @@ -69,6 +73,8 @@ jobs: lua: 'moonjit' - llvm: '17' lua: 'moonjit' + - llvm: '18' + lua: 'moonjit' steps: - uses: actions/checkout@v1 - run: ./travis.sh @@ -93,7 +99,7 @@ jobs: strategy: matrix: distro: ['ubuntu-18.04'] - llvm: ['11', '12.0.1', '13.0.1', '14.0.6', '15.0.2', '16.0.3', '17.0.5'] + 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'] @@ -111,6 +117,8 @@ jobs: cuda: '1' - llvm: '17.0.5' cuda: '1' + - llvm: '18.1.7' + cuda: '1' # Moonjit with LLVM 14 only: - llvm: '11' @@ -125,6 +133,8 @@ jobs: lua: 'moonjit' - llvm: '17.0.5' lua: 'moonjit' + - llvm: '18.1.7' + lua: 'moonjit' include: # Defaults: diff --git a/CHANGES.md b/CHANGES.md index 7e70eae03..c63785ffd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,8 @@ ## Added features - * Support for LLVM 17 + * Support for LLVM 17 and 18 + * Experimental support for SPIR-V code generation (e.g., for Intel GPUs) ## Fixed Bugs diff --git a/cmake/Modules/FindClang.cmake b/cmake/Modules/FindClang.cmake index f9466f560..2e89b62bc 100644 --- a/cmake/Modules/FindClang.cmake +++ b/cmake/Modules/FindClang.cmake @@ -38,6 +38,9 @@ FIND_AND_ADD_CLANG_LIB(clangBasic) if(LLVM_VERSION_MAJOR GREATER 14) FIND_AND_ADD_CLANG_LIB(clangSupport) endif() +if(LLVM_VERSION_MAJOR GREATER 17) + FIND_AND_ADD_CLANG_LIB(clangAPINotes) +endif() find_path(CLANG_INCLUDE_DIRS clang/Basic/Version.h HINTS ${LLVM_INCLUDE_DIRS}) diff --git a/release/share/terra/README.md b/release/share/terra/README.md index 3095224d1..f73739be9 100644 --- a/release/share/terra/README.md +++ b/release/share/terra/README.md @@ -150,6 +150,7 @@ The current recommended version of LLVM is **13**. The following versions are al | 15 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: | | | 16 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: | | | 17 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: | | +| 18 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: | | \* Please note that AMD GPU support is currently experimental. diff --git a/src/llvmheaders.h b/src/llvmheaders.h index ba9ac0b39..5a52588cb 100644 --- a/src/llvmheaders.h +++ b/src/llvmheaders.h @@ -19,7 +19,11 @@ #include "llvm/IR/LegacyPassManager.h" +#if LLVM_VERSION < 170 #include "llvm/Support/Host.h" +#else +#include "llvm/TargetParser/Host.h" +#endif #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Scalar.h" @@ -32,7 +36,9 @@ #include "llvm/Support/Program.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/IPO.h" +#if LLVM_VERSION < 180 #include "llvm/Transforms/Vectorize.h" +#endif #if LLVM_VERSION < 170 #include "llvm/Transforms/IPO/PassManagerBuilder.h" #else @@ -62,11 +68,13 @@ #include "llvmheaders_160.h" #elif LLVM_VERSION < 180 #include "llvmheaders_170.h" +#elif LLVM_VERSION < 190 +#include "llvmheaders_180.h" #else #error "unsupported LLVM version" // for OSX code completion -#define LLVM_VERSION 170 -#include "llvmheaders_170.h" +#define LLVM_VERSION 180 +#include "llvmheaders_180.h" #endif #define UNIQUEIFY(T, x) (std::unique_ptr(x)) diff --git a/src/llvmheaders_180.h b/src/llvmheaders_180.h new file mode 100644 index 000000000..348eff0f6 --- /dev/null +++ b/src/llvmheaders_180.h @@ -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 diff --git a/src/tcompiler.cpp b/src/tcompiler.cpp index df93db3d7..1084845dc 100644 --- a/src/tcompiler.cpp +++ b/src/tcompiler.cpp @@ -304,7 +304,12 @@ int terra_inittarget(lua_State *L) { // GV tables CodeModel::Small, #endif - CodeGenOpt::Aggressive); +#if LLVM_VERSION < 180 + CodeGenOpt::Aggressive +#else + CodeGenOptLevel::Aggressive +#endif + ); TT->external = new Module("external", *TT->ctx); TT->external->setTargetTriple(TT->Triple); lua_pushlightuserdata(L, TT); @@ -404,7 +409,13 @@ static void InitializeJIT(TerraCompilationUnit *CU) { .setMAttrs(mattrs) .setEngineKind(EngineKind::JIT) .setTargetOptions(CU->TT->tm->Options) - .setOptLevel(CodeGenOpt::Aggressive) + .setOptLevel( +#if LLVM_VERSION < 180 + CodeGenOpt::Aggressive +#else + CodeGenOptLevel::Aggressive +#endif + ) .setMCJITMemoryManager(std::make_unique(CU)) #if LLVM_VERSION < 120 .setUseOrcMCJITReplacement(true) @@ -545,7 +556,11 @@ class Types { CreatePrimitiveType(typ, t); } break; case T_niltype: { +#if LLVM_VERSION < 170 t->type = Type::getInt8PtrTy(*CU->TT->ctx); +#else + t->type = PointerType::get(*CU->TT->ctx, 0); +#endif } break; case T_opaque: { t->type = Type::getInt8Ty(*CU->TT->ctx); @@ -586,7 +601,13 @@ class Types { } break; } } - Type *FunctionPointerType() { return Type::getInt8PtrTy(*CU->TT->ctx); } + Type *FunctionPointerType() { +#if LLVM_VERSION < 170 + return Type::getInt8PtrTy(*CU->TT->ctx); +#else + return PointerType::get(*CU->TT->ctx, 0); +#endif + } bool LookupTypeCache(Obj *typ, TType **t) { *t = (TType *)CU->symbols->getud(typ); // try to look up the cached type if (*t == NULL) { @@ -1673,7 +1694,9 @@ static CallingConv::ID ParseCallingConv(const char *cc) { init = true; ccmap["fastcc"] = CallingConv::Fast; ccmap["coldcc"] = CallingConv::Cold; +#if LLVM_VERSION < 180 ccmap["webkit_jscc"] = CallingConv::WebKit_JS; +#endif ccmap["anyregcc"] = CallingConv::AnyReg; ccmap["preserve_mostcc"] = CallingConv::PreserveMost; ccmap["preserve_allcc"] = CallingConv::PreserveAll; @@ -2005,6 +2028,9 @@ struct FunctionEmitter { cast(fstate->func->getEntryBlock().getTerminator()); Constant *r = dyn_cast(term->getReturnValue()); assert(r || !"constant expression was not constant"); +#if LLVM_VERSION >= 170 + CU->fam.clear(*fstate->func, fstate->func->getName()); +#endif fstate->func->eraseFromParent(); return r; } diff --git a/src/tcuda.cpp b/src/tcuda.cpp index 02ccdd408..bc440ffcd 100644 --- a/src/tcuda.cpp +++ b/src/tcuda.cpp @@ -151,7 +151,13 @@ void moduleToPTX(terra_State *T, llvm::Module *M, int major, int minor, std::str PMB.Inliner = llvm::createFunctionInliningPass(PMB.OptLevel, 0, false); PMB.LoopVectorize = false; #endif - auto FileType = llvm::CGFT_AssemblyFile; + auto FileType = +#if LLVM_VERSION < 180 + llvm::CGFT_AssemblyFile +#else + llvm::CodeGenFileType::AssemblyFile +#endif + ; llvm::legacy::PassManager PM; #if LLVM_VERSION < 160 diff --git a/src/tllvmutil.cpp b/src/tllvmutil.cpp index d7c9e5995..a41d709d1 100644 --- a/src/tllvmutil.cpp +++ b/src/tllvmutil.cpp @@ -330,7 +330,19 @@ bool llvmutil_emitobjfile(Module *Mod, TargetMachine *TM, bool outputobjectfile, Mod->setDataLayout(TM->createDataLayout()); #endif - CodeGenFileType ft = outputobjectfile ? CGFT_ObjectFile : CGFT_AssemblyFile; + CodeGenFileType ft = outputobjectfile ? +#if LLVM_VERSION < 180 + CGFT_ObjectFile +#else + CodeGenFileType::ObjectFile +#endif + : +#if LLVM_VERSION < 180 + CGFT_AssemblyFile +#else + CodeGenFileType::AssemblyFile +#endif + ; emitobjfile_t &destf = dest; diff --git a/tests/constantinits.t b/tests/constantinits.t index f38cb4222..c78853ca7 100644 --- a/tests/constantinits.t +++ b/tests/constantinits.t @@ -1,8 +1,3 @@ -if terralib.llvm_version >= 170 and require("ffi").os == "Linux" then - print("Skipping broken test on Linux, see #644") - return -- FIXME: https://github.com/terralang/terra/issues/644 -end - function failit(match,fn) local success,msg = xpcall(fn,debug.traceback) --print(msg) diff --git a/travis.sh b/travis.sh index 1f3704f29..5670ba3eb 100755 --- a/travis.sh +++ b/travis.sh @@ -33,7 +33,13 @@ if [[ $(uname) = Linux ]]; then exit 1 elif [[ $(uname) = Darwin ]]; then - if [[ $LLVM_VERSION = 17 ]]; then + if [[ $LLVM_VERSION = 18 ]]; then + curl -L -O https://github.com/terralang/llvm-build/releases/download/llvm-18.1.7/clang+llvm-18.1.7-x86_64-apple-darwin.tar.xz + tar xf clang+llvm-18.1.7-x86_64-apple-darwin.tar.xz + ln -s clang+llvm-18.1.7-x86_64-apple-darwin/bin/llvm-config llvm-config-17 + ln -s clang+llvm-18.1.7-x86_64-apple-darwin/bin/clang clang-17 + export CMAKE_PREFIX_PATH=$PWD/clang+llvm-18.1.7-x86_64-apple-darwin + elif [[ $LLVM_VERSION = 17 ]]; then curl -L -O https://github.com/terralang/llvm-build/releases/download/llvm-17.0.5/clang+llvm-17.0.5-x86_64-apple-darwin.tar.xz tar xf clang+llvm-17.0.5-x86_64-apple-darwin.tar.xz ln -s clang+llvm-17.0.5-x86_64-apple-darwin/bin/llvm-config llvm-config-17