Skip to content

Commit

Permalink
add functions to print memory pool and jit function addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-xiaoming committed Mar 23, 2023
1 parent 47c80a0 commit 6933706
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/hobbes/eval/cc.H
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ public:
// dump the contents of the generated module (useful for debugging)
void dumpModule();

// show jit functions in name:addr format
[[nodiscard]] std::string showJitMemoryAddresses() const;

// get the x86 machine code for an expression (useful for debugging)
using bytes = std::vector<unsigned char>;
bytes machineCodeForExpr(const std::string &expr);
Expand Down
2 changes: 2 additions & 0 deletions include/hobbes/eval/funcdefs.H
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ char* memalloc(size_t, size_t);
const array<char>* makeString(const std::string& x);
std::string makeStdString(const array<char>* x);

[[nodiscard]] std::string showDetailedMemoryPool();

}

#endif
2 changes: 2 additions & 0 deletions include/hobbes/eval/jitcc.H
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public:
// get the address of a bound symbol
void* getSymbolAddress(const std::string&);

[[nodiscard]] std::string showJitMemoryAddresses();

#if LLVM_VERSION_MAJOR < 11
// print all module contents
void dump() const;
Expand Down
5 changes: 5 additions & 0 deletions lib/hobbes/eval/cc.C
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@ void cc::dumpModule() {
#endif
}

std::string cc::showJitMemoryAddresses() const {
hlock _;
return this->jit->showJitMemoryAddresses();
}

cc::bytes cc::machineCodeForExpr(const std::string& expr) {
hlock _;
return this->jit->machineCodeForExpr(unsweetenExpression(readExpr(expr)));
Expand Down
12 changes: 12 additions & 0 deletions lib/hobbes/eval/funcdefs.C
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ void printMemoryPool() {
std::cout << showMemoryPool() << std::flush;
}

std::string showDetailedMemoryPool() {
std::ostringstream oss;
oss << "{";
std::string sep;
for (const auto& region: threadRegions()) {
oss << sep << "\n\"" << region.first << "\": " << region.second->show();
sep = ",";
}
oss << "\n}";
return std::move(oss).str();
}

void resetMemoryPool() {
threadRegion().clear();
}
Expand Down
16 changes: 16 additions & 0 deletions lib/hobbes/eval/jitcc.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <llvm/Support/Compiler.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>

#include <algorithm>
Expand Down Expand Up @@ -728,6 +729,21 @@ void* jitcc::getSymbolAddress(const std::string& vn) {
return 0;
}

std::string jitcc::showJitMemoryAddresses() {
std::string s;
llvm::raw_string_ostream os(s);
for (const auto* m: this->modules) {
for (const auto& f: *m) {
if (!f.isDeclaration()) {
if (void* addr = this->getSymbolAddress(f.getName().str())) {
os << f.getName() << ':' << addr << '\n';
}
}
}
}
return s;
}

void jitcc::dump() const {
for (auto m : this->modules) {
#if LLVM_VERSION_MAJOR == 6 || LLVM_VERSION_MAJOR >= 8
Expand Down

0 comments on commit 6933706

Please sign in to comment.