Skip to content

Commit

Permalink
Drop LLVM <15 (#874)
Browse files Browse the repository at this point in the history
Having dropped support for Ubuntu Focal in K, we no longer need to
support versions of LLVM older than 15; this PR makes the required
changes to the backend to drop support for those older versions. I will
follow up with a K PR to update the corresponding documentation.

Fixes #868
  • Loading branch information
Baltoli authored Oct 31, 2023
1 parent 480b542 commit 44a675f
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 151 deletions.
9 changes: 5 additions & 4 deletions INSTALL.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Dependencies

## Ubuntu 20.04:
## Ubuntu 22.04:

```shell
sudo apt update
sudo apt install \
clang-12 \
clang-15 \
cmake \
curl \
flex \
Expand All @@ -17,9 +17,10 @@ sudo apt install \
libjemalloc-dev \
libmpfr-dev \
libyaml-dev \
lld-12 \
llvm-12-tools \
lld-15 \
llvm-15-tools \
maven \
openjdk-17-jdk \
pkg-config \
python3 \
python3-pip \
Expand Down
16 changes: 2 additions & 14 deletions cmake/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@ if (NOT LLVM_FOUND)
find_package(LLVM 15 QUIET CONFIG)
endif()

if (NOT LLVM_FOUND)
find_package(LLVM 14 QUIET CONFIG)
endif()

if (NOT LLVM_FOUND)
find_package(LLVM 13 QUIET CONFIG)
endif()

if (NOT LLVM_FOUND)
find_package(LLVM 12 QUIET CONFIG)
endif()

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
if (${LLVM_PACKAGE_VERSION} VERSION_LESS 12)
message(FATAL_ERROR "LLVM 12 or newer is required")
if (${LLVM_PACKAGE_VERSION} VERSION_LESS 15)
message(FATAL_ERROR "LLVM 15 or newer is required")
endif()

find_program(OPT opt
Expand Down
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let
# Avoid spurious rebuilds by ignoring files that don't affect the build.
mavenix = import sources."mavenix" { inherit (prev) pkgs; };
in {
llvm-version = 13;
llvm-version = 15;
llvm-backend-build-type = cmakeBuildType;
inherit (mavenix) buildMaven;
mavenix-cli = mavenix.cli;
Expand Down
16 changes: 8 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
builtins.listToAttrs (lib.imap0 (i: v: { name = "check_${toString i}"; value = v; }) checks);

matrix = builtins.listToAttrs (lib.forEach (lib.cartesianProductOfSets {
llvm-version = [12 13 14 15 16];
llvm-version = [15 16];
build-type = ["Debug" "Release" "RelWithDebInfo" "FastBuild" "GcStats"];
}) (
args:
Expand All @@ -116,15 +116,15 @@
llvm-backend-release = llvm-backend-16-Release.llvm-backend;
};
checks = listToChecks [
# Check that the backend compiles on each supported version of LLVM,
# but don't run the test suite on all possible configurations.
llvm-backend-12-FastBuild.llvm-backend
llvm-backend-13-FastBuild.llvm-backend
llvm-backend-14-FastBuild.llvm-backend
llvm-backend-15-FastBuild.llvm-backend
llvm-backend-16-Debug.llvm-backend
llvm-backend-16-Release.llvm-backend
llvm-backend-16-RelWithDebInfo.llvm-backend
llvm-backend-16-GcStats.llvm-backend

llvm-backend-15-FastBuild.integration-tests
llvm-backend-16-FastBuild.integration-tests
];
devShells.default = llvm-backend-15-FastBuild.devShell;
devShells.default = llvm-backend-16-FastBuild.devShell;
}) // {
# non-system suffixed items should go here
overlays.default = llvm-backend-overlay;
Expand Down
25 changes: 10 additions & 15 deletions include/kllvm/codegen/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@ namespace kllvm {
// one does not yet exist
llvm::Function *koreHeapAlloc(std::string name, llvm::Module *module);

// If Value is an instance of llvm::Function, cast and return. Otherwise, print
// errors and abort.
llvm::Function *castToFunctionOrAbort(llvm::Value *value);

// getOrInsertFunction on module, aborting on failure
template <class... Ts>
static llvm::Function *getOrInsertFunction(llvm::Module *module, Ts... Args) {
llvm::Value *callee;
auto ret = module->getOrInsertFunction(Args...);
#if LLVM_VERSION_MAJOR >= 9
callee = ret.getCallee();
#else
callee = ret;
#endif
return castToFunctionOrAbort(callee);
}
llvm::Function *getOrInsertFunction(llvm::Module *module, Ts... Args) {
auto callee = module->getOrInsertFunction(Args...).getCallee();
auto func = llvm::dyn_cast<llvm::Function>(callee);

llvm::StructType *getTypeByName(llvm::Module *module, std::string name);
if (!func) {
func->print(llvm::errs());
abort();
}

return func;
}

} // namespace kllvm

Expand Down
18 changes: 2 additions & 16 deletions lib/codegen/ApplyPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

#include "runtime/header.h"

#if LLVM_VERSION_MAJOR >= 14
#include <llvm/MC/TargetRegistry.h>
#else
#include <llvm/Support/TargetRegistry.h>
#endif

#include <llvm/IR/LegacyPassManager.h>

#if LLVM_VERSION_MAJOR >= 17
#include <llvm/TargetParser/Host.h>
#include <llvm/TargetParser/SubtargetFeature.h>
Expand All @@ -18,6 +10,8 @@
#include <llvm/Support/Host.h>
#endif

#include <llvm/IR/LegacyPassManager.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Pass.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Target/TargetMachine.h>
Expand Down Expand Up @@ -68,19 +62,11 @@ void apply_kllvm_opt_passes(llvm::Module &mod) {
}

void generate_object_file(llvm::Module &mod, llvm::raw_ostream &os) {
// The frame-pointer retention code in LLVM 12 and older is tied strongly to
// the actual command-line flag used to specify it for code generation, rather
// than being decoupled as it is in 13 and newer. Because LLVM 12 will be
// deprecated for our purposes sooner rather than later, and is not the
// default version for packaged versions of K, we simply disable the FP
// feature.
#if LLVM_VERSION_MAJOR > 12
if (KeepFramePointer) {
mod.setFramePointer(FramePointerKind::All);
} else {
mod.setFramePointer(FramePointerKind::None);
}
#endif

auto triple = BACKEND_TARGET_TRIPLE;
mod.setTargetTriple(triple);
Expand Down
56 changes: 37 additions & 19 deletions lib/codegen/CreateStaticTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ llvm::Constant *CreateStaticTerm::notInjectionCase(
if (!globalVar->hasInitializer()) {
std::vector<llvm::Constant *> blockVals;

llvm::StructType *BlockHeaderType
= getTypeByName(Module, BLOCKHEADER_STRUCT);
llvm::StructType *BlockHeaderType = llvm::StructType::getTypeByName(
Module->getContext(), BLOCKHEADER_STRUCT);
uint64_t headerVal
= getBlockHeaderVal(Module, symbol, BlockType) | NOT_YOUNG_OBJECT_BIT;
llvm::Constant *BlockHeader = llvm::ConstantStruct::get(
Expand Down Expand Up @@ -79,7 +79,8 @@ llvm::Constant *CreateStaticTerm::notInjectionCase(
= {llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), 0)};
return llvm::ConstantExpr::getBitCast(
llvm::ConstantExpr::getInBoundsGetElementPtr(BlockType, globalVar, Idxs),
llvm::PointerType::getUnqual(getTypeByName(Module, BLOCK_STRUCT)));
llvm::PointerType::getUnqual(
llvm::StructType::getTypeByName(Module->getContext(), BLOCK_STRUCT)));
}

std::pair<llvm::Constant *, bool>
Expand All @@ -97,7 +98,8 @@ CreateStaticTerm::operator()(KOREPattern *pattern) {
false);
}
if (symbol->getArguments().empty()) {
llvm::StructType *BlockType = getTypeByName(Module, BLOCK_STRUCT);
llvm::StructType *BlockType
= llvm::StructType::getTypeByName(Module->getContext(), BLOCK_STRUCT);
llvm::Constant *Cast = llvm::ConstantExpr::getIntToPtr(
llvm::ConstantInt::get(
llvm::Type::getInt64Ty(Ctx),
Expand Down Expand Up @@ -144,7 +146,8 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
assert(false && "cannot create tokens of collection category");
case SortCategory::Int: {
llvm::Constant *global = Module->getOrInsertGlobal(
"int_" + contents, getTypeByName(Module, INT_WRAPPER_STRUCT));
"int_" + contents, llvm::StructType::getTypeByName(
Module->getContext(), INT_WRAPPER_STRUCT));
llvm::GlobalVariable *globalVar
= llvm::dyn_cast<llvm::GlobalVariable>(global);
if (!globalVar->hasInitializer()) {
Expand All @@ -168,7 +171,8 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
limbsVar->setInitializer(
llvm::ConstantArray::get(limbsType, allocdLimbs));
llvm::Constant *hdr = llvm::ConstantStruct::get(
getTypeByName(Module, BLOCKHEADER_STRUCT),
llvm::StructType::getTypeByName(
Module->getContext(), BLOCKHEADER_STRUCT),
llvm::ConstantInt::get(
llvm::Type::getInt64Ty(Ctx),
sizeof(mpz_hdr) - sizeof(blockheader) | NOT_YOUNG_OBJECT_BIT));
Expand All @@ -178,9 +182,12 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
numLimbs,
llvm::ConstantInt::getSigned(llvm::Type::getInt32Ty(Ctx), sign));
globalVar->setInitializer(llvm::ConstantStruct::get(
getTypeByName(Module, INT_WRAPPER_STRUCT), hdr,
llvm::StructType::getTypeByName(
Module->getContext(), INT_WRAPPER_STRUCT),
hdr,
llvm::ConstantStruct::get(
getTypeByName(Module, INT_STRUCT), numLimbs, mp_size,
llvm::StructType::getTypeByName(Module->getContext(), INT_STRUCT),
numLimbs, mp_size,
llvm::ConstantExpr::getPointerCast(
limbsVar, llvm::Type::getInt64PtrTy(Ctx)))));
mpz_clear(value);
Expand All @@ -189,11 +196,14 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
= {llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), 0),
llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1)};
return llvm::ConstantExpr::getInBoundsGetElementPtr(
getTypeByName(Module, INT_WRAPPER_STRUCT), globalVar, Idxs);
llvm::StructType::getTypeByName(
Module->getContext(), INT_WRAPPER_STRUCT),
globalVar, Idxs);
}
case SortCategory::Float: {
llvm::Constant *global = Module->getOrInsertGlobal(
"float_" + contents, getTypeByName(Module, FLOAT_WRAPPER_STRUCT));
"float_" + contents, llvm::StructType::getTypeByName(
Module->getContext(), FLOAT_WRAPPER_STRUCT));
llvm::GlobalVariable *globalVar
= llvm::dyn_cast<llvm::GlobalVariable>(global);
if (!globalVar->hasInitializer()) {
Expand Down Expand Up @@ -245,7 +255,8 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
limbsVar->setInitializer(
llvm::ConstantArray::get(limbsType, allocdLimbs));
llvm::Constant *hdr = llvm::ConstantStruct::get(
getTypeByName(Module, BLOCKHEADER_STRUCT),
llvm::StructType::getTypeByName(
Module->getContext(), BLOCKHEADER_STRUCT),
llvm::ConstantInt::get(
llvm::Type::getInt64Ty(Ctx),
(sizeof(floating_hdr) - sizeof(blockheader))
Expand All @@ -265,9 +276,13 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
// assuming that the host and target have the same arch, but since we
// don't yet support cross compiling anyway, that's a safe assumption.
globalVar->setInitializer(llvm::ConstantStruct::get(
getTypeByName(Module, FLOAT_WRAPPER_STRUCT), hdr,
llvm::StructType::getTypeByName(
Module->getContext(), FLOAT_WRAPPER_STRUCT),
hdr,
llvm::ConstantStruct::get(
getTypeByName(Module, FLOAT_STRUCT), expbits,
llvm::StructType::getTypeByName(
Module->getContext(), FLOAT_STRUCT),
expbits,
llvm::ConstantStruct::getAnon(
{mpfr_prec, mpfr_sign, mpfr_exp,
llvm::ConstantExpr::getPointerCast(
Expand All @@ -278,7 +293,9 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
= {llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), 0),
llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1)};
return llvm::ConstantExpr::getInBoundsGetElementPtr(
getTypeByName(Module, FLOAT_WRAPPER_STRUCT), globalVar, Idxs);
llvm::StructType::getTypeByName(
Module->getContext(), FLOAT_WRAPPER_STRUCT),
globalVar, Idxs);
}
case SortCategory::StringBuffer:
assert(false && "not implemented yet: tokens");
Expand All @@ -296,15 +313,16 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
case SortCategory::Symbol: {
llvm::StructType *StringType = llvm::StructType::get(
Ctx,
{getTypeByName(Module, BLOCKHEADER_STRUCT),
{llvm::StructType::getTypeByName(
Module->getContext(), BLOCKHEADER_STRUCT),
llvm::ArrayType::get(llvm::Type::getInt8Ty(Ctx), contents.size())});
llvm::Constant *global
= Module->getOrInsertGlobal("token_" + escape(contents), StringType);
llvm::GlobalVariable *globalVar
= llvm::dyn_cast<llvm::GlobalVariable>(global);
if (!globalVar->hasInitializer()) {
llvm::StructType *BlockHeaderType
= getTypeByName(Module, BLOCKHEADER_STRUCT);
llvm::StructType *BlockHeaderType = llvm::StructType::getTypeByName(
Module->getContext(), BLOCKHEADER_STRUCT);
// this object does not live on the young generation, so we need to set
// the correct gc bit.
llvm::Constant *BlockHeader = llvm::ConstantStruct::get(
Expand All @@ -316,8 +334,8 @@ CreateStaticTerm::createToken(ValueType sort, std::string contents) {
llvm::ConstantDataArray::getString(Ctx, contents, false)));
}
return llvm::ConstantExpr::getPointerCast(
global,
llvm::PointerType::getUnqual(getTypeByName(Module, BLOCK_STRUCT)));
global, llvm::PointerType::getUnqual(llvm::StructType::getTypeByName(
Module->getContext(), BLOCK_STRUCT)));
}
case SortCategory::Uncomputed: abort();
}
Expand Down
Loading

0 comments on commit 44a675f

Please sign in to comment.