Skip to content

Commit 0281f6a

Browse files
committed
fix #1107: false positive with const removal
1 parent 8b4cbc3 commit 0281f6a

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

ir/memory.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,12 +2575,8 @@ Memory::refined(const Memory &other, bool fncall,
25752575
if (p.isByval().isTrue() && q.isByval().isTrue())
25762576
continue;
25772577

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

25862582
ret &= (ptr_bid == bid_expr).implies(blockRefined(p, q, bid, undef_vars));

ir/state.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,13 +1392,22 @@ void State::markGlobalAsAllocated(const string &glbvar) {
13921392
itr->second.second = true;
13931393
}
13941394

1395+
bool State::isGVUsed(unsigned bid) const {
1396+
for (auto &[gv_name, data] : glbvar_bids) {
1397+
if (bid == data.first)
1398+
return getFn().getUsers().count(getFn().getGlobalVar(gv_name));
1399+
}
1400+
assert(false);
1401+
return false;
1402+
}
1403+
13951404
void State::syncSEdataWithSrc(State &src) {
13961405
assert(glbvar_bids.empty());
13971406
assert(src.isSource() && !isSource());
13981407
glbvar_bids = src.glbvar_bids;
1399-
for (auto &itm : glbvar_bids)
1400-
itm.second.second = false;
1401-
1408+
for (auto &[gv_name, data] : glbvar_bids) {
1409+
data.second = false;
1410+
}
14021411
fn_call_data = std::move(src.fn_call_data);
14031412
inaccessiblemem_bids = std::move(src.inaccessiblemem_bids);
14041413
memory.syncWithSrc(src.returnMemory());

ir/state.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ class State {
363363
bool hasGlobalVarBid(const std::string &glbvar, unsigned &bid,
364364
bool &allocated) const;
365365
void markGlobalAsAllocated(const std::string &glbvar);
366+
bool isGVUsed(unsigned bid) const;
366367
void syncSEdataWithSrc(State &src);
367368

368369
void mkAxioms(State &tgt);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; this test must use separate files
2+
3+
@x = private constant i32 1
4+
5+
define i32 @f() {
6+
ret i32 0
7+
8+
dead:
9+
call void null(ptr @x)
10+
unreachable
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define i32 @f() {
2+
ret i32 0
3+
}

0 commit comments

Comments
 (0)