From 644cf11c89b8ceff0c2c74f080ff742d51530fb6 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sun, 27 Feb 2022 19:10:30 +0000 Subject: [PATCH] Finish ep17 and address trivial TODOs in the context --- docs/videos.org | 31 +++++++++++++++++++++++++++- libserene/include/serene/context.h | 11 +++++----- libserene/include/serene/namespace.h | 2 +- libserene/lib/context.cpp | 1 + libserene/lib/namespace.cpp | 6 +++++- 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/docs/videos.org b/docs/videos.org index aeb20f9..6840d55 100644 --- a/docs/videos.org +++ b/docs/videos.org @@ -806,7 +806,8 @@ A thread safe container for the LLVM module. - [[https://www.youtube.com/watch?v=i-inxFudrgI][ORCv2 -- LLVM JIT APIs Deep Dive]] - [[https://www.youtube.com/watch?v=MOQG5vkh9J8][Updating ORC JIT for Concurrency]] - [[https://www.youtube.com/watch?v=hILdR8XRvdQ][ORC -- LLVM's Next Generation of JIT API]] -* Eposide 16 - ORC Layers +* DONE Eposide 16 - ORC Layers +CLOSED: [2022-02-26 Sat 12:50] ** Updates: *** Support for adding AST directly to the JIT *** Minor change to SLIR (big changes are coming) @@ -923,3 +924,31 @@ A thread safe container for the LLVM module. #+RESULTS: ep-16-jit-3 [[file:/tmp/ep16-3.svg]] + +* Episode 17 - Custom ORC Layers +** Updates: +- Finished the basic compiler wiring +- Restructured the source tree +- Tweaked the build system mostly for install targets +- Refactoring, cleaning up the code and writing tests +** Quick overview an ORC based JIT engine +- JIT engines are made out of layers +- Engines have a hierarchy of layers +- Layers don't know about each other +- Layers wrap the program representation in a =MaterializationUnit=, which is + then stored in the =JITDylib=. +- =MaterializationUnits= are responsible for describing the definitions they provide, + and for unwrapping the program representation and passing it back to the layer when + compilation is required. +- When a =MaterializationUnit= hands a program representation back to the layer it comes + with an associated =MaterializationResponsibility= object. This object tracks the + definitions that must be materialized and provides a way to notify the =JITDylib= once + they are either successfully materialized or a failure occurs. + +** In order to build a custom layer we need: +*** A custom materialization unit +Let's have a look at the =MaterializationUnit= class. + +*** And the layer class itself +The layer classes are not special but conventionally the come with few functions +like: =add=, =emit= and =getInterface=. diff --git a/libserene/include/serene/context.h b/libserene/include/serene/context.h index bb6ec6d..7d4b6c8 100644 --- a/libserene/include/serene/context.h +++ b/libserene/include/serene/context.h @@ -249,20 +249,19 @@ public: private: CompilationPhase targetPhase; - // TODO: Change it to a LLVM::StringMap // TODO: We need to keep different instances of the namespace // because if any one of them gets cleaned up via reference // count (if we are still using shared ptr for namespaces if not // remove this todo) then we will end up with dangling references // it the JIT - // The namespace table. Every namespace that needs to be compiled has - // to register itself with the context and appear on this table. - // This table acts as a cache as well. + /// The namespace table. Every namespace that needs to be compiled has + /// to register itself with the context and appear on this table. + /// This table acts as a cache as well. std::map namespaces; - // Why string vs pointer? We might rewrite the namespace and - // holding a pointer means that it might point to the old version + /// Why string vs pointer? We might rewrite the namespace and + /// holding a pointer means that it might point to the old version std::string currentNS; /// A vector of pointers to all the jitDylibs for namespaces. Usually diff --git a/libserene/include/serene/namespace.h b/libserene/include/serene/namespace.h index 19be767..6116812 100644 --- a/libserene/include/serene/namespace.h +++ b/libserene/include/serene/namespace.h @@ -20,7 +20,7 @@ * Commentary: * Rules of a namespace: * - A namespace has have a name and it has to own it. - * - A namespace may or may not be assiciated with a file + * - A namespace may or may not be associated with a file * - The internal AST of a namespace is an evergrowing tree which may expand at * any given time. For example via iteration of a REPL * - `environments` vector is the owner of all the semantic envs diff --git a/libserene/lib/context.cpp b/libserene/lib/context.cpp index 771ffd9..955c919 100644 --- a/libserene/lib/context.cpp +++ b/libserene/lib/context.cpp @@ -137,6 +137,7 @@ MaybeNS SereneContext::importNamespace(const std::string &name, } llvm::orc::JITDylib *SereneContext::getLatestJITDylib(Namespace &ns) { + if (jitDylibs.count(ns.name) == 0) { return nullptr; } diff --git a/libserene/lib/namespace.cpp b/libserene/lib/namespace.cpp index 03aa744..8c60263 100644 --- a/libserene/lib/namespace.cpp +++ b/libserene/lib/namespace.cpp @@ -207,6 +207,10 @@ NSPtr Namespace::make(SereneContext &ctx, llvm::StringRef name, return std::make_shared(ctx, name, filename); }; -Namespace::~Namespace(){}; +Namespace::~Namespace() { + // TODO: Clean up anything related to this namespace in the context + // TODO: Remove anything related to this namespace in the JIT + NAMESPACE_LOG("Destructing NS: " << name); +}; } // namespace serene