Skip to content

Commit

Permalink
Add support for LLVM 18 (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter authored Jun 22, 2024
1 parent f673dd8 commit af046fb
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 17 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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:
# macOS: exclude cuda
- os: 'macos-12'
cuda: '1'

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

# CUDA: only LLVM 11
- llvm: '12'
Expand All @@ -55,6 +57,8 @@ jobs:
cuda: '1'
- llvm: '17'
cuda: '1'
- llvm: '18'
cuda: '1'

# Moonjit: only LLVM 12
- llvm: '11'
Expand All @@ -69,6 +73,8 @@ jobs:
lua: 'moonjit'
- llvm: '17'
lua: 'moonjit'
- llvm: '18'
lua: 'moonjit'
steps:
- uses: actions/checkout@v1
- run: ./travis.sh
Expand All @@ -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']
Expand All @@ -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'
Expand All @@ -125,6 +133,8 @@ jobs:
lua: 'moonjit'
- llvm: '17.0.5'
lua: 'moonjit'
- llvm: '18.1.7'
lua: 'moonjit'

include:
# Defaults:
Expand Down
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions cmake/Modules/FindClang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
1 change: 1 addition & 0 deletions release/share/terra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 10 additions & 2 deletions src/llvmheaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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<T>(x))
Expand Down
34 changes: 34 additions & 0 deletions src/llvmheaders_180.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
32 changes: 29 additions & 3 deletions src/tcompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<TerraSectionMemoryManager>(CU))
#if LLVM_VERSION < 120
.setUseOrcMCJITReplacement(true)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2005,6 +2028,9 @@ struct FunctionEmitter {
cast<ReturnInst>(fstate->func->getEntryBlock().getTerminator());
Constant *r = dyn_cast<Constant>(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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/tcuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/tllvmutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 0 additions & 5 deletions tests/constantinits.t
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 7 additions & 1 deletion travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af046fb

Please sign in to comment.