8000 [XLS JIT] Copy counted_for loop state with memcpy. by copybara-service[bot] · Pull Request #5547 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions xls/jit/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ cc_test(
"//xls/ir:ir_parser",
"//xls/ir:type",
"//xls/ir:value",
"//xls/ir:value_utils",
"//xls/ir:value_view",
"//xls/ir:xls_type_cc_proto",
"@abseil-cpp//absl/container:flat_hash_map",
Expand All @@ -628,6 +629,7 @@ cc_test(
"@abseil-cpp//absl/types:optional_ref",
"@abseil-cpp//absl/types:span",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
"@llvm-project//llvm:ir_headers",
],
)
Expand Down
91 changes: 91 additions & 0 deletions xls/jit/function_jit_test.cc
8000
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@
#include "absl/strings/substitute.h"
#include "absl/types/optional_ref.h"
#include "absl/types/span.h"
#include "llvm/include/llvm/IR/BasicBlock.h"
#include "llvm/include/llvm/IR/DataLayout.h"
#include "llvm/include/llvm/IR/Function.h"
#include "llvm/include/llvm/IR/Instruction.h"
#include "llvm/include/llvm/IR/Instructions.h"
#include "llvm/include/llvm/IR/Module.h"
#include "llvm/include/llvm/Support/Casting.h"
#include "xls/common/bits_util.h"
#include "xls/common/math_util.h"
#include "xls/common/status/matchers.h"
Expand All @@ -65,6 +71,7 @@
#include "xls/ir/package.h"
#include "xls/ir/type.h"
#include "xls/ir/value.h"
#include "xls/ir/value_utils.h"
#include "xls/ir/value_view.h"
#include "xls/ir/xls_type.pb.h"
#include "xls/jit/function_base_jit.h"
Expand Down Expand Up @@ -1403,5 +1410,89 @@ FUZZ_TEST(FunctionJitTest, TestPackedArrayWithType)
/*max_elements=*/68),
/*max_elements=*/1030)));

// Records the size of the largest first-class aggregate (array or struct)
// load in the unoptimized LLVM module.
class AggregateLoadObserver final : public JitObserver {
public:
JitObserverRequests GetNotificationOptions() const override {
return JitObserverRequests{.unoptimized_module = true};
}
void UnoptimizedModule(const llvm::Module* module) override {
const llvm::DataLayout& data_layout = module->getDataLayout();
for (const llvm::Function& function : *module) {
for (const llvm::BasicBlock& block : function) {
for (const llvm::Instruction& inst : block) {
const auto* load = llvm::dyn_cast<llvm::LoadInst>(&inst);
if (load != nullptr && load->getType()->isAggregateType()) {
max_aggregate_load_bytes_ = std::max<int64_t>(
max_aggregate_load_bytes_,
data_layout.getTypeAllocSize(load->getType()));
}
}
}
}
}
int64_t max_aggregate_load_bytes() const { return max_aggregate_load_bytes_; }

private:
int64_t max_aggregate_load_bytes_ = 0;
};

// Regression test: the loop state of a counted_for must be copied with memcpy.
// LLVM lowers first-class aggregate loads/stores element by element, which made
// compile time and memory super-linear in the state size (a loop updating a
// tuple of two 10240-element arrays took ~66s and ~13GB to AOT compile).
TEST(FunctionJitTest, CountedForCopiesLargeLoopStateWithMemcpy) {
constexpr int64_t kSize = 1024;
constexpr int64_t kTripCount = 15;
Package package("my_package");
Type* u64 = package.GetBitsType(64);
Type* u32 = package.GetBitsType(32);
ArrayType* values_type = package.GetArrayType(kSize, u64);
ArrayType* flags_type = package.GetArrayType(kSize, package.GetBitsType(1));
Type* state_type = package.GetTupleType({values_type, flags_type});

// body(i, (values, flags), v) =
// (update(values, i, v + i), update(flags, i, 1))
FunctionBuilder body_builder("body", &package);
BValue i = body_builder.Param("i", u32);
BValue state = body_builder.Param("state", state_type);
BValue v = body_builder.Param("v", u64);
BValue values = body_builder.ArrayUpdate(
body_builder.TupleIndex(state, 0),
body_builder.Add(v, body_builder.ZeroExtend(i, 64)), {i});
BValue flags =
body_builder.ArrayUpdate(body_builder.TupleIndex(state, 1),
body_builder.Literal(UBits(1, 1)), {i});
XLS_ASSERT_OK_AND_ASSIGN(
Function * body,
body_builder.BuildWithReturnValue(body_builder.Tuple({values, flags})));

// f(v) = loop(...).values[3] + loop(...).flags[kTripCount]
FunctionBuilder fb("f", &package);
BValue param_v = fb.Param("v", u64);
BValue init = fb.Literal(ZeroOfType(state_type));
BValue loop = fb.CountedFor(init, kTripCount, /*stride=*/1, body, {param_v});
BValue value_3 =
fb.ArrayIndex(fb.TupleIndex(loop, 0), {fb.Literal(UBits(3, 32))});
BValue flag_past_end = fb.ArrayIndex(fb.TupleIndex(loop, 1),
{fb.Literal(UBits(kTripCount, 32))});
XLS_ASSERT_OK_AND_ASSIGN(
Function * function,
fb.BuildWithReturnValue(fb.Tuple({value_3, flag_past_end})));

AggregateLoadObserver observer;
XLS_ASSERT_OK_AND_ASSIGN(
std::unique_ptr<FunctionJit> jit,
FunctionJit::Create(function, EvaluatorOptions(),
JitEvaluatorOptions().set_jit_observer(&observer)));
// Nothing as large as one of the state arrays may be loaded as a value.
EXPECT_LT(observer.max_aggregate_load_bytes(), kSize);

EXPECT_THAT(
RunJitNoEvents(jit.get(), {Value(UBits(100, 64))}),
IsOkAndHolds(Value::Tuple({Value(UBits(103, 64)), Value(UBits(0, 1))})));
}

} // namespace
} // namespace xls
21 changes: 13 additions & 8 deletions xls/jit/ir_builder_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2262,11 +2262,17 @@ absl::Status IrBuilderVisitor::HandleCountedFor(CountedFor* counted_for) {
XLS_ASSIGN_OR_RETURN(llvm::Function * body, GetFunction(counted_for->body()));

// Create a buffer to hold the loop state and write in the initial value.
// The state is copied with memcpy rather than with a first-class load/store
// of the state type: LLVM lowers aggregate loads/stores element by element,
// so compile time and memory grow super-linearly with the size of
// array/tuple loop states.
llvm::Type* state_type = type_converter()->ConvertToLlvmType(
counted_for->initial_value()->GetType());
int64_t state_size = type_converter()->GetTypeByteSize(
counted_for->initial_value()->GetType());
AllocaBuffer loop_state_buffer = node_context.CreateAlloca(state_type);
node_context.entry_builder().CreateStore(node_context.LoadOperand(0),
loop_state_buffer.value());
LlvmMemcpy(loop_state_buffer.value(), node_context.GetOperandPtr(0),
state_size, node_context.entry_builder());

std::vector<llvm::Value*> invariant_arg_buffers;
for (int64_t i = 1; i < counted_for->operand_count(); ++i) {
Expand Down Expand Up @@ -2305,12 +2311,11 @@ absl::Status IrBuilderVisitor::HandleCountedFor(CountedFor* counted_for) {
node_context.GetJitRuntimeArg(), loop.body_builder())
.status());
index_buffer.CleanUp(loop.body_builder());
// TODO(meheff): 2022/09/09 Rather than loading the state and storing it in
// the state buffer for the next iteration, simply swap the state and
// next-state buffer pointers passed to the loop body function.
llvm::Value* next_state =
loop.body_builder().CreateLoad(state_type, next_state_buffer.value());
loop.body_builder().CreateStore(next_state, loop_state_buffer.value());
// TODO(meheff): 2022/09/09 Rather than copying the next state into the state
// buffer for the next iteration, simply swap the state and next-state buffer
// pointers passed to the loop body function.
LlvmMemcpy(loop_state_buffer.value(), next_state_buffer.value(), state_size,
loop.body_builder());
next_state_buffer.CleanUp(loop.body_builder());

loop.Finalize();
Expand Down
Loading
0