Skip to content

Commit

Permalink
memory: optimize enconding when alignment of globals doesn't change
Browse files Browse the repository at this point in the history
This is the common case
  • Loading branch information
nunoplopes committed Dec 7, 2024
1 parent 4e72d5d commit 6f41394
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions ir/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bool has_ptr_arg = true;
bool has_initializes_attr = true;
bool has_null_block = true;
bool null_is_dereferenceable = false;
bool has_globals_diff_align = true;
bool does_int_mem_access = true;
bool does_ptr_mem_access = true;
bool does_ptr_store = true;
Expand Down
3 changes: 3 additions & 0 deletions ir/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ extern bool has_null_block;

extern bool null_is_dereferenceable;

/// Whether there is at least one global with different alignment in src/tgt
extern bool has_globals_diff_align;

/// Whether the programs do memory accesses that load/store int/ptrs
extern bool does_int_mem_access;
extern bool does_ptr_mem_access;
Expand Down
3 changes: 3 additions & 0 deletions ir/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,9 @@ Memory::alloc(const expr *size, uint64_t align, BlockKind blockKind,
expr nooverflow = true;
if (size) {
size_zext = size->zextOrTrunc(bits_size_t);
// we round up the size statically instead of creating a large expr later
if (!has_globals_diff_align)
size_zext = size_zext.round_up(expr::mkUInt(align, bits_size_t));
nooverflow = size->bits() <= bits_size_t ? true :
size->extract(size->bits()-1, bits_size_t) == 0;
}
Expand Down
9 changes: 7 additions & 2 deletions ir/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,11 @@ expr Pointer::blockSizeOffsetT() const {
}

expr Pointer::blockSizeAligned() const {
return blockSize().round_up_bits(blockAlignment().zextOrTrunc(bits_size_t));
auto size = blockSize();
// programs can't observe whether the size was increased up to alignment
if (!has_globals_diff_align)
return size;
return size.round_up_bits(blockAlignment().zextOrTrunc(bits_size_t));
}

expr Pointer::blockSizeAlignedOffsetT() const {
Expand Down Expand Up @@ -456,7 +460,8 @@ expr Pointer::inbounds(bool simplify_ptr) {

expr Pointer::blockAlignment() const {
return getValue("blk_align", m.local_blk_align, m.non_local_blk_align,
expr::mkUInt(0, Memory::bitsAlignmentInfo()), true);
expr::mkUInt(0, Memory::bitsAlignmentInfo()),
has_globals_diff_align);
}

expr Pointer::isBlockAligned(uint64_t align, bool exact) const {
Expand Down
4 changes: 4 additions & 0 deletions tools/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ static void calculateAndInitConstants(Transform &t) {
uint64_t glb_alloc_aligned_size = 0;

num_consts_src = 0;
has_globals_diff_align = false;

for (auto GV : globals_src) {
if (GV->isConst())
Expand All @@ -974,7 +975,10 @@ static void calculateAndInitConstants(Transform &t) {
[GVT](auto *GV) -> bool { return GVT->getName() == GV->getName(); });
if (I == globals_src.end()) {
++num_globals;
} else {
has_globals_diff_align |= GVT->getAlignment() != (*I)->getAlignment();
}

glb_alloc_aligned_size
= add_saturate(glb_alloc_aligned_size,
aligned_alloc_size(GVT->size(), GVT->getAlignment()));
Expand Down

0 comments on commit 6f41394

Please sign in to comment.