Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ To debug leaks on OS X:

leaks --atExit -- ./name_of_the_test

To show llvm IR for runtime:

llvm-dis runtime_uber.bc -o runtime_uber.ll


## Running

Expand Down
3 changes: 2 additions & 1 deletion backend-v2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ set(CMAKE_CXX_FLAGS_RELEASE "-Ofast")
set(CMAKE_CXX_STANDARD 20)
add_compile_definitions(BUILD_TYPE="${CMAKE_BUILD_TYPE}")

add_subdirectory(runtime)

# --- Dependency Discovery ---

Expand Down Expand Up @@ -138,6 +137,8 @@ set(BACKEND_SOURCES
codegen/ops/LocalNode.cpp
codegen/ops/WithMetaNode.cpp)

add_subdirectory(runtime)


# 2. Protobuf Detection
find_package(Protobuf CONFIG QUIET)
Expand Down
29 changes: 16 additions & 13 deletions backend-v2/codegen/CodeGen.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "CodeGen.h"
#include "CleanupChainGuard.h"
#include "../bridge/Exceptions.h"
#include "../cljassert.h"
#include "CleanupChainGuard.h"
#include "TypedValue.h"
#include "runtime/Object.h"
#include "runtime/RTValue.h"
#include "runtime/String.h"
#include "runtime/Object.h"

using namespace llvm;
using namespace clojure::rt::protobuf::bytecode;
Expand Down Expand Up @@ -67,7 +67,8 @@ std::string CodeGen::codegenTopLevel(const Node &node) {

// Declare personality function
FunctionType *personalityFnTy = FunctionType::get(types.i32Ty, true);
personalityFn = TheModule->getOrInsertFunction("__gxx_personality_v0", personalityFnTy);
personalityFn =
TheModule->getOrInsertFunction("__gxx_personality_v0", personalityFnTy);
F->setPersonalityFn(cast<Function>(personalityFn.getCallee()));

// Set initial debug location
Expand All @@ -83,10 +84,11 @@ std::string CodeGen::codegenTopLevel(const Node &node) {
}

TypedValue CodeGen::codegen(const Node &node,
const ObjectTypeSet &typeRestrictions) {
const ObjectTypeSet &typeRestrictions) {
CLJ_ASSERT(TSContext != nullptr, "Codegen was moved");

MemoryManagement::UnwindGuidanceGuard guidanceGuard(memoryManagement, &node.unwindmemory());
MemoryManagement::UnwindGuidanceGuard guidanceGuard(memoryManagement,
&node.unwindmemory());

auto env = node.env();
if (!LexicalBlocks.empty()) {
Expand Down Expand Up @@ -137,8 +139,8 @@ TypedValue CodeGen::codegen(const Node &node,
return codegen(node, node.subnode().if_(), typeRestrictions);
// case opImport:
// return codegen(node, node.subnode().import(), typeRestrictions);
// case opInstanceCall:
// return codegen(node, node.subnode().instancecall(), typeRestrictions);
//case opInstanceCall:
// return codegen(node, node.subnode().instancecall(), typeRestrictions);
// case opInstanceField:
// return codegen(node, node.subnode().instancefield(), typeRestrictions);
// case opIsInstance:
Expand Down Expand Up @@ -237,8 +239,8 @@ ObjectTypeSet CodeGen::getType(const Node &node,
return getType(node, node.subnode().if_(), typeRestrictions);
// case opImport:
// return getType(node, node.subnode().import(), typeRestrictions);
// case opInstanceCall:
// return getType(node, node.subnode().instancecall(), typeRestrictions);
//case opInstanceCall:
// return getType(node, node.subnode().instancecall(), typeRestrictions);
// case opInstanceField:
// return getType(node, node.subnode().instancefield(), typeRestrictions);
// case opIsInstance:
Expand Down Expand Up @@ -299,7 +301,8 @@ ObjectTypeSet CodeGen::getType(const Node &node,
Var *CodeGen::getOrCreateVar(std::string_view name) {
return compilerState.varRegistry.getOrCreate(
std::string(name).c_str(), [name]() {
RTValue keyword = Keyword_create(String_createDynamicStr(std::string(name).c_str()));
RTValue keyword =
Keyword_create(String_createDynamicStr(std::string(name).c_str()));
return Var_create(keyword);
});
}
Expand All @@ -314,11 +317,12 @@ bool CodeGen::canThrow(const Node &node) {
case opFn:
case opFnMethod:
return false;

case opLet: {
const auto &l = node.subnode().let();
for (int i = 0; i < l.bindings_size(); ++i) {
if (canThrow(l.bindings(i))) return true;
if (canThrow(l.bindings(i)))
return true;
}
return canThrow(l.body());
}
Expand Down Expand Up @@ -394,5 +398,4 @@ bool CodeGen::canThrow(const Node &node) {
}
}


} // namespace rt
17 changes: 13 additions & 4 deletions backend-v2/codegen/CodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CodeGen {
std::unique_ptr<llvm::DIBuilder> DIB;
llvm::DICompileUnit *CU;
std::vector<llvm::DIScope *> LexicalBlocks;

// Static Landing Pads management via manager
llvm::FunctionCallee personalityFn;

Expand Down Expand Up @@ -122,6 +122,8 @@ class CodeGen {
const ObjectTypeSet &typeRestrictions);
TypedValue codegen(const Node &node, const LocalNode &subnode,
const ObjectTypeSet &typeRestrictions);
TypedValue codegen(const Node &node, const InstanceCallNode &subnode,
const ObjectTypeSet &typeRestrictions);

ObjectTypeSet getType(const Node &node,
const ObjectTypeSet &typeRestrictions);
Expand Down Expand Up @@ -154,15 +156,22 @@ class CodeGen {
const ObjectTypeSet &typeRestrictions);
ObjectTypeSet getType(const Node &node, const LocalNode &subnode,
const ObjectTypeSet &typeRestrictions);
ObjectTypeSet getType(const Node &node, const InstanceCallNode &subnode,
const ObjectTypeSet &typeRestrictions);

Var *getOrCreateVar(std::string_view name);
bool canThrow(const clojure::rt::protobuf::bytecode::Node &node);

// Exception safety helpers
void pushResource(TypedValue val) { memoryManagement.pushResource(val); }
void popResource() { memoryManagement.popResource(); }
llvm::BasicBlock* getLandingPad(size_t skipCount = 0) { return memoryManagement.getLandingPad(skipCount); }
llvm::BasicBlock *getLandingPad(size_t skipCount = 0) {
return memoryManagement.getLandingPad(skipCount);
}
bool hasLandingPad() const { return memoryManagement.hasPushedResources(); }
bool hasPushedResources() const { return memoryManagement.hasPushedResources(); }
bool hasPushedResources() const {
return memoryManagement.hasPushedResources();
}
MemoryManagement &getMemoryManagement() { return memoryManagement; }
};

Expand Down
4 changes: 2 additions & 2 deletions backend-v2/codegen/MemoryManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ MemoryManagement::MemoryManagement(llvm::LLVMContext &c, IRBuilder<> &b,
LLVMTypes &t,
VariableBindings<TypedValue> &vb,
InvokeManager &i)
: context(c), builder(b), theModule(m), valueEncoder(v), types(t),
: context(c), builder(b), valueEncoder(v), types(t),
variableBindingStack(vb), invoke(i) {}

void MemoryManagement::initFunction(llvm::Function *F) {
Expand Down Expand Up @@ -183,7 +183,7 @@ void MemoryManagement::dynamicMemoryGuidance(
auto name = guidance.variablename();
auto change = guidance.requiredrefcountchange();

for (word_t depth = variableBindingStack.stackDepth() - 1; depth >= 0;
for (word_t depth = (word_t)variableBindingStack.stackDepth() - 1; depth >= 0;
depth--) {
auto val = variableBindingStack.find(name, depth);
if (val) {
Expand Down
2 changes: 1 addition & 1 deletion backend-v2/codegen/MemoryManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class MemoryManagement {
private:
llvm::LLVMContext &context;
llvm::IRBuilder<> &builder;
llvm::Module &theModule;

ValueEncoder &valueEncoder;
LLVMTypes &types;
VariableBindings<TypedValue> &variableBindingStack;
Expand Down
2 changes: 1 addition & 1 deletion backend-v2/codegen/invoke/InvokeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ TypedValue InvokeManager::generateIntrinsic(const IntrinsicDescription &id,
if (id.type == CallType::Intrinsic) {
auto git = genericIntrinsics.find(id.symbol);
if (git != genericIntrinsics.end()) {
return TypedValue(id.returnType, git->second(builder, args));
return TypedValue(id.returnType, git->second(builder, args, guard));
}

auto it = intrinsics.find(id.symbol);
Expand Down
2 changes: 1 addition & 1 deletion backend-v2/codegen/invoke/InvokeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using IntrinsicCall = std::function<llvm::Value *(llvm::IRBuilder<> &,
using TypeIntrinsicCall =
std::function<ObjectTypeSet(const std::vector<ObjectTypeSet> &)>;
using GenericIntrinsicCall = std::function<llvm::Value *(
llvm::IRBuilder<> &, const std::vector<TypedValue> &)>;
llvm::IRBuilder<> &, const std::vector<TypedValue> &, CleanupChainGuard *)>;

class InvokeManager {
friend void registerMathIntrinsics(InvokeManager &mgr);
Expand Down
Loading
Loading