Skip to content

Commit

Permalink
Merge pull request #1871 from xlsynth:cdleary/2025-01-19-bytecode-cac…
Browse files Browse the repository at this point in the history
…he-import-data

PiperOrigin-RevId: 718415111
  • Loading branch information
copybara-github committed Jan 22, 2025
2 parents 2a74d76 + 38f4282 commit 456ee42
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
7 changes: 2 additions & 5 deletions xls/dslx/bytecode/bytecode_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@

namespace xls::dslx {

BytecodeCache::BytecodeCache(ImportData* import_data)
: import_data_(import_data) {}

absl::StatusOr<BytecodeFunction*> BytecodeCache::GetOrCreateBytecodeFunction(
const Function& f, const TypeInfo* type_info,
ImportData& import_data, const Function& f, const TypeInfo* type_info,
const std::optional<ParametricEnv>& caller_bindings) {
XLS_RET_CHECK(type_info != nullptr);
Key key = std::make_tuple(&f, type_info, caller_bindings);
if (!cache_.contains(key)) {
XLS_ASSIGN_OR_RETURN(
std::unique_ptr<BytecodeFunction> bf,
BytecodeEmitter::Emit(import_data_, type_info, f, caller_bindings));
BytecodeEmitter::Emit(&import_data, type_info, f, caller_bindings));
cache_.emplace(key, std::move(bf));
}

Expand Down
5 changes: 2 additions & 3 deletions xls/dslx/bytecode/bytecode_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ namespace xls::dslx {

class BytecodeCache : public BytecodeCacheInterface {
public:
explicit BytecodeCache(ImportData* import_data);
BytecodeCache() = default;

absl::StatusOr<BytecodeFunction*> GetOrCreateBytecodeFunction(
const Function& f, const TypeInfo* type_info,
ImportData& import_data, const Function& f, const TypeInfo* type_info,
const std::optional<ParametricEnv>& caller_bindings) override;

private:
using Key = std::tuple<const Function*, const TypeInfo*,
std::optional<ParametricEnv>>;

ImportData* import_data_;
absl::flat_hash_map<Key, std::unique_ptr<BytecodeFunction>> cache_;
};

Expand Down
5 changes: 4 additions & 1 deletion xls/dslx/bytecode/bytecode_cache_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace xls::dslx {

// Forward decl.
class ImportData;

// Defines the interface a type must provide in order to serve as a bytecode
// cache. In practice, this type exists to avoid attaching too many concrete
// dependencies onto ImportData, which is the primary cache owner.
Expand All @@ -35,7 +38,7 @@ class BytecodeCacheInterface {
// constants are held inside the given TypeInfo - different instances of a
// parametric function will have different TypeInfos associated with them.
virtual absl::StatusOr<BytecodeFunction*> GetOrCreateBytecodeFunction(
const Function& f, const TypeInfo* type_info,
ImportData& import_data, const Function& f, const TypeInfo* type_info,
const std::optional<ParametricEnv>& caller_bindings) = 0;
};

Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/bytecode/bytecode_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ absl::StatusOr<BytecodeFunction*> BytecodeInterpreter::GetBytecodeFn(
import_data_->GetRootTypeInfo(f.owner()));
}

return cache->GetOrCreateBytecodeFunction(f, callee_type_info,
return cache->GetOrCreateBytecodeFunction(*import_data(), f, callee_type_info,
callee_bindings);
}

Expand Down
7 changes: 3 additions & 4 deletions xls/dslx/create_import_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ImportData CreateImportData(
WarningKindSet warnings, std::unique_ptr<VirtualizableFilesystem> vfs) {
ImportData import_data(stdlib_path, additional_search_paths, warnings,
std::move(vfs));
import_data.SetBytecodeCache(std::make_unique<BytecodeCache>(&import_data));
import_data.SetBytecodeCache(std::make_unique<BytecodeCache>());
return import_data;
}

Expand All @@ -46,7 +46,7 @@ ImportData CreateImportDataForTest(
absl::Span<const std::filesystem::path> additional_search_paths = {};
ImportData import_data(xls::kDefaultDslxStdlibPath, additional_search_paths,
kDefaultWarningsSet, std::move(vfs));
import_data.SetBytecodeCache(std::make_unique<BytecodeCache>(&import_data));
import_data.SetBytecodeCache(std::make_unique<BytecodeCache>());
return import_data;
}

Expand All @@ -55,8 +55,7 @@ std::unique_ptr<ImportData> CreateImportDataPtrForTest() {
new ImportData(xls::kDefaultDslxStdlibPath,
/*additional_search_paths=*/{}, kDefaultWarningsSet,
std::make_unique<RealFilesystem>()));
import_data->SetBytecodeCache(
std::make_unique<BytecodeCache>(import_data.get()));
import_data->SetBytecodeCache(std::make_unique<BytecodeCache>());
return import_data;
}

Expand Down
4 changes: 2 additions & 2 deletions xls/dslx/run_routines/run_routines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void HandleError(TestResultData& result, const absl::Status& status,
absl::Status RunDslxTestFunction(ImportData* import_data, TypeInfo* type_info,
Module* module, TestFunction* tf,
const BytecodeInterpreterOptions& options) {
auto cache = std::make_unique<BytecodeCache>(import_data);
auto cache = std::make_unique<BytecodeCache>();
import_data->SetBytecodeCache(std::move(cache));
XLS_ASSIGN_OR_RETURN(
std::unique_ptr<BytecodeFunction> bf,
Expand All @@ -149,7 +149,7 @@ absl::Status RunDslxTestFunction(ImportData* import_data, TypeInfo* type_info,
absl::Status RunDslxTestProc(ImportData* import_data, TypeInfo* type_info,
Module* module, TestProc* tp,
const BytecodeInterpreterOptions& options) {
auto cache = std::make_unique<BytecodeCache>(import_data);
auto cache = std::make_unique<BytecodeCache>();
import_data->SetBytecodeCache(std::move(cache));

XLS_ASSIGN_OR_RETURN(TypeInfo * ti,
Expand Down

0 comments on commit 456ee42

Please sign in to comment.