Skip to content

Commit

Permalink
fix #1107: false positive with const removal
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoplopes committed Nov 4, 2024
1 parent 8b4cbc3 commit 0281f6a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
8 changes: 2 additions & 6 deletions ir/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2575,12 +2575,8 @@ Memory::refined(const Memory &other, bool fncall,
if (p.isByval().isTrue() && q.isByval().isTrue())
continue;

// In assembly mode we verify each function individually and
// global constants are not validated (assumed to be correct).
// Hence we may not have all initializers if tgt doesn't reference them.
if (other.isAsmMode() &&
is_constglb(bid) &&
isInitialMemBlock(other.non_local_block_val[bid].val, false))
// Constants that are not referenced can be removed.
if (is_constglb(bid) && !other.state->isGVUsed(bid))
continue;

ret &= (ptr_bid == bid_expr).implies(blockRefined(p, q, bid, undef_vars));
Expand Down
15 changes: 12 additions & 3 deletions ir/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1392,13 +1392,22 @@ void State::markGlobalAsAllocated(const string &glbvar) {
itr->second.second = true;
}

bool State::isGVUsed(unsigned bid) const {
for (auto &[gv_name, data] : glbvar_bids) {
if (bid == data.first)
return getFn().getUsers().count(getFn().getGlobalVar(gv_name));
}
assert(false);
return false;
}

void State::syncSEdataWithSrc(State &src) {
assert(glbvar_bids.empty());
assert(src.isSource() && !isSource());
glbvar_bids = src.glbvar_bids;
for (auto &itm : glbvar_bids)
itm.second.second = false;

for (auto &[gv_name, data] : glbvar_bids) {
data.second = false;
}
fn_call_data = std::move(src.fn_call_data);
inaccessiblemem_bids = std::move(src.inaccessiblemem_bids);
memory.syncWithSrc(src.returnMemory());
Expand Down
1 change: 1 addition & 0 deletions ir/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ class State {
bool hasGlobalVarBid(const std::string &glbvar, unsigned &bid,
bool &allocated) const;
void markGlobalAsAllocated(const std::string &glbvar);
bool isGVUsed(unsigned bid) const;
void syncSEdataWithSrc(State &src);

void mkAxioms(State &tgt);
Expand Down
11 changes: 11 additions & 0 deletions tests/alive-tv/memory/const-removal.src.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
; this test must use separate files

@x = private constant i32 1

define i32 @f() {
ret i32 0

dead:
call void null(ptr @x)
unreachable
}
3 changes: 3 additions & 0 deletions tests/alive-tv/memory/const-removal.tgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define i32 @f() {
ret i32 0
}

0 comments on commit 0281f6a

Please sign in to comment.