Move over the SereneContext and a base JIT implementanio
ci/woodpecker/push/build Pipeline was successful Details
ci/woodpecker/push/docs Pipeline was successful Details
ci/woodpecker/manual/build Pipeline was successful Details
ci/woodpecker/manual/docs Pipeline was successful Details

This commit is contained in:
Sameer Rahmani 2023-07-21 23:23:16 +01:00
parent 839125eb18
commit 7a4e76fe08
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
10 changed files with 481 additions and 7 deletions

View File

@ -38,6 +38,7 @@ endif()
target_include_directories(serene
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_include_directories(serene SYSTEM PUBLIC

View File

@ -18,4 +18,6 @@ target_sources(serene PRIVATE
serene.cpp
commands/commands.cpp
jit/jit.cpp
context.cpp
)

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "serene/commands.h"
#include <commands/commands.h>
#include <stdio.h>

51
serene/src/context.cpp Normal file
View File

@ -0,0 +1,51 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <context.h>
#include <cstdlib> // for exit
namespace serene {
int SereneContext::getOptimizatioLevel() {
if (targetPhase <= CompilationPhase::NoOptimization) {
return 0;
}
if (targetPhase == CompilationPhase::O1) {
return 1;
}
if (targetPhase == CompilationPhase::O2) {
return 2;
}
return 3;
}
void terminate(SereneContext &ctx, int exitCode) {
(void)ctx;
// TODO: Since we are running in a single thread for now using exit is fine
// but we need to adjust and change it to a thread safe termination
// process later on.
// NOLINTNEXTLINE(concurrency-mt-unsafe)
std::exit(exitCode);
}
std::unique_ptr<SereneContext> makeSereneContext(Options opts) {
return std::make_unique<SereneContext>(opts);
};
}; // namespace serene

104
serene/src/context.h Normal file
View File

@ -0,0 +1,104 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CONTEXT_H
#define CONTEXT_H
#include "options.h" // for Options
#include <__fwd/string.h> // for string
#include <__memory/unique_ptr.h> // for make_unique, unique_ptr
#include <llvm/ADT/Twine.h> // for Twine
#include <llvm/IR/LLVMContext.h> // for LLVMContext
#include <llvm/TargetParser/Host.h> // for getDefaultTargetTriple
#include <llvm/TargetParser/Triple.h> // for Triple
#include <string> // for basic_string
#include <vector> // for vector
namespace serene {
class SereneContext;
/// This enum describes the different operational phases for the compiler
/// in order. Anything below `NoOptimization` is considered only for debugging
enum class CompilationPhase {
Parse,
Analysis,
SLIR,
MLIR, // Lowered slir to other dialects
LIR, // Lowered to the llvm ir dialect
IR, // Lowered to the LLVMIR itself
NoOptimization,
O1,
O2,
O3,
};
/// Terminates the serene compiler process in a thread safe manner
/// This function is only meant to be used in the compiler context
/// if you want to terminate the process in context of a serene program
/// via the JIT use an appropriate function in the `serene.core` ns.
void terminate(SereneContext &ctx, int exitCode);
// Why SereneContext and not Context? We will be using LLVMContext
// and MLIRContext through out Serene, so it's better to follow
// the same convention
class SereneContext {
public:
/// The set of options to change the compilers behaviors
Options options;
const llvm::Triple triple;
explicit SereneContext(Options &options)
: options(options), triple(llvm::sys::getDefaultTargetTriple()),
targetPhase(CompilationPhase::NoOptimization){};
/// Set the target compilation phase of the compiler. The compilation
/// phase dictates the behavior and the output type of the compiler.
void setOperationPhase(CompilationPhase phase);
CompilationPhase getTargetPhase() { return targetPhase; };
int getOptimizatioLevel();
static std::unique_ptr<llvm::LLVMContext> genLLVMContext() {
return std::make_unique<llvm::LLVMContext>();
};
/// Setup the load path for namespace lookups
void setLoadPaths(std::vector<std::string> &dirs) { loadPaths.swap(dirs); };
/// Return the load paths for namespaces
std::vector<std::string> &getLoadPaths() { return loadPaths; };
private:
CompilationPhase targetPhase;
std::vector<std::string> loadPaths;
};
/// Creates a new context object. Contexts are used through out the compilation
/// process to store the state.
///
/// \p opts is an instance of \c Options that can be used to set options of
/// of the compiler.
std::unique_ptr<SereneContext> makeSereneContext(Options opts = Options());
} // namespace serene
#endif

127
serene/src/jit/jit.cpp Normal file
View File

@ -0,0 +1,127 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jit/jit.h"
#include "options.h" // for Options
#include <system_error> // for error_code
#include <llvm/ADT/StringMapEntry.h> // for StringMapEntry
#include <llvm/ADT/iterator.h> // for iterator_facade_base
#include <llvm/ExecutionEngine/JITEventListener.h> // for JITEventListener
#include <llvm/ExecutionEngine/Orc/LLJIT.h> // IWYU pragma: keep
#include <llvm/IR/Module.h> // for Module
#include <llvm/Support/FileSystem.h> // for OpenFlags
#include <llvm/Support/ToolOutputFile.h> // for ToolOutputFile
#include <assert.h> // for assert
#include <string> // for operator+, char_t...
namespace serene::jit {
// ----------------------------------------------------------------------------
// ObjectCache Implementation
// ----------------------------------------------------------------------------
void ObjectCache::notifyObjectCompiled(const llvm::Module *m,
llvm::MemoryBufferRef objBuffer) {
cachedObjects[m->getModuleIdentifier()] =
llvm::MemoryBuffer::getMemBufferCopy(objBuffer.getBuffer(),
objBuffer.getBufferIdentifier());
}
std::unique_ptr<llvm::MemoryBuffer>
ObjectCache::getObject(const llvm::Module *m) {
auto i = cachedObjects.find(m->getModuleIdentifier());
if (i == cachedObjects.end()) {
HALLEY_LOG("No object for " + m->getModuleIdentifier() +
" in cache. Compiling.");
return nullptr;
}
HALLEY_LOG("Object for " + m->getModuleIdentifier() + " loaded from cache.");
return llvm::MemoryBuffer::getMemBuffer(i->second->getMemBufferRef());
}
void ObjectCache::dumpToObjectFile(llvm::StringRef outputFilename) {
// Set up the output file.
std::error_code error;
auto file = std::make_unique<llvm::ToolOutputFile>(outputFilename, error,
llvm::sys::fs::OF_None);
if (error) {
llvm::errs() << "cannot open output file '" + outputFilename.str() +
"': " + error.message()
<< "\n";
return;
}
// Dump the object generated for a single module to the output file.
// TODO: Replace this with a runtime check
assert(cachedObjects.size() == 1 && "Expected only one object entry.");
auto &cachedObject = cachedObjects.begin()->second;
file->os() << cachedObject->getBuffer();
file->keep();
}
// ----------------------------------------------------------------------------
// JIT Implementation
// ----------------------------------------------------------------------------
orc::JITDylib *JIT::getLatestJITDylib(const llvm::StringRef &nsName) {
if (jitDylibs.count(nsName) == 0) {
return nullptr;
}
auto vec = jitDylibs[nsName];
// TODO: Make sure that the returning Dylib still exists in the JIT
// by calling jit->engine->getJITDylibByName(dylib_name);
return vec.empty() ? nullptr : vec.back();
};
void JIT::pushJITDylib(const llvm::StringRef &nsName, llvm::orc::JITDylib *l) {
if (jitDylibs.count(nsName) == 0) {
llvm::SmallVector<llvm::orc::JITDylib *, 1> vec{l};
jitDylibs[nsName] = vec;
return;
}
auto vec = jitDylibs[nsName];
vec.push_back(l);
jitDylibs[nsName] = vec;
}
size_t JIT::getNumberOfJITDylibs(const llvm::StringRef &nsName) {
if (jitDylibs.count(nsName) == 0) {
return 0;
}
return jitDylibs[nsName].size();
};
JIT::JIT(llvm::orc::JITTargetMachineBuilder &&jtmb, Options &opts)
: isLazy(opts.JITLazy),
cache(opts.JITenableObjectCache ? new ObjectCache() : nullptr),
gdbListener(opts.JITenableGDBNotificationListener
? llvm::JITEventListener::createGDBRegistrationListener()
: nullptr),
perfListener(opts.JITenablePerfNotificationListener
? llvm::JITEventListener::createPerfJITEventListener()
: nullptr),
jtmb(jtmb){};
} // namespace serene::jit

144
serene/src/jit/jit.h Normal file
View File

@ -0,0 +1,144 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Commentary:
- It operates in lazy (for REPL) and non-lazy mode and wraps LLJIT
and LLLazyJIT
- It uses an object cache layer to cache module (not NSs) objects.
*/
#ifndef JIT_JIT_H
#define JIT_JIT_H
#include <__memory/unique_ptr.h>
#include <llvm/ADT/ArrayRef.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringMap.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/ExecutionEngine/ObjectCache.h>
#include <llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h>
#include <llvm/Support/Debug.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/MemoryBufferRef.h>
#include <llvm/Support/raw_ostream.h>
#include <optional>
#include <stddef.h>
#include <variant>
namespace llvm {
class JITEventListener;
} // namespace llvm
namespace llvm {
class Module;
} // namespace llvm
namespace llvm::orc {
class JITDylib;
class LLJIT;
class LLLazyJIT;
} // namespace llvm::orc
namespace serene {
struct Options;
} // namespace serene
#define HALLEY_LOG(...) \
DEBUG_WITH_TYPE("JIT", llvm::dbgs() << "[JIT]: " << __VA_ARGS__ << "\n");
namespace orc = llvm::orc;
namespace serene::jit {
class JIT;
using JITPtr = std::unique_ptr<JIT>;
using MaybeJIT = llvm::Expected<JITPtr>;
using JitWrappedAddress = void (*)(void **);
using MaybeJitAddress = llvm::Expected<JitWrappedAddress>;
/// A simple object cache following Lang's LLJITWithObjectCache example and
/// MLIR's SimpelObjectCache.
class ObjectCache : public llvm::ObjectCache {
public:
/// Cache the given `objBuffer` for the given module `m`. The buffer contains
/// the combiled objects of the module
void notifyObjectCompiled(const llvm::Module *m,
llvm::MemoryBufferRef objBuffer) override;
// Lookup the cache for the given module `m` or returen a nullptr.
std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module *m) override;
/// Dump cached object to output file `filename`.
void dumpToObjectFile(llvm::StringRef filename);
private:
llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> cachedObjects;
};
class JIT {
const bool isLazy;
std::variant<std::unique_ptr<orc::LLJIT>, std::unique_ptr<orc::LLLazyJIT>>
engine;
std::unique_ptr<ObjectCache> cache;
llvm::JITEventListener *gdbListener;
/// Perf notification listener.
llvm::JITEventListener *perfListener;
llvm::orc::JITTargetMachineBuilder jtmb;
// We keep the jibDylibs for each name space in a mapping from the ns
// name to a vector of jitdylibs, the last element is always the newest
// jitDylib
//
// Questions:
// Is using string as the key good enough, what about an ID for NSs
// or even a pointer to the ns?
llvm::StringMap<llvm::SmallVector<llvm::orc::JITDylib *, 1>> jitDylibs;
void pushJITDylib(const llvm::StringRef &nsName, llvm::orc::JITDylib *l);
size_t getNumberOfJITDylibs(const llvm::StringRef &nsName);
public:
JIT(llvm::orc::JITTargetMachineBuilder &&jtmb, Options &opts);
static MaybeJIT make(llvm::orc::JITTargetMachineBuilder &&jtmb);
/// Return a pointer to the most registered JITDylib of the given \p ns
////name
llvm::orc::JITDylib *getLatestJITDylib(const llvm::StringRef &nsName);
/// Looks up a packed-argument function with the given sym name and returns a
/// pointer to it. Propagates errors in case of failure.
MaybeJitAddress lookup(const llvm::StringRef &nsName,
const llvm::StringRef &sym) const;
/// Invokes the function with the given name passing it the list of opaque
/// pointers containing the actual arguments.
llvm::Error
invokePacked(const llvm::StringRef &symbolName,
llvm::MutableArrayRef<void *> args = std::nullopt) const;
llvm::Error loadModule(const llvm::StringRef &nsName,
const llvm::StringRef &file);
void dumpToObjectFile(const llvm::StringRef &filename);
};
MaybeJIT makeJIT();
} // namespace serene::jit
#endif

40
serene/src/options.h Normal file
View File

@ -0,0 +1,40 @@
/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPTIONS_H
#define OPTIONS_H
namespace serene {
/// Options describes the compiler options that can be passed to the
/// compiler via command line. Anything that user should be able to
/// tweak about the compiler has to end up here regardless of the
/// different subsystem that might use it.
struct Options {
/// Whether to use colors for the output or not
bool withColors = true;
// JIT related flags
bool JITenableObjectCache = true;
bool JITenableGDBNotificationListener = true;
bool JITenablePerfNotificationListener = true;
bool JITLazy = false;
};
} // namespace serene
#endif

View File

@ -16,14 +16,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "serene/commands.h"
#include "serene/config.h"
#include "serene/config.h" // for SERENE_VERSION
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/FormatVariadic.h>
#include "commands/commands.h" // for cc, run
#include <__fwd/string.h> // for string
#include <cstring>
#include <string>
#include <llvm/ADT/StringRef.h> // for StringRef
#include <llvm/Support/CommandLine.h> // for SubCommand, ParseCom...
#include <llvm/Support/FormatVariadic.h> // for formatv, formatv_object
#include <llvm/Support/FormatVariadicDetails.h> // for provider_format_adapter
#include <cstring> // for strcmp
#include <string> // for basic_string
#include <tuple> // for tuple
namespace cl = llvm::cl;