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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $ make bench
- [**inline**](./src/backend/pass/opt/inline.h): function inlining
- [**strengthreduce**](./src/backend/pass/opt/strength_reduce.h): induction-variable strength reduction
- [**loadcanon**](./src/backend/pass/opt/load_canon.h): moves loads to their canonical memory state
- [**slp**](./src/backend/pass/opt/slp/): scheduling-free SLP vectorization with runtime alias guards
- [**slp**](./src/backend/pass/opt/slp.h): scheduling-free SLP vectorization
- [**dfe**](./src/backend/pass/opt/dead_func_elim.h): dead function elimination
### machine passes
- [**GCM**](./src/backend/codegen/schedule.h): global code motion
Expand Down
3 changes: 0 additions & 3 deletions src/backend/ir/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,6 @@ namespace rat {

Node* StackRestoreNode::getSaved() const { return getInput(2); }

MemMergeNode::MemMergeNode(Function& fn, Type* memoryType, const List<Node*>& controlStores)
: FixedOpcodeNode(fn, memoryType, controlStores) {}

SplatNode::SplatNode(Function& fn, Type* vecType, Node* scalar)
: FixedOpcodeNode(fn, vecType, {scalar}) {}

Expand Down
6 changes: 1 addition & 5 deletions src/backend/ir/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,6 @@ namespace rat {
Node* getSaved() const;
};

struct MemMergeNode : FixedOpcodeNode<Opcode::MemMerge> {
MemMergeNode(Function& fn, Type* memoryType, const List<Node*>& controlStores);
};

// clang-format off
// isa/dyn_cast are null-safe
// cast is an unchecked static_cast and must only be used when the opcode is already known
Expand All @@ -459,7 +455,7 @@ namespace rat {
B32 isControlNode(const Node* n);
// the state a load, store, call, asm or stack op reads, else null
Node* memoryInput(const Node* n);
// same kind and payload with null inputs, null for Start, Stop, Return and MemMerge
// same kind and payload with null inputs, null for Start, Stop and Return
Node* cloneShell(Function& into, const Node* n);
// a Binary, Unary, Compare, Convert or Select node of op on its inputs, null for other ops
Node* createArith(Function& fn, Opcode op, Type* type, Node* const* inputs);
Expand Down
3 changes: 1 addition & 2 deletions src/backend/ir/opcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ namespace rat {
{"pack", 0, 0, 0, -1, 1, -1, OpClass::None},
{"shuffle", 0, 0, 0, -1, 2, 2, OpClass::None},
{"select", 0, 0, 0, -1, 3, 3, OpClass::None},
{"memmerge", 0, 0, 0, 0, 2, -1, OpClass::None},
};
static_assert(std::size(kOpcodeInfo) == (U32)Opcode::MemMerge + 1, "one per Opcode");
static_assert(std::size(kOpcodeInfo) == (U32)Opcode::Select + 1, "one per Opcode");
} // namespace detail

const OpcodeInfo& getOpcodeInfo(Opcode op) { return detail::kOpcodeInfo[(U32)op]; }
Expand Down
3 changes: 0 additions & 3 deletions src/backend/ir/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ namespace rat {

// branch-free choice
Select,

// join of the states of unordered disjoint stores
MemMerge,
};

enum class OpClass : U8 {
Expand Down
58 changes: 43 additions & 15 deletions src/backend/pass/opt/load_canon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "analysis/alias_analysis.h"
#include "ir/function.h"
#include "ir/node.h"
#include "ir/type.h"
#include "target/target.h"

namespace rat {
Expand All @@ -11,8 +12,35 @@ namespace rat {
chains(collectChains(fn)),
addrs(AliasAnalysis::restrictFacts(fn), fn.idBound()) {}

List<SlpPackPass::Chain> LoadCanonPass::collectChains(Function& fn) {
List<SlpPackPass::Chain> chains;
U64 LoadCanonPass::pairKey(U32 high, I64 low) { return (U64)high << 32 | (U32)low; }

// the end of the 16-byte block at from, or end when it comes first
I64 LoadCanonPass::blockEnd(I64 from, I64 end) {
return std::min(end, (from | (I64)(kVecBytes - 1)) + 1);
}

LoadCanonPass::Access LoadCanonPass::accessOf(AddrCache& addrs, U32 ptrBytes, Node* n) {
Type* type = n->getType();
if(StoreNode* st = dyn_cast<StoreNode>(n))
type = st->getValue()->getType();
Access access{addrs.get(n->getInput(2)), type->byteSize(ptrBytes)}; // input 2 is the pointer
I64 offset = access.addr.offset;
if(!access.size || access.size > kVecBytes || offset > kMaxOffset || offset < -kMaxOffset)
access.addr = Addr{};
return access;
}

// a load reads the state after a store of chain but the last
B32 LoadCanonPass::hasInnerReader(const Chain& chain) {
for(U32 k = 0; k + 1 < chain.size(); ++k)
for(Node* u : chain[k]->getUsers())
if(isa<LoadNode>(u))
return true;
return false;
}

List<LoadCanonPass::Chain> LoadCanonPass::collectChains(Function& fn) {
List<Chain> chains;
for(Node* n : fn) {
StoreNode* st = dyn_cast<StoreNode>(n);
if(!st || st->isVolatile())
Expand Down Expand Up @@ -44,19 +72,19 @@ namespace rat {
// a reader of the state after store k, not the last, reads the state after store target < k,
// the later of its last possible overlap and the latest state its cone reads
void LoadCanonPass::hoistLoads(Handoff& h, U32 ptrBytes, U32 chainIndex, List<U32>& stateOf) {
const SlpPackPass::Chain& chain = h.chains[chainIndex];
const Chain& chain = h.chains[chainIndex];
Node* block = chain[0]->getControl();
Conflicts seen;
for(U32 k = 1; k <= chain.size(); ++k)
stateOf[chain[k - 1]->getId()] = k;
for(U32 k = 1; k < chain.size(); ++k) { // a hoist of a final state reader only adds an order
seen.add(k, SlpPackPass::accessOf(h.addrs, ptrBytes, chain[k - 1]));
seen.add(k, accessOf(h.addrs, ptrBytes, chain[k - 1]));
SmallList<LoadNode*, 8> readers; // copy first, a hoist changes the store's users
for(Node* u : chain[k - 1]->getUsers())
if(LoadNode* l = dyn_cast<LoadNode>(u); l && l->getControl() == block && !l->isVolatile())
readers.push_back(l);
for(LoadNode* l : readers) {
SlpPackPass::Access access = SlpPackPass::accessOf(h.addrs, ptrBytes, l);
Access access = accessOf(h.addrs, ptrBytes, l);
if(!access.addr.group)
continue;
U32 target = std::max(seen.lastConflict(access), coneState(l, stateOf));
Expand Down Expand Up @@ -96,7 +124,7 @@ namespace rat {
return latest;
}

void LoadCanonPass::Conflicts::add(U32 store, const SlpPackPass::Access& a) {
void LoadCanonPass::Conflicts::add(U32 store, const Access& a) {
all.add(store, a.addr.group);
if(!a.addr.object)
lastWild = store;
Expand All @@ -106,27 +134,27 @@ namespace rat {
return;
I64 end = a.addr.offset + (I64)a.size;
for(I64 from = a.addr.offset; from < end;) { // each 16-byte block
I64 to = SlpPackPass::blockEnd(from, end);
U64 blockKey = SlpPackPass::pairKey(a.addr.group, from >> SlpPackPass::kVecShift);
std::array<U32, SlpPackPass::kVecBytes>& writers = blocks[blockKey];
I64 to = blockEnd(from, end);
U64 blockKey = pairKey(a.addr.group, from >> kVecShift);
std::array<U32, kVecBytes>& writers = blocks[blockKey];
for(I64 byte = from; byte < to; ++byte)
writers[byte & (SlpPackPass::kVecBytes - 1)] = store;
writers[byte & (kVecBytes - 1)] = store;
from = to;
}
}

U32 LoadCanonPass::Conflicts::lastConflict(const SlpPackPass::Access& a) const {
U32 LoadCanonPass::Conflicts::lastConflict(const Access& a) const {
U32 conflict = lastWild;
if(!a.addr.object)
conflict = all.outside(a.addr.group);
else if(auto it = objects.find(a.addr.object); it != objects.end())
conflict = std::max(conflict, it->second.outside(a.addr.group));
I64 end = a.addr.offset + (I64)a.size;
for(I64 from = a.addr.offset; from < end;) { // each 16-byte block
I64 to = SlpPackPass::blockEnd(from, end);
auto it = blocks.find(SlpPackPass::pairKey(a.addr.group, from >> SlpPackPass::kVecShift));
I64 to = blockEnd(from, end);
auto it = blocks.find(pairKey(a.addr.group, from >> kVecShift));
for(I64 byte = from; it != blocks.end() && byte < to; ++byte)
conflict = std::max(conflict, it->second[byte & (SlpPackPass::kVecBytes - 1)]);
conflict = std::max(conflict, it->second[byte & (kVecBytes - 1)]);
from = to;
}
return conflict;
Expand Down Expand Up @@ -169,7 +197,7 @@ namespace rat {
h = Handoff(fn);
List<U32> stateOf; // node id -> store number in the walked chain, sized on first use
for(U32 c = 0; c < h.chains.size(); ++c)
if(SlpPackPass::hasInnerReader(h.chains[c].data(), (U32)h.chains[c].size())) {
if(hasInnerReader(h.chains[c])) {
stateOf.resize(fn.idBound(), 0);
hoistLoads(h, target.getPointerSizeInBytes(), c, stateOf);
}
Expand Down
30 changes: 24 additions & 6 deletions src/backend/pass/opt/load_canon.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@

#include "core.h"

#include "pass/opt/slp/slp_pack.h"
#include "analysis/address.h"
#include "pass/pass.h"

#include <array>

namespace rat {
struct LoadNode;
struct StoreNode;

struct LoadCanonPass : FunctionPass {
static constexpr U32 kMaxCone = 64; // address cone nodes visited per load
static constexpr U32 kVecShift = 4;
static constexpr U32 kVecBytes = 1u << kVecShift; // bytes of a Conflicts block
// accessOf drops a larger offset, so an offset and its neighbors fit the low half of a pairKey
static constexpr I64 kMaxOffset = INT32_MAX - (I64)kVecBytes;

using Chain = List<StoreNode*>; // in chain order

struct Access {
Addr addr;
U32 size = 0;
};

// a load loadcanon moved off an inner state of a chain
struct Hoist {
Expand All @@ -31,7 +45,7 @@ namespace rat {
public:
const Function* fn = nullptr;
U64 version = 0; // of fn when loadcanon finished
List<SlpPackPass::Chain> chains;
List<Chain> chains;
AddrCache addrs;
List<Hoist> hoists;
};
Expand All @@ -48,18 +62,22 @@ namespace rat {

// the stores of a chain so far, by the bytes they may overlap
struct Conflicts {
void add(U32 store, const SlpPackPass::Access& a);
U32 lastConflict(const SlpPackPass::Access& a) const; // the last store that may overlap a
void add(U32 store, const Access& a);
U32 lastConflict(const Access& a) const; // the last store that may overlap a
private:
LastOutside all; // every store
U32 lastWild = 0; // the last store without an object
Map<U32, LastOutside> objects; // object -> its stores
// (group, 16-byte block) -> last store per byte
Map<U64, std::array<U32, SlpPackPass::kVecBytes>> blocks;
Map<U64, std::array<U32, kVecBytes>> blocks;
};
public:
U32 runOnFunction(Function& fn, const TargetInfo& target) override;
static List<SlpPackPass::Chain> collectChains(Function& fn);
static U64 pairKey(U32 high, I64 low);
static I64 blockEnd(I64 from, I64 end);
static Access accessOf(AddrCache& addrs, U32 ptrBytes, Node* n);
static B32 hasInnerReader(const Chain& chain);
static List<Chain> collectChains(Function& fn);
static StoreNode* chainSuccessor(const StoreNode* prev);
static void hoistLoads(Handoff& h, U32 ptrBytes, U32 chainIndex, List<U32>& stateOf);
static U32 coneState(LoadNode* l, const List<U32>& stateOf);
Expand Down
Loading
Loading