8000 make alias group zero universal by can-lehmann · Pull Request #167 · can-lehmann/metajit.cpp · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ b1:
### Aliasing

Aliasing information is encoded using aliasing groups.
Memory operations can only alias if they are in the same aliasing group.
Aliasing groups are represented as integers.
Negative integers are used to represent exact aliasing (i.e., two operations with the same negative aliasing group always alias).
Non-negative integers are used to represent potential aliasing (i.e., two operations with the same non-negative aliasing group may alias, but do not have to).
Positive integers represent potential aliasing within the same group.
Group 0 may alias every group, including exact groups. Distinct nonzero groups are disjoint.

## LLVM Frontend

Expand Down
117 changes: 77 additions & 40 deletions jitir.tmpl.hpp
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,15 @@ namespace metajit {
value = nullptr;
}
}

void invalidate_memory_for_store(AliasingGroup aliasing) {
if (aliasing == 0) {
invalidate_memory_state();
} else {
_valid_loads.erase(0);
_memory.erase(0);
}
}
public:
using Builder::Builder;
using Builder::build_call;
Expand Down Expand Up @@ -1941,12 +1950,11 @@ namespace metajit {
if (is_always_equal(_exact_memory[-aliasing], value)) {
return nullptr;
}
invalidate_memory_for_store(aliasing);
_exact_memory[-aliasing] = value;
_exact_loads[-aliasing] = nullptr;
return Builder::build_store(ptr, value, flags, aliasing, offset);
} else {
_memory[aliasing].store(ptr, offset, value);

bool noop_store = false;
for (auto it = _valid_loads[aliasing].begin(); it != _valid_loads[aliasing].end(); ) {
LoadInst* load = *it;
Expand All @@ -1962,6 +1970,10 @@ namespace metajit {
}
}

if (!noop_store) {
invalidate_memory_for_store(aliasing);
}
_memory[aliasing].store(ptr, offset, value);
if (noop_store) {
return nullptr;
}
Expand Down Expand Up @@ -2057,6 +2069,7 @@ namespace metajit {
}

void init_store(Value* ptr, Value* value, AliasingGroup aliasing, uint64_t offset) {
invalidate_memory_for_store(aliasing);
if (aliasing < 0) {
_exact_memory[-aliasing] = value;
} else {
Expand Down Expand Up @@ -2643,6 +2656,10 @@ namespace metajit {
};

inline bool could_alias(LoadInst* load, StoreInst* store) {
if (load->aliasing() == 0 || store->aliasing() == 0) {
return true;
}

if (load->aliasing() != store->aliasing()) {
return false;
}
Expand Down Expand Up @@ -2753,7 +2770,8 @@ namespace metajit {
}

for (auto& [alloca, info] : _alloca_info) {
if (info.eligible && info.existing_group && !_confined.at(*info.existing_group)) {
if (info.eligible && info.existing_group &&
(*info.existing_group == 0 || !_confined.at(*info.existing_group))) {
if (_max_group == INT32_MAX) {
break;
}
Expand Down Expand Up @@ -2804,7 +2822,7 @@ namespace metajit {
Value* _only_base;

void access(AliasingGroup aliasing, Value* ptr, uint64_t offset, Type type) {
assert(aliasing >= 0);
assert(aliasing > 0);
GroupInfo& group = _groups[aliasing];
if (!group.is_invalid) {
if (group.base == nullptr) {
Expand All @@ -2819,7 +2837,7 @@ namespace metajit {
}

AliasingGroup apply(AliasingGroup aliasing, uint64_t offset) {
assert(aliasing >= 0);
assert(aliasing > 0);
GroupInfo& group = _groups[aliasing];
if (group.is_invalid) {
return aliasing;
Expand All @@ -2837,14 +2855,14 @@ namespace metajit {
for (Block* block : *section) {
for (Inst* inst : *block) {
if (dynmatch(LoadInst, load, inst)) {
if (load->aliasing() >= 0) {
if (load->aliasing() > 0) {
_loads.push_back(load);
access(load->aliasing(), load->ptr(), load->offset(), inst->type());
} else {
_min_exact_group = std::min(_min_exact_group, load->aliasing());
}
} else if (dynmatch(StoreInst, store, inst)) {
if (store->aliasing() >= 0) {
if (store->aliasing() > 0) {
_stores.push_back(store);
access(store->aliasing(), store->ptr(), store->offset(), store->value()->type());
} else {
Expand All @@ -2854,6 +2872,11 @@ namespace metajit {
}
}

if (static_cast<int64_t>(_min_exact_group) + INT32_MAX <
static_cast<int64_t>(_loads.size() + _stores.size())) {
return;
}

for (LoadInst* load : _loads) {
load->set_aliasing(apply(load->aliasing(), load->offset()));
}
Expand All @@ -2865,46 +2888,46 @@ namespace metajit {
};

class DeadStoreElim: public Pass<DeadStoreElim> {
public:
DeadStoreElim(Section* section): Pass(section) {
NameMap<bool> unused(section);
ExpandingVector<StoreInst*> last_store;
private:
NameMap<bool> _unused;
ExpandingVector<StoreInst*> _last_store;

void mark_stores_live() {
for (StoreInst*& store : _last_store) {
if (store) {
_unused[store] = false;
store = nullptr;
}
}
}
public:
DeadStoreElim(Section* section): Pass(section), _unused(section) {
for (Block* block : *section) {
for (Inst* inst : *block) {
if (dynmatch(StoreInst, store, inst)) {
Pointer pointer(store->ptr(), store->offset());
if (store->aliasing() < 0) {
last_store[-store->aliasing()] = store;
unused[store] = true;
_last_store[-store->aliasing()] = store;
_unused[store] = true;
}
} else if (dynmatch(LoadInst, load, inst)) {
if (load->aliasing() < 0) {
StoreInst* store = last_store[-load->aliasing()];
StoreInst* store = _last_store[-load->aliasing()];
if (store) {
unused[store] = false;
_unused[store] = false;
}
} else if (load->aliasing() == 0) {
mark_stores_live();
}
} else if (dynamic_cast<CallInst*>(inst)) {
// Calls may observe memory from all exact groups.
for (StoreInst*& store : last_store) {
if (store) {
unused[store] = false;
store = nullptr;
}
}
mark_stores_live();
}
}

for (StoreInst*& store : last_store) {
if (store) {
unused[store] = false;
store = nullptr; // Clear for next block
}
}
mark_stores_live();

block->filter_inplace([&](Inst* inst) {
return !unused[inst];
return !_unused[inst];
});
}
}
Expand Down Expand Up @@ -4806,7 +4829,8 @@ class SimplifyTrace: public metajit::Pass<SimplifyTrace> {

for (Block* block : *loop->chain()) {
for (Inst* inst : *block) {
if (dynamic_cast<CallInst*>(inst)) {
if (dynmatch(StoreInst, store, inst);
dynamic_cast<CallInst*>(inst) || (store && store->aliasing() == 0)) {
// Conservative: calls can touch memory backing promoted aliases.
return;
}
Expand Down Expand Up @@ -4910,6 +4934,8 @@ class SimplifyTrace: public metajit::Pass<SimplifyTrace> {
if (dynmatch(StoreInst, store, inst)) {
if (store->aliasing() < 0) {
stores[-store->aliasing()] = 1;
} else if (store->aliasing() == 0) {
has_unknown_memory_write = true;
}
} else if (dynamic_cast<CallInst*>(inst)) {
has_unknown_memory_write = true;
Expand Down Expand Up @@ -6090,8 +6116,28 @@ class SimplifyTrace: public metajit::Pass<SimplifyTrace> {
}
}

if (dynmatch(StoreInst, store, inst)) {
if (dynmatch(StoreInst, store, inst);
(store && store->aliasing() == 0) || dynamic_cast<CallInst*>(inst)) {
// Calls can invalidate any cached memory-derived value.
for (auto& [group, loads] : valid_loads) {
for (LoadInst* load : loads) {
canon_erase(Lookup(load));
}
}
valid_loads.clear();
last_stores.clear();
if (store) {
last_stores[0] = store;
}
} else if (store) {
last_stores.erase(0);
last_stores[store->aliasing()] = store;
if (auto group = valid_loads.find(0); group != valid_loads.end()) {
for (LoadInst* load : group->second) {
canon_erase(Lookup(load));
}
valid_loads.erase(group);
}
std::vector<LoadInst*>& loads = valid_loads[store->aliasing()];
std::vector<LoadInst*> remaining_loads;
for (LoadInst* load : loads) {
Expand All @@ -6102,15 +6148,6 @@ class SimplifyTrace: public metajit::Pass<SimplifyTrace> {
}
}
loads = std::move(remaining_loads);
} else if (dynamic_cast<CallInst*>(inst)) {
// Calls can invalidate any cached memory-derived value.
for (auto& [group, loads] : valid_loads) {
for (LoadInst* load : loads) {
canon_erase(Lookup(load));
}
}
valid_loads.clear();
last_stores.clear();
} else if (dynmatch(LoadInst, load, inst)) {
if (LastStores::const_iterator last_store = last_stores.find(load->aliasing());
last_store != last_stores.end()) {
Expand Down
14 changes: 10 additions & 4 deletions layout2aliasing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ namespace metajit {

PointerLayouts& _pointers;
std::map<Pointer, AliasingGroup> _group_ids;
AliasingGroup _next_group = 1;
AliasingGroup _next_exact_group = -1;
AliasingGroup _last_group = 0;
AliasingGroup _last_exact_group = 0;

AliasingGroup group_for(Pointer ptr) {
if (dynamic_cast<HeapLayout*>(ptr.layout)) {
Expand All @@ -311,9 +311,15 @@ namespace metajit {
ptr = ptr.canonical();
if (_group_ids.find(ptr) == _group_ids.end()) {
if (dynamic_cast<RecordLayout*>(ptr.layout) && ptr.layout->singleton()) {
_group_ids[ptr] = _next_exact_group--;
if (_last_exact_group == -INT32_MAX) {
throw std::overflow_error("Layout2Aliasing: exhausted exact aliasing groups");
}
_group_ids[ptr] = --_last_exact_group;
} else {
_group_ids[ptr] = _next_group++;
if (_last_group == INT32_MAX) {
throw std::overflow_error("Layout2Aliasing: exhausted aliasing groups");
}
_group_ids[ptr] = ++_last_group;
}
}
return _group_ids[ptr];
Expand Down
3 changes: 3 additions & 0 deletions llvmgen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace metajit {
std::unordered_map<AliasingGroup, llvm::MDNode*> _tbaa_tags;

llvm::MDNode* tbaa_tag(AliasingGroup group) {
if (group == 0) {
return nullptr;
}
llvm::MDNode*& tag = _tbaa_tags[group];
if (!tag) {
llvm::MDBuilder metadata(_context);
Expand Down
28 changes: 18 additions & 10 deletions x86gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,31 +660,39 @@ namespace metajit {
void memory_deps() {
for (Block* block : *_section) {
std::unordered_map<AliasingGroup, void*> last_store;
void* last_call = block;
void* last_clobber = block;
void* last_write = block;
for (Inst* inst : *block) {
for (Value* arg : inst->args()) {
if (arg->is_named()) {
_use_counts[(NamedValue*) arg]++;
}
}
#define find_dep(inst) \
void* dep = last_call; \
if (last_store.find(inst->aliasing()) != last_store.end()) { \
dep = (void*) last_store.at(inst->aliasing()); \
void* dep = last_clobber; \
if (inst->aliasing() == 0) { \
dep = last_write; \
} else if (auto previous = last_store.find(inst->aliasing()); previous != last_store.end()) { \
dep = previous->second; \
} \
_memory_deps[inst] = dep;

if (dynmatch(LoadInst, load, inst)) {
find_dep(load);
} else if (dynmatch(StoreInst, store, inst)) {
find_dep(store);
last_store[store->aliasing()] = store;
last_write = store;
if (store->aliasing() == 0) {
last_clobber = store;
last_store.clear();
} else {
last_store[store->aliasing()] = store;
}
} else if (dynamic_cast<CallInst*>(inst)) {
_memory_deps[inst] = (void*) block;
last_call = inst;
for (auto& [aliasing, dep] : last_store) {
dep = (void*) inst;
}
last_clobber = inst;
last_write = inst;
last_store.clear();
} else {
_memory_deps[inst] = nullptr;
}
Expand Down
Loading
0