Skip to content

Commit

Permalink
Migrate uses of internal-only _PyJIT functions to
Browse files Browse the repository at this point in the history
Summary:
Removing the C-style getter functions from pyjit.h for users in Jit/ and
RuntimeTests/ that can load the config object directly.

Deduplicates FrameModeJitConfig and jir::hir::FrameMode.

Reviewed By: carljm

Differential Revision: D48234607

fbshipit-source-id: 2b65316c7b487f02736b7152d561add5030e0b84
  • Loading branch information
Alex Malyshev authored and facebook-github-bot committed Aug 11, 2023
1 parent c035e5a commit 9d5948d
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 200 deletions.
12 changes: 6 additions & 6 deletions Jit/code_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Jit/code_allocator.h"

#include "Jit/pyjit.h"
#include "Jit/config.h"
#include "Jit/threaded_compile.h"

#include <sys/mman.h>
Expand Down Expand Up @@ -32,9 +32,9 @@ CodeAllocator::~CodeAllocator() {}
void CodeAllocator::makeGlobalCodeAllocator() {
JIT_CHECK(
s_global_code_allocator_ == nullptr, "Global allocator already set");
if (_PyJIT_MultipleCodeSectionsEnabled()) {
if (getConfig().multiple_code_sections) {
s_global_code_allocator_ = new MultipleSectionCodeAllocator;
} else if (_PyJIT_UseHugePages()) {
} else if (getConfig().use_huge_pages) {
s_global_code_allocator_ = new CodeAllocatorCinder;
} else {
s_global_code_allocator_ = new CodeAllocatorAsmJit;
Expand Down Expand Up @@ -151,14 +151,14 @@ MultipleSectionCodeAllocator::~MultipleSectionCodeAllocator() {
void MultipleSectionCodeAllocator::createSlabs() noexcept {
// Linux's huge-page sizes are 2 MiB.
const size_t kHugePageSize = 1024 * 1024 * 2;
size_t hot_section_size =
asmjit::Support::alignUp(_PyJIT_HotCodeSectionSize(), kHugePageSize);
size_t hot_section_size = asmjit::Support::alignUp(
getConfig().hot_code_section_size, kHugePageSize);
JIT_CHECK(
hot_section_size > 0,
"Hot code section must have non-zero size when using multiple sections.");
code_section_free_sizes_[CodeSection::kHot] = hot_section_size;

size_t cold_section_size = _PyJIT_ColdCodeSectionSize();
size_t cold_section_size = getConfig().cold_code_section_size;
JIT_CHECK(
cold_section_size > 0,
"Cold code section must have non-zero size when using multiple "
Expand Down
7 changes: 4 additions & 3 deletions Jit/codegen/code_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#pragma once

#include "Jit/pyjit.h"
#include "Jit/config.h"

#include <asmjit/asmjit.h>

#include <cstring>
#include <vector>

namespace jit::codegen {

Expand Down Expand Up @@ -46,7 +47,7 @@ class CodeSectionOverride {
CodeHolderMetadata* metadata,
CodeSection section)
: as_{as}, code_{code}, metadata_{metadata} {
if (_PyJIT_MultipleCodeSectionsEnabled()) {
if (getConfig().multiple_code_sections) {
previous_section_ = metadata->section_;
metadata->section_ = section;
as->section(code->sectionByName(codeSectionName(section)));
Expand All @@ -60,7 +61,7 @@ class CodeSectionOverride {
if (as_ == nullptr || code_ == nullptr) {
return;
}
if (_PyJIT_MultipleCodeSectionsEnabled()) {
if (getConfig().multiple_code_sections) {
as_->section(code_->sectionByName(codeSectionName(previous_section_)));
metadata_->section_ = previous_section_;
}
Expand Down
2 changes: 1 addition & 1 deletion Jit/codegen/environ.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct Environ {

UnorderedMap<jit::lir::BasicBlock*, asmjit::Label> block_label_map;

hir::FrameMode frame_mode;
FrameMode frame_mode;
int initial_yield_spill_size_{-1};

int max_arg_buffer_size{0};
Expand Down
11 changes: 6 additions & 5 deletions Jit/codegen/gen_asm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Jit/codegen/autogen.h"
#include "Jit/codegen/code_section.h"
#include "Jit/codegen/gen_asm_utils.h"
#include "Jit/config.h"
#include "Jit/frame.h"
#include "Jit/hir/analysis.h"
#include "Jit/hir/hir.h"
Expand Down Expand Up @@ -89,7 +90,7 @@ void NativeGenerator::generateEpilogueUnlinkFrame(
PYSF_PYFRAME == 1 && _PyShadowFrame_NumPtrKindBits == 2,
"Unexpected constants");
bool might_have_heap_frame =
func_->canDeopt() || func_->frameMode == jit::hir::FrameMode::kNormal;
func_->canDeopt() || func_->frameMode == jit::FrameMode::kNormal;
if (might_have_heap_frame) {
as_->bt(
x86::qword_ptr(scratch_reg, offsetof(_PyShadowFrame, data)),
Expand Down Expand Up @@ -220,7 +221,7 @@ void* NativeGenerator::getVectorcallEntry() {
ThrowableErrorHandler eh;
code.setErrorHandler(&eh);

if (_PyJIT_MultipleCodeSectionsEnabled()) {
if (getConfig().multiple_code_sections) {
Section* cold_text;
code.newSection(
&cold_text,
Expand Down Expand Up @@ -447,7 +448,7 @@ void NativeGenerator::linkOnStackShadowFrame(
x86::Gp tstate_reg,
x86::Gp scratch_reg) {
const jit::hir::Function* func = GetFunction();
jit::hir::FrameMode frame_mode = func->frameMode;
jit::FrameMode frame_mode = func->frameMode;
using namespace shadow_frame;
x86::Mem shadow_stack_top_ptr = getStackTopPtr(tstate_reg);
uintptr_t data =
Expand All @@ -456,7 +457,7 @@ void NativeGenerator::linkOnStackShadowFrame(
as_->mov(scratch_reg, shadow_stack_top_ptr);
as_->mov(kInFramePrevPtr, scratch_reg);
// Set data
if (frame_mode == jit::hir::FrameMode::kNormal) {
if (frame_mode == jit::FrameMode::kNormal) {
as_->mov(scratch_reg, x86::ptr(tstate_reg, offsetof(PyThreadState, frame)));
static_assert(
PYSF_PYFRAME == 1 && _PyShadowFrame_NumPtrKindBits == 2,
Expand All @@ -470,7 +471,7 @@ void NativeGenerator::linkOnStackShadowFrame(
// This is only necessary when in normal-frame mode because the frame is
// already materialized on function entry. It is lazily filled when the frame
// is materialized in shadow-frame mode.
if (frame_mode == jit::hir::FrameMode::kNormal) {
if (frame_mode == jit::FrameMode::kNormal) {
as_->mov(scratch_reg, data);
as_->mov(shadow_frame::kInFrameOrigDataPtr, scratch_reg);
}
Expand Down
1 change: 0 additions & 1 deletion Jit/codegen/gen_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "Jit/hir/hir.h"
#include "Jit/jit_rt.h"
#include "Jit/lir/lir.h"
#include "Jit/pyjit.h"
#include "Jit/util.h"

#include <asmjit/asmjit.h>
Expand Down
3 changes: 2 additions & 1 deletion Jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Python.h"

#include "Jit/config.h"
#include "Jit/disassembler.h"
#include "Jit/hir/analysis.h"
#include "Jit/hir/builder.h"
Expand Down Expand Up @@ -140,7 +141,7 @@ std::unique_ptr<CompiledFunction> Compiler::Compile(

PassConfig createConfig() {
PassConfig result{PassConfig::kDefault};
if (_PyJIT_IsHIRInlinerEnabled()) {
if (getConfig().hir_inliner_enabled) {
result = static_cast<PassConfig>(result | PassConfig::kEnableHIRInliner);
}
return result;
Expand Down
24 changes: 15 additions & 9 deletions Jit/config.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
// Copyright (c) Meta Platforms, Inc. and affiliates. (http://www.meta.com)

#pragma once

#include <cstddef>
#include <cstdint>

namespace jit {

enum InitStateConfig : uint8_t {
JIT_NOT_INITIALIZED,
JIT_INITIALIZED,
JIT_FINALIZED,
enum class InitState : uint8_t {
kNotInitialized,
kInitialized,
kFinalized,
};

enum FrameModeConfig : uint8_t {
PY_FRAME,
SHADOW_FRAME,
enum class FrameMode : uint8_t {
kNormal,
kShadow,
};

struct Config {
bool is_enabled{false};
FrameModeConfig frame_mode{PY_FRAME};
InitStateConfig init_state{JIT_NOT_INITIALIZED};
FrameMode frame_mode{FrameMode::kNormal};
InitState init_state{InitState::kNotInitialized};
bool allow_jit_list_wildcards{false};
bool compile_all_static_functions{false};
bool hir_inliner_enabled{false};
bool multiple_code_sections{false};
bool multithreaded_compile_test{false};
bool use_huge_pages{true};
size_t batch_compile_workers{0};
// Sizes (in bytes) of the hot and cold code sections. Only applicable if
// multiple code sections are enabled.
size_t cold_code_section_size{0};
size_t hot_code_section_size{0};
// Size (in number of entries) of the LoadAttr and StoreAttr inline caches
// used by the JIT.
uint32_t attr_cache_size{1};
uint32_t auto_jit_threshold{0};
int code_watcher_id{-1};
Expand Down
2 changes: 1 addition & 1 deletion Jit/hir/hir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ bool usesRuntimeFunc(BorrowedRef<PyCodeObject> code) {
void Function::setCode(BorrowedRef<PyCodeObject> code) {
this->code.reset(code);
uses_runtime_func = usesRuntimeFunc(code);
frameMode = _PyJIT_ShadowFrame() ? FrameMode::kShadow : FrameMode::kNormal;
frameMode = getConfig().frame_mode;
}

void Function::Print() const {
Expand Down
6 changes: 1 addition & 5 deletions Jit/hir/hir.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "opcode.h"

#include "Jit/bytecode.h"
#include "Jit/config.h"
#include "Jit/deopt_patcher.h"
#include "Jit/hir/register.h"
#include "Jit/hir/type.h"
Expand Down Expand Up @@ -4335,11 +4336,6 @@ class Environment {
int next_load_type_method_cache_{0};
};

enum class FrameMode {
kNormal,
kShadow,
};

struct TypedArgument {
TypedArgument(
long locals_idx,
Expand Down
2 changes: 1 addition & 1 deletion Jit/inline_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void AttributeCache::fill(
}

std::span<AttributeMutator> AttributeCache::entries() {
return {entries_, _PyJIT_AttrCacheSize()};
return {entries_, getConfig().attr_cache_size};
}

AttributeMutator* AttributeCache::findEmptyEntry() {
Expand Down
5 changes: 3 additions & 2 deletions Jit/inline_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include "Python.h"
#include "StaticPython/classloader.h"

#include "Jit/config.h"
#include "Jit/jit_rt.h"
#include "Jit/log.h"
#include "Jit/pyjit.h"
#include "Jit/ref.h"
#include "Jit/util.h"

#include <array>
#include <memory>
#include <span>
#include <unordered_map>

namespace jit {
Expand Down Expand Up @@ -125,7 +126,7 @@ class AttributeCache {
struct AttributeCacheSizeTrait {
static size_t size() {
auto base = sizeof(AttributeCache);
auto extra = sizeof(AttributeMutator) * _PyJIT_AttrCacheSize();
auto extra = sizeof(AttributeMutator) * getConfig().attr_cache_size;
return base + extra;
}
};
Expand Down
2 changes: 1 addition & 1 deletion Jit/jit_rt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ static inline PyObject* make_gen_object(
jit::CodeRuntime* code_rt,
PyCodeObject* code) {
PyGenObject* gen = nullptr;
if (_PyJIT_ShadowFrame()) {
if (jit::getConfig().frame_mode == jit::FrameMode::kShadow) {
if (mode == MakeGenObjectMode::kCoroutine) {
gen = reinterpret_cast<PyGenObject*>(CiCoro_New_NoFrame(tstate, code));
} else if (mode == MakeGenObjectMode::kAsyncGenerator) {
Expand Down
5 changes: 3 additions & 2 deletions Jit/lir/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pystate.h"

#include "Jit/codegen/x86_64.h"
#include "Jit/config.h"
#include "Jit/containers.h"
#include "Jit/deopt.h"
#include "Jit/frame.h"
Expand Down Expand Up @@ -559,7 +560,7 @@ void LIRGenerator::MakeDecref(

bbb.AppendCode("BranchNZ {}", end_decref);
bbb.AppendLabel(dealloc);
if (_PyJIT_MultipleCodeSectionsEnabled()) {
if (getConfig().multiple_code_sections) {
bbb.SetBlockSection(dealloc, codegen::CodeSection::kCold);
}

Expand Down Expand Up @@ -2506,7 +2507,7 @@ LIRGenerator::TranslatedBlock LIRGenerator::TranslateOneBasicBlock(
// This is only necessary when in normal-frame mode because the frame
// is already materialized on function entry. It is lazily filled when
// the frame is materialized in shadow-frame mode.
if (func_->frameMode == jit::hir::FrameMode::kNormal) {
if (func_->frameMode == jit::FrameMode::kNormal) {
bbb.AppendCode(
"Store {}, {}, {}",
data_reg,
Expand Down
Loading

0 comments on commit 9d5948d

Please sign in to comment.