Skip to content

Commit

Permalink
Extract recording (#1245)
Browse files Browse the repository at this point in the history
The type feedback is no longer in the byte code instruction, instead it is at a function, currently only at the baseline.
  • Loading branch information
fikovnik authored Nov 23, 2023
1 parent a3739d3 commit 0f8ef7e
Show file tree
Hide file tree
Showing 38 changed files with 926 additions and 429 deletions.
6 changes: 6 additions & 0 deletions .gdbinit
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ define ds
dumpsxp $arg0 1
end

define ninja
shell ninja
python gdb.execute("file " + gdb.current_progspace().filename)
directory
end

# source .pirpp.py


Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ benchmarks/
*.DS_Store
external/*
!external/custom-r
.history
.history
.cache
compile_commands.json
4 changes: 4 additions & 0 deletions documentation/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ complete.
PIR_WARMUP=
number: after how many invocations a function is (re-) optimized

PIR_OPT_BC_SIZE=
number: controls the maximum byte code size of a callee that could
trigger OSR in the caller (cf. https://github.com/reactorlabs/rir/issues/1252#issuecomment-1775517529)

#### Extended debug flags

RIR_CHECK_PIR_TYPES=
Expand Down
12 changes: 5 additions & 7 deletions rir/src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "api.h"
#include "R/Serialize.h"
#include "Rinternals.h"
#include "bc/BC.h"
#include "bc/Compiler.h"
#include "compiler/backend.h"
Expand All @@ -15,6 +16,7 @@
#include "compiler/test/PirCheck.h"
#include "compiler/test/PirTests.h"
#include "interpreter/interp_incl.h"
#include "runtime/DispatchTable.h"
#include "utils/measuring.h"

#include <cassert>
Expand Down Expand Up @@ -56,13 +58,9 @@ REXPORT SEXP rirDisassemble(SEXP what, SEXP verbose) {
if (!t)
Rf_error("Not a rir compiled code (CLOSXP but not DispatchTable)");

std::cout << "== closure " << what << " (dispatch table " << t << ", env "
<< CLOENV(what) << ") ==\n";
for (size_t entry = 0; entry < t->size(); ++entry) {
Function* f = t->get(entry);
std::cout << "= version " << entry << " (" << f << ") =\n";
f->disassemble(std::cout);
}
std::cout << "== closure " << what << " (env " << CLOENV(what) << ") ==\n";

t->print(std::cout, Rf_asLogical(verbose));

return R_NilValue;
}
Expand Down
75 changes: 8 additions & 67 deletions rir/src/bc/BC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ void BC::write(CodeStream& cs) const {
cs.insert(immediate.cacheIdx);
return;

case Opcode::record_call_:
// Call feedback targets are stored in the code extra pool. We don't
// have access to them here, so we can't write a call feedback with
// preseeded values.
assert(immediate.callFeedback.numTargets == 0 &&
"cannot write call feedback targets");
cs.insert(immediate.callFeedback);
return;

case Opcode::record_test_:
cs.insert(immediate.testFeedback);
break;

case Opcode::record_type_:
cs.insert(immediate.typeFeedback);
break;

case Opcode::push_:
case Opcode::ldfun_:
case Opcode::ldddvar_:
Expand Down Expand Up @@ -96,6 +79,9 @@ void BC::write(CodeStream& cs) const {
case Opcode::pull_:
case Opcode::is_:
case Opcode::put_:
case Opcode::record_call_:
case Opcode::record_test_:
case Opcode::record_type_:
cs.insert(immediate.i);
return;

Expand Down Expand Up @@ -331,9 +317,7 @@ void BC::printOpcode(std::ostream& out) const { out << name(bc) << " "; }

void BC::print(std::ostream& out) const {
out << " ";
if (bc != Opcode::record_call_ && bc != Opcode::record_type_ &&
bc != Opcode::record_test_)
printOpcode(out);
printOpcode(out);

switch (bc) {
case Opcode::invalid_:
Expand Down Expand Up @@ -402,54 +386,11 @@ void BC::print(std::ostream& out) const {
case Opcode::is_:
out << (BC::RirTypecheck)immediate.i;
break;
case Opcode::record_call_: {
ObservedCallees prof = immediate.callFeedback;
out << "[ ";
if (prof.taken == ObservedCallees::CounterOverflow)
out << "*, <";
else
out << prof.taken << ", <";
if (prof.numTargets == ObservedCallees::MaxTargets)
out << "*>, ";
else
out << prof.numTargets << ">, ";

out << (prof.invalid ? "invalid" : "valid");
out << (prof.numTargets ? ", " : " ");

for (int i = 0; i < prof.numTargets; ++i)
out << callFeedbackExtra().targets[i] << "("
<< Rf_type2char(TYPEOF(callFeedbackExtra().targets[i])) << ") ";
out << "]";
break;
}

case Opcode::record_test_: {
out << "[ ";
switch (immediate.testFeedback.seen) {
case ObservedTest::None:
out << "_";
break;
case ObservedTest::OnlyTrue:
out << "T";
break;
case ObservedTest::OnlyFalse:
out << "F";
break;
case ObservedTest::Both:
out << "?";
break;
}
out << " ]";
break;
}

case Opcode::record_type_: {
out << "[ ";
immediate.typeFeedback.print(out);
out << " ]";
case Opcode::record_test_:
case Opcode::record_type_:
case Opcode::record_call_:
out << "#" << immediate.i;
break;
}

#define V(NESTED, name, name_) case Opcode::name_##_:
BC_NOARGS(V, _)
Expand Down
18 changes: 15 additions & 3 deletions rir/src/bc/BC.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ class CodeStream;
BC_NOARGS(V, _)
#undef V

BC BC::recordCall() { return BC(Opcode::record_call_); }
BC BC::recordCall(uint32_t idx) {
ImmediateArguments i;
i.i = idx;
return BC(Opcode::record_call_, i);
}

BC BC::recordType() { return BC(Opcode::record_type_); }
BC BC::recordType(uint32_t idx) {
ImmediateArguments i;
i.i = idx;
return BC(Opcode::record_type_, i);
}

BC BC::recordTest() { return BC(Opcode::record_test_); }
BC BC::recordTest(uint32_t idx) {
ImmediateArguments i;
i.i = idx;
return BC(Opcode::record_test_, i);
}

BC BC::asSwitchIdx() { return BC(Opcode::as_switch_idx_); }

Expand Down
43 changes: 9 additions & 34 deletions rir/src/bc/BC_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ class BC {
uint32_t i;
RirTypecheck typecheck;
NumLocals loc;
ObservedCallees callFeedback;
ObservedValues typeFeedback;
ObservedTest testFeedback;
PoolAndCachePositionRange poolAndCache;
CachePositionRange cacheIdx;
ImmediateArguments() {
Expand Down Expand Up @@ -267,6 +264,11 @@ class BC {

bool isJmp() const { return isCondJmp() || isUncondJmp(); }

bool isRecord() const {
return bc == Opcode::record_call_ || bc == Opcode::record_test_ ||
bc == Opcode::record_type_;
}

bool isExit() const { return bc == Opcode::ret_ || bc == Opcode::return_; }

// This code performs the same as `BC::decode(pc).size()`, but for
Expand Down Expand Up @@ -309,10 +311,10 @@ class BC {
#define V(NESTED, name, name_) inline static BC name();
BC_NOARGS(V, _)
#undef V
inline static BC recordCall();
inline static BC recordCall(uint32_t idx);
inline static BC recordBinop();
inline static BC recordType();
inline static BC recordTest();
inline static BC recordType(uint32_t idx);
inline static BC recordTest(uint32_t idx);
inline static BC asSwitchIdx();
inline static BC popn(unsigned n);
inline static BC push(SEXP constant);
Expand Down Expand Up @@ -406,14 +408,6 @@ class BC {
extraInformation.get());
}

CallFeedbackExtraInformation& callFeedbackExtra() const {
assert(bc == Opcode::record_call_ && "not a record call instruction");
assert(extraInformation.get() &&
"missing extra information. created through decodeShallow?");
return *static_cast<CallFeedbackExtraInformation*>(
extraInformation.get());
}

private:
void allocExtraInformation() {
assert(extraInformation == nullptr);
Expand All @@ -428,10 +422,6 @@ class BC {
extraInformation.reset(new CallInstructionExtraInformation);
break;
}
case Opcode::record_call_: {
extraInformation.reset(new CallFeedbackExtraInformation);
break;
}
default: {
}
}
Expand All @@ -455,13 +445,6 @@ class BC {
break;
}

case Opcode::record_call_: {
// Read call target feedback from the extra pool
for (size_t i = 0; i < immediate.callFeedback.numTargets; ++i)
callFeedbackExtra().targets.push_back(
immediate.callFeedback.getTarget(code, i));
break;
}
default: {
}
}
Expand Down Expand Up @@ -580,18 +563,10 @@ class BC {
case Opcode::pull_:
case Opcode::is_:
case Opcode::put_:
memcpy(&immediate.i, pc, sizeof(immediate.i));
break;
case Opcode::record_call_:
memcpy(&immediate.callFeedback, pc, sizeof(ObservedCallees));
break;
case Opcode::record_test_:
memcpy(reinterpret_cast<void*>(&immediate.testFeedback), pc,
sizeof(ObservedValues));
break;
case Opcode::record_type_:
memcpy(reinterpret_cast<void*>(&immediate.typeFeedback), pc,
sizeof(ObservedValues));
memcpy(&immediate.i, pc, sizeof(immediate.i));
break;
#define V(NESTED, name, name_) case Opcode::name_##_:
BC_NOARGS(V, _)
Expand Down
Loading

0 comments on commit 0f8ef7e

Please sign in to comment.