Skip to content

Commit

Permalink
enable logging IR via env variable
Browse files Browse the repository at this point in the history
  HOBBES_IR_TRACE_FILE: file that IR will be stored
  • Loading branch information
mo-xiaoming committed Mar 19, 2023
1 parent dc4ec73 commit b93b46b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions include/hobbes/eval/jitcc.H
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <hobbes/util/llvm.H>
#include <hobbes/eval/func.H>
#include <hobbes/eval/ctype.H>
#include <memory>

#if LLVM_VERSION_MAJOR >= 11
#include <llvm/IR/ValueHandle.h>
Expand Down Expand Up @@ -260,6 +261,9 @@ private:
#if LLVM_VERSION_MAJOR >= 11
std::unique_ptr<ORCJIT> orcjit;
#endif

struct IrTracer;
std::unique_ptr<IrTracer> irTracer;
};

// shorthand for compilation over a sequence of expressions
Expand Down
48 changes: 45 additions & 3 deletions lib/hobbes/eval/jitcc.C
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <cstdint>
#include <hobbes/eval/func.H>
#include <hobbes/util/llvm.H>
#include <hobbes/eval/cexpr.H>
Expand All @@ -12,10 +13,17 @@
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/ValueHandle.h>
#include <llvm/IR/ValueSymbolTable.h>
#include <llvm/Support/Casting.h>
#include <llvm/Support/Compiler.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/raw_os_ostream.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>

#include <fstream>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <utility>

Expand Down Expand Up @@ -546,11 +554,43 @@ private:
};
#endif

struct jitcc::IrTracer {
IrTracer() {
std::error_code ec;
const char* name = std::getenv("HOBBES_IR_TRACE_FILE");
if (name == nullptr) {
return;
}
out = std::make_unique<llvm::raw_fd_ostream>(name, ec);
if (ec != std::errc()) {
out = nullptr;
return;
}
}

void log(const llvm::Module& m) {
if (!isEnabled()) {
return;
}

m.print(*out, nullptr, false, true);
}

private:
[[nodiscard]] bool isEnabled() const noexcept {
return !!out;
}

std::unique_ptr<llvm::raw_fd_ostream> out;
};

#if LLVM_VERSION_MAJOR >= 11
jitcc::jitcc(const TEnvPtr& tenv)
: tenv(tenv), vtenv(std::make_unique<VTEnv>()), ignoreLocalScope(false),
globals(std::make_unique<Globals>()), globalData(32768 /* min global page size = 32K */),
constants(std::make_unique<ConstantList>()) {
constants(std::make_unique<ConstantList>()),
irTracer(std::make_unique<IrTracer>())
{
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmParser();
llvm::InitializeNativeTargetAsmPrinter();
Expand Down Expand Up @@ -579,7 +619,8 @@ jitcc::~jitcc() {
jitcc::jitcc(const TEnvPtr& tenv) :
tenv(tenv),
ignoreLocalScope(false),
globalData(32768 /* min global page size = 32K */)
globalData(32768 /* min global page size = 32K */),
irTracer(std::make_unique<IrTracer>())
{
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmParser();
Expand Down Expand Up @@ -743,6 +784,7 @@ void* jitcc::getMachineCode(llvm::Function* f, llvm::JITEventListener* listener)
if (!this->currentModule) {
throw std::runtime_error("Internal compiler error, can't derive machine code for unknown function");
}
irTracer->log(*this->currentModule);

// make a new execution engine out of this module (finalizing the module)
std::string err;
Expand Down Expand Up @@ -792,7 +834,7 @@ void* jitcc::getMachineCode(llvm::Function* f, llvm::JITEventListener* listener)
ee->finalizeObject();

// now we can't touch this module again
this->currentModule = 0;
this->currentModule = nullptr;

// and _now_ we must be able to get machine code for this function
void* pf = ee->getPointerToFunction(f);
Expand Down

0 comments on commit b93b46b

Please sign in to comment.