Skip to content

Commit

Permalink
bintr: Use arena pointer directly with libtcc
Browse files Browse the repository at this point in the history
Measurably increases memory performance with libtcc
  • Loading branch information
fwsGonzo committed Jun 16, 2024
1 parent aad93e5 commit 0139c0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
38 changes: 33 additions & 5 deletions lib/libriscv/tr_emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,34 @@ struct Emitter
bool gpr_exists_at(int reg) const noexcept { return this->gpr_exists.at(reg); }
auto& get_gpr_exists() const noexcept { return this->gpr_exists; }

std::string arena_at(const std::string& address) {
if constexpr (libtcc_enabled) {
if (cpu.machine().memory.uses_32bit_encompassing_arena()) {
return "(*(char*)(" + std::to_string(tinfo.arena_ptr) + " + (uint32_t)(" + address + ")))";
} else {
return "(*(char*)(" + std::to_string(tinfo.arena_ptr) + " + " + speculation_safe(address) + "))";
}
} else if (cpu.machine().memory.uses_32bit_encompassing_arena()) {
return "ARENA_AT(cpu, (uint32_t)(" + address + "))";
} else {
return "ARENA_AT(cpu, " + speculation_safe(address) + ")";
}
}

std::string arena_at_fixed(address_t address) {
if constexpr (libtcc_enabled) {
if (cpu.machine().memory.uses_32bit_encompassing_arena()) {
return "(*(char*)" + std::to_string(tinfo.arena_ptr + uint32_t(address)) + "ul)";
} else {
return "(*(char*)" + std::to_string(tinfo.arena_ptr + address) + "ul)";
}
} else if (cpu.machine().memory.uses_32bit_encompassing_arena()) {
return "ARENA_AT(cpu, (uint32_t)(" + std::to_string(address) + "))";
} else {
return "ARENA_AT(cpu, " + speculation_safe(address) + ")";
}
}

template <typename T>
void memory_load(std::string dst, std::string type, int reg, int32_t imm)
{
Expand All @@ -202,7 +230,7 @@ struct Emitter
const address_t absolute_vaddr = tinfo.gp + imm;
if (absolute_vaddr >= 0x1000 && absolute_vaddr + sizeof(T) <= this->cpu.machine().memory.memory_arena_size()) {
add_code(
dst + " = " + cast + "*(" + type + "*)&ARENA_AT(cpu, " + speculation_safe(absolute_vaddr) + ");"
dst + " = " + cast + "*(" + type + "*)&" + arena_at_fixed(absolute_vaddr) + ";"
);
return;
}
Expand All @@ -211,12 +239,12 @@ struct Emitter
const auto address = from_reg(reg) + " + " + from_imm(imm);
if (cpu.machine().memory.uses_32bit_encompassing_arena())
{
add_code(dst + " = " + cast + "*(" + type + "*)&ARENA_AT(cpu, (uint32_t)(" + address + "));");
add_code(dst + " = " + cast + "*(" + type + "*)&" + arena_at(address) + ";");
}
else if (cpu.machine().memory.uses_flat_memory_arena()) {
add_code(
"if (LIKELY(ARENA_READABLE(" + address + ")))",
dst + " = " + cast + "*(" + type + "*)&ARENA_AT(cpu, " + speculation_safe(address) + ");",
dst + " = " + cast + "*(" + type + "*)&" + arena_at(address) + ";",
"else {",
"const char* " + data + " = api.mem_ld(cpu, " + address + ");",
dst + " = " + cast + "*(" + type + "*)&" + data + "[PAGEOFF(" + address + ")];",
Expand Down Expand Up @@ -245,12 +273,12 @@ struct Emitter
const auto address = from_reg(reg) + " + " + from_imm(imm);
if (cpu.machine().memory.uses_32bit_encompassing_arena())
{
add_code("*(" + type + "*)&ARENA_AT(cpu, (uint32_t)(" + address + ")) = " + value + ";");
add_code("*(" + type + "*)&" + arena_at(address) + " = " + value + ";");
}
else if (cpu.machine().memory.uses_flat_memory_arena()) {
add_code(
"if (LIKELY(ARENA_WRITABLE(" + address + ")))",
" *(" + type + "*)&ARENA_AT(cpu, " + speculation_safe(address) + ") = " + value + ";",
" *(" + type + "*)&" + arena_at(address) + " = " + value + ";",
"else {",
" char *" + data + " = api.mem_st(cpu, " + address + ");",
" *(" + type + "*)&" + data + "[PAGEOFF(" + address + ")] = " + value + ";",
Expand Down
3 changes: 2 additions & 1 deletion lib/libriscv/tr_translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ if constexpr (SCAN_FOR_GP) {
true,
std::move(jump_locations),
nullptr, // blocks
global_jump_locations
global_jump_locations,
(uintptr_t)machine().memory.memory_arena_ptr_ref()
});
icounter += length;
// we can't translate beyond this estimate, otherwise
Expand Down
2 changes: 2 additions & 0 deletions lib/libriscv/tr_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ namespace riscv
std::vector<TransInfo<W>>* blocks = nullptr;

std::unordered_set<address_type<W>>& global_jump_locations;

uintptr_t arena_ptr;
};
}

0 comments on commit 0139c0f

Please sign in to comment.