Skip to content

Commit

Permalink
Log helper functions of all Call instructions
Browse files Browse the repository at this point in the history
Summary:
We want to log C helper function usage using a new flag `-X jit-dump-c-helper`. We will log the memory address of every `Call`, `VectorCall`, `Invoke` instruction that is processed in the `Cinder/Jit/lir/block_builder/AppendCodeLine` function. If the memory address can be resolved into a name, then the function name will be logged instead.

This change involves setting up a new logging flag ( `Cinder/Jit/log.h`, `Cinder/Jit/pyjit.cpp`) and adding logging logic to  `Cinder/Jit/lir/block_builder.cpp` .

Reviewed By: swtaarrs

Differential Revision: D28691583

fbshipit-source-id: 234bc89
  • Loading branch information
linda-zheng authored and facebook-github-bot committed May 31, 2021
1 parent 6aad3ad commit ca5d308
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Jit/lir/block_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Jit/lir/lir.h"
#include "Jit/util.h"

#include <dlfcn.h>

// XXX: this file needs to be revisited when we optimize HIR-to-LIR translation
// in codegen.cpp/h. Currently, this file is almost an identical copy from
// bbbuilder.cpp with some interfaces changes so that it works with the new
Expand Down Expand Up @@ -273,6 +275,21 @@ void BasicBlockBuilder::AppendCodeLine(const std::string& s) {
bool is_invoke = (instr_str == "Invoke");
bool is_vector_call = (instr_str == "Vectorcall");

if (g_dump_c_helper) {
size_t dest_idx = is_invoke ? 1 : 2;
if (dest_idx < tokens.size() && IsConstant(tokens[dest_idx])) {
std::string helper_id = GetId(tokens[dest_idx]);
uint64_t helper_addr = stoull(helper_id);
Dl_info helper_info;
if (dladdr(reinterpret_cast<void*>(helper_addr), &helper_info) != 0 &&
helper_info.dli_sname != NULL) {
JIT_LOG("Call to function %s.", helper_info.dli_sname);
} else {
JIT_LOG("Call to function at %s.", tokens[dest_idx]);
}
}
}

auto instr = createInstr(
is_vector_call ? Instruction::kVectorCall : Instruction::kCall);

Expand Down
1 change: 1 addition & 0 deletions Jit/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ int g_dump_hir_passes = 0;
int g_dump_final_hir = 0;
int g_dump_lir = 0;
int g_dump_lir_no_origin = 0;
int g_dump_c_helper = 0;
int g_disas_funcs = 0;
int g_dump_stats = 0;
FILE* g_log_file = stderr;
Expand Down
1 change: 1 addition & 0 deletions Jit/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern int g_dump_hir_passes;
extern int g_dump_final_hir;
extern int g_dump_lir;
extern int g_dump_lir_no_origin;
extern int g_dump_c_helper;
extern int g_disas_funcs;
extern int g_dump_stats;
extern FILE* g_log_file;
Expand Down
4 changes: 4 additions & 0 deletions Jit/pyjit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ int _PyJIT_Initialize() {
g_dump_lir = 1;
g_dump_lir_no_origin = 1;
}
if (_is_flag_set("jit-dump-c-helper", "PYTHONJITDUMPCHELPER")) {
JIT_DLOG("Enable JIT dump-c-helper mode.");
g_dump_c_helper = 1;
}
if (_is_flag_set("jit-disas-funcs", "PYTHONJITDISASFUNCS")) {
JIT_DLOG("Enabling JIT disas-funcs mode.");
g_disas_funcs = 1;
Expand Down

0 comments on commit ca5d308

Please sign in to comment.