Skip to content

Commit

Permalink
[vm] MemoryCopyInstr increase element size if possible
Browse files Browse the repository at this point in the history
A larger element size results in larger mov instructions. This reduces
code size and increase performance.

The element size can only be increased if
* src_offset, dest_offset, and length parameters are const,
* and if they contain a common denominator (powers of two).

TEST=runtime/vm/compiler/backend/memory_copy_test.cc

Bug: #51031
Change-Id: If35fb419aa118c497b15c122bdf6279266e2294a
Cq-Include-Trybots: luci.dart.try:vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,vm-kernel-nnbd-mac-debug-arm64-try,vm-kernel-nnbd-win-debug-x64-try,vm-kernel-win-debug-x64c-try,vm-kernel-win-debug-ia32-try,vm-kernel-nnbd-linux-debug-ia32-try,vm-reload-rollback-linux-debug-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279506
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
  • Loading branch information
dcharkes authored and Commit Queue committed Feb 3, 2023
1 parent 534453e commit 360fd45
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
39 changes: 39 additions & 0 deletions runtime/vm/compiler/backend/il.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "vm/compiler/frontend/kernel_translation_helper.h"
#include "vm/compiler/jit/compiler.h"
#include "vm/compiler/method_recognizer.h"
#include "vm/compiler/runtime_api.h"
#include "vm/cpu.h"
#include "vm/dart_entry.h"
#include "vm/object.h"
Expand Down Expand Up @@ -6585,6 +6586,44 @@ Representation StoreIndexedInstr::RequiredInputRepresentation(
return RepresentationOfArrayElement(class_id());
}

Instruction* MemoryCopyInstr::Canonicalize(FlowGraph* flow_graph) {
if (!length()->BindsToSmiConstant() || !src_start()->BindsToSmiConstant() ||
!dest_start()->BindsToSmiConstant()) {
// TODO(https://dartbug.com/51031): Consider adding support for src/dest
// starts to be in bytes rather than element size.
return this;
}

intptr_t new_length = length()->BoundSmiConstant();
intptr_t new_src_start = src_start()->BoundSmiConstant();
intptr_t new_dest_start = dest_start()->BoundSmiConstant();
intptr_t new_element_size = element_size_;
while (((new_length | new_src_start | new_dest_start) & 1) == 0 &&
new_element_size < compiler::target::kWordSize) {
new_length >>= 1;
new_src_start >>= 1;
new_dest_start >>= 1;
new_element_size <<= 1;
}
if (new_element_size == element_size_) {
return this;
}

Zone* const zone = flow_graph->zone();
auto* const length_instr = flow_graph->GetConstant(
Integer::ZoneHandle(zone, Integer::New(new_length, Heap::kOld)),
unboxed_length_ ? kUnboxedIntPtr : kTagged);
auto* const src_start_instr = flow_graph->GetConstant(
Integer::ZoneHandle(zone, Integer::New(new_src_start, Heap::kOld)));
auto* const dest_start_instr = flow_graph->GetConstant(
Integer::ZoneHandle(zone, Integer::New(new_dest_start, Heap::kOld)));
length()->BindTo(length_instr);
src_start()->BindTo(src_start_instr);
dest_start()->BindTo(dest_start_instr);
element_size_ = new_element_size;
return this;
}

bool Utf8ScanInstr::IsScanFlagsUnboxed() const {
return scan_flags_field_.is_unboxed();
}
Expand Down
6 changes: 6 additions & 0 deletions runtime/vm/compiler/backend/il.h
Original file line number Diff line number Diff line change
Expand Up @@ -3078,6 +3078,12 @@ class MemoryCopyInstr : public TemplateInstruction<5, NoThrow> {
Value* dest_start() const { return inputs_[kDestStartPos]; }
Value* length() const { return inputs_[kLengthPos]; }

intptr_t element_size() const { return element_size_; }
bool unboxed_length() const { return unboxed_length_; }

// Optimizes MemoryCopyInstr with constant parameters to use larger moves.
virtual Instruction* Canonicalize(FlowGraph* flow_graph);

#define FIELD_LIST(F) \
F(classid_t, src_cid_) \
F(classid_t, dest_cid_) \
Expand Down
5 changes: 5 additions & 0 deletions runtime/vm/compiler/backend/memory_copy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,9 @@ MEMORY_COPY_TEST(5, 72, 1, 8)
MEMORY_COPY_TEST(13, 14, 15, 16)
#endif

// Size promotions with offsets.
MEMORY_COPY_TEST(2, 2, 8, 1) // promoted to 2.
MEMORY_COPY_TEST(4, 4, 8, 1) // promoted to 4.
MEMORY_COPY_TEST(8, 8, 8, 1) // promoted to 8.

} // namespace dart

0 comments on commit 360fd45

Please sign in to comment.