diff --git a/README.md b/README.md index 64b3da99..4bebeee8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/backend/ir/node.cpp b/src/backend/ir/node.cpp index ae6abd40..0f9ff744 100644 --- a/src/backend/ir/node.cpp +++ b/src/backend/ir/node.cpp @@ -317,9 +317,6 @@ namespace rat { Node* StackRestoreNode::getSaved() const { return getInput(2); } - MemMergeNode::MemMergeNode(Function& fn, Type* memoryType, const List& controlStores) - : FixedOpcodeNode(fn, memoryType, controlStores) {} - SplatNode::SplatNode(Function& fn, Type* vecType, Node* scalar) : FixedOpcodeNode(fn, vecType, {scalar}) {} diff --git a/src/backend/ir/node.h b/src/backend/ir/node.h index 83f46e36..e37e995b 100644 --- a/src/backend/ir/node.h +++ b/src/backend/ir/node.h @@ -434,10 +434,6 @@ namespace rat { Node* getSaved() const; }; - struct MemMergeNode : FixedOpcodeNode { - MemMergeNode(Function& fn, Type* memoryType, const List& 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 @@ -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); diff --git a/src/backend/ir/opcode.cpp b/src/backend/ir/opcode.cpp index 8a007165..018aad4b 100644 --- a/src/backend/ir/opcode.cpp +++ b/src/backend/ir/opcode.cpp @@ -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]; } diff --git a/src/backend/ir/opcode.h b/src/backend/ir/opcode.h index ec52d8b6..df9b67af 100644 --- a/src/backend/ir/opcode.h +++ b/src/backend/ir/opcode.h @@ -107,9 +107,6 @@ namespace rat { // branch-free choice Select, - - // join of the states of unordered disjoint stores - MemMerge, }; enum class OpClass : U8 { diff --git a/src/backend/pass/opt/load_canon.cpp b/src/backend/pass/opt/load_canon.cpp index 3d204088..b7c60bbe 100644 --- a/src/backend/pass/opt/load_canon.cpp +++ b/src/backend/pass/opt/load_canon.cpp @@ -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 { @@ -11,8 +12,35 @@ namespace rat { chains(collectChains(fn)), addrs(AliasAnalysis::restrictFacts(fn), fn.idBound()) {} - List LoadCanonPass::collectChains(Function& fn) { - List 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(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(u)) + return true; + return false; + } + + List LoadCanonPass::collectChains(Function& fn) { + List chains; for(Node* n : fn) { StoreNode* st = dyn_cast(n); if(!st || st->isVolatile()) @@ -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& 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 readers; // copy first, a hoist changes the store's users for(Node* u : chain[k - 1]->getUsers()) if(LoadNode* l = dyn_cast(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)); @@ -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; @@ -106,16 +134,16 @@ 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& writers = blocks[blockKey]; + I64 to = blockEnd(from, end); + U64 blockKey = pairKey(a.addr.group, from >> kVecShift); + std::array& 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); @@ -123,10 +151,10 @@ namespace rat { 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; @@ -169,7 +197,7 @@ namespace rat { h = Handoff(fn); List 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); } diff --git a/src/backend/pass/opt/load_canon.h b/src/backend/pass/opt/load_canon.h index dc483d8b..722d1ee3 100644 --- a/src/backend/pass/opt/load_canon.h +++ b/src/backend/pass/opt/load_canon.h @@ -7,14 +7,28 @@ #include "core.h" -#include "pass/opt/slp/slp_pack.h" +#include "analysis/address.h" #include "pass/pass.h" #include 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; // in chain order + + struct Access { + Addr addr; + U32 size = 0; + }; // a load loadcanon moved off an inner state of a chain struct Hoist { @@ -31,7 +45,7 @@ namespace rat { public: const Function* fn = nullptr; U64 version = 0; // of fn when loadcanon finished - List chains; + List chains; AddrCache addrs; List hoists; }; @@ -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 objects; // object -> its stores // (group, 16-byte block) -> last store per byte - Map> blocks; + Map> blocks; }; public: U32 runOnFunction(Function& fn, const TargetInfo& target) override; - static List 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 collectChains(Function& fn); static StoreNode* chainSuccessor(const StoreNode* prev); static void hoistLoads(Handoff& h, U32 ptrBytes, U32 chainIndex, List& stateOf); static U32 coneState(LoadNode* l, const List& stateOf); diff --git a/src/backend/pass/opt/slp.cpp b/src/backend/pass/opt/slp.cpp new file mode 100644 index 00000000..1365fe36 --- /dev/null +++ b/src/backend/pass/opt/slp.cpp @@ -0,0 +1,568 @@ +#include "pass/opt/slp.h" + +#include "ir/function.h" +#include "ir/node.h" +#include "ir/type.h" +#include "pass/opt/load_canon.h" +#include "target/target.h" + +#include + +namespace rat { + namespace detail { + // type bits of a scalar + enum TypeBit : U8 { + kInt8 = 1, + kInt16 = 2, + kInt32 = 4, + kInt64 = 8, + kFloat32 = 16, + kFloat64 = 32, + kPtr = 64, + kInt32Int64 = kInt32 | kInt64, + kIntUpTo32 = kInt8 | kInt16 | kInt32, + kFloat = kFloat32 | kFloat64, + kElem = kInt32Int64 | kFloat | kPtr, // what a stored or gathered lane can be + }; + + constexpr U32 kValueInput = 3; // of a store + + struct LaneRule { + Opcode first; + Opcode last; + U8 laneTypes; // of a lane, of its operands for a compare + U8 inputTypes; // of input 0 of a convert, 0 for no check + }; + + // clang-format off + constexpr LaneRule kLaneRules[] = { + {Opcode::Add, Opcode::Sub, kInt32Int64, 0}, + {Opcode::Mul, Opcode::Mul, kInt32, 0}, // pmulld, i32 only + {Opcode::And, Opcode::LShr, kInt32Int64, 0}, // logic, shl, lshr + {Opcode::AShr, Opcode::AShr, kInt32, 0}, // psrad, no i64 sar + {Opcode::FAdd, Opcode::FDiv, kFloat, 0}, + {Opcode::FNeg, Opcode::FNeg, kFloat, 0}, + {Opcode::Eq, Opcode::Ne, kInt32Int64, 0}, // pcmpeqd/q + {Opcode::Slt, Opcode::Ule, kInt32, 0}, // pcmpgtd, pminud + {Opcode::FEq, Opcode::FGe, kFloat, 0}, // cmpps/pd + {Opcode::SExt, Opcode::ZExt, kInt32Int64, kIntUpTo32}, // pmovsx/zx + {Opcode::SIToFP, Opcode::SIToFP, kFloat, kInt32}, // cvtdq2ps/pd + {Opcode::FPToSI, Opcode::FPToSI, kInt32, kFloat}, // cvttps2dq/pd2dq + {Opcode::FPExt, Opcode::FPExt, kFloat64, kFloat32}, // cvtps2pd + {Opcode::FPTrunc, Opcode::FPTrunc, kFloat32, kFloat64}, // cvtpd2ps + {Opcode::Select, Opcode::Select, kInt32Int64, 0}, // blendv + }; + // clang-format on + } // namespace detail + + U8 SlpPass::typeBits(const Type* t) { + using namespace detail; + if(t->isPtr()) + return kPtr; + if(t->isFloat()) { + U32 width = t->getFloatWidth(); + return (width == 32) * kFloat32 | (width == 64) * kFloat64; + } + U8 narrow = t->isInt(8) * kInt8 | t->isInt(16) * kInt16; + return narrow | t->isInt(32) * kInt32 | t->isInt(64) * kInt64; + } + + void SlpPass::printStats(const Module&, std::ostream& os) const { + os << "slp: windows " << packedWindows; + os << " reductions " << packedReductions << "\n"; + } + + U32 SlpPass::runOnFunction(Function& function, const TargetInfo& target) { + fn = &function; + ptrBytes = target.getPointerSizeInBytes(); + LoadCanonPass::Handoff handoff = LoadCanonPass::takeHandoff(function); + addrs = std::move(handoff.addrs); + storedObjects.clear(); + for(const List& chain : handoff.chains) + for(StoreNode* st : chain) + storedObjects.insert(addrs.get(st->getPointer()).object); + U32 before = packedWindows + packedReductions; + List packed; // per chain + for(const List& chain : handoff.chains) + packed.push_back(packChain(chain)); + // a chain that packs nothing gets its loads back, else they could load long before their use + LoadCanonPass::unhoistLoads(handoff.hoists, packed); + function.eliminateDeadNodes(); + // a copy of the nodes, as a pack adds nodes + for(Node* n : List(function.begin(), function.end())) + packReduction(n); + // the replaced scalars and the vectors of rejected packs + function.eliminateDeadNodes(); + return packedWindows + packedReductions - before; + } + + // cut the chain into stretches + B32 SlpPass::packChain(const List& chain) { + U32 windowsBefore = packedWindows; + List stretch; + for(StoreNode* st : chain) { + U32 size = st->getValue()->getType()->byteSize(ptrBytes); + Addr addr = addrs.get(st->getPointer()); + StoreAccess access{st, addr, size}; + StoreNode* prev = stretch.empty() ? nullptr : stretch.back().store; + B32 prevUnread = !prev || prev->getUsers().size() == 1; + B32 joins = prevUnread && stretch.size() < kMaxStretch; + for(const StoreAccess& member : stretch) + joins = joins && disjoint(addr, size, member.addr, member.size); + if(!joins) + packStretch(stretch); + stretch.push_back(access); // AddrCache gives every store a group + } + packStretch(stretch); + return packedWindows != windowsBefore; + } + + // pack the windows of the stretch in address order, then empty it + void SlpPass::packStretch(List& stretch) { + vectors.clear(); // the packs of a stretch share vectors + vectorsOf = Map>(); + std::sort(stretch.begin(), stretch.end(), byAddress); + for(U32 first = 0; first < stretch.size();) { + const StoreAccess& low = stretch[first]; + Type* type = low.store->getValue()->getType(); + B32 elem = typeBits(type) & detail::kElem; + U32 maxCount = elem ? kVecBytes / low.size : 1; + maxCount = std::min(maxCount, (U32)stretch.size() - first); + U32 count = 1; + for(; count < maxCount; ++count) { + const Addr& addr = stretch[first + count].addr; + I64 nextOffset = low.addr.offset + count * low.size; + if(addr.group != low.addr.group || addr.offset != nextOffset) + break; + } + // an odd count rounds down to even, so a lone store tries nothing + U32 packedCount = packWindow(stretch.data() + first, count & ~1u); + first += std::max(packedCount, 1u); + } + stretch.clear(); + } + + B32 SlpPass::byAddress(const StoreAccess& a, const StoreAccess& b) { + if(a.addr.group != b.addr.group) + return a.addr.group < b.addr.group; + return a.addr.offset < b.addr.offset; + } + + U32 SlpPass::packWindow(const StoreAccess* window, U32 count) { + if(!count) + return 0; + Lanes values; + Set stores; + for(U32 k = 0; k < count; ++k) { + values.push_back(window[k].store->getValue()); + stores.insert(window[k].store); + } + List roots = tryPack({values}, std::move(stores), (I32)count - 1); + if(roots.empty()) + return packWindow(window, count - 2); + window[0].store->setInput(detail::kValueInput, roots[0]); + for(U32 k = 1; k < count; ++k) { + StoreNode* st = window[k].store; + st->replaceAllUsesWith(st->getMemory()); + st->clearInputs(); // a store with no control is dead + } + ++packedWindows; + return count; + } + + void SlpPass::packReduction(Node* root) { + Opcode op = root->getOpcode(); + Type* type = root->getType(); + if(!root->hasUsers()) + return; // a dead node is no root + if(!root->isCommutative()) + return; + if(!(typeBits(type) & detail::kInt32Int64) || !hasRule(root)) + return; + for(Node* u : root->getUsers()) + if(u->getOpcode() == op && u->getType() == type) + return; + Terms terms; + Terms steps; + flatten(root, terms, steps); + U32 laneCount = kVecBytes / type->byteSize(ptrBytes); + U32 vectorCount = (U32)terms.size() / laneCount; + // most roots have fewer terms than lanes, they return before the vector reset + if(!vectorCount) + return; + List tuples(vectorCount); + for(U32 i = 0; i < vectorCount * laneCount; ++i) + tuples[i / laneCount].push_back(terms[i]); + // scalar steps removed minus vector ops added. The fold is a shuffle and an op per round, then the extract + I32 removed = (I32)(vectorCount * (laneCount - 1)); + I32 gain = removed - 2 * (I32)(laneCount / 2) - 1; + vectors.clear(); + vectorsOf = Map>(); + List termVectors = tryPack(tuples, Set(steps.begin(), steps.end()), gain); + if(termVectors.empty()) + return; + Node* result = foldLanes(op, termVectors, type); + for(U32 i = vectorCount * laneCount; i < terms.size(); ++i) + result = fn->binary(op, result, terms[i]); + root->replaceAllUsesWith(result); + ++packedReductions; + } + + void SlpPass::flatten(Node* root, Terms& terms, Terms& steps) { + Opcode op = root->getOpcode(); + Type* type = root->getType(); + Terms todo = {root}; + while(!todo.empty()) { + Node* n = todo.back(); + todo.pop_back(); + B32 sameOp = n->getOpcode() == op && n->getType() == type; + B32 isStep = sameOp && (n == root || n->getUsers().size() == 1); + // a step adds two terms + B32 room = terms.size() + todo.size() + 2 <= kMaxTerms; + if(isStep && room) { + steps.push_back(n); + todo.push_back(n->getInput(0)); + todo.push_back(n->getInput(1)); + } else + terms.push_back(n); + } + std::reverse(terms.begin(), terms.end()); + } + + // the vectors combined by op, then their lanes folded by shuffles into lane 0 + Node* SlpPass::foldLanes(Opcode op, const List& termVectors, Type* scalarType) { + Node* folded = termVectors[0]; + for(U32 i = 1; i < termVectors.size(); ++i) + folded = fn->binary(op, folded, termVectors[i]); + Type* vecType = folded->getType(); + // kFoldSel[lanes / 4] holds the shuffle of each round, nibble i is the source of lane i + constexpr U16 kFoldSel[2][2] = {{0x01}, {0x1032, 0x2301}}; + for(U16 sel : kFoldSel[vecType->getVecLanes() / 4]) { + if(!sel) // 0 pads the 2-lane row + continue; + Node* shuffle = fn->create(vecType, folded, folded, sel); + folded = fn->binary(op, folded, shuffle); + } + return fn->create(scalarType, folded, 0); + } + + List SlpPass::tryPack(const List& tuples, Set dead, I32 gain) { + packStart = (U32)vectors.size(); + budget = kMaxOps * (U32)tuples.size(); + List roots; + for(const Lanes& tuple : tuples) + roots.push_back(vectorize(tuple)); + // users before their operands + for(U32 i = (U32)vectors.size(); i-- > packStart;) { + for(Node* n : vectors[i].lanes) + gain += markDead(n, dead); + gain -= vectors[i].cost; + } + B32 allPacked = !std::count(roots.begin(), roots.end(), nullptr); + if(gain >= kMinProfit && allPacked) + return roots; + rollback(packStart); + return {}; + } + + B32 SlpPass::markDead(Node* n, Set& dead) { + if(n->getUsers().size() > kMaxOps) + return false; + for(U32 i = 0; i < n->getUsers().size(); ++i) { + Node* u = n->getUsers()[i]; + B32 valueSlot = n->getUserSlot(i) == detail::kValueInput; + B32 copy = isa(n) && isa(u) && valueSlot; + if(!copy && !dead.count(u)) + return false; + } + return dead.insert(n).second; // counted once + } + + // the vector whose lane i is lanes[i], null when the lanes cannot be one vector + Node* SlpPass::vectorize(const Lanes& lanes) { + const List& sameLane0 = vectorsOf[lanes[0]]; + for(U32 k = (U32)sameLane0.size(); k-- > 0;) { // the newest first + const Vector& vec = vectors[sameLane0[k]]; + const Lanes& known = vec.lanes; + if(!std::equal(lanes.begin(), lanes.end(), known.begin(), known.end())) + continue; + if(sameLane0[k] >= packStart) + return vec.node; + // an earlier pack's vector joins this pack once, so its lanes count as dead here too + return addVector(lanes, vec.node, 0); + } + U32 bytes = lanes.size() * laneType(lanes[0])->byteSize(ptrBytes); + if(bytes > kVecBytes || bytes < 4) + return nullptr; + for(Node* lane : lanes) + if(lane->getType() != lanes[0]->getType()) + return nullptr; + if(Node* load = wideLoad(lanes)) + return load; + U32 vectorsBefore = (U32)vectors.size(); + if(Node* op = vectorOp(lanes)) + return op; + rollback(vectorsBefore); // the operand vectors of a failed op + return gather(lanes); + } + + Node* SlpPass::wideLoad(const Lanes& lanes) { + LoadNode* lane0 = dyn_cast(lanes[0]); + U16 sel = 0; + Lanes ordered; // the window in address order + if(!lane0 || !loadSlots(lanes, lane0, sel, ordered)) + return nullptr; + Type* scalarType = lane0->getType(); + U32 size = scalarType->byteSize(ptrBytes); + Type* type = fn->types().getVec(scalarType, lanes.size()); + B32 oneAddress = sel == 0; // every lane at element 0 + B32 permuted = !std::equal(lanes.begin(), lanes.end(), ordered.begin()); + if(size < 4 && (oneAddress || permuted)) + return nullptr; // x86 splats and shuffles only dword and qword lanes + Node* control = lane0->getControl(); + Node* state = lane0->getMemory(); + // a new load, so the scheduler puts it next to its use + if(oneAddress) { + Node* pointer = lane0->getPointer(); + Node* one = fn->create(scalarType, control, state, pointer); + return addVector(lanes, fn->create(type, one), 1); + } + // of the window start + Node* pointer = cast(ordered[0])->getPointer(); + if(forwardStalls(state, pointer, size * lanes.size())) + return nullptr; + // a shuffle of the wide load of the window, which passes the same checks + if(permuted) { + Node* wide = wideLoad(ordered); + Node* shuffle = fn->create(type, wide, wide, sel); + return addVector(lanes, shuffle, 1); + } + // one per address, type, state and control in a stretch + for(U32 i : vectorsOf[pointer]) { + LoadNode* load = dyn_cast(vectors[i].node); + if(!load || load->getPointer() != pointer || load->getType() != type) + continue; + if(load->getMemory() == state && load->getControl() == control) + return addVector(lanes, load, 0); // so its lanes count as dead + } + Node* wide = fn->create(type, control, state, pointer); + return addVector(lanes, wide, 1); + } + + B32 SlpPass::loadSlots(const Lanes& lanes, LoadNode* lane0, U16& sel, Lanes& ordered) { + U32 count = lanes.size(), frame = 2 * count - 1, start = count - 1; + U32 slots[kMaxLanes]; // the element of each lane + // by elementOf, lanes win over other loads of their bytes + Node* loads[2 * kMaxLanes - 1] = {}; + for(U32 k = 0; k < count; ++k) { + slots[k] = elementOf(lanes[k], lane0, count); + if(slots[k] >= frame) + return false; + start = std::min(start, slots[k]); + loads[slots[k]] = lanes[k]; + } + U32 holes = (U32)std::count(loads + start, loads + start + count, nullptr); + B32 oneAddress = holes == count - 1; + if(holes && !oneAddress) + for(Node* user : lane0->getMemory()->getUsers()) { + U32 at = elementOf(user, lane0, count); + if(at < frame && !loads[at] && user->hasUsers()) + loads[at] = user; + } + for(; !oneAddress; --start) { + if(!std::count(loads + start, loads + start + count, nullptr)) + break; + if(!start) + return false; // no full window + } + ordered.assign(loads + start, loads + start + count); + for(U32 k = 0; k < count; ++k) + sel |= (U16)((slots[k] - start) << (ShuffleNode::kSourceBits * k)); + // no lane above the window + return *std::max_element(slots, slots + count) - start < count; + } + + U32 SlpPass::elementOf(Node* n, LoadNode* lane0, U32 count) { + U32 frame = 2 * count - 1; + LoadNode* load = dyn_cast(n); + if(!load) + return frame; + Addr addr = addrs.get(load->getPointer()); + Addr addr0 = addrs.get(lane0->getPointer()); + if(!addr.group || addr.group != addr0.group) + return frame; + if(load->isVolatile() || load->getType() != lane0->getType()) + return frame; + B32 sameState = load->getMemory() == lane0->getMemory(); + if(!sameState || load->getControl() != lane0->getControl()) + return frame; + U32 size = lane0->getType()->byteSize(ptrBytes); + U64 above = (U64)(addr.offset - addr0.offset) + (count - 1) * size; + if(above % size || above / size >= frame) + return frame; + return (U32)(above / size); + } + + B32 SlpPass::forwardStalls(Node* state, Node* pointer, U32 bytes) { + Addr loaded = addrs.get(pointer); + for(U32 hops = 0; hops < kMaxHops; ++hops) { + PhiNode* phi = dyn_cast(state); + if(phi && phi->getRegion()->isLoopHeader()) { + state = phi->getValue(0); // the first iteration reads the entry state + continue; + } + StoreNode* st = dyn_cast(state); + if(!st) + return callWrites(state, loaded.object); + Addr addr = addrs.get(st->getPointer()); + U32 size = st->getValue()->getType()->byteSize(ptrBytes); + B32 startsBelow = addr.offset <= loaded.offset; + B32 endsAbove = addr.offset + size >= loaded.offset + bytes; + B32 holdsAll = startsBelow && endsAbove; + // an unproven overlap with another group is no stall + if(!disjoint(loaded, bytes, addr, size)) + return addr.group == loaded.group && !holdsAll; + state = st->getMemory(); + } + return false; + } + + B32 SlpPass::callWrites(Node* state, U32 object) { + ProjNode* proj = dyn_cast(state); + CallNode* call = proj ? dyn_cast(proj->getProducer()) : nullptr; + if(!call || !object || storedObjects.count(object)) + return false; + for(U32 i = 0; i < call->getArgCount(); ++i) { + Node* arg = call->getArg(i); + if(arg->getType()->isPtr() && addrs.get(arg).object == object) + return true; + } + return false; + } + + Node* SlpPass::vectorOp(const Lanes& lanes) { + Node* lane0 = lanes[0]; + Opcode op = lane0->getOpcode(); + Opcode alt = op; // the other opcode of a blend + for(Node* lane : lanes) + if(lane->getOpcode() != op) + alt = lane->getOpcode(); + B32 shift = isShiftOpcode(op); + B32 binary = isBinaryOpcode(op) && isBinaryOpcode(alt); + B32 blendable = binary && !shift && !isShiftOpcode(alt); + B32 fits = budget && (alt == op || blendable); + for(Node* lane : lanes) { + B32 sameOp = lane->getOpcode() == op || lane->getOpcode() == alt; + fits = fits && sameOp && hasRule(lane); + // SSE shifts every lane by one amount + fits = fits && (!shift || lane->getInput(1) == lane0->getInput(1)); + } + if(!fits) + return nullptr; + // SSE zeroes the lanes of a shift past the width + const ConstantNode* amount = nullptr; + if(shift) + amount = dyn_cast(lane0->getInput(1)); + if(amount && (U64)amount->getValue() >= lane0->getType()->getIntWidth()) + return nullptr; + --budget; + // a shift amount stays scalar + Node* inputs[3] = {nullptr, shift ? lane0->getInput(1) : nullptr}; + U32 operandCount = shift ? 1 : (U32)getOpcodeInfo(op).minInputs; + Type* type = fn->types().getVec(laneType(lane0), lanes.size()); + for(U32 k = 0; k < operandCount; ++k) { + Lanes operands; // input k of each lane + for(Node* lane : lanes) + operands.push_back(lane->getInput(k)); + inputs[k] = vectorize(operands); + if(!inputs[k]) + return nullptr; + if(op == Opcode::Select && inputs[0]->getType() != type) + return nullptr; // blendv needs a mask of the lane type + } + Node* node = createArith(*fn, op, type, inputs); + if(alt == op) { + I32 variableAmount = shift && !amount; // a variable amount moves to xmm + return addVector(lanes, node, 1 + variableAmount); + } + return blend(lanes, node, createArith(*fn, alt, type, inputs)); + } + + // lane i from other when it has other's opcode + Node* SlpPass::blend(const Lanes& lanes, Node* node, Node* other) { + U16 sel = 0; + B32 addSub = true; // the lanes subtract and add in turn, one addsubps + for(U32 i = 0; i < lanes.size(); ++i) { + Opcode laneOp = lanes[i]->getOpcode(); + U32 source = i + lanes.size() * (laneOp == other->getOpcode()); + sel |= (U16)(source << (ShuffleNode::kSourceBits * i)); + addSub &= laneOp == (i % 2 ? Opcode::FAdd : Opcode::FSub); + } + Node* shuffle = fn->create(node->getType(), node, other, sel); + // addsubps, else two ops and a blend + return addVector(lanes, shuffle, addSub ? 1 : 3); + } + + // a rule in kLaneRules fits the opcode and types of lane + B32 SlpPass::hasRule(const Node* lane) { + Opcode op = lane->getOpcode(); + const Node* typed = isCompareOpcode(op) ? lane->getInput(0) : lane; + U8 laneBits = typeBits(typed->getType()); + for(const detail::LaneRule& rule : detail::kLaneRules) { + if(op < rule.first || op > rule.last || !(laneBits & rule.laneTypes)) + continue; + U8 inputBits = typeBits(lane->getInput(0)->getType()); + return !rule.inputTypes || (inputBits & rule.inputTypes); + } + return false; + } + + // a compare lane is a mask as wide as its operands + Type* SlpPass::laneType(const Node* lane) { + if(!isCompareOpcode(lane->getOpcode())) + return lane->getType(); + U32 bytes = lane->getInput(0)->getType()->byteSize(ptrBytes); + return fn->types().getInt(8 * bytes); + } + + Node* SlpPass::gather(const Lanes& lanes) { + Node* lane0 = lanes[0]; + if(!(typeBits(lane0->getType()) & detail::kElem)) + return nullptr; + Type* type = fn->types().getVec(lane0->getType(), lanes.size()); + U32 copies = (U32)std::count(lanes.begin(), lanes.end(), lane0); + if(!isa(lane0) && copies == lanes.size()) + return addVector(lanes, fn->create(type, lane0), 1); + I32 variables = 0; + B32 poolLoad = isa(lane0); + for(Node* n : lanes) { + const ConstantNode* c = dyn_cast(n); + variables += !c; + poolLoad |= c && c->getValue() != 0; + } + List scalars(lanes.begin(), lanes.end()); + Node* pack = fn->create(type, scalars); + return addVector(lanes, pack, variables + poolLoad); + } + + Node* SlpPass::addVector(const Lanes& lanes, Node* node, I32 cost) { + U32 index = (U32)vectors.size(); + vectorsOf[lanes[0]].push_back(index); + // a wide load is also found by its pointer + if(LoadNode* load = dyn_cast(node)) + vectorsOf[load->getPointer()].push_back(index); + vectors.push_back({lanes, node, cost}); + return node; + } + + void SlpPass::rollback(U32 keep) { + for(U32 i = (U32)vectors.size(); i-- > keep;) { + vectorsOf[vectors[i].lanes[0]].pop_back(); + if(LoadNode* load = dyn_cast(vectors[i].node)) + vectorsOf[load->getPointer()].pop_back(); + if(vectors[i].cost) + vectors[i].node->clearInputs(); + } + vectors.resize(keep); + } +} // namespace rat diff --git a/src/backend/pass/opt/slp.h b/src/backend/pass/opt/slp.h new file mode 100644 index 00000000..f1e67694 --- /dev/null +++ b/src/backend/pass/opt/slp.h @@ -0,0 +1,95 @@ +// superword-level parallelism +// +// references: +// - S. Larsen and S. Amarasinghe, "Exploiting Superword Level Parallelism with +// Multimedia Instruction Sets", PLDI, 2000 +// - C. Click, "Global Code Motion / Global Value Numbering", PLDI, 1995 + +#ifndef RAT_PASS_OPT_SLP_H +#define RAT_PASS_OPT_SLP_H + +#include "core.h" + +#include "analysis/address.h" +#include "ir/opcode.h" +#include "pass/pass.h" + +namespace rat { + struct LoadNode; + struct StoreNode; + struct Type; + + struct SlpPass : FunctionPass { + static constexpr U32 kVecBytes = 16; // SSE + static constexpr U32 kMaxLanes = 4; + // stores of a stretch, compared pairwise + static constexpr U32 kMaxStretch = 64; + static constexpr U32 kMaxOps = 64; // op tuples tried per seed tuple + static constexpr U32 kMaxHops = 16; // stores walked above a wide load + static constexpr U32 kMaxTerms = 64; // terms of one reduction + static constexpr I32 kMinProfit = 2; // scalar ops a pack must save + + using Lanes = SmallList; + using Terms = SmallList; + + void printStats(const Module& module, std::ostream& os) const override; + U32 runOnFunction(Function& function, const TargetInfo& target) override; + private: + struct StoreAccess { + StoreNode* store = nullptr; + Addr addr; + U32 size = 0; + }; + + // a vector node and the scalar lanes it replaces + struct Vector { + Lanes lanes; + Node* node = nullptr; + I32 cost = 0; + }; + + // the type bit of a scalar type, 0 for others + static U8 typeBits(const Type* t); + + // stores + B32 packChain(const List& chain); + void packStretch(List& stretch); + static B32 byAddress(const StoreAccess& a, const StoreAccess& b); + U32 packWindow(const StoreAccess* window, U32 count); + + // reductions + void packReduction(Node* root); + static void flatten(Node* root, Terms& terms, Terms& steps); + Node* foldLanes(Opcode op, const List& termVectors, Type* scalarType); + + // vectors + List tryPack(const List& tuples, Set dead, I32 gain); + static B32 markDead(Node* n, Set& dead); + Node* vectorize(const Lanes& lanes); + Node* wideLoad(const Lanes& lanes); + B32 loadSlots(const Lanes& lanes, LoadNode* lane0, U16& sel, Lanes& ordered); + U32 elementOf(Node* n, LoadNode* lane0, U32 count); + B32 forwardStalls(Node* state, Node* pointer, U32 bytes); + B32 callWrites(Node* state, U32 object); + Node* vectorOp(const Lanes& lanes); + Node* blend(const Lanes& lanes, Node* node, Node* other); + static B32 hasRule(const Node* lane); + Type* laneType(const Node* lane); + Node* gather(const Lanes& lanes); + Node* addVector(const Lanes& lanes, Node* node, I32 cost); + void rollback(U32 keep); + private: + Function* fn = nullptr; + U32 ptrBytes = 8; + AddrCache addrs; + Set storedObjects; + List vectors; + Map> vectorsOf; + U32 packStart = 0; // first vector of the pack under work + U32 budget = 0; // op tuples left to try + U32 packedWindows = 0; + U32 packedReductions = 0; + }; +} // namespace rat + +#endif diff --git a/src/backend/pass/opt/slp/slp_apply.cpp b/src/backend/pass/opt/slp/slp_apply.cpp deleted file mode 100644 index ce2b904f..00000000 --- a/src/backend/pass/opt/slp/slp_apply.cpp +++ /dev/null @@ -1,282 +0,0 @@ -#include "pass/opt/slp/slp_pack.h" - -#include "ir/function.h" -#include "ir/node.h" -#include "ir/type.h" - -namespace rat { - void SlpPackPass::Slp::apply(Plan& plan) { - const Seed& seed = plan.seed; - emitter.seedAnchors(plan); - emitter.emitVectors(plan); - if(seed.root) { - emitter.commitReduction(plan); - ++stats.packedReduction; - } else if(plan.guards.empty()) { - emitter.commitRun(plan); - stats.packedUnguarded += (U32)seed.lowestStores.size(); - } else { - emitter.commitGuarded(plan, check); - stats.packedGuarded += (U32)seed.lowestStores.size(); - ++stats.guardedRuns; - stats.guardChecks += (U32)plan.guards.size(); - } - } - - SlpPackPass::Emitter::Emitter(Context& cx) - : cx(cx) {} - - // a new (control, group) takes the anchorOf base at its lowest offset in the plan, the first on a tie - void SlpPackPass::Emitter::seedAnchors(const Plan& plan) { - List> ptrs; - for(const Vector& vec : plan.vectors) - if(vec.kind == VectorKind::Load) - ptrs.push_back({vec.lowestLoad->getPointer(), vec.lowestLoad->getControl()}); - for(StoreNode* st : plan.seed.lowestStores) - ptrs.push_back({st->getPointer(), st->getControl()}); - Map> best; - for(const auto& [ptr, ctrl] : ptrs) { - Pair anchor = anchorOf(ptr); - U64 key = pairKey(ctrl->getId(), cx.addrs.get(ptr).group); - auto it = best.try_emplace(key, anchor).first; - if(anchor.second < it->second.second) - it->second = anchor; - } - anchors.insert(best.begin(), best.end()); - } - - // the base of ptr's constant adds when it shares ptr's group - Pair SlpPackPass::Emitter::anchorOf(Node* ptr) { - Node* base = ptr; - while(base->getOpcode() == Opcode::Add && isa(base->getInput(1))) { - if(!base->getInput(0)->getType()->isPtr()) - break; - base = base->getInput(0); - } - Addr ptrAddr = cx.addrs.get(ptr); - Addr baseAddr = cx.addrs.get(base); - if(baseAddr.group != ptrAddr.group) - return {ptr, ptrAddr.offset}; - return {base, baseAddr.offset}; - } - - // the address at offset in ptr's group, as its anchor plus a constant, so lowering folds it - Node* SlpPackPass::Emitter::anchorPtr(Node* ptr, I64 offset, const Node* ctrl) { - auto it = anchors.find(pairKey(ctrl->getId(), cx.addrs.get(ptr).group)); - assert(it != anchors.end() && "seedAnchors covers every pointer of the plan"); - auto [anchor, anchorOffset] = it->second; - if(offset == anchorOffset) - return anchor; - return cx.fn.add(anchor, cx.fn.constInt(cx.fn.types().getInt(64), offset - anchorOffset)); - } - - // plan order puts operands first, so an Op finds their nodes - void SlpPackPass::Emitter::emitVectors(Plan& plan) { - for(Vector& vec : plan.vectors) { - Type* vecTy = cx.fn.types().getVec(cx.laneType(vec.lanes[0]), vec.lanes.size()); - if(vec.kind == VectorKind::Load) - vec.node = emitLoad(vec, vecTy, plan); - else if(vec.kind == VectorKind::Op) - vec.node = emitOp(vec, vecTy, plan); - else if(isSplat(vec.lanes)) - vec.node = cx.fn.create(vecTy, vec.lanes[0]); - else - vec.node = cx.fn.create(vecTy, List(vec.lanes.begin(), vec.lanes.end())); - } - } - - Node* SlpPackPass::Emitter::emitLoad(const Vector& vec, Type* vecTy, Plan& plan) { - if(!vec.wideLoads) { - Node* load = loadAt(vec, vec.lowestLoad->getType(), vec.offset, plan); - return cx.fn.create(vecTy, load); - } - Node* low = loadAt(vec, vecTy, vec.offset, plan); - if(!vec.permuted) - return low; - Node* high = low; - if(vec.wideLoads == 2) - high = loadAt(vec, vecTy, vec.offset + (I64)kVecBytes, plan); - return cx.fn.create(vecTy, low, high, vec.sel); - } - - // a load of type ty at offset of vec's group from vec.mem, or an earlier vector's equal load - Node* SlpPackPass::Emitter::loadAt(const Vector& vec, Type* ty, I64 offset, Plan& plan) { - Node* control = vec.lowestLoad->getControl(); - Addr addr = cx.addrs.get(vec.lowestLoad->getPointer()); - addr.offset = offset; - for(const auto& [ld, ldAddr] : plan.loads) { - B32 sameAccess = ld->getType() == ty && ldAddr.group == addr.group && ldAddr.offset == offset; - if(sameAccess && ld->getMemory() == vec.mem && ld->getControl() == control) - return ld; - } - Node* ptr = anchorPtr(vec.lowestLoad->getPointer(), offset, control); - LoadNode* ld = cx.fn.create(ty, control, vec.mem, ptr); - plan.loads.push_back({ld, addr}); - return ld; - } - - // mainOp over the operand vectors, or mainOp and altOp blended by sel - Node* SlpPackPass::Emitter::emitOp(const Vector& vec, Type* vecTy, const Plan& plan) { - Node* inputs[kMaxInputs] = {}; - for(U32 i = 0; i < kMaxInputs; ++i) - if(vec.operands[i] != kNoVector) - inputs[i] = plan.vectors[vec.operands[i]].node; - if(vec.shiftAmount) - inputs[1] = vec.shiftAmount; - Node* result = createArith(cx.fn, vec.mainOp, vecTy, inputs); - if(vec.altOp == kNoOp) - return result; - Node* alt = createArith(cx.fn, vec.altOp, vecTy, inputs); - return cx.fn.create(vecTy, result, alt, vec.sel); - } - - void SlpPackPass::Emitter::commitRun(const Plan& plan) { - const Seed& seed = plan.seed; - Node* wide = emitWideStores(plan, seed.stores[0]->getControl()); - if(MemMergeNode* set = setOf(seed)) { - U32 firstSlot = ~0u; - for(StoreNode* st : seed.stores) { - assert(st->getUsers().size() == 1 && "a set member's only user is its set"); - U32 slot = st->getUserSlot(0); - firstSlot = std::min(firstSlot, slot); - set->setInput(slot, nullptr); - } - set->setInput(firstSlot, wide); - } else - seed.stores.back()->replaceAllUsesWith(wide); - for(StoreNode* st : seed.stores) - st->clearInputs(); // dead without inputs, the sweep in run erases it - } - - Node* SlpPackPass::Emitter::emitWideStores(const Plan& plan, Node* ctrl) { - const Seed& seed = plan.seed; - Node* state = memIn(seed); - for(U32 window = 0; window < seed.lowestStores.size(); ++window) { - StoreNode* lowest = seed.lowestStores[window]; - I64 offset = cx.addrs.get(lowest->getPointer()).offset; - Node* ptr = anchorPtr(lowest->getPointer(), offset, lowest->getControl()); - state = cx.fn.create(cx.fn.memTy(), ctrl, state, ptr, plan.tupleNode(window)); - } - return state; - } - - // the wide stores behind the guards, the scalar run in the else arm, a phi merges the states - void SlpPackPass::Emitter::commitGuarded(const Plan& plan, const Check& check) { - const Seed& seed = plan.seed; - assert(!setOf(seed) && "no load reads a set member, so no lane edge is weak"); - StoreNode* last = seed.stores.back(); - Node* block = last->getControl(); - Node* firstIf = nullptr; - Node* elseP = nullptr; - Node* thenP = emitGuardCascade(plan, firstIf, elseP); - // the new loads of memIn run in the vector arm, so nothing speculative runs if a check fails - for(const auto& [ld, addr] : plan.loads) - if(ld->getControl() == block && ld->getMemory() == memIn(seed)) - ld->setInput(0, thenP); - Node* wide = emitWideStores(plan, thenP); - Node* region = cx.fn.create(cx.fn.ctrlTy(), List{thenP, elseP}); - PhiNode* phi = cx.fn.create(cx.fn.memTy(), List{region, wide, last}); - last->replaceAllUsesWith(phi); - phi->setInput(2, last); // replaceAllUsesWith also rewired the phi's own else input - moveToElse(plan, block, elseP, check); - guardedRuns.push_back({block, seed.stores[0], phi, firstIf}); - ++cx.guardedRunsPerBlock[block]; - } - - // per load group, two branches test that its range ends before the run or starts after it - Node* SlpPackPass::Emitter::emitGuardCascade(const Plan& plan, Node*& firstIf, Node*& elseP) { - const Seed& seed = plan.seed; - Function& fn = cx.fn; - Type* ctrlTy = fn.ctrlTy(); - Type* iffTy = fn.types().getTuple({ctrlTy, ctrlTy}); - Node* runPtr = seed.lowestStores[0]->getPointer(); - U32 elemSize = seed.lowestStores[0]->getValue()->getType()->byteSize(cx.ptrBytes); - I64 runBytes = (I64)(seed.stores.size() * elemSize); - Node* runEnd = fn.add(runPtr, fn.constInt(fn.types().getInt(64), runBytes)); - Node* ctrl = seed.stores.back()->getControl(); - List fails; - for(const GuardRange& range : plan.guards) { - Node* rangeSize = fn.constInt(fn.types().getInt(64), range.end - range.begin); - Node* rangeEnd = fn.add(range.ptr, rangeSize); - Node* endsBefore = fn.compare(Opcode::Ule, rangeEnd, runPtr); - Node* before = fn.create(iffTy, ctrl, endsBefore); - Node* beforeThen = fn.create(ctrlTy, before, IfNode::thenProjIndex(), "slp.then"); - Node* beforeElse = fn.create(ctrlTy, before, IfNode::elseProjIndex(), "slp.chk"); - Node* startsAfter = fn.compare(Opcode::Ule, runEnd, range.ptr); - Node* after = fn.create(iffTy, beforeElse, startsAfter); - Node* afterThen = fn.create(ctrlTy, after, IfNode::thenProjIndex(), "slp.then"); - if(fails.empty()) - firstIf = before; - fails.push_back(fn.create(ctrlTy, after, IfNode::elseProjIndex(), "slp.else")); - ctrl = fn.create(ctrlTy, List{beforeThen, afterThen}); - } - elseP = fails[0]; - if(fails.size() > 1) - elseP = fn.create(ctrlTy, fails); - return ctrl; - } - - // the run's stores, and the lane loads of its states that die - void - SlpPackPass::Emitter::moveToElse(const Plan& plan, Node* block, Node* elseP, const Check& check) { - const Seed& seed = plan.seed; - for(StoreNode* st : seed.stores) - st->setInput(0, elseP); - for(const Vector& vec : plan.vectors) - if(vec.kind == VectorKind::Load) - for(Node* lane : vec.lanes) { - LoadNode* ld = cast(lane); - B32 runState = ld->getMemory() == memIn(seed) || isInner(seed, ld->getMemory()); - if(ld->getControl() == block && runState && check.laneDies(ld)) - ld->setInput(0, elseP); - } - } - - // the control users of a block, but its guard branches, move below its last guarded run - void SlpPackPass::Emitter::splitBlocks() { - Map runAt; // memIn -> guarded run - Set firstIfs; - for(U32 i = 0; i < guardedRuns.size(); ++i) { - runAt[guardedRuns[i].firstStore->getMemory()] = i; - firstIfs.insert(guardedRuns[i].firstIf); - } - for(const GuardedRun& guarded : guardedRuns) { - if(moveLaterUsers(guarded, runAt)) - continue; - // backward, as setInput swap-removes the user from the block's list - for(U32 k = guarded.block->getUsers().size(); k-- > 0;) { - Node* user = guarded.block->getUsers()[k]; - if(isControlNode(user) && !firstIfs.count(user)) - user->setInput(guarded.block->getUserSlot(k), guarded.phi->getRegion()); - } - } - } - - // the block's users of the merged state or a later one move below the merge region, so does a - // later guarded run of the block - B32 SlpPackPass::Emitter::moveLaterUsers(const GuardedRun& guarded, - const Map& runAt) { - Node* region = guarded.phi->getRegion(); - B32 foundLater = false; - List states = {guarded.phi}; - while(!states.empty()) { - Node* state = states.back(); - states.pop_back(); - auto it = runAt.find(state); - if(it != runAt.end() && guardedRuns[it->second].block == guarded.block) { - guardedRuns[it->second].firstIf->setInput(0, region); - foundLater = true; - } - for(Node* user : state->getUsers()) { - if(memoryInput(user) != state || user->getInput(0) != guarded.block) - continue; - user->setInput(0, region); - if(user->getType()->isMemory()) - states.push_back(user); - else if(ProjNode* mem = user->projection(CallNode::memoryProjIndex())) - states.push_back(mem); // a call or asm - } - } - return foundLater; - } -} // namespace rat diff --git a/src/backend/pass/opt/slp/slp_chain.cpp b/src/backend/pass/opt/slp/slp_chain.cpp deleted file mode 100644 index f1ce222b..00000000 --- a/src/backend/pass/opt/slp/slp_chain.cpp +++ /dev/null @@ -1,247 +0,0 @@ -#include "pass/opt/slp/slp_pack.h" - -#include "ir/function.h" -#include "ir/node.h" -#include "ir/type.h" - -namespace rat { - // a disjoint stretch becomes a set, the sets and the parts between are searched in chain order - U32 SlpPackPass::Slp::packChains(const List& chains, List& packed) { - U32 changed = 0; - packed.reserve(chains.size()); - Map written; - for(const Chain& chain : chains) { - U32 before = changed; - U32 partStart = 0; // first store of the part not searched yet - for(U32 first = 0; first < chain.size();) { - U32 last = first; // each store before last has the next store as its only user - while(last + 1 < chain.size() && chain[last]->getUsers().size() == 1) - ++last; - if(last != first && isDisjointSet(chain, first, last, written)) { - changed += packWindows(Chain(chain.begin() + partStart, chain.begin() + first)); - changed += packSet(chain, first, last); - partStart = last + 1; - } - first = last + 1; - } - changed += packWindows(Chain(chain.begin() + partStart, chain.end())); - packed.push_back(changed != before); - } - return changed; - } - - // chain[first..last] share no byte, in at most kMaxSetGroups groups with disjoint bases (F3) - B32 SlpPackPass::Slp::isDisjointSet(const Chain& chain, - U32 first, - U32 last, - Map& written) { - List groups; // address of each group's first store - written.clear(); - for(U32 i = first; i <= last; ++i) { - Access access = accessOf(cx.addrs, cx.ptrBytes, chain[i]); - const Addr& a = access.addr; - if(!a.group) - return false; - B32 seen = false; - for(const Addr& g : groups) { - if(g.group == a.group) - seen = true; - else if(!disjointBases(g, a)) - return false; // two groups may overlap - } - if(!seen) - groups.push_back(a); - if(groups.size() > kMaxSetGroups) - return false; - I64 end = a.offset + (I64)access.size; - for(I64 from = a.offset; from < end;) { // each 16-byte block - I64 to = blockEnd(from, end); - U16 bits = (U16)(((1u << (to - from)) - 1) << (from & (kVecBytes - 1))); - U16& mask = written[pairKey(a.group, from >> kVecShift)]; - if(mask & bits) - return false; // two stores share a byte - mask |= bits; - from = to; - } - } - return true; - } - - SlpPackPass::Access SlpPackPass::accessOf(AddrCache& addrs, U32 ptrBytes, Node* n) { - Type* type = n->getType(); - if(StoreNode* st = dyn_cast(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; - } - - // windows along a chain, or along consecutive stores of a set - U32 SlpPackPass::Slp::packWindows(const Chain& chain) { - U32 changed = 0; - for(U32 i = 0; i < chain.size();) { - U32 packedStores = packRunAt(chain, i, kVecBytes); - if(!packedStores) - packedStores = packRunAt(chain, i, kVecBytes / 2); // 2 x 32-bit - changed += packedStores != 0; - i += std::max(packedStores, 1u); - } - return changed; - } - - // the stores packed by the run at chain[start], 0 when it does not pack - U32 SlpPackPass::Slp::packRunAt(const Chain& chain, U32 start, U32 bytes) { - Window first; - if(!windowAt(chain, start, bytes, first)) - return 0; - ++stats.windowsSeen; - B32 observed = hasInnerReader(chain.data() + start, first.laneCount); - if(observed && cx.guards == SlpGuards::Off) { - ++stats.rejected; - return 0; - } - Seed seed; - addWindow(seed, chain, first); - // an observed window takes the next windows that continue its bytes, under one guard - Window next; - for(U32 windows = 1; observed && windows < kMaxRunWindows; ++windows) { - if(!windowAt(chain, start + windows * first.laneCount, bytes, next)) - break; - B32 sameShape = next.group == first.group && next.laneCount == first.laneCount; - if(!sameShape || next.offset != first.offset + (I64)(windows * bytes)) - break; - addWindow(seed, chain, next); - ++stats.windowsSeen; - } - if(!pack(seed)) - return 0; - return (U32)seed.stores.size(); - } - - // bytes / element size stores from chain[start] of one type and group that fill one span - B32 SlpPackPass::Slp::windowAt(const Chain& chain, U32 start, U32 bytes, Window& window) { - if(start >= chain.size()) - return false; - Type* type = chain[start]->getValue()->getType(); - if(!(typeBits(type) & Elem)) - return false; - U32 elemSize = type->byteSize(cx.ptrBytes); - U32 laneCount = bytes / elemSize; - if(laneCount < 2 || start + laneCount > chain.size()) - return false; - Addr firstAddr = cx.addrs.get(chain[start]->getPointer()); - I64 lowest = firstAddr.offset; - I64 offsets[kMaxLanes]; - for(U32 j = 0; j < laneCount; ++j) { - Addr a = cx.addrs.get(chain[start + j]->getPointer()); - if(chain[start + j]->getValue()->getType() != type || a.group != firstAddr.group) - return false; - offsets[j] = a.offset; - lowest = std::min(lowest, a.offset); - } - window = Window{start, laneCount, firstAddr.group, lowest}; - for(U32 j = 0; j < laneCount; ++j) { // the offsets fill the lanes above lowest once each - U64 delta = (U64)(offsets[j] - lowest); - U64 lane = delta / elemSize; - if(delta % elemSize != 0 || lane >= laneCount || window.laneStore[lane]) - return false; - window.laneStore[lane] = chain[start + j]; - } - return true; - } - - // a load reads the state after one of stores[0..count-2] - B32 SlpPackPass::hasInnerReader(StoreNode* const* stores, U32 count) { - for(U32 k = 0; k + 1 < count; ++k) - for(Node* u : stores[k]->getUsers()) - if(isa(u)) - return true; - return false; - } - - void SlpPackPass::addWindow(Seed& seed, const Chain& chain, const Window& window) { - seed.lowestStores.push_back(window.laneStore[0]); - Lanes& tuple = seed.tuples.emplace_back(); - for(U32 lane = 0; lane < window.laneCount; ++lane) { - seed.stores.push_back(chain[window.start + lane]); - tuple.push_back(window.laneStore[lane]->getValue()); - } - seed.profit += (I32)window.laneCount - 1; // the fused store - } - - // chain[first..last] as a set, searched by offset, then lowered back to a chain - U32 SlpPackPass::Slp::packSet(const Chain& chain, U32 first, U32 last) { - MemMergeNode* set = mergeStretch(chain, first, last); - Map> members; // (group, offset) -> member and its value type - for(U32 i = 1; i < set->getInputCount(); ++i) { - StoreNode* st = cast(set->getInput(i)); - Addr addr = cx.addrs.get(st->getPointer()); - members[pairKey(addr.group, addr.offset)] = {st, st->getValue()->getType()}; - } - U32 changed = 0; - Chain consecutive; - for(U32 i = 1; i < set->getInputCount(); ++i) { - StoreNode* st = dyn_cast(set->getInput(i)); - if(!st || !(typeBits(st->getValue()->getType()) & Elem)) - continue; // a slot a pack emptied, or a narrow or vector store - Type* type = st->getValue()->getType(); - I64 elemSize = type->byteSize(cx.ptrBytes); - Addr addr = cx.addrs.get(st->getPointer()); - auto below = members.find(pairKey(addr.group, addr.offset - elemSize)); - if(below != members.end() && below->second.second == type) - continue; // not the lowest of its consecutive stores - consecutive.clear(); - for(I64 offset = addr.offset;; offset += elemSize) { - auto it = members.find(pairKey(addr.group, offset)); - if(it == members.end() || it->second.second != type) - break; // a gap or another type ends them - consecutive.push_back(it->second.first); - } - changed += packWindows(consecutive); - } - lowerSet(set); // later parts and chains walk its stores as plain stores - return changed; - } - - // every store of the stretch reads the state before the stretch, a MemMerge joins them - MemMergeNode* SlpPackPass::Slp::mergeStretch(const Chain& chain, U32 first, U32 last) { - Node* before = chain[first]->getMemory(); - StoreNode* lastStore = chain[last]; - List inputs = {lastStore->getControl()}; - inputs.insert(inputs.end(), chain.begin() + first, chain.begin() + last + 1); - // made even when nothing packs, dropping it shifts the ids of later nodes - MemMergeNode* set = cx.fn.create(cx.fn.memTy(), inputs); - lastStore->replaceAllUsesWith(set); // the readers of the final state - set->setInput(last - first + 1, lastStore); // replaceAllUsesWith also rewired set's own input - for(U32 i = first + 1; i <= last; ++i) - chain[i]->setInput(1, before); - return set; - } - - void SlpPackPass::lowerSet(MemMergeNode* set) { - Node* prev = nullptr; - for(U32 i = 1; i < set->getInputCount(); ++i) - if(Node* st = set->getInput(i)) { // a slot a pack emptied is null - if(prev) - st->setInput(1, prev); - prev = st; - } - set->replaceAllUsesWith(prev); - set->clearInputs(); - } - - // the state before a run, valid until commitRun clears the stores - Node* SlpPackPass::memIn(const Seed& seed) { return seed.stores[0]->getMemory(); } - - // state is a run store but the last. Only runs ask, so stores is not empty - B32 SlpPackPass::isInner(const Seed& seed, const Node* state) { - return std::count(seed.stores.begin(), seed.stores.end() - 1, state) != 0; - } - - // the set of a run of set members, their only user, or null for a chain run - MemMergeNode* SlpPackPass::setOf(const Seed& seed) { - return dyn_cast(seed.stores[0]->getUsers()[0]); - } -} // namespace rat diff --git a/src/backend/pass/opt/slp/slp_load.cpp b/src/backend/pass/opt/slp/slp_load.cpp deleted file mode 100644 index ca2869bf..00000000 --- a/src/backend/pass/opt/slp/slp_load.cpp +++ /dev/null @@ -1,229 +0,0 @@ -#include "pass/opt/slp/slp_pack.h" - -#include "ir/function.h" -#include "ir/node.h" -#include "ir/type.h" - -namespace rat { - U32 SlpPackPass::Plan::planLoad(const Lanes& lanes) { - Vector vec{lanes, VectorKind::Load}; - if(!loadShape(lanes, vec) || !loadState(lanes, vec) || forwardBlocked(vec)) - return kNoVector; - I32 laneCount = (I32)lanes.size(); - vec.profit = laneCount - 2; // one load and a broadcast - if(vec.wideLoads) // wide loads and a permute - vec.profit = laneCount - (I32)vec.wideLoads - vec.permuted; - vectors.push_back(vec); - return (U32)vectors.size() - 1; - } - - B32 SlpPackPass::Plan::loadShape(const Lanes& lanes, Vector& vec) { - LoadNode* firstLoad = dyn_cast(lanes[0]); - if(!firstLoad) - return false; - U32 laneCount = lanes.size(); - U32 laneSize = firstLoad->getType()->byteSize(cx.ptrBytes); - Addr firstAddr = cx.addrs.get(firstLoad->getPointer()); - // i8 and i16 lanes, the source of a convert, load only adjacent - B32 elem = (typeBits(firstLoad->getType()) & Elem) != 0; - B32 adjacent = true; - B32 equal = elem; - vec.end = firstAddr.offset + (I64)laneSize; - vec.lowestLoad = firstLoad; - vec.offset = firstAddr.offset; - for(U32 i = 0; i < laneCount; ++i) { - LoadNode* l = dyn_cast(lanes[i]); - if(!l || l->isVolatile() || l->getControl() != firstLoad->getControl()) - return false; - Addr a = cx.addrs.get(l->getPointer()); - if(a.group != firstAddr.group) - return false; - adjacent &= a.offset == firstAddr.offset + (I64)(i * laneSize); - equal &= a.offset == firstAddr.offset; - if(a.offset < vec.offset) - vec.lowestLoad = l; - vec.offset = std::min(vec.offset, a.offset); - vec.end = std::max(vec.end, a.offset + (I64)laneSize); - } - if(!equal) // else one load and a broadcast - vec.wideLoads = 1 + (vec.end - vec.offset > (I64)(laneCount * laneSize)); - vec.permuted = !adjacent && !equal; - return adjacent || equal || (elem && permuteSpan(lanes, vec)); - } - - // some load at ctrl reads each element no lane reads, so the wide loads add no fault - B32 SlpPackPass::Plan::permuteSpan(const Lanes& lanes, Vector& vec) { - U32 laneCount = lanes.size(); - U32 laneSize = vec.lowestLoad->getType()->byteSize(cx.ptrBytes); - U32 group = cx.addrs.get(vec.lowestLoad->getPointer()).group; - Node* ctrl = vec.lowestLoad->getControl(); - I64 bytes = (I64)(vec.wideLoads * laneCount * laneSize); - // two loads must be full vectors, as the second starts kVecBytes higher - if(vec.end - vec.offset > bytes || (vec.wideLoads == 2 && laneCount * laneSize != kVecBytes)) - return false; - for(I64 start : {vec.offset, vec.end - bytes}) { - U32 readElements = 0; // bit per element - vec.sel = 0; - for(U32 i = 0; i < laneCount; ++i) { - I64 delta = cx.addrs.get(cast(lanes[i])->getPointer()).offset - start; - if(delta % laneSize) - return false; - readElements |= 1u << (delta / laneSize); - vec.sel |= (U16)(delta / laneSize << (ShuffleNode::kSourceBits * i)); - } - B32 covered = true; - for(U32 element = 0; element < vec.wideLoads * laneCount; ++element) - if(!(readElements >> element & 1)) - covered &= cx.bytesRead(group, start + (I64)(element * laneSize), laneSize, ctrl); - if(covered) { - vec.offset = start; - return true; - } - } - return false; - } - - // some load at ctrl reads each 4-byte piece of [offset, offset + size) of group - B32 SlpPackPass::Context::bytesRead(U32 group, I64 offset, U32 size, const Node* ctrl) { - if(!readsIndexed) - indexReads(); - auto it = reads.find(pairKey(ctrl->getId(), group)); - for(U32 piece = 0; piece < size; piece += kReadPiece) - if(it == reads.end() || !it->second.count(offset + (I64)piece)) - return false; - return true; - } - - void SlpPackPass::Context::indexReads() { - readsIndexed = true; - for(Node* n : fn) { - LoadNode* load = dyn_cast(n); - if(!load || load->isVolatile()) - continue; - U32 size = load->getType()->byteSize(ptrBytes); - if(!size || size % kReadPiece != 0) - continue; - Addr addr = addrs.get(load->getPointer()); - Set& pieces = reads[pairKey(load->getControl()->getId(), addr.group)]; - for(U32 piece = 0; piece < size; piece += kReadPiece) - pieces.insert(addr.offset + (I64)piece); - } - } - - // the state the new loads read, the lanes' shared state, or memIn when each lane may read it - B32 SlpPackPass::Plan::loadState(const Lanes& lanes, Vector& vec) { - B32 shared = true; - vec.mem = vec.lowestLoad->getMemory(); - for(Node* l : lanes) - shared &= cast(l)->getMemory() == vec.mem; - if(seed.root) - return shared; // a reduction has no memIn and adds no guard - // in a block with a guarded run the new loads read memIn, as loads of an earlier state would - // stay live across that guard - if(shared && !cx.guardedRunsIn(vec.lowestLoad->getControl()) && !isInner(seed, vec.mem)) - return true; - vec.mem = memIn(seed); - Addr runAddr = cx.addrs.get(seed.lowestStores[0]->getPointer()); - for(Node* l : lanes) - if(!canReadMemIn(cast(l), runAddr, vec.needsGuard)) - return false; - return true; - } - - // l may read memIn when its edge to the run is none or weak, a weak edge sets needsGuard - B32 SlpPackPass::Plan::canReadMemIn(LoadNode* l, const Addr& runAddr, B32& needsGuard) { - Addr loadAddr = cx.addrs.get(l->getPointer()); - B32 inner = isInner(seed, l->getMemory()); - // only run stores lie between, all in the run's group, so the bases decide - if(inner && loadAddr.group != runAddr.group) { - needsGuard |= !disjointBases(loadAddr, runAddr); - return true; - } - Node* later = memIn(seed); - Node* earlier = l->getMemory(); - if(inner) - std::swap(later, earlier); - U32 size = l->getType()->byteSize(cx.ptrBytes); - for(U32 hops = 0; later != earlier; ++hops) { - StoreNode* st = dyn_cast(later); - if(!st || hops >= kMaxHops || st->isVolatile() || st->getControl() != l->getControl()) - return false; - Addr storeAddr = cx.addrs.get(st->getPointer()); - if(!disjoint(loadAddr, size, storeAddr, st->getValue()->getType()->byteSize(cx.ptrBytes))) - return false; - later = st->getMemory(); - } - return true; - } - - // a wide load forwards only from one store with all its bytes, else stalls - B32 SlpPackPass::Plan::forwardBlocked(const Vector& vec) { - U32 bytes = (U32)vec.lanes.size() * vec.lowestLoad->getType()->byteSize(cx.ptrBytes); - Access load{cx.addrs.get(vec.lowestLoad->getPointer()), bytes}; - for(U32 k = 0; k < vec.wideLoads; ++k) { - load.addr.offset = vec.offset + (I64)(k * bytes); - SmallList walk = {vec.mem}; - for(U32 i = 0; i < walk.size() && i < kMaxHops; ++i) - if(splitsLoad(walk[i], load, walk)) - return true; - } - return false; - } - - // state writes part of load or is a call that writes it, else walk takes the states before it - B32 SlpPackPass::Plan::splitsLoad(Node* state, - const Access& load, - SmallList& walk) { - if(PhiNode* phi = dyn_cast(state)) { - // only a loop header's entry, the walk cannot compare the previous iteration's addresses - U32 count = phi->getRegion()->isLoopHeader() ? 1 : phi->getValueCount(); - for(U32 i = 0; i < count; ++i) - walk.push_back(phi->getValue(i)); - return false; - } - ProjNode* proj = dyn_cast(state); - if(proj && isa(proj->getProducer())) - return callWrites(cast(proj->getProducer()), load.addr); - StoreNode* st = dyn_cast(state); - if(!st) - return false; // the entry state or another writer, taken as old - Access store = accessOf(cx.addrs, cx.ptrBytes, st); - if(disjoint(load.addr, load.size, store.addr, store.size)) { - walk.push_back(st->getMemory()); - return false; - } - // a store of the group splits the load unless it holds all its bytes, a store of another - // group that may overlap ends the walk - I64 loadEnd = load.addr.offset + (I64)load.size; - I64 storeEnd = store.addr.offset + (I64)store.size; - B32 holdsAll = store.addr.offset <= load.addr.offset && storeEnd >= loadEnd; - return store.addr.group == load.addr.group && !holdsAll; - } - - // call takes the address of an object no store of fn writes, an out parameter or a struct return - B32 SlpPackPass::Plan::callWrites(const CallNode* call, const Addr& addr) { - if(!addr.object || cx.storesTo(addr.object)) - return false; - for(U32 i = 0; i < call->getArgCount(); ++i) { - Node* arg = call->getArg(i); - if(arg->getType()->isPtr() && cx.addrs.get(arg).object == addr.object) - return true; - } - return false; - } - - B32 SlpPackPass::Context::storesTo(U32 object) { - if(!storesIndexed) - indexStores(); - return storedObjects.count(object) != 0; - } - - void SlpPackPass::Context::indexStores() { - storesIndexed = true; - for(Node* n : fn) { - StoreNode* st = dyn_cast(n); - if(st && st->getInputCount()) // a store commitRun packed has no inputs - storedObjects.insert(addrs.get(st->getPointer()).object); - } - } -} // namespace rat diff --git a/src/backend/pass/opt/slp/slp_pack.cpp b/src/backend/pass/opt/slp/slp_pack.cpp deleted file mode 100644 index 7d40d900..00000000 --- a/src/backend/pass/opt/slp/slp_pack.cpp +++ /dev/null @@ -1,200 +0,0 @@ -#include "pass/opt/slp/slp_pack.h" - -#include "ir/function.h" -#include "ir/module.h" -#include "ir/node.h" -#include "ir/type.h" -#include "pass/opt/load_canon.h" -#include "target/target.h" - -#include - -namespace rat { - namespace detail { - // kLane a Load or Op lane, kSink a run store or reduction step, 0 other nodes and kept lanes - enum Role : U8 { - kLane = 1, - kSink = 2 - }; - } // namespace detail - - U8 SlpPackPass::typeBits(const Type* t) { - if(!t || !(t->isInt() || t->isFloat() || t->isPtr())) - return 0; - if(t->isPtr()) - return Ptr; - if(t->isFloat()) - return (t->getFloatWidth() == 32) * Float32 | (t->getFloatWidth() == 64) * Float64; - U32 bits = t->getIntWidth(); - return (bits == 8) * Int8 | (bits == 16) * Int16 | (bits == 32) * Int32 | (bits == 64) * Int64; - } - - U64 SlpPackPass::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 SlpPackPass::blockEnd(I64 from, I64 end) { - return std::min(end, (from | (I64)(kVecBytes - 1)) + 1); - } - - SlpPackPass::Context::Context(Function& fn, const TargetInfo& target, SlpGuards guards) - : fn(fn), - ptrBytes(target.getPointerSizeInBytes()), - sse41(target.hasSse41()), - guards(guards) {} - - Type* SlpPackPass::Context::laneType(const Node* n) { - if(isCompareOpcode(n->getOpcode())) - return fn.types().getInt(8 * n->getInput(0)->getType()->byteSize(ptrBytes)); - return n->getType(); - } - - U32 SlpPackPass::Context::guardedRunsIn(const Node* block) const { - auto it = guardedRunsPerBlock.find(block); - return it == guardedRunsPerBlock.end() ? 0 : it->second; - } - - SlpPackPass::Slp::Slp(Function& fn, const TargetInfo& target, SlpStats& stats, SlpGuards guards) - : cx(fn, target, guards), - stats(stats), - check(fn), - emitter(cx) {} - - U32 SlpPackPass::Slp::run() { - LoadCanonPass::Handoff h = LoadCanonPass::takeHandoff(cx.fn); - cx.addrs = std::move(h.addrs); - List packed; // per chain - U32 changed = packChains(h.chains, packed); - LoadCanonPass::unhoistLoads(h.hoists, packed); - emitter.splitBlocks(); - cx.fn.eliminateDeadNodes(); // so every reduction root found is live - changed += packReductions(); - cx.fn.eliminateDeadNodes(); - return changed; - } - - B32 SlpPackPass::Slp::pack(const Seed& seed) { - Plan plan(cx, seed); - I32 margin = -1; - if(plan.build()) - margin = profitMargin(plan, cx.guards); - if(margin < 0) { - ++stats.rejected; - return false; - } - check.setRoles(plan, true); - // the first innerReadersDie is an early reject, kept lanes can only make it fail - B32 ok = check.innerReadersDie(seed) && (I32)check.markKeptLanes(plan) <= margin; - ok = ok && check.innerReadersDie(seed); - if(ok) - apply(plan); - check.setRoles(plan, false); - stats.rejected += !ok; - return ok; - } - - // the profit above kMinProfit per window, what kept lanes may cost - I32 SlpPackPass::profitMargin(const Plan& plan, SlpGuards guards) { - I32 windows = std::max(1, (I32)plan.seed.lowestStores.size()); // a reduction counts as one - I32 profit = plan.seed.profit - kMinProfit * windows; - for(const Vector& vec : plan.vectors) - profit += vec.profit; - if(!plan.guards.empty() && guards != SlpGuards::NoCost) - profit -= (I32)plan.guards.size() * kGuardCheckCost + kGuardBranchCost; - return profit; - } - - SlpPackPass::Check::Check(const Function& fn) - : fn(fn) {} - - void SlpPackPass::Check::setRoles(const Plan& plan, B32 on) { - if(roles.size() < fn.idBound()) - roles.resize(2 * fn.idBound(), 0); // room for the nodes later seeds make - U8 laneRole = (U8)(detail::kLane * on); - U8 sinkRole = (U8)(detail::kSink * on); - for(const Vector& vec : plan.vectors) - if(vec.kind != VectorKind::Gather) - for(Node* n : vec.lanes) - roles[n->getId()] = laneRole; - for(Node* n : plan.seed.stores) - roles[n->getId()] = sinkRole; - for(Node* n : plan.seed.steps) - roles[n->getId()] = sinkRole; - } - - // the wide stores make no inner state, so every load of one must die - B32 SlpPackPass::Check::innerReadersDie(const Seed& seed) const { - for(U32 j = 0; j + 1 < seed.stores.size(); ++j) - for(Node* u : seed.stores[j]->getUsers()) - if(isa(u) && !laneDies(u)) - return false; - return true; - } - - B32 SlpPackPass::Check::laneDies(const Node* n) const { - return roleOf(n) == detail::kLane && !usedOutside(n, false); - } - - // a lane a gather, shift or outside user reads stays scalar with its lane inputs, returns count - U32 SlpPackPass::Check::markKeptLanes(const Plan& plan) { - SmallList work; - for(const Vector& vec : plan.vectors) { - for(Node* n : vec.lanes) - if(vec.kind == VectorKind::Gather || usedOutside(n, vec.kind == VectorKind::Load)) - work.push_back(n); - if(vec.shiftAmount) - work.push_back(vec.shiftAmount); - } - U32 kept = 0; - while(!work.empty()) { - const Node* n = work.back(); - work.pop_back(); - if(roleOf(n) != detail::kLane) - continue; - roles[n->getId()] = 0; - ++kept; - for(U32 i = 0; i < n->getInputCount(); ++i) - work.push_back(n->getInput(i)); - } - return kept; - } - - // n has a user besides the plan's Op lanes and sinks. With copies a store of n's value is - // inside even when it is no sink, so a Load lane only copied to memory is not kept - B32 SlpPackPass::Check::usedOutside(const Node* n, B32 copies) const { - for(U32 i = 0; i < n->getUsers().size(); ++i) { - const Node* user = n->getUsers()[i]; - // a load lane reads n as its scalar pointer - B32 inside = roleOf(user) != 0 && !isa(user); - if(isa(user)) // input 3 is the value - inside = n->getUserSlot(i) == 3 && (copies || roleOf(user) == detail::kSink); - if(!inside) - return true; - } - return false; - } - - U8 SlpPackPass::Check::roleOf(const Node* n) const { - return n->getId() < roles.size() ? roles[n->getId()] : 0; - } - - SlpPackPass::SlpPackPass(SlpGuards guards) - : guards(guards) {} - - void SlpPackPass::beginModule(Module&, const CallGraph&) { - stats = SlpStats{}; - LoadCanonPass::handoff() = {}; // a handoff from an earlier pipeline stage is stale - } - - void SlpPackPass::printStats(const Module&, std::ostream& os) const { - const SlpStats& s = stats; - U32 packed = s.packedUnguarded + s.packedGuarded; - os << "slp[module]: windows " << s.windowsSeen << " packed " << packed; - os << " (static " << s.packedUnguarded << ", guarded " << s.packedGuarded; - os << " in " << s.guardedRuns << " runs, " << s.guardChecks << " checks)"; - os << " reductions " << s.packedReduction << " rejected " << s.rejected << "\n"; - } - - U32 SlpPackPass::runOnFunction(Function& fn, const TargetInfo& target) { - return Slp(fn, target, stats, guards).run(); - } -} // namespace rat diff --git a/src/backend/pass/opt/slp/slp_pack.h b/src/backend/pass/opt/slp/slp_pack.h deleted file mode 100644 index be4e385c..00000000 --- a/src/backend/pass/opt/slp/slp_pack.h +++ /dev/null @@ -1,320 +0,0 @@ -// SLP vectorization: windows of stores in store chains and sets, and integer reductions, pack -// into SSE vectors - -// SLP internals by stage -// 1. chains: windows of stores along a chain or a set make one Seed per run -// 2. plan: the vectors and runtime checks of a Seed -// 3. check: the profit covers the kept lanes, every inner reader dies (L3) -// 4. apply: the vectors, then the wide stores, guarded if a lane needs it -// 5. reductions: each reduction root is a Seed for stages 2 to 4 - -#ifndef RAT_PASS_OPT_SLPPACK_H -#define RAT_PASS_OPT_SLPPACK_H - -#include "core.h" - -#include "analysis/address.h" -#include "ir/opcode.h" -#include "pass/pass.h" - -#include - -namespace rat { - struct CallNode; - struct StoreNode; - struct LoadNode; - struct MemMergeNode; - struct PhiNode; - struct Type; - - struct SlpStats { - U32 windowsSeen = 0; // windows, and reductions of kMinTerms or more terms, examined - U32 packedUnguarded = 0; // windows fused statically - U32 packedGuarded = 0; // windows fused behind a runtime guard - U32 packedReduction = 0; // reductions packed into vectors - U32 guardedRuns = 0; // merged guard regions emitted - U32 guardChecks = 0; // runtime disjointness checks emitted - U32 rejected = 0; // windows rejected early, and seeds that did not pack - }; - - enum class SlpGuards : U8 { - On, - NoCost, // fuzzing - Off - }; - - struct SlpPackPass : FunctionPass { - static constexpr U32 kVecShift = 4; - static constexpr U32 kNoVector = ~0u; - static constexpr Opcode kNoOp = Opcode::Start; - static constexpr U32 kVecBytes = 1u << kVecShift; // SSE baseline - static constexpr U32 kMaxLanes = 4; // i32/f32 in 16 bytes - static constexpr U32 kMaxInputs = 3; // inputs of a lane op, a select has 3 - static constexpr U32 kReadPiece = 4; // bytes per entry of the reads index - // 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; - - static constexpr I32 kMinProfit = 2; // scalar ops a window or a reduction must save - static constexpr I32 kGuardCheckCost = 6; // ops per runtime disjointness check - static constexpr I32 kGuardBranchCost = 6; // branch and else-arm bloat per guarded run - static constexpr U32 kMaxRunWindows = 4; // windows of one run, which share one guard - static constexpr U32 kMaxGuards = 4; // runtime checks per guarded run - static constexpr U32 kMaxGuardedRuns = 16; // per block, loads above a guard live across splits - static constexpr U32 kMinTerms = 4; // fewest terms of a reduction - static constexpr U32 kMaxTerms = 64; // most terms of a reduction - - // work bounds, so each search stays linear in the function - static constexpr U32 kMaxOps = 64; // Op vectors tried per seed tuple - static constexpr U32 kMaxSetGroups = 16; // groups of a set, compared pairwise by their bases - static constexpr U32 kMaxHops = 16; // states walked from a load to memIn or to its writers - - enum TypeBits : U8 { - Int8 = 1, - Int16 = 2, - Int32 = 4, - Int64 = 8, - Float32 = 16, - Float64 = 32, - Ptr = 64, - Int32Int64 = Int32 | Int64, - Float = Float32 | Float64, - Elem = Int32Int64 | Float | Ptr - }; - - using Lanes = SmallList; // a tuple, one scalar per lane, all of one type - using Chain = List; // in chain order, or a set's consecutive stores - - struct Context { - Context(Function& fn, const TargetInfo& target, SlpGuards guards); - Type* laneType(const Node* n); - U32 guardedRunsIn(const Node* block) const; - B32 bytesRead(U32 group, I64 offset, U32 size, const Node* ctrl); - B32 storesTo(U32 object); - Function& fn; - U32 ptrBytes; - B32 sse41; - SlpGuards guards; - AddrCache addrs; - Map guardedRunsPerBlock; // block -> guarded runs, commitGuarded counts - private: - void indexReads(); - void indexStores(); - private: - Map> reads; // (control id, group) -> offsets of the 4-byte pieces loads read - B32 readsIndexed = false; - Set storedObjects; - B32 storesIndexed = false; - }; - - // chains - struct Access { - Addr addr; - U32 size = 0; - }; - - struct Window { - // laneCount stores of group from chain[start] fill bytes from offset, laneStore in offset order - U32 start = 0; - U32 laneCount = 0; - U32 group = 0; - I64 offset = 0; - StoreNode* laneStore[kMaxLanes] = {}; - }; - - // the tuples to pack for a run of stores or a reduction - struct Seed { - List tuples; // one vector each - I32 profit = 0; // scalar ops saved besides the plan's vectors - // run - SmallList stores; // chain order, in a set by offset - SmallList lowestStores; // the lane 0 store of each window - // reduction - Node* root = nullptr; - List steps; // the ops the vectors replace - List scalarTerms; - }; - - // plan - enum class VectorKind : U8 { - Gather, // Pack or Splat - Load, // wide loads or a broadcast load - Op // lane ops as vector ops - }; - - struct Vector { - Lanes lanes; - VectorKind kind = VectorKind::Gather; - I32 profit = 0; // scalar ops saved, negative for a gather - Opcode mainOp = kNoOp; - Opcode altOp = kNoOp; // opcode of the lanes sel takes from the second op - U32 operands[kMaxInputs] = {kNoVector, kNoVector, kNoVector}; // vector of each Op input - Node* shiftAmount = nullptr; - Node* mem = nullptr; // state the loads read - LoadNode* lowestLoad = nullptr; // lane load at the lowest offset - I64 offset = 0; // of the first wide load, or of the one load - I64 end = 0; // end of the highest lane - U32 wideLoads = 0; // 1 or 2, 0 for one load and a broadcast - B32 permuted = false; - B32 needsGuard = false; // a lane load has a weak edge - U16 sel = 0; // ShuffleNode selector of the permute or the blend - Node* node = nullptr; // what apply emitted - }; - - // one runtime check, the loads of group read [begin, end), ptr points at begin - struct GuardRange { - U32 group = 0; - Node* ptr = nullptr; - I64 begin = 0; - I64 end = 0; - }; - - // the lanes of an Op vector as one opcode each, with their operand tuples - struct LaneOps { - Opcode opcode[kMaxLanes] = {}; // Neg as Sub from 0, Not as Xor with -1 - Lanes operands[kMaxInputs]; // tuple k holds input k of each lane - Opcode mainOp = kNoOp; - Opcode altOp = kNoOp; - }; - - struct Plan { - Plan(Context& cx, const Seed& seed); - B32 build(); - Node* tupleNode(U32 tuple) const; // what apply emitted for seed tuple - const Seed& seed; - List vectors; // operands first - SmallList tupleVectors; // vector of each seed tuple - List guards; - List> loads; // the loads apply made, loadAt reuses them - private: - void addGuard(const Vector& vec); - U32 planVector(const Lanes& lanes); - U32 planGather(const Lanes& lanes); - // Op - U32 planOp(const Lanes& lanes); - B32 readLaneOps(const Lanes& lanes, LaneOps& ops); - B32 hasRuleForOps(const LaneOps& ops, Node* lane0); - B32 hasRule(Opcode op, Node* lane); - U32 operandTuples(const Lanes& lanes, LaneOps& ops); - void orientOperands(LaneOps& ops); - std::tuple operandKey(Node* n); - // Load - U32 planLoad(const Lanes& lanes); - B32 loadShape(const Lanes& lanes, Vector& vec); - B32 permuteSpan(const Lanes& lanes, Vector& vec); - B32 loadState(const Lanes& lanes, Vector& vec); - B32 canReadMemIn(LoadNode* l, const Addr& runAddr, B32& needsGuard); - B32 forwardBlocked(const Vector& vec); - B32 splitsLoad(Node* state, const Access& load, SmallList& walk); - B32 callWrites(const CallNode* call, const Addr& addr); - private: - Context& cx; - U32 budget = 0; // Op vectors left to try, kMaxOps more per seed tuple - }; - - // check - // the role of each node in the plan under check and apply - struct Check { - explicit Check(const Function& fn); - void setRoles(const Plan& plan, B32 on); - B32 innerReadersDie(const Seed& seed) const; - B32 laneDies(const Node* n) const; - U32 markKeptLanes(const Plan& plan); - private: - B32 usedOutside(const Node* n, B32 copies) const; - U8 roleOf(const Node* n) const; - private: - const Function& fn; - List roles; // node id -> detail::Role, set while one plan is checked and applied - }; - - // apply - struct GuardedRun { - Node* block = nullptr; - StoreNode* firstStore = nullptr; // its memory follows every rewire of memIn - PhiNode* phi = nullptr; // final state, input 0 is the region that merges the arms - Node* firstIf = nullptr; - }; - - struct Emitter { - explicit Emitter(Context& cx); - void seedAnchors(const Plan& plan); - void emitVectors(Plan& plan); - void commitRun(const Plan& plan); - void commitGuarded(const Plan& plan, const Check& check); - void commitReduction(const Plan& plan); // slp_reduce.cpp - void splitBlocks(); - private: - Pair anchorOf(Node* ptr); - Node* anchorPtr(Node* ptr, I64 offset, const Node* ctrl); - Node* emitLoad(const Vector& vec, Type* vecTy, Plan& plan); - Node* loadAt(const Vector& vec, Type* ty, I64 offset, Plan& plan); - Node* emitOp(const Vector& vec, Type* vecTy, const Plan& plan); - Node* emitWideStores(const Plan& plan, Node* ctrl); - Node* emitGuardCascade(const Plan& plan, Node*& firstIf, Node*& elseP); - void moveToElse(const Plan& plan, Node* block, Node* elseP, const Check& check); - B32 moveLaterUsers(const GuardedRun& guarded, const Map& runAt); - private: - Context& cx; - Map> anchors; // (control, group) -> anchor and its offset - List guardedRuns; // commitGuarded adds, splitBlocks reads - }; - - // driver - struct Slp { - Slp(Function& fn, const TargetInfo& target, SlpStats& stats, SlpGuards guards); - Slp(const Slp&) = delete; // the stages hold references to cx - U32 run(); - private: - B32 pack(const Seed& seed); - void apply(Plan& plan); // slp_apply.cpp - // chains - U32 packChains(const List& chains, List& packed); - B32 isDisjointSet(const Chain& chain, U32 first, U32 last, Map& written); - U32 packWindows(const Chain& chain); - U32 packRunAt(const Chain& chain, U32 start, U32 bytes); - B32 windowAt(const Chain& chain, U32 start, U32 bytes, Window& window); - U32 packSet(const Chain& chain, U32 first, U32 last); - MemMergeNode* mergeStretch(const Chain& chain, U32 first, U32 last); - // reductions - U32 packReductions(); - B32 isReductionRoot(const Node* n); - U32 packReduction(Node* root); - private: - Context cx; - SlpStats& stats; - Check check; - Emitter emitter; - }; - public: - explicit SlpPackPass(SlpGuards guards = SlpGuards::On); - void beginModule(Module& module, const CallGraph& graph) override; - void printStats(const Module& module, std::ostream& os) const override; - U32 runOnFunction(Function& fn, const TargetInfo& target) override; - static U8 typeBits(const Type* t); - static U64 pairKey(U32 high, I64 low); - static I64 blockEnd(I64 from, I64 end); - // chains - static Access accessOf(AddrCache& addrs, U32 ptrBytes, Node* n); - static B32 hasInnerReader(StoreNode* const* stores, U32 count); - static void addWindow(Seed& seed, const Chain& chain, const Window& window); - static void lowerSet(MemMergeNode* set); - static Node* memIn(const Seed& seed); - static B32 isInner(const Seed& seed, const Node* state); - static MemMergeNode* setOf(const Seed& seed); - // plan - static B32 isSplat(const Lanes& lanes); - static I32 gatherCost(const Lanes& lanes, B32 reduction); - static B32 pickOpcodes(LaneOps& ops, U32 laneCount); - static B32 isUniformShift(const Lanes& lanes); - static I32 blendProfit(const LaneOps& ops, U32 laneCount, U16& sel); - // check - static I32 profitMargin(const Plan& plan, SlpGuards guards); - // reductions - static List flattenReduction(Node* root, List& steps); - private: - SlpGuards guards; - SlpStats stats; - }; -} // namespace rat - -#endif diff --git a/src/backend/pass/opt/slp/slp_plan.cpp b/src/backend/pass/opt/slp/slp_plan.cpp deleted file mode 100644 index c4f4a071..00000000 --- a/src/backend/pass/opt/slp/slp_plan.cpp +++ /dev/null @@ -1,292 +0,0 @@ -#include "pass/opt/slp/slp_pack.h" - -#include "ir/function.h" -#include "ir/node.h" -#include "ir/type.h" - -namespace rat { - namespace detail { - // lanes with an opcode in [first, last] become one vector op of that opcode - struct OpRule { - Opcode first; - Opcode last; - U8 laneTypes; // a compare lane is its mask - U8 inputTypes; // of input 0 of a compare or convert, 0 for no check - B32 needsSse41; - }; - } // namespace detail - - SlpPackPass::Plan::Plan(Context& cx, const Seed& seed) - : seed(seed), - cx(cx) { - vectors.reserve(4 * seed.tuples.size()); - } - - B32 SlpPackPass::Plan::build() { - for(const Lanes& tuple : seed.tuples) { - budget += kMaxOps; - U32 index = planVector(tuple); - if(index == kNoVector) - return false; - tupleVectors.push_back(index); - } - for(const Vector& vec : vectors) - if(vec.needsGuard) - addGuard(vec); - if(guards.empty()) - return true; - Node* control = seed.stores.back()->getControl(); - return guards.size() <= kMaxGuards && cx.guardedRunsIn(control) < kMaxGuardedRuns; - } - - // add the bytes of vec's lanes to the runtime check of their group - void SlpPackPass::Plan::addGuard(const Vector& vec) { - Addr lowest = cx.addrs.get(vec.lowestLoad->getPointer()); - for(GuardRange& range : guards) - if(range.group == lowest.group) { - if(lowest.offset < range.begin) - range = {range.group, vec.lowestLoad->getPointer(), lowest.offset, range.end}; - range.end = std::max(range.end, vec.end); - return; - } - guards.push_back({lowest.group, vec.lowestLoad->getPointer(), lowest.offset, vec.end}); - } - - U32 SlpPackPass::Plan::planVector(const Lanes& lanes) { - for(const Vector& vec : vectors) - if(std::equal(lanes.begin(), lanes.end(), vec.lanes.begin(), vec.lanes.end())) - return (U32)(&vec - vectors.data()); - if(isSplat(lanes)) - return planGather(lanes); - U32 index = planLoad(lanes); - if(index == kNoVector) - index = planOp(lanes); - if(index == kNoVector) - index = planGather(lanes); - return index; - } - - // a constant tuple stays a Pack, one pool load - B32 SlpPackPass::isSplat(const Lanes& lanes) { - B32 equal = std::count(lanes.begin(), lanes.end(), lanes[0]) == (I64)lanes.size(); - return equal && !isa(lanes[0]); - } - - U32 SlpPackPass::Plan::planGather(const Lanes& lanes) { - if(!(typeBits(lanes[0]->getType()) & Elem)) - return kNoVector; - Vector vec{lanes, VectorKind::Gather}; - vec.profit = -gatherCost(lanes, seed.root != nullptr); - vectors.push_back(vec); - return (U32)vectors.size() - 1; - } - - // as SSE4.1 lowers it, a movd or insert per variable lane and a pool vector when lane 0 or a - // nonzero lane is a constant - I32 SlpPackPass::gatherCost(const Lanes& lanes, B32 reduction) { - if(isSplat(lanes)) - return 1; - I32 variables = 0; - B32 poolLoad = isa(lanes[0]); - for(Node* n : lanes) { - if(const ConstantNode* c = dyn_cast(n)) - poolLoad |= c->getValue() != 0; - else - ++variables; - } - // a reduction of lanes that are all scalars waits for their moves into xmm - B32 moveLatency = reduction && variables == (I32)lanes.size(); - return variables + poolLoad + moveLatency; - } - - U32 SlpPackPass::Plan::planOp(const Lanes& lanes) { - LaneOps ops; - if(!budget || !readLaneOps(lanes, ops) || !hasRuleForOps(ops, lanes[0])) - return kNoVector; - U32 tupleCount = operandTuples(lanes, ops); - if(!tupleCount) - return kNoVector; - --budget; - Vector vec{lanes, VectorKind::Op}; - U32 firstNew = (U32)vectors.size(); - for(U32 i = 0; i < tupleCount; ++i) { - vec.operands[i] = planVector(ops.operands[i]); - if(vec.operands[i] == kNoVector) { - vectors.resize(firstNew); - return kNoVector; - } - } - vec.mainOp = ops.mainOp; - vec.altOp = ops.altOp; - if(isShiftOpcode(ops.mainOp)) - vec.shiftAmount = lanes[0]->getInput(1); - // a variable shift amount costs a move to xmm - vec.profit = (I32)lanes.size() - 1 - (vec.shiftAmount && !isa(vec.shiftAmount)); - if(ops.altOp != kNoOp) - vec.profit = blendProfit(ops, lanes.size(), vec.sel); - vectors.push_back(vec); - return (U32)vectors.size() - 1; - } - - B32 SlpPackPass::Plan::readLaneOps(const Lanes& lanes, LaneOps& ops) { - // made even when planOp rejects the lanes, dropping zero or ones shifts later node ids - Node* zero = nullptr; - Node* ones = nullptr; - for(U32 i = 0; i < lanes.size(); ++i) { - Node* lane = lanes[i]; - ops.opcode[i] = lane->getOpcode(); - if(!isArithmeticOpcode(ops.opcode[i]) && ops.opcode[i] != Opcode::Select) - return false; - for(U32 k = 0; k < lane->getInputCount(); ++k) - ops.operands[k].push_back(lane->getInput(k)); - if(ops.opcode[i] == Opcode::Neg) { - if(!zero) - zero = cx.fn.create(lane->getType(), 0); - ops.opcode[i] = Opcode::Sub; - ops.operands[1].push_back(ops.operands[0].back()); - ops.operands[0].back() = zero; - } else if(ops.opcode[i] == Opcode::Not) { - if(!ones) - ones = cx.fn.create(lane->getType(), -1); - ops.opcode[i] = Opcode::Xor; - ops.operands[1].push_back(ones); - } - } - return pickOpcodes(ops, lanes.size()); - } - - // mainOp is the opcode of lane 0, altOp the one other opcode, false for a third - B32 SlpPackPass::pickOpcodes(LaneOps& ops, U32 laneCount) { - ops.mainOp = ops.opcode[0]; - for(U32 i = 0; i < laneCount; ++i) - if(ops.opcode[i] != ops.mainOp && ops.opcode[i] != ops.altOp) { - if(ops.altOp != kNoOp) - return false; - ops.altOp = ops.opcode[i]; - } - return true; - } - - B32 SlpPackPass::Plan::hasRuleForOps(const LaneOps& ops, Node* lane0) { - if(!hasRule(ops.mainOp, lane0)) - return false; - if(ops.altOp == kNoOp) - return true; - B32 binary = isBinaryOpcode(ops.mainOp) && isBinaryOpcode(ops.altOp); - B32 shift = isShiftOpcode(ops.mainOp) || isShiftOpcode(ops.altOp); - // a blend lowers to SSE4.1 blendps - return cx.sse41 && binary && !shift && hasRule(ops.altOp, lane0); - } - - B32 SlpPackPass::Plan::hasRule(Opcode op, Node* lane) { - // clang-format off - static constexpr detail::OpRule kOpRules[] = { - {Opcode::Add, Opcode::Sub, Int32Int64, 0, false}, - {Opcode::Mul, Opcode::Mul, Int32, 0, true }, // pmulld, no i64 mul - {Opcode::And, Opcode::Xor, Int32Int64, 0, false}, - {Opcode::Shl, Opcode::LShr, Int32Int64, 0, false}, - {Opcode::AShr, Opcode::AShr, Int32, 0, false}, // psrad, no i64 sar - {Opcode::FAdd, Opcode::FDiv, Float, 0, false}, - {Opcode::FNeg, Opcode::FNeg, Float, 0, false}, - {Opcode::Eq, Opcode::Ne, Int32Int64, Int32Int64, false}, // pcmpeqd, pcmpeqq - {Opcode::Slt, Opcode::Ule, Int32, Int32, false}, // pcmpgtd, pminud - {Opcode::FEq, Opcode::FGe, Int32Int64, Float, false}, // cmpps, cmppd - {Opcode::SExt, Opcode::ZExt, Int32Int64, Int8 | Int16 | Int32, true }, // pmovsx/zx - {Opcode::SIToFP, Opcode::SIToFP, Float, Int32, false}, // cvtdq2ps/pd - {Opcode::FPToSI, Opcode::FPToSI, Int32, Float, false}, // cvttps2dq/pd2dq - {Opcode::FPExt, Opcode::FPExt, Float64, Float32, false}, // cvtps2pd - {Opcode::FPTrunc, Opcode::FPTrunc, Float32, Float64, false}, // cvtpd2ps - {Opcode::Select, Opcode::Select, Int32Int64, 0, true}, // blendv - }; - static_assert((I32)Opcode::Sub - (I32)Opcode::Add == 1 && - (I32)Opcode::Xor - (I32)Opcode::And == 2 && - (I32)Opcode::LShr - (I32)Opcode::Shl == 1 && - (I32)Opcode::FDiv - (I32)Opcode::FAdd == 3 && - (I32)Opcode::Ne - (I32)Opcode::Eq == 1 && - (I32)Opcode::Ule - (I32)Opcode::Slt == 3 && - (I32)Opcode::FGe - (I32)Opcode::FEq == 5 && - (I32)Opcode::ZExt - (I32)Opcode::SExt == 1, - "kOpRules ranges follow ir/opcode.h"); - // clang-format on - U8 laneTypes = typeBits(cx.laneType(lane)); - for(const detail::OpRule& row : kOpRules) { - if(op < row.first || op > row.last || !(laneTypes & row.laneTypes)) - continue; - B32 inputOk = !row.inputTypes || (typeBits(lane->getInput(0)->getType()) & row.inputTypes); - return inputOk && (!row.needsSse41 || cx.sse41); - } - return false; - } - - // how many operand tuples become vector inputs, 0 when they do not fit one vector op - U32 SlpPackPass::Plan::operandTuples(const Lanes& lanes, LaneOps& ops) { - U32 tupleCount = (U32)getOpcodeInfo(ops.mainOp).minInputs; - if(isShiftOpcode(ops.mainOp)) - tupleCount = 1; // the amount is one scalar for all lanes - for(U32 input = 0; input < tupleCount; ++input) - for(Node* n : ops.operands[input]) - if(n->getType() != ops.operands[input][0]->getType()) - return 0; - for(U32 input = 0; input < tupleCount; ++input) { - U32 bytes = lanes.size() * cx.laneType(ops.operands[input][0])->byteSize(cx.ptrBytes); - if(bytes > kVecBytes || bytes < 4) // at most a vector, at least a movd - return 0; - } - if(isShiftOpcode(ops.mainOp) && !isUniformShift(lanes)) - return 0; - if(ops.mainOp == Opcode::Select && cx.laneType(ops.operands[0][0]) != lanes[0]->getType()) - return 0; // blendv needs a mask of the lane type - if(tupleCount == 2) - orientOperands(ops); - return tupleCount; - } - - B32 SlpPackPass::isUniformShift(const Lanes& lanes) { - Node* amount = lanes[0]->getInput(1); - for(Node* n : lanes) - if(n->getInput(1) != amount) - return false; - const ConstantNode* constant = dyn_cast(amount); - if(!constant) - return true; - I64 laneBits = lanes[0]->getType()->getIntWidth(); - return constant->getValue() >= 0 && constant->getValue() < laneBits; - } - - // swap the operands of a commutative lane when they match lane 0's in reverse order - void SlpPackPass::Plan::orientOperands(LaneOps& ops) { - auto key0 = operandKey(ops.operands[0][0]); - auto key1 = operandKey(ops.operands[1][0]); - for(U32 i = 1; i < ops.operands[0].size() && key0 != key1; ++i) { - if(!getOpcodeInfo(ops.opcode[i]).isCommutative) - continue; - auto left = operandKey(ops.operands[0][i]); - auto right = operandKey(ops.operands[1][i]); - if(left != key0 && right == key0 && left == key1) - std::swap(ops.operands[0][i], ops.operands[1][i]); - } - } - - // what orientOperands compares, the opcode, the type and a load's group - std::tuple SlpPackPass::Plan::operandKey(Node* n) { - U32 group = 0; - if(LoadNode* l = dyn_cast(n)) - group = cx.addrs.get(l->getPointer()).group; - return {n->getOpcode(), n->getType(), group}; - } - - // two vector ops and a blend, or one addsubps when the lanes subtract and add in turn - I32 SlpPackPass::blendProfit(const LaneOps& ops, U32 laneCount, U16& sel) { - constexpr Opcode kAddSub[2] = {Opcode::FSub, Opcode::FAdd}; // lane i % 2 of an addsubps - B32 addSub = true; - sel = 0; - for(U32 i = 0; i < laneCount; ++i) { - B32 fromAlt = ops.opcode[i] == ops.altOp; - sel |= (U16)((i + laneCount * fromAlt) << (ShuffleNode::kSourceBits * i)); - addSub &= ops.opcode[i] == kAddSub[i % 2]; - } - return (I32)laneCount - 3 + 2 * (I32)addSub; - } - - Node* SlpPackPass::Plan::tupleNode(U32 tuple) const { return vectors[tupleVectors[tuple]].node; } -} // namespace rat diff --git a/src/backend/pass/opt/slp/slp_reduce.cpp b/src/backend/pass/opt/slp/slp_reduce.cpp deleted file mode 100644 index ae72e0ba..00000000 --- a/src/backend/pass/opt/slp/slp_reduce.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "pass/opt/slp/slp_pack.h" - -#include "ir/function.h" -#include "ir/node.h" -#include "ir/type.h" - -namespace rat { - U32 SlpPackPass::Slp::packReductions() { - // roots first, as a pack adds nodes to fn - List roots; - for(Node* n : cx.fn) - if(isReductionRoot(n)) - roots.push_back(n); - U32 changed = 0; - for(Node* root : roots) - changed += packReduction(root); - return changed; - } - - // a reducible op with an input of its op and no user of its op and type - B32 SlpPackPass::Slp::isReductionRoot(const Node* n) { - Opcode op = n->getOpcode(); - if(op != Opcode::Add && op != Opcode::Mul && (op < Opcode::And || op > Opcode::Xor)) - return false; - U8 typeClass = typeBits(n->getType()); - // a Mul packs only as SSE4.1 pmulld on i32 - if(!(typeClass & Int32Int64) || (op == Opcode::Mul && (typeClass != Int32 || !cx.sse41))) - return false; - B32 root = false; - for(U32 i = 0; i < n->getInputCount(); ++i) - root |= n->getInput(i)->getOpcode() == op; - for(Node* u : n->getUsers()) - root &= u->getOpcode() != op || u->getType() != n->getType(); - return root; - } - - U32 SlpPackPass::Slp::packReduction(Node* root) { - Seed seed; - seed.root = root; - List terms = flattenReduction(root, seed.steps); - U32 termCount = (U32)terms.size(); - if(termCount < kMinTerms) - return 0; - ++stats.windowsSeen; - U32 laneCount = kVecBytes / root->getType()->byteSize(cx.ptrBytes); - U32 vectorCount = termCount / laneCount; - seed.tuples.resize(vectorCount); - for(U32 i = 0; i < vectorCount; ++i) - seed.tuples[i].assign(terms.data() + i * laneCount, terms.data() + (i + 1) * laneCount); - seed.scalarTerms.assign(terms.begin() + vectorCount * laneCount, terms.end()); - I32 finishOps = 2 * (I32)(laneCount / 2) + 1; // shuffle and op per round, then the extract - // termCount - 1 scalar ops become vectorCount - 1 vector ops, finishOps and the scalar terms - I32 vectorOps = (I32)vectorCount - 1 + finishOps; - seed.profit = (I32)termCount - 1 - (I32)seed.scalarTerms.size() - vectorOps; - return pack(seed); - } - - // root's terms left to right, below its steps, the single-use ops of its opcode and type - List SlpPackPass::flattenReduction(Node* root, List& steps) { - List terms; - List todo = {root}; - while(!todo.empty()) { - Node* n = todo.back(); - todo.pop_back(); - B32 sameOp = n->getOpcode() == root->getOpcode() && n->getType() == root->getType(); - B32 isStep = sameOp && (n == root || n->getUsers().size() == 1); - // a step replaces n by its two inputs, which can each end as a term - if(!isStep || terms.size() + todo.size() + 2 > kMaxTerms) { - terms.push_back(n); - continue; - } - steps.push_back(n); - todo.push_back(n->getInput(0)); - todo.push_back(n->getInput(1)); - } - std::reverse(terms.begin(), terms.end()); - return terms; - } - - // combine the vectors by the op, fold the lanes by shuffles, then the other terms as scalars - void SlpPackPass::Emitter::commitReduction(const Plan& plan) { - const Seed& seed = plan.seed; - Opcode op = seed.root->getOpcode(); - Node* acc = plan.tupleNode(0); - Type* vecTy = acc->getType(); - for(U32 i = 1; i < plan.tupleVectors.size(); ++i) - acc = cx.fn.binary(op, acc, plan.tupleNode(i)); - U32 laneCount = vecTy->getVecLanes(); - // finish shuffles by [laneCount / 4][round], nibble i is the source of lane i - constexpr U16 kFinishSel[2][2] = {{0x01}, {0x1032, 0x2301}}; - for(U32 round = 0; round < laneCount / 2; ++round) { - U16 sel = kFinishSel[laneCount / 4][round]; - Node* swapped = cx.fn.create(vecTy, acc, acc, sel); - acc = cx.fn.binary(op, acc, swapped); - } - Node* result = cx.fn.create(seed.root->getType(), acc, 0); - for(Node* term : seed.scalarTerms) - result = cx.fn.binary(op, result, term); - seed.root->replaceAllUsesWith(result); - } -} // namespace rat diff --git a/src/backend/pass/pass_registry.cpp b/src/backend/pass/pass_registry.cpp index 0ad0b829..947ece75 100644 --- a/src/backend/pass/pass_registry.cpp +++ b/src/backend/pass/pass_registry.cpp @@ -24,7 +24,7 @@ #include "pass/opt/memory_opt.h" #include "pass/opt/sccp.h" #include "pass/opt/simplify_cfg.h" -#include "pass/opt/slp/slp_pack.h" +#include "pass/opt/slp.h" #include "pass/opt/strength_reduce.h" namespace rat { @@ -36,10 +36,6 @@ namespace rat { return std::make_unique

(); } - template UniquePtr mkSlp(std::ostream&) { - return std::make_unique(G); - } - template struct Entry { const C8 *name, *description; UniquePtr (*make)(std::ostream&); @@ -70,9 +66,7 @@ namespace rat { {"inline", "function inlining", &mk}, {"strengthreduce", "induction-variable strength reduction", &mk}, {"loadcanon", "move loads to their canonical memory state", &mk}, - {"slp", "scheduling-free superword (SLP) vectorization", &mk}, - {"slp-noguards", "slp without runtime guards, an ablation", &mkSlp}, - {"slp-noguardcost", "slp with guard cost 0, a fuzz test mode", &mkSlp}, + {"slp", "scheduling-free SLP vectorization", &mk}, {"dfe", "dead (unreferenced internal) function elimination", &mk}, {"verify", "edge consistency + structural invariants", &mk}, {"text-emitter", "textual IR visualization", &mk}, diff --git a/src/backend/test/fold_reassoc_drops_nsw.rat b/src/backend/test/fold_reassoc_drops_nsw.rat index dd595f99..2adc637c 100644 --- a/src/backend/test/fold_reassoc_drops_nsw.rat +++ b/src/backend/test/fold_reassoc_drops_nsw.rat @@ -123,7 +123,7 @@ func flagged(ptr, i32, i32) -> void { v12 = sext : i64 v11 v13 = shl : i64 v12, v7 v14 = add : ptr v4, v13 - v31 = return : ctrl v2, v40 - v39 = splat : <4 x i32> v6 - v40 = store : mem v2, v3, v14, v39 + v15 = store : mem v2, v3, v14, v38 + v31 = return : ctrl v2, v15 + v38 = splat : <4 x i32> v6 } diff --git a/src/backend/test/gvn_merge_drops_nsw.rat b/src/backend/test/gvn_merge_drops_nsw.rat index 9faad6a7..2dfda5bd 100644 --- a/src/backend/test/gvn_merge_drops_nsw.rat +++ b/src/backend/test/gvn_merge_drops_nsw.rat @@ -127,9 +127,9 @@ func flagged(ptr, ptr, i32, i32) -> void { v12 = sext : i64 v6 v13 = shl : i64 v12, v8 v14 = add : ptr v4, v13 + v15 = store : mem v2, v3, v14, v34 v16 = add : i32 nsw v6, v9 - v32 = store : mem v2, v35, v5, v16 + v32 = store : mem v2, v15, v5, v16 v33 = return : ctrl v2, v32 v34 = splat : <4 x i32> v7 - v35 = store : mem v2, v3, v14, v34 } diff --git a/src/backend/test/slp_anchor_per_control.rat b/src/backend/test/slp_anchor_per_control.rat deleted file mode 100644 index aefe797e..00000000 --- a/src/backend/test/slp_anchor_per_control.rat +++ /dev/null @@ -1,115 +0,0 @@ -@name slp: p + 4 * sext(i + 1) and p + 4 * sext(i) share a group only where i + 1 does not overflow, so the else arm takes its own anchor (v75) and not the then arm's v23, whose i + 1 the else arm never computes -@passes loadcanon slp - -@input -func f(ptr restrict, i32, i32, ptr restrict) -> void noinline { - v0 = start : (ctrl, mem, ptr, i32, i32, ptr) - v1 = stop : ctrl v117 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : i32 #3 "arg1" of v0 - v6 = proj : i32 #4 "arg2" of v0 - v7 = proj : ptr #5 "arg3" of v0 - v8 = const : i32 0 - v9 = ne : i1 v6, v8 - v10 = if : (ctrl, ctrl) v2, v9 - v11 = proj : ctrl #0 "then" of v10 - v12 = proj : ctrl #1 "else" of v10 - v18 = const : i32 1 - v19 = add : i32 nsw v5, v18 - v20 = sext : i64 v19 - v23 = add : ptr v4, v135 - v24 = load : i32 v11, v3, v23 - v25 = store : mem v11, v3, v7, v24 - v30 = add : ptr v7, v121 - v31 = const : i32 2 - v32 = add : i32 nsw v5, v31 - v33 = sext : i64 v32 - v36 = add : ptr v4, v137 - v37 = load : i32 v11, v25, v36 - v38 = store : mem v11, v25, v30, v37 - v43 = add : ptr v7, v123 - v44 = const : i32 3 - v45 = add : i32 nsw v5, v44 - v46 = sext : i64 v45 - v49 = add : ptr v4, v139 - v50 = load : i32 v11, v38, v49 - v51 = store : mem v11, v38, v43, v50 - v56 = add : ptr v7, v125 - v57 = const : i32 4 - v58 = add : i32 nsw v5, v57 - v59 = sext : i64 v58 - v62 = add : ptr v4, v141 - v63 = load : i32 v11, v51, v62 - v64 = store : mem v11, v51, v56, v63 - v71 = sub : i32 nsw v5, v44 - v72 = sext : i64 v71 - v75 = add : ptr v4, v143 - v76 = load : i32 v12, v3, v75 - v77 = store : mem v12, v3, v7, v76 - v84 = sub : i32 nsw v5, v31 - v85 = sext : i64 v84 - v88 = add : ptr v4, v145 - v89 = load : i32 v12, v77, v88 - v90 = store : mem v12, v77, v30, v89 - v97 = sub : i32 nsw v5, v18 - v98 = sext : i64 v97 - v101 = add : ptr v4, v147 - v102 = load : i32 v12, v90, v101 - v103 = store : mem v12, v90, v43, v102 - v109 = sext : i64 v5 - v112 = add : ptr v4, v149 - v113 = load : i32 v12, v103, v112 - v114 = store : mem v12, v103, v56, v113 - v115 = region : ctrl v11, v12 - v116 = phi : mem v115, v64, v114 - v117 = return : ctrl v115, v116 - v121 = const : i64 4 - v123 = const : i64 8 - v125 = const : i64 12 - v134 = const : i64 2 - v135 = shl : i64 v20, v134 - v137 = shl : i64 v33, v134 - v139 = shl : i64 v46, v134 - v141 = shl : i64 v59, v134 - v143 = shl : i64 v72, v134 - v145 = shl : i64 v85, v134 - v147 = shl : i64 v98, v134 - v149 = shl : i64 v109, v134 -} - -@expect -func f(ptr restrict, i32, i32, ptr restrict) -> void noinline { - v0 = start : (ctrl, mem, ptr, i32, i32, ptr) - v1 = stop : ctrl v21 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : i32 #3 "arg1" of v0 - v6 = proj : i32 #4 "arg2" of v0 - v7 = proj : ptr #5 "arg3" of v0 - v8 = const : i32 0 - v9 = ne : i1 v6, v8 - v10 = if : (ctrl, ctrl) v2, v9 - v11 = proj : ctrl #0 "then" of v10 - v12 = proj : ctrl #1 "else" of v10 - v13 = const : i32 1 - v14 = add : i32 nsw v5, v13 - v15 = sext : i64 v14 - v16 = const : i32 3 - v17 = sub : i32 nsw v5, v16 - v18 = sext : i64 v17 - v19 = region : ctrl v11, v12 - v20 = phi : mem v19, v28, v30 - v21 = return : ctrl v19, v20 - v22 = const : i64 2 - v23 = shl : i64 v15, v22 - v24 = shl : i64 v18, v22 - v25 = add : ptr v4, v23 - v26 = add : ptr v4, v24 - v27 = load : <4 x i32> v11, v3, v25 - v28 = store : mem v11, v3, v7, v27 - v29 = load : <4 x i32> v12, v3, v26 - v30 = store : mem v12, v3, v7, v29 -} diff --git a/src/backend/test/slp_blend_addsub.rat b/src/backend/test/slp_blend_addsub.rat new file mode 100644 index 00000000..3dd3626a --- /dev/null +++ b/src/backend/test/slp_blend_addsub.rat @@ -0,0 +1,60 @@ +@name slp: lanes that subtract and add in turn pack into two vector ops and a blend +@passes slp + +@input +func t(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v16 = add : ptr v6, v7 + v17 = add : ptr v6, v8 + v18 = add : ptr v6, v9 + v20 = load : f32 v2, v3, v5 + v21 = load : f32 v2, v3, v13 + v22 = load : f32 v2, v3, v14 + v23 = load : f32 v2, v3, v15 + v24 = load : f32 v2, v3, v6 + v25 = load : f32 v2, v3, v16 + v26 = load : f32 v2, v3, v17 + v27 = load : f32 v2, v3, v18 + v28 = fsub : f32 v20, v24 + v29 = fadd : f32 v21, v25 + v30 = fsub : f32 v22, v26 + v31 = fadd : f32 v23, v27 + v32 = store : mem v2, v3, v4, v28 + v33 = store : mem v2, v32, v10, v29 + v34 = store : mem v2, v33, v11, v30 + v35 = store : mem v2, v34, v12, v31 + v40 = return : ctrl v2, v35 +} + +@expect +func t(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v35 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v35 = return : ctrl v2, v41 + v36 = load : <4 x f32> v2, v3, v5 + v37 = load : <4 x f32> v2, v3, v6 + v38 = fsub : <4 x f32> v36, v37 + v39 = fadd : <4 x f32> v36, v37 + v40 = shuffle : <4 x f32> #29264 v38, v39 + v41 = store : mem v2, v3, v4, v40 +} diff --git a/src/backend/test/slp_broadcast_load.rat b/src/backend/test/slp_broadcast_load.rat new file mode 100644 index 00000000..b4cf6893 --- /dev/null +++ b/src/backend/test/slp_broadcast_load.rat @@ -0,0 +1,56 @@ +@name slp: lanes that load one address become one load and a broadcast +@passes slp + +@input +func t(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v6, v7 + v14 = add : ptr v6, v8 + v15 = add : ptr v6, v9 + v20 = load : f32 v2, v3, v5 + v21 = load : f32 v2, v3, v5 + v22 = load : f32 v2, v3, v5 + v23 = load : f32 v2, v3, v5 + v24 = load : f32 v2, v3, v6 + v25 = load : f32 v2, v3, v13 + v26 = load : f32 v2, v3, v14 + v27 = load : f32 v2, v3, v15 + v28 = fmul : f32 v20, v24 + v29 = fmul : f32 v21, v25 + v30 = fmul : f32 v22, v26 + v31 = fmul : f32 v23, v27 + v32 = store : mem v2, v3, v4, v28 + v33 = store : mem v2, v32, v10, v29 + v34 = store : mem v2, v33, v11, v30 + v35 = store : mem v2, v34, v12, v31 + v40 = return : ctrl v2, v35 +} + +@expect +func t(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v32 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v32 = return : ctrl v2, v37 + v33 = load : f32 v2, v3, v5 + v34 = splat : <4 x f32> v33 + v35 = load : <4 x f32> v2, v3, v6 + v36 = fmul : <4 x f32> v34, v35 + v37 = store : mem v2, v3, v4, v36 +} diff --git a/src/backend/test/slp_copy_to_two.rat b/src/backend/test/slp_copy_to_two.rat new file mode 100644 index 00000000..9acce623 --- /dev/null +++ b/src/backend/test/slp_copy_to_two.rat @@ -0,0 +1,43 @@ +@name slp: loads copied to two places count as dead, both copies pack and share a wide load +@passes slp + +@input +var s : [2 x i64] = "" + +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v30 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "p" of v0 + v5 = proj : ptr #3 "q" of v0 + v6 = global : ptr "s" + v7 = const : i64 8 + v8 = add : ptr v6, v7 + v9 = add : ptr v4, v7 + v10 = add : ptr v5, v7 + v11 = load : i64 v2, v3, v6 + v12 = load : i64 v2, v3, v8 + v13 = store : mem v2, v3, v4, v11 + v14 = store : mem v2, v13, v9, v12 + v15 = store : mem v2, v14, v5, v11 + v16 = store : mem v2, v15, v10, v12 + v30 = return : ctrl v2, v16 +} + +@expect +var s : [2 x i64] = "" + +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v17 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "p" of v0 + v5 = proj : ptr #3 "q" of v0 + v6 = global : ptr "s" + v13 = store : mem v2, v3, v4, v18 + v15 = store : mem v2, v13, v5, v18 + v17 = return : ctrl v2, v15 + v18 = load : <2 x i64> v2, v3, v6 +} diff --git a/src/backend/test/slp_guard_later_run_gather.rat b/src/backend/test/slp_guard_later_run_gather.rat deleted file mode 100644 index 600455f1..00000000 --- a/src/backend/test/slp_guard_later_run_gather.rat +++ /dev/null @@ -1,226 +0,0 @@ -@name slp: a later guarded run of a block packs although its c lanes, loaded above the first guard, are gathered at one op per lane -@passes loadcanon slp - -@input -func t(ptr, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v326 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "c" of v0 - v5 = proj : ptr #3 "a" of v0 - v16 = load : i32 v2, v3, v4 - v22 = load : i32 v2, v3, v5 - v23 = add : i32 v16, v22 - v30 = xor : i32 v23, v22 - v37 = sub : i32 v30, v22 - v44 = and : i32 v37, v22 - v45 = store : mem v2, v3, v4, v44 - v50 = add : ptr v4, v340 - v56 = load : i32 v2, v45, v50 - v61 = add : ptr v5, v340 - v62 = load : i32 v2, v45, v61 - v63 = add : i32 v56, v62 - v70 = xor : i32 v63, v62 - v77 = sub : i32 v70, v62 - v84 = and : i32 v77, v62 - v85 = store : mem v2, v45, v50, v84 - v90 = add : ptr v4, v352 - v96 = load : i32 v2, v85, v90 - v101 = add : ptr v5, v352 - v102 = load : i32 v2, v85, v101 - v103 = add : i32 v96, v102 - v110 = xor : i32 v103, v102 - v117 = sub : i32 v110, v102 - v124 = and : i32 v117, v102 - v125 = store : mem v2, v85, v90, v124 - v130 = add : ptr v4, v364 - v136 = load : i32 v2, v125, v130 - v141 = add : ptr v5, v364 - v142 = load : i32 v2, v125, v141 - v143 = add : i32 v136, v142 - v150 = xor : i32 v143, v142 - v157 = sub : i32 v150, v142 - v164 = and : i32 v157, v142 - v165 = store : mem v2, v125, v130, v164 - v170 = add : ptr v4, v376 - v176 = load : i32 v2, v165, v170 - v181 = add : ptr v5, v376 - v182 = load : i32 v2, v165, v181 - v183 = add : i32 v176, v182 - v190 = xor : i32 v183, v182 - v197 = sub : i32 v190, v182 - v204 = and : i32 v197, v182 - v205 = store : mem v2, v165, v170, v204 - v210 = add : ptr v4, v388 - v216 = load : i32 v2, v205, v210 - v221 = add : ptr v5, v388 - v222 = load : i32 v2, v205, v221 - v223 = add : i32 v216, v222 - v230 = xor : i32 v223, v222 - v237 = sub : i32 v230, v222 - v244 = and : i32 v237, v222 - v245 = store : mem v2, v205, v210, v244 - v250 = add : ptr v4, v400 - v256 = load : i32 v2, v245, v250 - v261 = add : ptr v5, v400 - v262 = load : i32 v2, v245, v261 - v263 = add : i32 v256, v262 - v270 = xor : i32 v263, v262 - v277 = sub : i32 v270, v262 - v284 = and : i32 v277, v262 - v285 = store : mem v2, v245, v250, v284 - v290 = add : ptr v4, v412 - v296 = load : i32 v2, v285, v290 - v301 = add : ptr v5, v412 - v302 = load : i32 v2, v285, v301 - v303 = add : i32 v296, v302 - v310 = xor : i32 v303, v302 - v317 = sub : i32 v310, v302 - v324 = and : i32 v317, v302 - v325 = store : mem v2, v285, v290, v324 - v326 = return : ctrl v2, v325 - v340 = const : i64 4 - v352 = const : i64 8 - v364 = const : i64 12 - v376 = const : i64 32 - v388 = const : i64 36 - v400 = const : i64 40 - v412 = const : i64 44 -} - -@expect -func t(ptr, ptr) -> void { -v0 = start : (ctrl, mem, ptr, ptr) -v1 = stop : ctrl v83 -v2 = proj : ctrl #0 "ctrl" of v0 -v3 = proj : mem #1 "mem" of v0 -v4 = proj : ptr #2 "c" of v0 -v5 = proj : ptr #3 "a" of v0 -v6 = load : i32 v101, v3, v4 -v7 = load : i32 v101, v3, v5 -v8 = add : i32 v6, v7 -v9 = xor : i32 v8, v7 -v10 = sub : i32 v9, v7 -v11 = and : i32 v10, v7 -v12 = store : mem v101, v3, v4, v11 -v13 = const : i64 4 -v14 = const : i64 8 -v15 = const : i64 12 -v16 = const : i64 32 -v17 = const : i64 36 -v18 = const : i64 40 -v19 = const : i64 44 -v20 = add : ptr v4, v13 -v21 = load : i32 v101, v3, v20 -v22 = add : ptr v5, v13 -v23 = load : i32 v101, v12, v22 -v24 = add : i32 v21, v23 -v25 = xor : i32 v24, v23 -v26 = sub : i32 v25, v23 -v27 = and : i32 v26, v23 -v28 = store : mem v101, v12, v20, v27 -v29 = add : ptr v4, v14 -v30 = load : i32 v101, v3, v29 -v31 = add : ptr v5, v14 -v32 = load : i32 v101, v28, v31 -v33 = add : i32 v30, v32 -v34 = xor : i32 v33, v32 -v35 = sub : i32 v34, v32 -v36 = and : i32 v35, v32 -v37 = store : mem v101, v28, v29, v36 -v38 = add : ptr v4, v15 -v39 = load : i32 v101, v3, v38 -v40 = add : ptr v5, v15 -v41 = load : i32 v101, v37, v40 -v42 = add : i32 v39, v41 -v43 = xor : i32 v42, v41 -v44 = sub : i32 v43, v41 -v45 = and : i32 v44, v41 -v46 = store : mem v101, v37, v38, v45 -v47 = add : ptr v4, v16 -v48 = load : i32 v2, v3, v47 -v49 = add : ptr v5, v16 -v50 = load : i32 v125, v105, v49 -v51 = add : i32 v48, v50 -v52 = xor : i32 v51, v50 -v53 = sub : i32 v52, v50 -v54 = and : i32 v53, v50 -v55 = store : mem v125, v105, v47, v54 -v56 = add : ptr v4, v17 -v57 = load : i32 v2, v3, v56 -v58 = add : ptr v5, v17 -v59 = load : i32 v125, v55, v58 -v60 = add : i32 v57, v59 -v61 = xor : i32 v60, v59 -v62 = sub : i32 v61, v59 -v63 = and : i32 v62, v59 -v64 = store : mem v125, v55, v56, v63 -v65 = add : ptr v4, v18 -v66 = load : i32 v2, v3, v65 -v67 = add : ptr v5, v18 -v68 = load : i32 v125, v64, v67 -v69 = add : i32 v66, v68 -v70 = xor : i32 v69, v68 -v71 = sub : i32 v70, v68 -v72 = and : i32 v71, v68 -v73 = store : mem v125, v64, v65, v72 -v74 = add : ptr v4, v19 -v75 = load : i32 v2, v3, v74 -v76 = add : ptr v5, v19 -v77 = load : i32 v125, v73, v76 -v78 = add : i32 v75, v77 -v79 = xor : i32 v78, v77 -v80 = sub : i32 v79, v77 -v81 = and : i32 v80, v77 -v82 = store : mem v125, v73, v74, v81 -v83 = return : ctrl v130, v131 -v84 = load : <4 x i32> v102, v3, v4 -v85 = load : <4 x i32> v102, v3, v5 -v86 = add : <4 x i32> v84, v85 -v87 = xor : <4 x i32> v86, v85 -v88 = sub : <4 x i32> v87, v85 -v89 = and : <4 x i32> v88, v85 -v90 = const : i64 16 -v91 = add : ptr v4, v90 -v92 = const : i64 16 -v93 = add : ptr v5, v92 -v94 = ule : i1 v93, v4 -v95 = if : (ctrl, ctrl) v2, v94 -v96 = proj : ctrl #0 "slp.then" of v95 -v97 = proj : ctrl #1 "slp.chk" of v95 -v98 = ule : i1 v91, v5 -v99 = if : (ctrl, ctrl) v97, v98 -v100 = proj : ctrl #0 "slp.then" of v99 -v101 = proj : ctrl #1 "slp.else" of v99 -v102 = region : ctrl v96, v100 -v103 = store : mem v102, v3, v4, v89 -v104 = region : ctrl v102, v101 -v105 = phi : mem v104, v103, v46 -v106 = pack : <4 x i32> v48, v57, v66, v75 -v107 = const : i64 32 -v108 = add : ptr v5, v107 -v109 = load : <4 x i32> v126, v105, v108 -v110 = add : <4 x i32> v106, v109 -v111 = xor : <4 x i32> v110, v109 -v112 = sub : <4 x i32> v111, v109 -v113 = and : <4 x i32> v112, v109 -v114 = const : i64 16 -v115 = add : ptr v47, v114 -v116 = const : i64 16 -v117 = add : ptr v49, v116 -v118 = ule : i1 v117, v47 -v119 = if : (ctrl, ctrl) v104, v118 -v120 = proj : ctrl #0 "slp.then" of v119 -v121 = proj : ctrl #1 "slp.chk" of v119 -v122 = ule : i1 v115, v49 -v123 = if : (ctrl, ctrl) v121, v122 -v124 = proj : ctrl #0 "slp.then" of v123 -v125 = proj : ctrl #1 "slp.else" of v123 -v126 = region : ctrl v120, v124 -v127 = const : i64 32 -v128 = add : ptr v4, v127 -v129 = store : mem v126, v105, v128, v113 -v130 = region : ctrl v126, v125 -v131 = phi : mem v130, v129, v82 -} diff --git a/src/backend/test/slp_guard_load_above_guard.rat b/src/backend/test/slp_guard_load_above_guard.rat deleted file mode 100644 index a4105879..00000000 --- a/src/backend/test/slp_guard_load_above_guard.rat +++ /dev/null @@ -1,183 +0,0 @@ -@name slp: a wide load of a state before the run stays above the guard, re-read lanes load in the vector arm -@passes slp - -@input -func k(ptr, ptr, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr) - v1 = stop : ctrl v166 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : ptr #3 "arg1" of v0 - v6 = proj : ptr #4 "arg2" of v0 - v12 = load : i32 v2, v3, v5 - v17 = add : ptr v5, v170 - v18 = load : i32 v2, v3, v17 - v23 = add : ptr v5, v172 - v24 = load : i32 v2, v3, v23 - v29 = add : ptr v5, v174 - v30 = load : i32 v2, v3, v29 - v35 = add : ptr v5, v176 - v36 = load : i32 v2, v3, v35 - v41 = add : ptr v5, v178 - v42 = load : i32 v2, v3, v41 - v47 = add : ptr v5, v180 - v48 = load : i32 v2, v3, v47 - v53 = add : ptr v5, v182 - v54 = load : i32 v2, v3, v53 - v59 = add : ptr v4, v184 - v60 = const : i32 7 - v61 = store : mem v2, v3, v59, v60 - v72 = load : i32 v2, v61, v6 - v73 = add : i32 v12, v72 - v74 = store : mem v2, v61, v4, v73 - v79 = add : ptr v4, v170 - v84 = add : ptr v6, v170 - v85 = load : i32 v2, v74, v84 - v86 = add : i32 v18, v85 - v87 = store : mem v2, v74, v79, v86 - v92 = add : ptr v4, v172 - v97 = add : ptr v6, v172 - v98 = load : i32 v2, v87, v97 - v99 = add : i32 v24, v98 - v100 = store : mem v2, v87, v92, v99 - v105 = add : ptr v4, v174 - v110 = add : ptr v6, v174 - v111 = load : i32 v2, v100, v110 - v112 = add : i32 v30, v111 - v113 = store : mem v2, v100, v105, v112 - v118 = add : ptr v4, v176 - v123 = add : ptr v6, v176 - v124 = load : i32 v2, v113, v123 - v125 = add : i32 v36, v124 - v126 = store : mem v2, v113, v118, v125 - v131 = add : ptr v4, v178 - v136 = add : ptr v6, v178 - v137 = load : i32 v2, v126, v136 - v138 = add : i32 v42, v137 - v139 = store : mem v2, v126, v131, v138 - v144 = add : ptr v4, v180 - v149 = add : ptr v6, v180 - v150 = load : i32 v2, v139, v149 - v151 = add : i32 v48, v150 - v152 = store : mem v2, v139, v144, v151 - v157 = add : ptr v4, v182 - v162 = add : ptr v6, v182 - v163 = load : i32 v2, v152, v162 - v164 = add : i32 v54, v163 - v165 = store : mem v2, v152, v157, v164 - v166 = return : ctrl v2, v165 - v170 = const : i64 4 - v172 = const : i64 8 - v174 = const : i64 12 - v176 = const : i64 16 - v178 = const : i64 20 - v180 = const : i64 24 - v182 = const : i64 28 - v184 = const : i64 32 -} - -@expect -func k(ptr, ptr, ptr) -> void { -v0 = start : (ctrl, mem, ptr, ptr, ptr) -v1 = stop : ctrl v94 -v2 = proj : ctrl #0 "ctrl" of v0 -v3 = proj : mem #1 "mem" of v0 -v4 = proj : ptr #2 "arg0" of v0 -v5 = proj : ptr #3 "arg1" of v0 -v6 = proj : ptr #4 "arg2" of v0 -v7 = load : i32 v2, v3, v5 -v8 = const : i32 7 -v9 = const : i64 4 -v10 = const : i64 8 -v11 = const : i64 12 -v12 = const : i64 16 -v13 = const : i64 20 -v14 = const : i64 24 -v15 = const : i64 28 -v16 = const : i64 32 -v17 = add : ptr v5, v9 -v18 = load : i32 v2, v3, v17 -v19 = add : ptr v5, v10 -v20 = load : i32 v2, v3, v19 -v21 = add : ptr v5, v11 -v22 = load : i32 v2, v3, v21 -v23 = add : ptr v5, v12 -v24 = load : i32 v2, v3, v23 -v25 = add : ptr v5, v13 -v26 = load : i32 v2, v3, v25 -v27 = add : ptr v5, v14 -v28 = load : i32 v2, v3, v27 -v29 = add : ptr v5, v15 -v30 = load : i32 v2, v3, v29 -v31 = add : ptr v4, v16 -v32 = store : mem v2, v3, v31, v8 -v33 = add : ptr v4, v9 -v34 = add : ptr v6, v9 -v35 = add : ptr v4, v10 -v36 = add : ptr v6, v10 -v37 = add : ptr v4, v11 -v38 = add : ptr v6, v11 -v39 = add : ptr v4, v12 -v40 = add : ptr v6, v12 -v41 = add : ptr v4, v13 -v42 = add : ptr v6, v13 -v43 = add : ptr v4, v14 -v44 = add : ptr v6, v14 -v45 = add : ptr v4, v15 -v46 = add : ptr v6, v15 -v47 = load : <4 x i32> v2, v3, v5 -v48 = const : i64 16 -v49 = add : ptr v5, v48 -v50 = load : <4 x i32> v2, v3, v49 -v51 = const : i64 16 -v52 = add : ptr v6, v51 -v53 = const : i64 32 -v54 = add : ptr v4, v53 -v55 = const : i64 32 -v56 = add : ptr v6, v55 -v57 = ule : i1 v56, v4 -v58 = if : (ctrl, ctrl) v2, v57 -v59 = proj : ctrl #0 "slp.then" of v58 -v60 = proj : ctrl #1 "slp.chk" of v58 -v61 = ule : i1 v54, v6 -v62 = if : (ctrl, ctrl) v60, v61 -v63 = proj : ctrl #0 "slp.then" of v62 -v64 = proj : ctrl #1 "slp.else" of v62 -v65 = region : ctrl v59, v63 -v66 = const : i64 16 -v67 = add : ptr v4, v66 -v68 = region : ctrl v65, v64 -v69 = phi : mem v68, v100, v93 -v70 = load : i32 v64, v32, v6 -v71 = add : i32 v7, v70 -v72 = store : mem v64, v32, v4, v71 -v73 = load : i32 v64, v72, v34 -v74 = add : i32 v18, v73 -v75 = store : mem v64, v72, v33, v74 -v76 = load : i32 v64, v75, v36 -v77 = add : i32 v20, v76 -v78 = store : mem v64, v75, v35, v77 -v79 = load : i32 v64, v78, v38 -v80 = add : i32 v22, v79 -v81 = store : mem v64, v78, v37, v80 -v82 = load : i32 v64, v81, v40 -v83 = add : i32 v24, v82 -v84 = store : mem v64, v81, v39, v83 -v85 = load : i32 v64, v84, v42 -v86 = add : i32 v26, v85 -v87 = store : mem v64, v84, v41, v86 -v88 = load : i32 v64, v87, v44 -v89 = add : i32 v28, v88 -v90 = store : mem v64, v87, v43, v89 -v91 = load : i32 v64, v90, v46 -v92 = add : i32 v30, v91 -v93 = store : mem v64, v90, v45, v92 -v94 = return : ctrl v68, v69 -v95 = load : <4 x i32> v65, v32, v6 -v96 = add : <4 x i32> v47, v95 -v97 = load : <4 x i32> v65, v32, v52 -v98 = add : <4 x i32> v50, v97 -v99 = store : mem v65, v32, v4, v96 -v100 = store : mem v65, v99, v67, v98 -} diff --git a/src/backend/test/slp_guard_shared_load.rat b/src/backend/test/slp_guard_shared_load.rat deleted file mode 100644 index fc816341..00000000 --- a/src/backend/test/slp_guard_shared_load.rat +++ /dev/null @@ -1,144 +0,0 @@ -@name slp: a guarded run must not sink a pre-existing shared load into the speculative arm -@passes slp - -@input -func t(ptr, ptr, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr) - v1 = stop : ctrl v200 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "c" of v0 - v5 = proj : ptr #3 "a" of v0 - v6 = proj : ptr #4 "b" of v0 - v7 = load : i32 v2, v3, v6 - v10 = load : i32 v2, v3, v5 - v11 = add : i32 v10, v7 - v12 = store : mem v2, v3, v4, v11 - v13 = const : i64 4 - v14 = add : ptr v4, v13 - v15 = add : ptr v5, v13 - v16 = load : i32 v2, v12, v15 - v17 = add : i32 v16, v7 - v18 = store : mem v2, v12, v14, v17 - v19 = const : i64 8 - v20 = add : ptr v4, v19 - v21 = add : ptr v5, v19 - v22 = load : i32 v2, v18, v21 - v23 = add : i32 v22, v7 - v24 = store : mem v2, v18, v20, v23 - v25 = const : i64 12 - v26 = add : ptr v4, v25 - v27 = add : ptr v5, v25 - v28 = load : i32 v2, v24, v27 - v29 = add : i32 v28, v7 - v30 = store : mem v2, v24, v26, v29 - v31 = const : i64 16 - v32 = add : ptr v4, v31 - v33 = add : ptr v5, v31 - v34 = load : i32 v2, v30, v33 - v35 = add : i32 v34, v7 - v36 = store : mem v2, v30, v32, v35 - v37 = const : i64 20 - v38 = add : ptr v4, v37 - v39 = add : ptr v5, v37 - v40 = load : i32 v2, v36, v39 - v41 = add : i32 v40, v7 - v42 = store : mem v2, v36, v38, v41 - v43 = const : i64 24 - v44 = add : ptr v4, v43 - v45 = add : ptr v5, v43 - v46 = load : i32 v2, v42, v45 - v47 = add : i32 v46, v7 - v48 = store : mem v2, v42, v44, v47 - v49 = const : i64 28 - v50 = add : ptr v4, v49 - v51 = add : ptr v5, v49 - v52 = load : i32 v2, v48, v51 - v53 = add : i32 v52, v7 - v54 = store : mem v2, v48, v50, v53 - v200 = return : ctrl v2, v54 -} - -@expect -func t(ptr, ptr, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr) - v1 = stop : ctrl v73 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "c" of v0 - v5 = proj : ptr #3 "a" of v0 - v6 = proj : ptr #4 "b" of v0 - v7 = load : i32 v2, v3, v6 - v8 = const : i64 4 - v9 = add : ptr v4, v8 - v10 = add : ptr v5, v8 - v11 = const : i64 8 - v12 = add : ptr v4, v11 - v13 = add : ptr v5, v11 - v14 = const : i64 12 - v15 = add : ptr v4, v14 - v16 = add : ptr v5, v14 - v17 = const : i64 16 - v18 = add : ptr v4, v17 - v19 = add : ptr v5, v17 - v20 = const : i64 20 - v21 = add : ptr v4, v20 - v22 = add : ptr v5, v20 - v23 = const : i64 24 - v24 = add : ptr v4, v23 - v25 = add : ptr v5, v23 - v26 = const : i64 28 - v27 = add : ptr v4, v26 - v28 = add : ptr v5, v26 - v29 = splat : <4 x i32> v7 - v30 = const : i64 16 - v31 = add : ptr v5, v30 - v32 = const : i64 32 - v33 = add : ptr v4, v32 - v34 = const : i64 32 - v35 = add : ptr v5, v34 - v36 = ule : i1 v35, v4 - v37 = if : (ctrl, ctrl) v2, v36 - v38 = proj : ctrl #0 "slp.then" of v37 - v39 = proj : ctrl #1 "slp.chk" of v37 - v40 = ule : i1 v33, v5 - v41 = if : (ctrl, ctrl) v39, v40 - v42 = proj : ctrl #0 "slp.then" of v41 - v43 = proj : ctrl #1 "slp.else" of v41 - v44 = region : ctrl v38, v42 - v45 = const : i64 16 - v46 = add : ptr v4, v45 - v47 = region : ctrl v44, v43 - v48 = phi : mem v47, v79, v72 - v49 = load : i32 v43, v3, v5 - v50 = add : i32 v49, v7 - v51 = store : mem v43, v3, v4, v50 - v52 = load : i32 v43, v51, v10 - v53 = add : i32 v52, v7 - v54 = store : mem v43, v51, v9, v53 - v55 = load : i32 v43, v54, v13 - v56 = add : i32 v55, v7 - v57 = store : mem v43, v54, v12, v56 - v58 = load : i32 v43, v57, v16 - v59 = add : i32 v58, v7 - v60 = store : mem v43, v57, v15, v59 - v61 = load : i32 v43, v60, v19 - v62 = add : i32 v61, v7 - v63 = store : mem v43, v60, v18, v62 - v64 = load : i32 v43, v63, v22 - v65 = add : i32 v64, v7 - v66 = store : mem v43, v63, v21, v65 - v67 = load : i32 v43, v66, v25 - v68 = add : i32 v67, v7 - v69 = store : mem v43, v66, v24, v68 - v70 = load : i32 v43, v69, v28 - v71 = add : i32 v70, v7 - v72 = store : mem v43, v69, v27, v71 - v73 = return : ctrl v47, v48 - v74 = load : <4 x i32> v44, v3, v5 - v75 = add : <4 x i32> v74, v29 - v76 = load : <4 x i32> v44, v3, v31 - v77 = add : <4 x i32> v76, v29 - v78 = store : mem v44, v3, v4, v75 - v79 = store : mem v44, v78, v46, v77 -} diff --git a/src/backend/test/slp_guarded_window.rat b/src/backend/test/slp_guarded_window.rat deleted file mode 100644 index a77d4ed6..00000000 --- a/src/backend/test/slp_guarded_window.rat +++ /dev/null @@ -1,141 +0,0 @@ -@name slp: may-alias pointer run vectorizes behind a runtime disjointness guard -@passes slp - -@input -func t(ptr, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v60 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "c" of v0 - v5 = proj : ptr #3 "a" of v0 - v6 = const : i64 4 - v7 = const : i64 8 - v8 = const : i64 12 - v9 = const : i64 16 - v10 = const : i64 20 - v11 = const : i64 24 - v12 = const : i64 28 - v13 = add : ptr v4, v6 - v14 = add : ptr v4, v7 - v15 = add : ptr v4, v8 - v16 = add : ptr v4, v9 - v17 = add : ptr v4, v10 - v18 = add : ptr v4, v11 - v19 = add : ptr v4, v12 - v20 = add : ptr v5, v6 - v21 = add : ptr v5, v7 - v22 = add : ptr v5, v8 - v23 = add : ptr v5, v9 - v24 = add : ptr v5, v10 - v25 = add : ptr v5, v11 - v26 = add : ptr v5, v12 - v27 = load : i32 v2, v3, v5 - v28 = add : i32 v27, v27 - v29 = store : mem v2, v3, v4, v28 - v30 = load : i32 v2, v29, v20 - v31 = add : i32 v30, v30 - v32 = store : mem v2, v29, v13, v31 - v33 = load : i32 v2, v32, v21 - v34 = add : i32 v33, v33 - v35 = store : mem v2, v32, v14, v34 - v36 = load : i32 v2, v35, v22 - v37 = add : i32 v36, v36 - v38 = store : mem v2, v35, v15, v37 - v39 = load : i32 v2, v38, v23 - v40 = add : i32 v39, v39 - v41 = store : mem v2, v38, v16, v40 - v42 = load : i32 v2, v41, v24 - v43 = add : i32 v42, v42 - v44 = store : mem v2, v41, v17, v43 - v45 = load : i32 v2, v44, v25 - v46 = add : i32 v45, v45 - v47 = store : mem v2, v44, v18, v46 - v48 = load : i32 v2, v47, v26 - v49 = add : i32 v48, v48 - v50 = store : mem v2, v47, v19, v49 - v60 = return : ctrl v2, v50 -} - -@expect -func t(ptr, ptr) -> void { -v0 = start : (ctrl, mem, ptr, ptr) -v1 = stop : ctrl v70 -v2 = proj : ctrl #0 "ctrl" of v0 -v3 = proj : mem #1 "mem" of v0 -v4 = proj : ptr #2 "c" of v0 -v5 = proj : ptr #3 "a" of v0 -v6 = const : i64 4 -v7 = const : i64 8 -v8 = const : i64 12 -v9 = const : i64 16 -v10 = const : i64 20 -v11 = const : i64 24 -v12 = const : i64 28 -v13 = add : ptr v4, v6 -v14 = add : ptr v4, v7 -v15 = add : ptr v4, v8 -v16 = add : ptr v4, v9 -v17 = add : ptr v4, v10 -v18 = add : ptr v4, v11 -v19 = add : ptr v4, v12 -v20 = add : ptr v5, v6 -v21 = add : ptr v5, v7 -v22 = add : ptr v5, v8 -v23 = add : ptr v5, v9 -v24 = add : ptr v5, v10 -v25 = add : ptr v5, v11 -v26 = add : ptr v5, v12 -v27 = const : i64 16 -v28 = add : ptr v5, v27 -v29 = const : i64 32 -v30 = add : ptr v4, v29 -v31 = const : i64 32 -v32 = add : ptr v5, v31 -v33 = ule : i1 v32, v4 -v34 = if : (ctrl, ctrl) v2, v33 -v35 = proj : ctrl #0 "slp.then" of v34 -v36 = proj : ctrl #1 "slp.chk" of v34 -v37 = ule : i1 v30, v5 -v38 = if : (ctrl, ctrl) v36, v37 -v39 = proj : ctrl #0 "slp.then" of v38 -v40 = proj : ctrl #1 "slp.else" of v38 -v41 = region : ctrl v35, v39 -v42 = const : i64 16 -v43 = add : ptr v4, v42 -v44 = region : ctrl v41, v40 -v45 = phi : mem v44, v76, v69 -v46 = load : i32 v40, v3, v5 -v47 = add : i32 v46, v46 -v48 = store : mem v40, v3, v4, v47 -v49 = load : i32 v40, v48, v20 -v50 = add : i32 v49, v49 -v51 = store : mem v40, v48, v13, v50 -v52 = load : i32 v40, v51, v21 -v53 = add : i32 v52, v52 -v54 = store : mem v40, v51, v14, v53 -v55 = load : i32 v40, v54, v22 -v56 = add : i32 v55, v55 -v57 = store : mem v40, v54, v15, v56 -v58 = load : i32 v40, v57, v23 -v59 = add : i32 v58, v58 -v60 = store : mem v40, v57, v16, v59 -v61 = load : i32 v40, v60, v24 -v62 = add : i32 v61, v61 -v63 = store : mem v40, v60, v17, v62 -v64 = load : i32 v40, v63, v25 -v65 = add : i32 v64, v64 -v66 = store : mem v40, v63, v18, v65 -v67 = load : i32 v40, v66, v26 -v68 = add : i32 v67, v67 -v69 = store : mem v40, v66, v19, v68 -v70 = return : ctrl v44, v45 -v71 = load : <4 x i32> v41, v3, v5 -v72 = add : <4 x i32> v71, v71 -v73 = load : <4 x i32> v41, v3, v28 -v74 = add : <4 x i32> v73, v73 -v75 = store : mem v41, v3, v4, v72 -v76 = store : mem v41, v75, v43, v74 -} - - diff --git a/src/backend/test/slp_half_window.rat b/src/backend/test/slp_half_window.rat new file mode 100644 index 00000000..3f0258bb --- /dev/null +++ b/src/backend/test/slp_half_window.rat @@ -0,0 +1,68 @@ +@name slp: a 4-lane window that does not pack packs its low half +@passes slp + +@input +func t(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v16 = add : ptr v6, v7 + v18 = add : ptr v6, v9 + v20 = load : f32 v2, v3, v5 + v21 = load : f32 v2, v3, v13 + v22 = load : f32 v2, v3, v14 + v23 = load : f32 v2, v3, v15 + v24 = load : f32 v2, v3, v6 + v25 = load : f32 v2, v3, v16 + v27 = load : f32 v2, v3, v18 + v28 = fmul : f32 v20, v24 + v29 = fmul : f32 v21, v25 + v31 = fmul : f32 v23, v27 + v32 = store : mem v2, v3, v4, v28 + v33 = store : mem v2, v32, v10, v29 + v34 = store : mem v2, v33, v11, v22 + v35 = store : mem v2, v34, v12, v31 + v40 = return : ctrl v2, v35 +} + +@expect +func t(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v32 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v8 = const : i64 8 + v9 = const : i64 12 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v17 = add : ptr v6, v9 + v20 = load : f32 v2, v3, v14 + v21 = load : f32 v2, v3, v15 + v24 = load : f32 v2, v3, v17 + v27 = fmul : f32 v21, v24 + v28 = store : mem v2, v3, v4, v36 + v30 = store : mem v2, v28, v11, v20 + v31 = store : mem v2, v30, v12, v27 + v32 = return : ctrl v2, v31 + v34 = load : <2 x f32> v2, v3, v5 + v35 = load : <2 x f32> v2, v3, v6 + v36 = fmul : <2 x f32> v34, v35 +} diff --git a/src/backend/test/slp_inner_reader_cuts_stretch.rat b/src/backend/test/slp_inner_reader_cuts_stretch.rat new file mode 100644 index 00000000..74805646 --- /dev/null +++ b/src/backend/test/slp_inner_reader_cuts_stretch.rat @@ -0,0 +1,50 @@ +@name slp: a load of an inner state ends the stretch, so the window around it stays scalar +@passes slp + +@input +func t(ptr, ptr) -> i32 { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v30 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : ptr #3 "b" of v0 + v6 = const : i64 4 + v7 = const : i64 8 + v8 = const : i64 12 + v9 = add : ptr v4, v6 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v5, v6 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = load : i32 v2, v3, v5 + v16 = load : i32 v2, v3, v12 + v17 = load : i32 v2, v3, v13 + v18 = load : i32 v2, v3, v14 + v20 = store : mem v2, v3, v4, v15 + v21 = store : mem v2, v20, v9, v16 + v22 = load : i32 v2, v21, v5 + v23 = store : mem v2, v21, v10, v17 + v24 = store : mem v2, v23, v11, v18 + v30 = return : ctrl v2, v24, v22 +} + +@expect +func t(ptr, ptr) -> i32 { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v24 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : ptr #3 "b" of v0 + v7 = const : i64 8 + v10 = add : ptr v4, v7 + v13 = add : ptr v5, v7 + v19 = store : mem v2, v3, v4, v25 + v21 = load : i32 v2, v19, v5 + v22 = store : mem v2, v19, v10, v26 + v24 = return : ctrl v2, v22, v21 + v25 = load : <2 x i32> v2, v3, v5 + v26 = load : <2 x i32> v2, v3, v13 +} diff --git a/src/backend/test/slp_overlap_cuts_stretch.rat b/src/backend/test/slp_overlap_cuts_stretch.rat new file mode 100644 index 00000000..aaf81ce6 --- /dev/null +++ b/src/backend/test/slp_overlap_cuts_stretch.rat @@ -0,0 +1,52 @@ +@name slp: a store that overlaps a member starts a new stretch, so no window spans the overwrite +@passes slp + +@input +func t(ptr, ptr) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v30 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : ptr #3 "b" of v0 + v6 = const : i64 4 + v7 = const : i64 8 + v8 = const : i64 12 + v9 = add : ptr v4, v6 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v5, v6 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = load : i32 v2, v3, v5 + v16 = load : i32 v2, v3, v12 + v17 = load : i32 v2, v3, v13 + v18 = load : i32 v2, v3, v14 + v19 = const : i32 7 + v20 = store : mem v2, v3, v4, v15 + v21 = store : mem v2, v20, v9, v16 + v22 = store : mem v2, v21, v4, v19 + v23 = store : mem v2, v22, v10, v17 + v24 = store : mem v2, v23, v11, v18 + v30 = return : ctrl v2, v24 +} + +@expect +func t(ptr, ptr) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v25 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : ptr #3 "b" of v0 + v7 = const : i64 8 + v10 = add : ptr v4, v7 + v13 = add : ptr v5, v7 + v19 = const : i32 7 + v20 = store : mem v2, v3, v4, v26 + v22 = store : mem v2, v20, v4, v19 + v23 = store : mem v2, v22, v10, v27 + v25 = return : ctrl v2, v23 + v26 = load : <2 x i32> v2, v3, v5 + v27 = load : <2 x i32> v2, v3, v13 +} diff --git a/src/backend/test/slp_pack_alternating_ops.rat b/src/backend/test/slp_pack_alternating_ops.rat deleted file mode 100644 index ef209330..00000000 --- a/src/backend/test/slp_pack_alternating_ops.rat +++ /dev/null @@ -1,157 +0,0 @@ -@name slp: lanes of two opcodes build both vector ops and blend them, a difference in the even lanes and a sum in the odd ones of the same operands for addsubps, duplicated lanes shuffle one wide load, and a window with a lane that is just x stays scalar -@passes loadcanon slp - -@input -func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict, i32) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr, ptr, i32) - v1 = stop : ctrl v252 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : ptr #3 "arg1" of v0 - v6 = proj : ptr #4 "arg2" of v0 - v7 = proj : ptr #5 "arg3" of v0 - v8 = proj : ptr #6 "arg4" of v0 - v9 = proj : i32 #7 "arg5" of v0 - v20 = load : f32 v2, v3, v7 - v26 = load : f32 v2, v3, v8 - v27 = fadd : f32 v20, v26 - v28 = store : mem v2, v3, v4, v27 - v33 = add : ptr v4, v260 - v38 = add : ptr v7, v260 - v39 = load : f32 v2, v28, v38 - v44 = add : ptr v8, v260 - v45 = load : f32 v2, v28, v44 - v46 = fsub : f32 v39, v45 - v47 = store : mem v2, v28, v33, v46 - v52 = add : ptr v4, v266 - v57 = add : ptr v7, v266 - v58 = load : f32 v2, v47, v57 - v63 = add : ptr v8, v266 - v64 = load : f32 v2, v47, v63 - v65 = fadd : f32 v58, v64 - v66 = store : mem v2, v47, v52, v65 - v71 = add : ptr v4, v272 - v76 = add : ptr v7, v272 - v77 = load : f32 v2, v66, v76 - v82 = add : ptr v8, v272 - v83 = load : f32 v2, v66, v82 - v84 = fsub : f32 v77, v83 - v85 = store : mem v2, v66, v71, v84 - v95 = add : ptr v7, v280 - v96 = load : f32 v2, v85, v95 - v101 = add : ptr v8, v280 - v102 = load : f32 v2, v85, v101 - v103 = fmul : f32 v96, v102 - v108 = add : ptr v7, v284 - v109 = load : f32 v2, v85, v108 - v114 = add : ptr v8, v284 - v115 = load : f32 v2, v85, v114 - v116 = fmul : f32 v109, v115 - v117 = fsub : f32 v103, v116 - v118 = store : mem v2, v85, v5, v117 - v123 = add : ptr v5, v260 - v129 = load : f32 v2, v118, v95 - v135 = load : f32 v2, v118, v114 - v136 = fmul : f32 v129, v135 - v142 = load : f32 v2, v118, v108 - v148 = load : f32 v2, v118, v101 - v149 = fmul : f32 v142, v148 - v150 = fadd : f32 v136, v149 - v151 = store : mem v2, v118, v123, v150 - v156 = add : ptr v5, v266 - v161 = add : ptr v7, v300 - v162 = load : f32 v2, v151, v161 - v167 = add : ptr v8, v300 - v168 = load : f32 v2, v151, v167 - v169 = fmul : f32 v162, v168 - v174 = add : ptr v7, v304 - v175 = load : f32 v2, v151, v174 - v180 = add : ptr v8, v304 - v181 = load : f32 v2, v151, v180 - v182 = fmul : f32 v175, v181 - v183 = fsub : f32 v169, v182 - v184 = store : mem v2, v151, v156, v183 - v189 = add : ptr v5, v272 - v195 = load : f32 v2, v184, v161 - v201 = load : f32 v2, v184, v180 - v202 = fmul : f32 v195, v201 - v208 = load : f32 v2, v184, v174 - v214 = load : f32 v2, v184, v167 - v215 = fmul : f32 v208, v214 - v216 = fadd : f32 v202, v215 - v217 = store : mem v2, v184, v189, v216 - v227 = store : mem v2, v217, v6, v326 - v232 = add : ptr v6, v260 - v233 = const : i32 2246822519 - v234 = add : i32 v9, v233 - v235 = store : mem v2, v227, v232, v234 - v240 = add : ptr v6, v266 - v243 = store : mem v2, v235, v240, v9 - v248 = add : ptr v6, v272 - v249 = const : i32 2654435761 - v250 = sub : i32 v9, v249 - v251 = store : mem v2, v243, v248, v250 - v252 = return : ctrl v2, v251 - v260 = const : i64 4 - v266 = const : i64 8 - v272 = const : i64 12 - v280 = const : i64 16 - v284 = const : i64 20 - v300 = const : i64 24 - v304 = const : i64 28 - v325 = const : i32 606290984 - v326 = add : i32 v9, v325 -} - -@expect -func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict, i32) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr, ptr, i32) - v1 = stop : ctrl v47 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : ptr #3 "arg1" of v0 - v6 = proj : ptr #4 "arg2" of v0 - v7 = proj : ptr #5 "arg3" of v0 - v8 = proj : ptr #6 "arg4" of v0 - v9 = proj : i32 #7 "arg5" of v0 - v10 = const : i32 2246822519 - v11 = add : i32 v9, v10 - v12 = const : i32 2654435761 - v13 = sub : i32 v9, v12 - v14 = const : i64 4 - v15 = const : i64 8 - v16 = const : i64 12 - v17 = const : i32 606290984 - v18 = add : i32 v9, v17 - v19 = add : ptr v6, v14 - v20 = add : ptr v6, v15 - v21 = add : ptr v6, v16 - v22 = load : <4 x f32> v2, v3, v7 - v23 = load : <4 x f32> v2, v3, v8 - v24 = fadd : <4 x f32> v22, v23 - v25 = fsub : <4 x f32> v22, v23 - v26 = shuffle : <4 x f32> #29264 v24, v25 - v27 = store : mem v2, v3, v4, v26 - v28 = const : i64 16 - v29 = add : ptr v7, v28 - v30 = load : <4 x f32> v2, v3, v29 - v31 = shuffle : <4 x f32> #8704 v30, v30 - v32 = const : i64 16 - v33 = add : ptr v8, v32 - v34 = load : <4 x f32> v2, v3, v33 - v35 = fmul : <4 x f32> v31, v34 - v36 = shuffle : <4 x f32> #13073 v30, v30 - v37 = shuffle : <4 x f32> #8961 v34, v34 - v38 = fmul : <4 x f32> v36, v37 - v39 = fsub : <4 x f32> v35, v38 - v40 = fadd : <4 x f32> v35, v38 - v41 = shuffle : <4 x f32> #29264 v39, v40 - v42 = store : mem v2, v27, v5, v41 - v43 = store : mem v2, v42, v6, v18 - v44 = store : mem v2, v43, v19, v11 - v45 = store : mem v2, v44, v20, v9 - v46 = store : mem v2, v45, v21, v13 - v47 = return : ctrl v2, v46 -} diff --git a/src/backend/test/slp_pack_lane_ops.rat b/src/backend/test/slp_pack_lane_ops.rat deleted file mode 100644 index 86dbd4f8..00000000 --- a/src/backend/test/slp_pack_lane_ops.rat +++ /dev/null @@ -1,122 +0,0 @@ -@name slp: a min select shifted by a constant and a sign extension converted to float pack as vector lane ops -@passes slp verify - -@input -var a : [4 x i32] = "" -var b : [4 x i32] = "" -var s : [4 x i16] = "" -var d : [4 x i32] = "" -var e : [4 x f32] = "" - -func t() -> void { - v0 = start : (ctrl, mem) - v1 = stop : ctrl v200 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = global : ptr "a" - v5 = global : ptr "b" - v6 = global : ptr "s" - v7 = global : ptr "d" - v8 = global : ptr "e" - v9 = const : i32 3 - v20 = const : i64 0 - v21 = const : i64 0 - v22 = add : ptr v4, v20 - v23 = add : ptr v5, v20 - v24 = add : ptr v6, v21 - v25 = add : ptr v7, v20 - v26 = add : ptr v8, v20 - v27 = load : i32 v2, v3, v22 - v28 = load : i32 v2, v3, v23 - v29 = load : i16 v2, v3, v24 - v30 = slt : i1 v27, v28 - v31 = select : i32 v30, v27, v28 - v32 = shl : i32 v31, v9 - v33 = sext : i32 v29 - v34 = sitofp : f32 v33 - v40 = const : i64 4 - v41 = const : i64 2 - v42 = add : ptr v4, v40 - v43 = add : ptr v5, v40 - v44 = add : ptr v6, v41 - v45 = add : ptr v7, v40 - v46 = add : ptr v8, v40 - v47 = load : i32 v2, v3, v42 - v48 = load : i32 v2, v3, v43 - v49 = load : i16 v2, v3, v44 - v50 = slt : i1 v47, v48 - v51 = select : i32 v50, v47, v48 - v52 = shl : i32 v51, v9 - v53 = sext : i32 v49 - v54 = sitofp : f32 v53 - v60 = const : i64 8 - v61 = const : i64 4 - v62 = add : ptr v4, v60 - v63 = add : ptr v5, v60 - v64 = add : ptr v6, v61 - v65 = add : ptr v7, v60 - v66 = add : ptr v8, v60 - v67 = load : i32 v2, v3, v62 - v68 = load : i32 v2, v3, v63 - v69 = load : i16 v2, v3, v64 - v70 = slt : i1 v67, v68 - v71 = select : i32 v70, v67, v68 - v72 = shl : i32 v71, v9 - v73 = sext : i32 v69 - v74 = sitofp : f32 v73 - v80 = const : i64 12 - v81 = const : i64 6 - v82 = add : ptr v4, v80 - v83 = add : ptr v5, v80 - v84 = add : ptr v6, v81 - v85 = add : ptr v7, v80 - v86 = add : ptr v8, v80 - v87 = load : i32 v2, v3, v82 - v88 = load : i32 v2, v3, v83 - v89 = load : i16 v2, v3, v84 - v90 = slt : i1 v87, v88 - v91 = select : i32 v90, v87, v88 - v92 = shl : i32 v91, v9 - v93 = sext : i32 v89 - v94 = sitofp : f32 v93 - v35 = store : mem v2, v3, v25, v32 - v55 = store : mem v2, v35, v45, v52 - v75 = store : mem v2, v55, v65, v72 - v95 = store : mem v2, v75, v85, v92 - v36 = store : mem v2, v95, v26, v34 - v56 = store : mem v2, v36, v46, v54 - v76 = store : mem v2, v56, v66, v74 - v96 = store : mem v2, v76, v86, v94 - v200 = return : ctrl v2, v96 -} - -@expect -var a : [4 x i32] = "" -var b : [4 x i32] = "" -var s : [4 x i16] = "" -var d : [4 x i32] = "" -var e : [4 x f32] = "" - -func t() -> void { - v0 = start : (ctrl, mem) - v1 = stop : ctrl v20 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = global : ptr "a" - v5 = global : ptr "b" - v6 = global : ptr "s" - v7 = global : ptr "d" - v8 = global : ptr "e" - v9 = const : i32 3 - v10 = load : <4 x i32> v2, v3, v4 - v11 = load : <4 x i32> v2, v3, v5 - v12 = slt : <4 x i32> v10, v11 - v13 = select : <4 x i32> v12, v10, v11 - v14 = shl : <4 x i32> v13, v9 - v15 = store : mem v2, v3, v7, v14 - v16 = load : <4 x i16> v2, v3, v6 - v17 = sext : <4 x i32> v16 - v18 = sitofp : <4 x f32> v17 - v19 = store : mem v2, v15, v8, v18 - v20 = return : ctrl v2, v19 -} diff --git a/src/backend/test/slp_pack_overlap_window.rat b/src/backend/test/slp_pack_overlap_window.rat deleted file mode 100644 index ca004e93..00000000 --- a/src/backend/test/slp_pack_overlap_window.rat +++ /dev/null @@ -1,56 +0,0 @@ -@name slp: a load range that overlaps the store window but reads the incoming state packs -@passes slp - -@input -func t(ptr) -> void { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v20 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = const : i64 4 - v6 = const : i64 8 - v7 = const : i64 12 - v8 = const : i64 16 - v9 = const : i64 20 - v10 = const : i64 24 - v11 = add : ptr v4, v5 - v12 = add : ptr v4, v6 - v13 = add : ptr v4, v7 - v14 = add : ptr v4, v8 - v15 = add : ptr v4, v9 - v16 = add : ptr v4, v10 - v30 = load : i32 v2, v3, v13 - v31 = load : i32 v2, v3, v14 - v32 = load : i32 v2, v3, v15 - v33 = load : i32 v2, v3, v16 - v40 = load : i32 v2, v3, v4 - v41 = load : i32 v2, v3, v11 - v42 = load : i32 v2, v3, v12 - v43 = load : i32 v2, v3, v13 - v50 = xor : i32 v40, v30 - v51 = xor : i32 v41, v31 - v52 = xor : i32 v42, v32 - v53 = xor : i32 v43, v33 - v60 = store : mem v2, v3, v4, v50 - v61 = store : mem v2, v60, v11, v51 - v62 = store : mem v2, v61, v12, v52 - v63 = store : mem v2, v62, v13, v53 - v20 = return : ctrl v2, v63 -} - -@expect -func t(ptr) -> void { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v11 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = load : <4 x i32> v2, v3, v4 - v6 = const : i64 12 - v7 = add : ptr v4, v6 - v8 = load : <4 x i32> v2, v3, v7 - v9 = xor : <4 x i32> v5, v8 - v10 = store : mem v2, v3, v4, v9 - v11 = return : ctrl v2, v10 -} diff --git a/src/backend/test/slp_pack_partly_constant.rat b/src/backend/test/slp_pack_partly_constant.rat deleted file mode 100644 index 24f134bc..00000000 --- a/src/backend/test/slp_pack_partly_constant.rat +++ /dev/null @@ -1,70 +0,0 @@ -@name slp: a build vector of variable and constant lanes costs an insert per variable lane and a constant base, which a lane 0 into zeros does not need, so one variable among zero lanes stores as one vector -@passes loadcanon slp - -@input -func t(ptr restrict, ptr restrict, ptr restrict, i32, f32) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr, i32, f32) - v1 = stop : ctrl v76 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : ptr #3 "arg1" of v0 - v6 = proj : ptr #4 "arg2" of v0 - v7 = proj : i32 #5 "arg3" of v0 - v8 = proj : f32 #6 "arg4" of v0 - v9 = store : mem v2, v3, v4, v7 - v10 = const : i64 4 - v11 = add : ptr v4, v10 - v12 = const : i32 0 - v13 = store : mem v2, v9, v11, v12 - v14 = const : i64 8 - v15 = add : ptr v4, v14 - v17 = store : mem v2, v13, v15, v12 - v18 = const : i64 12 - v19 = add : ptr v4, v18 - v21 = store : mem v2, v17, v19, v12 - v32 = load : f32 v2, v21, v6 - v33 = fmul : f32 v32, v8 - v34 = store : mem v2, v21, v5, v33 - v39 = add : ptr v5, v10 - v44 = add : ptr v6, v10 - v45 = load : f32 v2, v34, v44 - v46 = const : f32 1073741824 - v47 = fmul : f32 v45, v46 - v48 = store : mem v2, v34, v39, v47 - v53 = add : ptr v5, v14 - v58 = add : ptr v6, v14 - v59 = load : f32 v2, v48, v58 - v60 = fmul : f32 v59, v8 - v61 = store : mem v2, v48, v53, v60 - v66 = add : ptr v5, v18 - v71 = add : ptr v6, v18 - v72 = load : f32 v2, v61, v71 - v73 = const : f32 1077936128 - v74 = fmul : f32 v72, v73 - v75 = store : mem v2, v61, v66, v74 - v76 = return : ctrl v2, v75 -} - -@expect -func t(ptr restrict, ptr restrict, ptr restrict, i32, f32) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr, i32, f32) - v1 = stop : ctrl v40 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : ptr #3 "arg1" of v0 - v6 = proj : ptr #4 "arg2" of v0 - v7 = proj : i32 #5 "arg3" of v0 - v8 = proj : f32 #6 "arg4" of v0 - v12 = const : i32 0 - v26 = const : f32 1073741824 - v37 = const : f32 1077936128 - v40 = return : ctrl v2, v46 - v41 = pack : <4 x i32> v7, v12, v12, v12 - v42 = store : mem v2, v3, v4, v41 - v43 = load : <4 x f32> v2, v3, v6 - v44 = pack : <4 x f32> v8, v26, v8, v37 - v45 = fmul : <4 x f32> v43, v44 - v46 = store : mem v2, v42, v5, v45 -} diff --git a/src/backend/test/slp_pack_permuted_loads.rat b/src/backend/test/slp_pack_permuted_loads.rat deleted file mode 100644 index b2a6e469..00000000 --- a/src/backend/test/slp_pack_permuted_loads.rat +++ /dev/null @@ -1,160 +0,0 @@ -@name slp: lanes reversed in one block load it once and shuffle, every other element of two blocks loads both and shuffles each half, and every other element without a load of the elements between stays scalar -@passes loadcanon slp - -@input -func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr, ptr, ptr) - v1 = stop : ctrl v218 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : ptr #3 "arg1" of v0 - v6 = proj : ptr #4 "arg2" of v0 - v7 = proj : ptr #5 "arg3" of v0 - v8 = proj : ptr #6 "arg4" of v0 - v9 = proj : ptr #7 "arg5" of v0 - v19 = add : ptr v7, v222 - v20 = load : i32 v2, v3, v19 - v26 = load : i32 v2, v3, v9 - v27 = add : i32 v20, v26 - v28 = store : mem v2, v3, v4, v27 - v33 = add : ptr v4, v226 - v38 = add : ptr v7, v228 - v39 = load : i32 v2, v28, v38 - v44 = add : ptr v9, v226 - v45 = load : i32 v2, v28, v44 - v46 = add : i32 v39, v45 - v47 = store : mem v2, v28, v33, v46 - v52 = add : ptr v4, v228 - v57 = add : ptr v7, v226 - v58 = load : i32 v2, v47, v57 - v63 = add : ptr v9, v228 - v64 = load : i32 v2, v47, v63 - v65 = add : i32 v58, v64 - v66 = store : mem v2, v47, v52, v65 - v71 = add : ptr v4, v222 - v77 = load : i32 v2, v66, v7 - v82 = add : ptr v9, v222 - v83 = load : i32 v2, v66, v82 - v84 = add : i32 v77, v83 - v85 = store : mem v2, v66, v71, v84 - v96 = load : f32 v2, v85, v8 - v101 = add : ptr v8, v226 - v102 = load : f32 v2, v85, v101 - v103 = fsub : f32 v96, v102 - v104 = store : mem v2, v85, v5, v103 - v109 = add : ptr v5, v226 - v114 = add : ptr v8, v228 - v115 = load : f32 v2, v104, v114 - v120 = add : ptr v8, v222 - v121 = load : f32 v2, v104, v120 - v122 = fsub : f32 v115, v121 - v123 = store : mem v2, v104, v109, v122 - v128 = add : ptr v5, v228 - v133 = add : ptr v8, v258 - v134 = load : f32 v2, v123, v133 - v139 = add : ptr v8, v260 - v140 = load : f32 v2, v123, v139 - v141 = fsub : f32 v134, v140 - v142 = store : mem v2, v123, v128, v141 - v147 = add : ptr v5, v222 - v152 = add : ptr v8, v264 - v153 = load : f32 v2, v142, v152 - v158 = add : ptr v8, v266 - v159 = load : f32 v2, v142, v158 - v160 = fsub : f32 v153, v159 - v161 = store : mem v2, v142, v147, v160 - v171 = add : ptr v9, v270 - v172 = load : i32 v2, v161, v171 - v173 = const : i32 1 - v174 = add : i32 v172, v173 - v175 = store : mem v2, v161, v6, v174 - v180 = add : ptr v6, v226 - v185 = add : ptr v9, v274 - v186 = load : i32 v2, v175, v185 - v187 = const : i32 2 - v188 = add : i32 v186, v187 - v189 = store : mem v2, v175, v180, v188 - v194 = add : ptr v6, v228 - v199 = add : ptr v9, v278 - v200 = load : i32 v2, v189, v199 - v201 = const : i32 3 - v202 = add : i32 v200, v201 - v203 = store : mem v2, v189, v194, v202 - v208 = add : ptr v6, v222 - v213 = add : ptr v9, v282 - v214 = load : i32 v2, v203, v213 - v215 = const : i32 4 - v216 = add : i32 v214, v215 - v217 = store : mem v2, v203, v208, v216 - v218 = return : ctrl v2, v217 - v222 = const : i64 12 - v226 = const : i64 4 - v228 = const : i64 8 - v258 = const : i64 16 - v260 = const : i64 20 - v264 = const : i64 24 - v266 = const : i64 28 - v270 = const : i64 32 - v274 = const : i64 40 - v278 = const : i64 48 - v282 = const : i64 56 -} - -@expect -func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict) -> void { - v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr, ptr, ptr) - v1 = stop : ctrl v95 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "arg0" of v0 - v5 = proj : ptr #3 "arg1" of v0 - v6 = proj : ptr #4 "arg2" of v0 - v7 = proj : ptr #5 "arg3" of v0 - v8 = proj : ptr #6 "arg4" of v0 - v9 = proj : ptr #7 "arg5" of v0 - v11 = const : i32 1 - v12 = const : i32 2 - v13 = const : i32 3 - v14 = const : i32 4 - v15 = const : i64 12 - v16 = const : i64 4 - v17 = const : i64 8 - v22 = const : i64 32 - v23 = const : i64 40 - v24 = const : i64 48 - v25 = const : i64 56 - v76 = add : ptr v9, v22 - v77 = load : i32 v2, v3, v76 - v78 = add : i32 v77, v11 - v79 = store : mem v2, v108, v6, v78 - v80 = add : ptr v6, v16 - v81 = add : ptr v9, v23 - v82 = load : i32 v2, v3, v81 - v83 = add : i32 v82, v12 - v84 = store : mem v2, v79, v80, v83 - v85 = add : ptr v6, v17 - v86 = add : ptr v9, v24 - v87 = load : i32 v2, v3, v86 - v88 = add : i32 v87, v13 - v89 = store : mem v2, v84, v85, v88 - v90 = add : ptr v6, v15 - v91 = add : ptr v9, v25 - v92 = load : i32 v2, v3, v91 - v93 = add : i32 v92, v14 - v94 = store : mem v2, v89, v90, v93 - v95 = return : ctrl v2, v94 - v96 = load : <4 x i32> v2, v3, v7 - v97 = shuffle : <4 x i32> #291 v96, v96 - v98 = load : <4 x i32> v2, v3, v9 - v99 = add : <4 x i32> v97, v98 - v100 = store : mem v2, v3, v4, v99 - v101 = load : <4 x f32> v2, v3, v8 - v102 = const : i64 16 - v103 = add : ptr v8, v102 - v104 = load : <4 x f32> v2, v3, v103 - v105 = shuffle : <4 x f32> #25632 v101, v104 - v106 = shuffle : <4 x f32> #30001 v101, v104 - v107 = fsub : <4 x f32> v105, v106 - v108 = store : mem v2, v100, v5, v107 -} diff --git a/src/backend/test/slp_pack_pointer_lanes.rat b/src/backend/test/slp_pack_pointer_lanes.rat deleted file mode 100644 index 98c1a04c..00000000 --- a/src/backend/test/slp_pack_pointer_lanes.rat +++ /dev/null @@ -1,39 +0,0 @@ -@name slp: pointer stores pack as integer lanes -@passes slp - -@input -var a : [2 x ptr] = "" -var b : [2 x ptr] = "" - -func t() -> void { - v0 = start : (ctrl, mem) - v1 = stop : ctrl v20 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = global : ptr "a" - v5 = global : ptr "b" - v6 = const : i64 8 - v7 = add : ptr v4, v6 - v8 = add : ptr v5, v6 - v9 = load : ptr v2, v3, v5 - v10 = store : mem v2, v3, v4, v9 - v11 = load : ptr v2, v10, v8 - v12 = store : mem v2, v10, v7, v11 - v20 = return : ctrl v2, v12 -} - -@expect -var a : [2 x ptr] = "" -var b : [2 x ptr] = "" - -func t() -> void { - v0 = start : (ctrl, mem) - v1 = stop : ctrl v8 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = global : ptr "a" - v5 = global : ptr "b" - v6 = load : <2 x ptr> v2, v3, v5 - v7 = store : mem v2, v3, v4, v6 - v8 = return : ctrl v2, v7 -} diff --git a/src/backend/test/slp_pack_reread_above_memin.rat b/src/backend/test/slp_pack_reread_above_memin.rat deleted file mode 100644 index a41c6aa8..00000000 --- a/src/backend/test/slp_pack_reread_above_memin.rat +++ /dev/null @@ -1,48 +0,0 @@ -@name slp: a lane load hoisted above the window's incoming state is re-read there, as no store in between touches its bytes -@passes slp - -@input -func t(ptr, i64) -> void { - v0 = start : (ctrl, mem, ptr, i64) - v1 = stop : ctrl v60 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "g" of v0 - v5 = proj : i64 #3 "x" of v0 - v6 = alloc : ptr [16 x i8] - v7 = alloc : ptr [16 x i8] - v8 = const : i64 8 - v9 = add : ptr v6, v8 - v10 = add : ptr v7, v8 - v11 = const : i64 0 - v12 = pack : <2 x i64> v11, v5 - v13 = store : mem v2, v3, v6, v12 - v14 = load : i64 v2, v13, v6 - v15 = store : mem v2, v13, v7, v14 - v16 = load : i64 v2, v15, v9 - v17 = store : mem v2, v15, v10, v16 - v18 = call : (ctrl, mem) "use" v2, v17, v7 - v19 = proj : ctrl #0 "ctrl" of v18 - v20 = proj : mem #1 "mem" of v18 - v60 = return : ctrl v19, v20 -} - -@expect -func t(ptr, i64) -> void { - v0 = start : (ctrl, mem, ptr, i64) - v1 = stop : ctrl v15 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : i64 #3 "x" of v0 - v5 = alloc : ptr [16 x i8] - v6 = alloc : ptr [16 x i8] - v7 = const : i64 0 - v8 = pack : <2 x i64> v7, v4 - v9 = store : mem v2, v3, v5, v8 - v10 = load : <2 x i64> v2, v9, v5 - v11 = store : mem v2, v9, v6, v10 - v12 = call : (ctrl, mem) "use" v2, v11, v6 - v13 = proj : ctrl #0 "ctrl" of v12 - v14 = proj : mem #1 "mem" of v12 - v15 = return : ctrl v13, v14 -} diff --git a/src/backend/test/slp_pack_restrict_param.rat b/src/backend/test/slp_pack_restrict_param.rat deleted file mode 100644 index 4b6aaba7..00000000 --- a/src/backend/test/slp_pack_restrict_param.rat +++ /dev/null @@ -1,43 +0,0 @@ -@name slp: parameter loads between stores through a restrict parameter are re-read from the entry state, so the copy packs statically -@passes slp - -@input -func t(ptr restrict, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v40 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "d" of v0 - v5 = proj : ptr #3 "a" of v0 - v6 = const : i64 4 - v7 = const : i64 8 - v8 = const : i64 12 - v9 = add : ptr v4, v6 - v10 = add : ptr v4, v7 - v11 = add : ptr v4, v8 - v12 = add : ptr v5, v6 - v13 = add : ptr v5, v7 - v14 = add : ptr v5, v8 - v20 = load : i32 v2, v3, v5 - v21 = store : mem v2, v3, v4, v20 - v22 = load : i32 v2, v21, v12 - v23 = store : mem v2, v21, v9, v22 - v24 = load : i32 v2, v23, v13 - v25 = store : mem v2, v23, v10, v24 - v26 = load : i32 v2, v25, v14 - v27 = store : mem v2, v25, v11, v26 - v40 = return : ctrl v2, v27 -} - -@expect -func t(ptr restrict, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v8 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "d" of v0 - v5 = proj : ptr #3 "a" of v0 - v6 = load : <4 x i32> v2, v3, v5 - v7 = store : mem v2, v3, v4, v6 - v8 = return : ctrl v2, v7 -} diff --git a/src/backend/test/slp_pack_unhoisted_observers.rat b/src/backend/test/slp_pack_unhoisted_observers.rat deleted file mode 100644 index 322ce221..00000000 --- a/src/backend/test/slp_pack_unhoisted_observers.rat +++ /dev/null @@ -1,155 +0,0 @@ -@name slp: observers whose address cone is too big to hoist are re-read under a static fact, no guard -@passes slp - -@input -var a : [512 x i32] = "" -var c : [4 x i32] = "" - -func t(i64) -> void { - v0 = start : (ctrl, mem, i64) - v1 = stop : ctrl v90 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : i64 #2 "x" of v0 - v5 = global : ptr "c" - v6 = global : ptr "a" - v7 = const : i64 4 - v8 = const : i64 8 - v9 = const : i64 12 - v10 = xor : i64 v4, v4 - v11 = xor : i64 v10, v4 - v12 = xor : i64 v11, v4 - v13 = xor : i64 v12, v4 - v14 = xor : i64 v13, v4 - v15 = xor : i64 v14, v4 - v16 = xor : i64 v15, v4 - v17 = xor : i64 v16, v4 - v18 = xor : i64 v17, v4 - v19 = xor : i64 v18, v4 - v20 = xor : i64 v19, v4 - v21 = xor : i64 v20, v4 - v22 = xor : i64 v21, v4 - v23 = xor : i64 v22, v4 - v24 = xor : i64 v23, v4 - v25 = xor : i64 v24, v4 - v26 = xor : i64 v25, v4 - v27 = xor : i64 v26, v4 - v28 = xor : i64 v27, v4 - v29 = xor : i64 v28, v4 - v30 = xor : i64 v29, v4 - v31 = xor : i64 v30, v4 - v32 = xor : i64 v31, v4 - v33 = xor : i64 v32, v4 - v34 = xor : i64 v33, v4 - v35 = xor : i64 v34, v4 - v36 = xor : i64 v35, v4 - v37 = xor : i64 v36, v4 - v38 = xor : i64 v37, v4 - v39 = xor : i64 v38, v4 - v40 = xor : i64 v39, v4 - v41 = xor : i64 v40, v4 - v42 = xor : i64 v41, v4 - v43 = xor : i64 v42, v4 - v44 = xor : i64 v43, v4 - v45 = xor : i64 v44, v4 - v46 = xor : i64 v45, v4 - v47 = xor : i64 v46, v4 - v48 = xor : i64 v47, v4 - v49 = xor : i64 v48, v4 - v50 = xor : i64 v49, v4 - v51 = xor : i64 v50, v4 - v52 = xor : i64 v51, v4 - v53 = xor : i64 v52, v4 - v54 = xor : i64 v53, v4 - v55 = xor : i64 v54, v4 - v56 = xor : i64 v55, v4 - v57 = xor : i64 v56, v4 - v58 = xor : i64 v57, v4 - v59 = xor : i64 v58, v4 - v60 = xor : i64 v59, v4 - v61 = xor : i64 v60, v4 - v62 = xor : i64 v61, v4 - v63 = xor : i64 v62, v4 - v64 = xor : i64 v63, v4 - v65 = xor : i64 v64, v4 - v66 = xor : i64 v65, v4 - v67 = xor : i64 v66, v4 - v68 = xor : i64 v67, v4 - v69 = xor : i64 v68, v4 - v70 = mul : i64 v69, v7 - v71 = add : ptr v6, v70 - v72 = add : ptr v71, v7 - v73 = add : ptr v71, v8 - v74 = add : ptr v71, v9 - v75 = add : ptr v5, v7 - v76 = add : ptr v5, v8 - v77 = add : ptr v5, v9 - v78 = load : i32 v2, v3, v71 - v79 = add : i32 v78, v78 - v80 = store : mem v2, v3, v5, v79 - v81 = load : i32 v2, v80, v72 - v82 = add : i32 v81, v81 - v83 = store : mem v2, v80, v75, v82 - v84 = load : i32 v2, v83, v73 - v85 = add : i32 v84, v84 - v86 = store : mem v2, v83, v76, v85 - v87 = load : i32 v2, v86, v74 - v88 = add : i32 v87, v87 - v89 = store : mem v2, v86, v77, v88 - v90 = return : ctrl v2, v89 -} - -@expect -var a : [512 x i32] = "" -var c : [4 x i32] = "" - -func t(i64) -> void { - v0 = start : (ctrl, mem, i64) - v1 = stop : ctrl v47 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : i64 #2 "x" of v0 - v5 = global : ptr "c" - v6 = global : ptr "a" - v7 = const : i64 4 - v8 = splat : <2 x i64> v4 - v9 = xor : <2 x i64> v8, v8 - v10 = xor : <2 x i64> v9, v8 - v11 = xor : <2 x i64> v10, v8 - v12 = xor : <2 x i64> v11, v8 - v13 = xor : <2 x i64> v12, v8 - v14 = xor : <2 x i64> v13, v8 - v15 = xor : <2 x i64> v14, v8 - v16 = xor : <2 x i64> v15, v8 - v17 = xor : <2 x i64> v16, v8 - v18 = xor : <2 x i64> v17, v8 - v19 = xor : <2 x i64> v18, v8 - v20 = xor : <2 x i64> v19, v8 - v21 = xor : <2 x i64> v20, v8 - v22 = xor : <2 x i64> v21, v8 - v23 = xor : <2 x i64> v22, v8 - v24 = xor : <2 x i64> v23, v8 - v25 = xor : <2 x i64> v24, v8 - v26 = xor : <2 x i64> v25, v8 - v27 = xor : <2 x i64> v26, v8 - v28 = xor : <2 x i64> v27, v8 - v29 = xor : <2 x i64> v28, v8 - v30 = xor : <2 x i64> v29, v8 - v31 = xor : <2 x i64> v30, v8 - v32 = xor : <2 x i64> v31, v8 - v33 = xor : <2 x i64> v32, v8 - v34 = xor : <2 x i64> v33, v8 - v35 = xor : <2 x i64> v34, v8 - v36 = xor : <2 x i64> v35, v8 - v37 = xor : <2 x i64> v36, v8 - v38 = shuffle : <2 x i64> #1 v37, v37 - v39 = xor : <2 x i64> v37, v38 - v40 = extract : i64 #0 v39 - v41 = xor : i64 v40, v4 - v42 = mul : i64 v41, v7 - v43 = add : ptr v6, v42 - v44 = load : <4 x i32> v2, v3, v43 - v45 = add : <4 x i32> v44, v44 - v46 = store : mem v2, v3, v5, v45 - v47 = return : ctrl v2, v46 -} diff --git a/src/backend/test/slp_partial_permute.rat b/src/backend/test/slp_partial_permute.rat new file mode 100644 index 00000000..f1acb2cc --- /dev/null +++ b/src/backend/test/slp_partial_permute.rat @@ -0,0 +1,117 @@ +@name slp: lanes that skip elements of a window load it once and shuffle when live loads read the gaps, else they gather +@passes slp + +@input +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v20 = load : f32 v2, v3, v5 + v21 = load : f32 v2, v3, v13 + v22 = load : f32 v2, v3, v14 + v23 = load : f32 v2, v3, v15 + v28 = fsub : f32 v20, v21 + v29 = fadd : f32 v20, v21 + v30 = fsub : f32 v22, v23 + v31 = fadd : f32 v22, v23 + v32 = store : mem v2, v3, v4, v28 + v33 = store : mem v2, v32, v10, v29 + v34 = store : mem v2, v33, v11, v30 + v35 = store : mem v2, v34, v12, v31 + v40 = return : ctrl v2, v35 +} + +func u(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v16 = add : ptr v6, v7 + v17 = add : ptr v6, v8 + v18 = add : ptr v6, v9 + v19 = const : i64 16 + v20 = load : f32 v2, v3, v5 + v21 = load : f32 v2, v3, v13 + v22 = load : f32 v2, v3, v14 + v24 = load : f32 v2, v3, v6 + v25 = load : f32 v2, v3, v16 + v26 = load : f32 v2, v3, v17 + v27 = load : f32 v2, v3, v18 + v28 = fmul : f32 v20, v24 + v29 = fmul : f32 v20, v25 + v30 = fmul : f32 v22, v26 + v31 = fmul : f32 v22, v27 + v32 = store : mem v2, v3, v4, v28 + v33 = store : mem v2, v32, v10, v29 + v34 = store : mem v2, v33, v11, v30 + v35 = store : mem v2, v34, v12, v31 + v36 = add : ptr v4, v19 + v37 = store : mem v2, v35, v36, v21 + v40 = return : ctrl v2, v37 +} + +@expect +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v27 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v23 = store : mem v2, v3, v4, v33 + v27 = return : ctrl v2, v23 + v28 = load : <4 x f32> v2, v3, v5 + v29 = shuffle : <4 x f32> #8704 v28, v28 + v30 = shuffle : <4 x f32> #13073 v28, v28 + v31 = fsub : <4 x f32> v29, v30 + v32 = fadd : <4 x f32> v29, v30 + v33 = shuffle : <4 x f32> #29264 v31, v32 +} + +func u(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v36 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = proj : ptr #4 "b" of v0 + v7 = const : i64 4 + v8 = const : i64 8 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v18 = const : i64 16 + v19 = load : f32 v2, v3, v5 + v20 = load : f32 v2, v3, v13 + v21 = load : f32 v2, v3, v14 + v30 = store : mem v2, v3, v4, v39 + v34 = add : ptr v4, v18 + v35 = store : mem v2, v30, v34, v20 + v36 = return : ctrl v2, v35 + v37 = pack : <4 x f32> v19, v19, v21, v21 + v38 = load : <4 x f32> v2, v3, v6 + v39 = fmul : <4 x f32> v37, v38 +} diff --git a/src/backend/test/slp_permuted_load.rat b/src/backend/test/slp_permuted_load.rat new file mode 100644 index 00000000..f633a6a8 --- /dev/null +++ b/src/backend/test/slp_permuted_load.rat @@ -0,0 +1,49 @@ +@name slp: a permutation of consecutive loads becomes one wide load and a shuffle +@passes slp + +@input +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v20 = load : i32 v2, v3, v5 + v21 = load : i32 v2, v3, v13 + v22 = load : i32 v2, v3, v14 + v23 = load : i32 v2, v3, v15 + v28 = add : i32 v21, v20 + v29 = add : i32 v20, v21 + v30 = add : i32 v23, v22 + v31 = add : i32 v22, v23 + v32 = store : mem v2, v3, v4, v28 + v33 = store : mem v2, v32, v10, v29 + v34 = store : mem v2, v33, v11, v30 + v35 = store : mem v2, v34, v12, v31 + v40 = return : ctrl v2, v35 +} + +@expect +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v27 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v27 = return : ctrl v2, v31 + v28 = load : <4 x i32> v2, v3, v5 + v29 = shuffle : <4 x i32> #8961 v28, v28 + v30 = add : <4 x i32> v29, v28 + v31 = store : mem v2, v3, v4, v30 +} diff --git a/src/backend/test/slp_reduce_dot_i32.rat b/src/backend/test/slp_reduce_dot_i32.rat index dd7b74e6..8d009311 100644 --- a/src/backend/test/slp_reduce_dot_i32.rat +++ b/src/backend/test/slp_reduce_dot_i32.rat @@ -64,29 +64,29 @@ func dot(ptr, ptr) -> i32 { v99 = return : ctrl v2, v3, v66 } + @expect func dot(ptr, ptr) -> i32 { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v22 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = proj : ptr #3 "b" of v0 - v6 = load : <4 x i32> v2, v3, v4 - v7 = load : <4 x i32> v2, v3, v5 - v8 = mul : <4 x i32> v6, v7 - v9 = const : i64 16 - v10 = add : ptr v4, v9 - v11 = load : <4 x i32> v2, v3, v10 - v12 = const : i64 16 - v13 = add : ptr v5, v12 - v14 = load : <4 x i32> v2, v3, v13 - v15 = mul : <4 x i32> v11, v14 - v16 = add : <4 x i32> v8, v15 - v17 = shuffle : <4 x i32> #4146 v16, v16 - v18 = add : <4 x i32> v16, v17 - v19 = shuffle : <4 x i32> #8961 v18, v18 - v20 = add : <4 x i32> v18, v19 - v21 = extract : i32 #0 v20 - v22 = return : ctrl v2, v3, v21 + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v58 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : ptr #3 "b" of v0 + v9 = const : i64 16 + v16 = add : ptr v4, v9 + v23 = add : ptr v5, v9 + v58 = return : ctrl v2, v3, v70 + v59 = load : <4 x i32> v2, v3, v4 + v60 = load : <4 x i32> v2, v3, v5 + v61 = mul : <4 x i32> v59, v60 + v62 = load : <4 x i32> v2, v3, v16 + v63 = load : <4 x i32> v2, v3, v23 + v64 = mul : <4 x i32> v62, v63 + v65 = add : <4 x i32> v61, v64 + v66 = shuffle : <4 x i32> #4146 v65, v65 + v67 = add : <4 x i32> v65, v66 + v68 = shuffle : <4 x i32> #8961 v67, v67 + v69 = add : <4 x i32> v67, v68 + v70 = extract : i32 #0 v69 } diff --git a/src/backend/test/slp_reduce_kinds.rat b/src/backend/test/slp_reduce_kinds.rat deleted file mode 100644 index e57232f0..00000000 --- a/src/backend/test/slp_reduce_kinds.rat +++ /dev/null @@ -1,119 +0,0 @@ -@name slp: a four-term i32 dot product packs as one vector with a horizontal finish, an i64 xor chain as two-lane xors -@passes slp - -@input -func dot4(ptr restrict, ptr restrict) -> i32 { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v99 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = proj : ptr #3 "b" of v0 - v6 = const : i64 4 - v7 = const : i64 8 - v8 = const : i64 12 - v13 = add : ptr v4, v6 - v14 = add : ptr v4, v7 - v15 = add : ptr v4, v8 - v20 = add : ptr v5, v6 - v21 = add : ptr v5, v7 - v22 = add : ptr v5, v8 - v30 = load : i32 v2, v3, v4 - v31 = load : i32 v2, v3, v13 - v32 = load : i32 v2, v3, v14 - v33 = load : i32 v2, v3, v15 - v40 = load : i32 v2, v3, v5 - v41 = load : i32 v2, v3, v20 - v42 = load : i32 v2, v3, v21 - v43 = load : i32 v2, v3, v22 - v50 = mul : i32 v30, v40 - v51 = mul : i32 v31, v41 - v52 = mul : i32 v32, v42 - v53 = mul : i32 v33, v43 - v60 = add : i32 v50, v51 - v61 = add : i32 v60, v52 - v62 = add : i32 v61, v53 - v99 = return : ctrl v2, v3, v62 -} - -func xor8(ptr restrict) -> i64 { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v99 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v6 = const : i64 8 - v7 = const : i64 16 - v8 = const : i64 24 - v9 = const : i64 32 - v10 = const : i64 40 - v11 = const : i64 48 - v12 = const : i64 56 - v13 = add : ptr v4, v6 - v14 = add : ptr v4, v7 - v15 = add : ptr v4, v8 - v16 = add : ptr v4, v9 - v17 = add : ptr v4, v10 - v18 = add : ptr v4, v11 - v19 = add : ptr v4, v12 - v30 = load : i64 v2, v3, v4 - v31 = load : i64 v2, v3, v13 - v32 = load : i64 v2, v3, v14 - v33 = load : i64 v2, v3, v15 - v34 = load : i64 v2, v3, v16 - v35 = load : i64 v2, v3, v17 - v36 = load : i64 v2, v3, v18 - v37 = load : i64 v2, v3, v19 - v40 = xor : i64 v30, v31 - v41 = xor : i64 v40, v32 - v42 = xor : i64 v41, v33 - v43 = xor : i64 v42, v34 - v44 = xor : i64 v43, v35 - v45 = xor : i64 v44, v36 - v46 = xor : i64 v45, v37 - v99 = return : ctrl v2, v3, v46 -} - -@expect -func dot4(ptr restrict, ptr restrict) -> i32 { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v30 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = proj : ptr #3 "b" of v0 - v30 = return : ctrl v2, v3, v38 - v31 = load : <4 x i32> v2, v3, v4 - v32 = load : <4 x i32> v2, v3, v5 - v33 = mul : <4 x i32> v31, v32 - v34 = shuffle : <4 x i32> #4146 v33, v33 - v35 = add : <4 x i32> v33, v34 - v36 = shuffle : <4 x i32> #8961 v35, v35 - v37 = add : <4 x i32> v35, v36 - v38 = extract : i32 #0 v37 -} - -func xor8(ptr restrict) -> i64 { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v34 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v34 = return : ctrl v2, v3, v50 - v35 = load : <2 x i64> v2, v3, v4 - v36 = const : i64 16 - v37 = add : ptr v4, v36 - v38 = load : <2 x i64> v2, v3, v37 - v39 = const : i64 32 - v40 = add : ptr v4, v39 - v41 = load : <2 x i64> v2, v3, v40 - v42 = const : i64 48 - v43 = add : ptr v4, v42 - v44 = load : <2 x i64> v2, v3, v43 - v45 = xor : <2 x i64> v35, v38 - v46 = xor : <2 x i64> v45, v41 - v47 = xor : <2 x i64> v46, v44 - v48 = shuffle : <2 x i64> #1 v47, v47 - v49 = xor : <2 x i64> v47, v48 - v50 = extract : i64 #0 v49 -} diff --git a/src/backend/test/slp_reduce_odd_term_remainder.rat b/src/backend/test/slp_reduce_odd_term_remainder.rat deleted file mode 100644 index 0b49cee9..00000000 --- a/src/backend/test/slp_reduce_odd_term_remainder.rat +++ /dev/null @@ -1,101 +0,0 @@ -@name slp: reduction terms pack in source order, a term of another opcode gathers with the loads beside it -@passes slp - -@input -func t(ptr) -> i32 { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v55 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = const : i64 4 - v6 = const : i64 8 - v7 = const : i64 12 - v8 = const : i64 16 - v9 = const : i64 20 - v10 = const : i64 24 - v11 = const : i64 28 - v12 = const : i64 32 - v13 = const : i64 36 - v14 = const : i64 40 - v15 = const : i64 44 - v16 = const : i64 48 - v17 = add : ptr v4, v5 - v18 = add : ptr v4, v6 - v19 = add : ptr v4, v7 - v20 = add : ptr v4, v8 - v21 = add : ptr v4, v9 - v22 = add : ptr v4, v10 - v23 = add : ptr v4, v11 - v24 = add : ptr v4, v12 - v25 = add : ptr v4, v13 - v26 = add : ptr v4, v14 - v27 = add : ptr v4, v15 - v28 = add : ptr v4, v16 - v29 = load : i32 v2, v3, v4 - v30 = load : i32 v2, v3, v17 - v31 = load : i32 v2, v3, v18 - v32 = load : i32 v2, v3, v19 - v33 = load : i32 v2, v3, v20 - v34 = load : i32 v2, v3, v21 - v35 = load : i32 v2, v3, v22 - v36 = load : i32 v2, v3, v23 - v37 = load : i32 v2, v3, v24 - v38 = load : i32 v2, v3, v25 - v39 = load : i32 v2, v3, v26 - v40 = load : i32 v2, v3, v27 - v41 = load : i32 v2, v3, v28 - v42 = mul : i32 v29, v29 - v43 = add : i32 v42, v30 - v44 = add : i32 v43, v31 - v45 = add : i32 v44, v32 - v46 = add : i32 v45, v33 - v47 = add : i32 v46, v34 - v48 = add : i32 v47, v35 - v49 = add : i32 v48, v36 - v50 = add : i32 v49, v37 - v51 = add : i32 v50, v38 - v52 = add : i32 v51, v39 - v53 = add : i32 v52, v40 - v54 = add : i32 v53, v41 - v55 = return : ctrl v2, v3, v54 -} - -@expect -func t(ptr) -> i32 { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v34 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = const : i64 4 - v6 = const : i64 8 - v7 = const : i64 12 - v8 = const : i64 48 - v9 = add : ptr v4, v5 - v10 = add : ptr v4, v6 - v11 = add : ptr v4, v7 - v12 = add : ptr v4, v8 - v13 = load : i32 v2, v3, v4 - v14 = load : i32 v2, v3, v9 - v15 = load : i32 v2, v3, v10 - v16 = load : i32 v2, v3, v11 - v17 = load : i32 v2, v3, v12 - v18 = mul : i32 v13, v13 - v19 = pack : <4 x i32> v18, v14, v15, v16 - v20 = const : i64 16 - v21 = add : ptr v4, v20 - v22 = load : <4 x i32> v2, v3, v21 - v23 = const : i64 32 - v24 = add : ptr v4, v23 - v25 = load : <4 x i32> v2, v3, v24 - v26 = add : <4 x i32> v19, v22 - v27 = add : <4 x i32> v26, v25 - v28 = shuffle : <4 x i32> #4146 v27, v27 - v29 = add : <4 x i32> v27, v28 - v30 = shuffle : <4 x i32> #8961 v29, v29 - v31 = add : <4 x i32> v29, v30 - v32 = extract : i32 #0 v31 - v33 = add : i32 v32, v17 - v34 = return : ctrl v2, v3, v33 -} diff --git a/src/backend/test/slp_reduce_remainder_i32.rat b/src/backend/test/slp_reduce_remainder_i32.rat deleted file mode 100644 index 214b5570..00000000 --- a/src/backend/test/slp_reduce_remainder_i32.rat +++ /dev/null @@ -1,69 +0,0 @@ -@name slp: a 9-term i32 sum packs 8 terms into two vectors and adds the ninth to the extracted sum -@passes slp - -@input -func t(ptr) -> i32 { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v40 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v5 = const : i64 4 - v6 = const : i64 8 - v7 = const : i64 12 - v8 = const : i64 16 - v9 = const : i64 20 - v10 = const : i64 24 - v11 = const : i64 28 - v12 = const : i64 32 - v13 = add : ptr v4, v5 - v14 = add : ptr v4, v6 - v15 = add : ptr v4, v7 - v16 = add : ptr v4, v8 - v17 = add : ptr v4, v9 - v18 = add : ptr v4, v10 - v19 = add : ptr v4, v11 - v20 = add : ptr v4, v12 - v21 = load : i32 v2, v3, v4 - v22 = load : i32 v2, v3, v13 - v23 = load : i32 v2, v3, v14 - v24 = load : i32 v2, v3, v15 - v25 = load : i32 v2, v3, v16 - v26 = load : i32 v2, v3, v17 - v27 = load : i32 v2, v3, v18 - v28 = load : i32 v2, v3, v19 - v29 = load : i32 v2, v3, v20 - v31 = add : i32 v21, v22 - v32 = add : i32 v31, v23 - v33 = add : i32 v32, v24 - v34 = add : i32 v33, v25 - v35 = add : i32 v34, v26 - v36 = add : i32 v35, v27 - v37 = add : i32 v36, v28 - v38 = add : i32 v37, v29 - v40 = return : ctrl v2, v3, v38 -} - -@expect -func t(ptr) -> i32 { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v38 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "a" of v0 - v12 = const : i64 32 - v20 = add : ptr v4, v12 - v29 = load : i32 v2, v3, v20 - v38 = return : ctrl v2, v3, v49 - v39 = load : <4 x i32> v2, v3, v4 - v40 = const : i64 16 - v41 = add : ptr v4, v40 - v42 = load : <4 x i32> v2, v3, v41 - v43 = add : <4 x i32> v39, v42 - v44 = shuffle : <4 x i32> #4146 v43, v43 - v45 = add : <4 x i32> v43, v44 - v46 = shuffle : <4 x i32> #8961 v45, v45 - v47 = add : <4 x i32> v45, v46 - v48 = extract : i32 #0 v47 - v49 = add : i32 v48, v29 -} diff --git a/src/backend/test/slp_reject_dead_inner_reader.rat b/src/backend/test/slp_reject_dead_inner_reader.rat deleted file mode 100644 index a6083c65..00000000 --- a/src/backend/test/slp_reject_dead_inner_reader.rat +++ /dev/null @@ -1,58 +0,0 @@ -@name slp: a load of an inner state whose value reaches no store of the run is no covered lane, so the run that removes its state is rejected and the stores after the last such state pack -@passes slp - -@input -func t(ptr, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v30 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "p" of v0 - v5 = proj : ptr #3 "q" of v0 - v6 = const : i64 4 - v7 = const : i64 8 - v8 = const : i64 12 - v9 = add : ptr v4, v6 - v10 = add : ptr v4, v7 - v11 = add : ptr v4, v8 - v12 = add : ptr v5, v6 - v13 = add : ptr v5, v7 - v14 = add : ptr v5, v8 - v15 = load : i32 v2, v3, v5 - v16 = load : i32 v2, v3, v12 - v17 = load : i32 v2, v3, v13 - v18 = load : i32 v2, v3, v14 - v19 = store : mem v2, v3, v4, v15 - v20 = store : mem v2, v19, v9, v16 - v21 = store : mem v2, v20, v10, v17 - v22 = store : mem v2, v21, v11, v18 - v23 = load : i32 v2, v19, v5 - v24 = load : i32 v2, v20, v5 - v25 = add : i32 v23, v23 - v26 = add : i32 v24, v24 - v30 = return : ctrl v2, v22 -} - -@expect -func t(ptr, ptr) -> void { - v0 = start : (ctrl, mem, ptr, ptr) - v1 = stop : ctrl v27 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "p" of v0 - v5 = proj : ptr #3 "q" of v0 - v6 = const : i64 4 - v9 = add : ptr v4, v6 - v12 = add : ptr v5, v6 - v15 = load : i32 v2, v3, v5 - v16 = load : i32 v2, v3, v12 - v19 = store : mem v2, v3, v4, v15 - v20 = store : mem v2, v19, v9, v16 - v27 = return : ctrl v2, v33 - v28 = const : i64 8 - v29 = add : ptr v5, v28 - v30 = load : <2 x i32> v2, v3, v29 - v31 = const : i64 8 - v32 = add : ptr v4, v31 - v33 = store : mem v2, v20, v32, v30 -} diff --git a/src/backend/test/slp_reject_gathers.rat b/src/backend/test/slp_reject_gathers.rat new file mode 100644 index 00000000..a7fa38b9 --- /dev/null +++ b/src/backend/test/slp_reject_gathers.rat @@ -0,0 +1,50 @@ +@name slp: a window of unrelated scalars saves no op, so it stays scalar +@passes slp + +@input +func t(ptr, i32, i32, i32, i32) -> void { + v0 = start : (ctrl, mem, ptr, i32, i32, i32, i32) + v1 = stop : ctrl v30 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : i32 #3 "w" of v0 + v6 = proj : i32 #4 "x" of v0 + v7 = proj : i32 #5 "y" of v0 + v8 = proj : i32 #6 "z" of v0 + v9 = const : i64 4 + v10 = const : i64 8 + v11 = const : i64 12 + v12 = add : ptr v4, v9 + v13 = add : ptr v4, v10 + v14 = add : ptr v4, v11 + v20 = store : mem v2, v3, v4, v5 + v21 = store : mem v2, v20, v12, v6 + v22 = store : mem v2, v21, v13, v7 + v23 = store : mem v2, v22, v14, v8 + v30 = return : ctrl v2, v23 +} + +@expect +func t(ptr, i32, i32, i32, i32) -> void { + v0 = start : (ctrl, mem, ptr, i32, i32, i32, i32) + v1 = stop : ctrl v19 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : i32 #3 "w" of v0 + v6 = proj : i32 #4 "x" of v0 + v7 = proj : i32 #5 "y" of v0 + v8 = proj : i32 #6 "z" of v0 + v9 = const : i64 4 + v10 = const : i64 8 + v11 = const : i64 12 + v12 = add : ptr v4, v9 + v13 = add : ptr v4, v10 + v14 = add : ptr v4, v11 + v15 = store : mem v2, v3, v4, v5 + v16 = store : mem v2, v15, v12, v6 + v17 = store : mem v2, v16, v13, v7 + v18 = store : mem v2, v17, v14, v8 + v19 = return : ctrl v2, v18 +} diff --git a/src/backend/test/slp_reject_misaligned_lane.rat b/src/backend/test/slp_reject_misaligned_lane.rat deleted file mode 100644 index 449f4eaa..00000000 --- a/src/backend/test/slp_reject_misaligned_lane.rat +++ /dev/null @@ -1,107 +0,0 @@ -@name slp: a lane load that straddles a freshly stored lane must veto the window -@passes loadcanon slp - -@input -func t(ptr) -> void { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v300 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "c" of v0 - v5 = const : i64 2 - v6 = add : ptr v4, v5 - v10 = load : i32 v2, v3, v6 - v11 = const : i32 1 - v12 = add : i32 v10, v11 - v13 = store : mem v2, v3, v4, v12 - v14 = const : i64 4 - v15 = add : ptr v4, v14 - v16 = load : i32 v2, v13, v6 - v17 = const : i32 2 - v18 = add : i32 v16, v17 - v19 = store : mem v2, v13, v15, v18 - v20 = const : i64 8 - v21 = add : ptr v4, v20 - v22 = load : i32 v2, v19, v6 - v23 = const : i32 3 - v24 = add : i32 v22, v23 - v25 = store : mem v2, v19, v21, v24 - v26 = const : i64 12 - v27 = add : ptr v4, v26 - v28 = load : i32 v2, v25, v6 - v29 = const : i32 4 - v30 = add : i32 v28, v29 - v31 = store : mem v2, v25, v27, v30 - v32 = const : i64 16 - v33 = add : ptr v4, v32 - v34 = load : i32 v2, v31, v6 - v35 = const : i32 5 - v36 = add : i32 v34, v35 - v37 = store : mem v2, v31, v33, v36 - v38 = const : i64 20 - v39 = add : ptr v4, v38 - v40 = load : i32 v2, v37, v6 - v41 = const : i32 6 - v42 = add : i32 v40, v41 - v43 = store : mem v2, v37, v39, v42 - v44 = const : i64 24 - v45 = add : ptr v4, v44 - v46 = load : i32 v2, v43, v6 - v47 = const : i32 7 - v48 = add : i32 v46, v47 - v49 = store : mem v2, v43, v45, v48 - v50 = const : i64 28 - v51 = add : ptr v4, v50 - v52 = load : i32 v2, v49, v6 - v53 = const : i32 8 - v54 = add : i32 v52, v53 - v55 = store : mem v2, v49, v51, v54 - v300 = return : ctrl v2, v55 -} - -@expect -func t(ptr) -> void { - v0 = start : (ctrl, mem, ptr) - v1 = stop : ctrl v42 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "c" of v0 - v5 = const : i64 2 - v6 = add : ptr v4, v5 - v7 = load : i32 v2, v3, v6 - v8 = const : i32 1 - v9 = add : i32 v7, v8 - v10 = store : mem v2, v3, v4, v9 - v11 = const : i64 4 - v12 = add : ptr v4, v11 - v13 = load : i32 v2, v10, v6 - v14 = const : i32 2 - v15 = add : i32 v13, v14 - v16 = store : mem v2, v10, v12, v15 - v17 = const : i32 3 - v18 = const : i32 4 - v19 = const : i32 5 - v20 = const : i32 6 - v21 = const : i64 24 - v22 = add : ptr v4, v21 - v23 = load : i32 v2, v16, v6 - v24 = const : i32 7 - v25 = add : i32 v23, v24 - v26 = const : i64 28 - v27 = add : ptr v4, v26 - v28 = load : i32 v2, v16, v6 - v29 = const : i32 8 - v30 = add : i32 v28, v29 - v31 = const : i64 2 - v32 = add : ptr v4, v31 - v33 = load : i32 v2, v16, v32 - v34 = splat : <4 x i32> v33 - v35 = pack : <4 x i32> v17, v18, v19, v20 - v36 = add : <4 x i32> v34, v35 - v37 = const : i64 8 - v38 = add : ptr v4, v37 - v39 = store : mem v2, v16, v38, v36 - v40 = store : mem v2, v39, v22, v25 - v41 = store : mem v2, v40, v27, v30 - v42 = return : ctrl v2, v41 -} diff --git a/src/backend/test/slp_reject_mul_shl_blend.rat b/src/backend/test/slp_reject_mul_shl_blend.rat new file mode 100644 index 00000000..7ece3fc1 --- /dev/null +++ b/src/backend/test/slp_reject_mul_shl_blend.rat @@ -0,0 +1,68 @@ +@name slp: a mul and a shl lane do not blend, so the window stays scalar +@passes slp + +@input +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = const : i32 3 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v20 = load : i32 v2, v3, v5 + v21 = load : i32 v2, v3, v13 + v22 = load : i32 v2, v3, v14 + v23 = load : i32 v2, v3, v15 + v28 = mul : i32 v20, v6 + v29 = shl : i32 v21, v6 + v30 = mul : i32 v22, v6 + v31 = shl : i32 v23, v6 + v32 = store : mem v2, v3, v4, v28 + v33 = store : mem v2, v32, v10, v29 + v34 = store : mem v2, v33, v11, v30 + v35 = store : mem v2, v34, v12, v31 + v40 = return : ctrl v2, v35 +} + +@expect +func t(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v28 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "a" of v0 + v6 = const : i32 3 + v7 = const : i64 4 + v8 = const : i64 8 + v9 = const : i64 12 + v10 = add : ptr v4, v7 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v5, v7 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v16 = load : i32 v2, v3, v5 + v17 = load : i32 v2, v3, v13 + v18 = load : i32 v2, v3, v14 + v19 = load : i32 v2, v3, v15 + v20 = mul : i32 v16, v6 + v21 = shl : i32 v17, v6 + v22 = mul : i32 v18, v6 + v23 = shl : i32 v19, v6 + v24 = store : mem v2, v3, v4, v20 + v25 = store : mem v2, v24, v10, v21 + v26 = store : mem v2, v25, v11, v22 + v27 = store : mem v2, v26, v12, v23 + v28 = return : ctrl v2, v27 +} diff --git a/src/backend/test/slp_reject_narrow_broadcast.rat b/src/backend/test/slp_reject_narrow_broadcast.rat new file mode 100644 index 00000000..dec5bea9 --- /dev/null +++ b/src/backend/test/slp_reject_narrow_broadcast.rat @@ -0,0 +1,67 @@ +@name slp: i8 lanes at one address stay a gather, as x86 splats only dword and qword lanes +@passes loadcanon slp + +@input +func f(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v87 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "arg0" of v0 + v5 = proj : ptr #3 "arg1" of v0 + v6 = proj : ptr #4 "arg2" of v0 + v17 = load : i8 v2, v3, v5 + v23 = load : i32 v2, v3, v6 + v24 = sext : i32 v17 + v25 = mul : i32 nsw v24, v23 + v26 = store : mem v2, v3, v4, v25 + v31 = add : ptr v4, v95 + v37 = load : i8 v2, v26, v5 + v42 = add : ptr v6, v95 + v43 = load : i32 v2, v26, v42 + v44 = sext : i32 v37 + v45 = mul : i32 nsw v44, v43 + v46 = store : mem v2, v26, v31, v45 + v51 = add : ptr v4, v101 + v57 = load : i8 v2, v46, v5 + v62 = add : ptr v6, v101 + v63 = load : i32 v2, v46, v62 + v64 = sext : i32 v57 + v65 = mul : i32 nsw v64, v63 + v66 = store : mem v2, v46, v51, v65 + v71 = add : ptr v4, v107 + v77 = load : i8 v2, v66, v5 + v82 = add : ptr v6, v107 + v83 = load : i32 v2, v66, v82 + v84 = sext : i32 v77 + v85 = mul : i32 nsw v84, v83 + v86 = store : mem v2, v66, v71, v85 + v87 = return : ctrl v2, v86 + v95 = const : i64 4 + v101 = const : i64 8 + v107 = const : i64 12 +} + +@expect +func f(ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr) + v1 = stop : ctrl v36 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "arg0" of v0 + v5 = proj : ptr #3 "arg1" of v0 + v6 = proj : ptr #4 "arg2" of v0 + v7 = load : i8 v2, v3, v5 + v9 = sext : i32 v7 + v12 = load : i8 v2, v3, v5 + v13 = sext : i32 v12 + v23 = load : i8 v2, v3, v5 + v26 = sext : i32 v23 + v30 = load : i8 v2, v3, v5 + v33 = sext : i32 v30 + v36 = return : ctrl v2, v40 + v37 = pack : <4 x i32> v9, v13, v26, v33 + v38 = load : <4 x i32> v2, v3, v6 + v39 = mul : <4 x i32> v37, v38 + v40 = store : mem v2, v3, v4, v39 +} diff --git a/src/backend/test/slp_reuse_load_across_packs.rat b/src/backend/test/slp_reuse_load_across_packs.rat new file mode 100644 index 00000000..710ed006 --- /dev/null +++ b/src/backend/test/slp_reuse_load_across_packs.rat @@ -0,0 +1,73 @@ +@name slp: two packs of the same loads share one wide load per address +@passes slp + +@input +func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr) + v1 = stop : ctrl v60 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "d" of v0 + v6 = proj : ptr #4 "a" of v0 + v7 = proj : ptr #5 "b" of v0 + v8 = const : i64 4 + v9 = const : i64 8 + v10 = const : i64 12 + v11 = add : ptr v4, v8 + v12 = add : ptr v4, v9 + v13 = add : ptr v4, v10 + v14 = add : ptr v5, v8 + v15 = add : ptr v5, v9 + v16 = add : ptr v5, v10 + v17 = add : ptr v6, v8 + v18 = add : ptr v6, v9 + v19 = add : ptr v6, v10 + v20 = add : ptr v7, v8 + v21 = add : ptr v7, v9 + v22 = add : ptr v7, v10 + v23 = load : f32 v2, v3, v6 + v24 = load : f32 v2, v3, v17 + v25 = load : f32 v2, v3, v18 + v26 = load : f32 v2, v3, v19 + v27 = load : f32 v2, v3, v7 + v28 = load : f32 v2, v3, v20 + v29 = load : f32 v2, v3, v21 + v30 = load : f32 v2, v3, v22 + v31 = fadd : f32 v23, v27 + v32 = fadd : f32 v24, v28 + v33 = fadd : f32 v25, v29 + v34 = fadd : f32 v26, v30 + v35 = fmul : f32 v23, v27 + v36 = fmul : f32 v24, v28 + v37 = fmul : f32 v25, v29 + v38 = fmul : f32 v26, v30 + v40 = store : mem v2, v3, v4, v31 + v41 = store : mem v2, v40, v11, v32 + v42 = store : mem v2, v41, v12, v33 + v43 = store : mem v2, v42, v13, v34 + v44 = store : mem v2, v43, v5, v35 + v45 = store : mem v2, v44, v14, v36 + v46 = store : mem v2, v45, v15, v37 + v47 = store : mem v2, v46, v16, v38 + v60 = return : ctrl v2, v47 +} + +@expect +func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr) + v1 = stop : ctrl v60 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "c" of v0 + v5 = proj : ptr #3 "d" of v0 + v6 = proj : ptr #4 "a" of v0 + v7 = proj : ptr #5 "b" of v0 + v40 = store : mem v2, v3, v4, v63 + v44 = store : mem v2, v40, v5, v64 + v60 = return : ctrl v2, v44 + v61 = load : <4 x f32> v2, v3, v6 + v62 = load : <4 x f32> v2, v3, v7 + v63 = fadd : <4 x f32> v61, v62 + v64 = fmul : <4 x f32> v61, v62 +} diff --git a/src/backend/test/slp_reuse_load_same_type.rat b/src/backend/test/slp_reuse_load_same_type.rat new file mode 100644 index 00000000..63869ac0 --- /dev/null +++ b/src/backend/test/slp_reuse_load_same_type.rat @@ -0,0 +1,44 @@ +@name slp: a wide load is reused only at the same vector type +@passes loadcanon slp + +@input +func f(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v46 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "arg0" of v0 + v5 = proj : ptr #3 "arg1" of v0 + v16 = load : i32 v2, v3, v5 + v17 = sext : i64 v16 + v23 = load : i64 v2, v3, v5 + v24 = add : i64 nsw v17, v23 + v25 = store : mem v2, v3, v4, v24 + v30 = add : ptr v4, v54 + v35 = add : ptr v5, v56 + v36 = load : i32 v2, v25, v35 + v37 = sext : i64 v36 + v42 = add : ptr v5, v54 + v43 = load : i64 v2, v25, v42 + v44 = add : i64 nsw v37, v43 + v45 = store : mem v2, v25, v30, v44 + v46 = return : ctrl v2, v45 + v54 = const : i64 8 + v56 = const : i64 4 +} + +@expect +func f(ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr) + v1 = stop : ctrl v21 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "arg0" of v0 + v5 = proj : ptr #3 "arg1" of v0 + v21 = return : ctrl v2, v26 + v22 = load : <2 x i32> v2, v3, v5 + v23 = sext : <2 x i64> v22 + v24 = load : <2 x i64> v2, v3, v5 + v25 = add : <2 x i64> v23, v24 + v26 = store : mem v2, v3, v4, v25 +} diff --git a/src/backend/test/slp_same_symbol_globals.rat b/src/backend/test/slp_same_symbol_globals.rat deleted file mode 100644 index 2cf14f57..00000000 --- a/src/backend/test/slp_same_symbol_globals.rat +++ /dev/null @@ -1,139 +0,0 @@ -@name slp: two global nodes for one symbol are one object, so the window is not disjoint -@passes slp - -@input -func t() -> void { - v0 = start : (ctrl, mem) - v1 = stop : ctrl v60 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = global : ptr "g" - v5 = global : ptr "g" - v6 = const : i64 4 - v7 = const : i64 8 - v8 = const : i64 12 - v9 = const : i64 16 - v10 = const : i64 20 - v11 = const : i64 24 - v12 = const : i64 28 - v13 = add : ptr v4, v6 - v14 = add : ptr v4, v7 - v15 = add : ptr v4, v8 - v16 = add : ptr v4, v9 - v17 = add : ptr v4, v10 - v18 = add : ptr v4, v11 - v19 = add : ptr v4, v12 - v20 = add : ptr v5, v6 - v21 = add : ptr v5, v7 - v22 = add : ptr v5, v8 - v23 = add : ptr v5, v9 - v24 = add : ptr v5, v10 - v25 = add : ptr v5, v11 - v26 = add : ptr v5, v12 - v27 = load : i32 v2, v3, v5 - v28 = add : i32 v27, v27 - v29 = store : mem v2, v3, v4, v28 - v30 = load : i32 v2, v29, v20 - v31 = add : i32 v30, v30 - v32 = store : mem v2, v29, v13, v31 - v33 = load : i32 v2, v32, v21 - v34 = add : i32 v33, v33 - v35 = store : mem v2, v32, v14, v34 - v36 = load : i32 v2, v35, v22 - v37 = add : i32 v36, v36 - v38 = store : mem v2, v35, v15, v37 - v39 = load : i32 v2, v38, v23 - v40 = add : i32 v39, v39 - v41 = store : mem v2, v38, v16, v40 - v42 = load : i32 v2, v41, v24 - v43 = add : i32 v42, v42 - v44 = store : mem v2, v41, v17, v43 - v45 = load : i32 v2, v44, v25 - v46 = add : i32 v45, v45 - v47 = store : mem v2, v44, v18, v46 - v48 = load : i32 v2, v47, v26 - v49 = add : i32 v48, v48 - v50 = store : mem v2, v47, v19, v49 - v60 = return : ctrl v2, v50 -} - -@expect -func t() -> void { - v0 = start : (ctrl, mem) - v1 = stop : ctrl v51 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = global : ptr "g" - v5 = global : ptr "g" - v6 = const : i64 4 - v7 = const : i64 8 - v8 = const : i64 12 - v9 = const : i64 16 - v10 = const : i64 20 - v11 = const : i64 24 - v12 = const : i64 28 - v13 = add : ptr v4, v6 - v14 = add : ptr v4, v7 - v15 = add : ptr v4, v8 - v16 = add : ptr v4, v9 - v17 = add : ptr v4, v10 - v18 = add : ptr v4, v11 - v19 = add : ptr v4, v12 - v20 = add : ptr v5, v6 - v21 = add : ptr v5, v7 - v22 = add : ptr v5, v8 - v23 = add : ptr v5, v9 - v24 = add : ptr v5, v10 - v25 = add : ptr v5, v11 - v26 = add : ptr v5, v12 - v27 = load : i32 v69, v3, v5 - v28 = add : i32 v27, v27 - v29 = store : mem v69, v3, v4, v28 - v30 = load : i32 v69, v29, v20 - v31 = add : i32 v30, v30 - v32 = store : mem v69, v29, v13, v31 - v33 = load : i32 v69, v32, v21 - v34 = add : i32 v33, v33 - v35 = store : mem v69, v32, v14, v34 - v36 = load : i32 v69, v35, v22 - v37 = add : i32 v36, v36 - v38 = store : mem v69, v35, v15, v37 - v39 = load : i32 v69, v38, v23 - v40 = add : i32 v39, v39 - v41 = store : mem v69, v38, v16, v40 - v42 = load : i32 v69, v41, v24 - v43 = add : i32 v42, v42 - v44 = store : mem v69, v41, v17, v43 - v45 = load : i32 v69, v44, v25 - v46 = add : i32 v45, v45 - v47 = store : mem v69, v44, v18, v46 - v48 = load : i32 v69, v47, v26 - v49 = add : i32 v48, v48 - v50 = store : mem v69, v47, v19, v49 - v51 = return : ctrl v75, v76 - v52 = load : <4 x i32> v70, v3, v5 - v53 = add : <4 x i32> v52, v52 - v54 = const : i64 16 - v55 = add : ptr v5, v54 - v56 = load : <4 x i32> v70, v3, v55 - v57 = add : <4 x i32> v56, v56 - v58 = const : i64 32 - v59 = add : ptr v4, v58 - v60 = const : i64 32 - v61 = add : ptr v5, v60 - v62 = ule : i1 v61, v4 - v63 = if : (ctrl, ctrl) v2, v62 - v64 = proj : ctrl #0 "slp.then" of v63 - v65 = proj : ctrl #1 "slp.chk" of v63 - v66 = ule : i1 v59, v5 - v67 = if : (ctrl, ctrl) v65, v66 - v68 = proj : ctrl #0 "slp.then" of v67 - v69 = proj : ctrl #1 "slp.else" of v67 - v70 = region : ctrl v64, v68 - v71 = store : mem v70, v3, v4, v53 - v72 = const : i64 16 - v73 = add : ptr v4, v72 - v74 = store : mem v70, v71, v73, v57 - v75 = region : ctrl v70, v69 - v76 = phi : mem v75, v74, v50 -} diff --git a/src/backend/test/slp_select.rat b/src/backend/test/slp_select.rat new file mode 100644 index 00000000..d18c8f0d --- /dev/null +++ b/src/backend/test/slp_select.rat @@ -0,0 +1,83 @@ +@name slp: lanes that select by a float compare pack as a mask as wide as the operands and a blend +@passes slp + +@input +func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr, ptr) + v1 = stop : ctrl v90 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "o" of v0 + v5 = proj : ptr #3 "f" of v0 + v6 = proj : ptr #4 "g" of v0 + v7 = proj : ptr #5 "a" of v0 + v8 = proj : ptr #6 "b" of v0 + v10 = const : i64 4 + v11 = const : i64 8 + v12 = const : i64 12 + v13 = add : ptr v4, v10 + v14 = add : ptr v4, v11 + v15 = add : ptr v4, v12 + v16 = add : ptr v5, v10 + v17 = add : ptr v5, v11 + v18 = add : ptr v5, v12 + v19 = add : ptr v6, v10 + v20 = add : ptr v6, v11 + v21 = add : ptr v6, v12 + v22 = add : ptr v7, v10 + v23 = add : ptr v7, v11 + v24 = add : ptr v7, v12 + v25 = add : ptr v8, v10 + v26 = add : ptr v8, v11 + v27 = add : ptr v8, v12 + v30 = load : f32 v2, v3, v5 + v31 = load : f32 v2, v3, v16 + v32 = load : f32 v2, v3, v17 + v33 = load : f32 v2, v3, v18 + v34 = load : f32 v2, v3, v6 + v35 = load : f32 v2, v3, v19 + v36 = load : f32 v2, v3, v20 + v37 = load : f32 v2, v3, v21 + v40 = load : i32 v2, v3, v7 + v41 = load : i32 v2, v3, v22 + v42 = load : i32 v2, v3, v23 + v43 = load : i32 v2, v3, v24 + v44 = load : i32 v2, v3, v8 + v45 = load : i32 v2, v3, v25 + v46 = load : i32 v2, v3, v26 + v47 = load : i32 v2, v3, v27 + v50 = flt : i1 v30, v34 + v51 = flt : i1 v31, v35 + v52 = flt : i1 v32, v36 + v53 = flt : i1 v33, v37 + v60 = select : i32 v50, v40, v44 + v61 = select : i32 v51, v41, v45 + v62 = select : i32 v52, v42, v46 + v63 = select : i32 v53, v43, v47 + v70 = store : mem v2, v3, v4, v60 + v71 = store : mem v2, v70, v13, v61 + v72 = store : mem v2, v71, v14, v62 + v73 = store : mem v2, v72, v15, v63 + v90 = return : ctrl v2, v73 +} + +@expect +func t(ptr restrict, ptr restrict, ptr restrict, ptr restrict, ptr restrict) -> void { + v0 = start : (ctrl, mem, ptr, ptr, ptr, ptr, ptr) + v1 = stop : ctrl v55 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "o" of v0 + v5 = proj : ptr #3 "f" of v0 + v6 = proj : ptr #4 "g" of v0 + v7 = proj : ptr #5 "a" of v0 + v8 = proj : ptr #6 "b" of v0 + v51 = store : mem v2, v3, v4, v61 + v55 = return : ctrl v2, v51 + v56 = load : <4 x f32> v2, v3, v5 + v57 = load : <4 x f32> v2, v3, v6 + v58 = flt : <4 x i32> v56, v57 + v59 = load : <4 x i32> v2, v3, v7 + v60 = load : <4 x i32> v2, v3, v8 + v61 = select : <4 x i32> v58, v59, v60 +} diff --git a/src/backend/test/slp_share_splat_across_packs.rat b/src/backend/test/slp_share_splat_across_packs.rat new file mode 100644 index 00000000..e70e30e3 --- /dev/null +++ b/src/backend/test/slp_share_splat_across_packs.rat @@ -0,0 +1,51 @@ +@name slp: two windows of one scalar share its splat +@passes slp + +@input +func t(ptr restrict, i32) -> void { + v0 = start : (ctrl, mem, ptr, i32) + v1 = stop : ctrl v40 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "p" of v0 + v5 = proj : i32 #3 "x" of v0 + v6 = const : i64 4 + v7 = const : i64 8 + v8 = const : i64 12 + v9 = const : i64 16 + v10 = const : i64 20 + v11 = const : i64 24 + v12 = const : i64 28 + v13 = add : ptr v4, v6 + v14 = add : ptr v4, v7 + v15 = add : ptr v4, v8 + v16 = add : ptr v4, v9 + v17 = add : ptr v4, v10 + v18 = add : ptr v4, v11 + v19 = add : ptr v4, v12 + v20 = store : mem v2, v3, v4, v5 + v21 = store : mem v2, v20, v13, v5 + v22 = store : mem v2, v21, v14, v5 + v23 = store : mem v2, v22, v15, v5 + v24 = store : mem v2, v23, v16, v5 + v25 = store : mem v2, v24, v17, v5 + v26 = store : mem v2, v25, v18, v5 + v27 = store : mem v2, v26, v19, v5 + v40 = return : ctrl v2, v27 +} + +@expect +func t(ptr restrict, i32) -> void { + v0 = start : (ctrl, mem, ptr, i32) + v1 = stop : ctrl v28 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "p" of v0 + v5 = proj : i32 #3 "x" of v0 + v9 = const : i64 16 + v16 = add : ptr v4, v9 + v20 = store : mem v2, v3, v4, v29 + v24 = store : mem v2, v20, v16, v29 + v28 = return : ctrl v2, v24 + v29 = splat : <4 x i32> v5 +} diff --git a/src/backend/test/slp_split_narrow_only_nsw.rat b/src/backend/test/slp_split_narrow_only_nsw.rat deleted file mode 100644 index 614e4629..00000000 --- a/src/backend/test/slp_split_narrow_only_nsw.rat +++ /dev/null @@ -1,123 +0,0 @@ -@name slp: p + 4 * sext(u + k) splits off k only when the add is nsw, plain lanes stay scalar -@passes slp - -@input -func plain(ptr, i32, i32) -> void { - v0 = start : (ctrl, mem, ptr, i32, i32) - v1 = stop : ctrl v33 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "p" of v0 - v5 = proj : i32 #3 "u" of v0 - v6 = proj : i32 #4 "v" of v0 - v7 = sext : i64 v5 - v8 = const : i64 2 - v9 = shl : i64 v7, v8 - v10 = add : ptr v4, v9 - v11 = store : mem v2, v3, v10, v6 - v12 = const : i32 1 - v13 = add : i32 v5, v12 - v14 = sext : i64 v13 - v15 = shl : i64 v14, v8 - v17 = add : ptr v4, v15 - v18 = store : mem v2, v11, v17, v6 - v19 = const : i32 2 - v20 = add : i32 v5, v19 - v21 = sext : i64 v20 - v22 = shl : i64 v21, v8 - v24 = add : ptr v4, v22 - v25 = store : mem v2, v18, v24, v6 - v26 = const : i32 3 - v27 = add : i32 v5, v26 - v28 = sext : i64 v27 - v29 = shl : i64 v28, v8 - v31 = add : ptr v4, v29 - v32 = store : mem v2, v25, v31, v6 - v33 = return : ctrl v2, v32 -} - -func flagged(ptr, i32, i32) -> void { - v0 = start : (ctrl, mem, ptr, i32, i32) - v1 = stop : ctrl v33 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "p" of v0 - v5 = proj : i32 #3 "u" of v0 - v6 = proj : i32 #4 "v" of v0 - v7 = sext : i64 v5 - v8 = const : i64 2 - v9 = shl : i64 v7, v8 - v10 = add : ptr v4, v9 - v11 = store : mem v2, v3, v10, v6 - v12 = const : i32 1 - v13 = add : i32 nsw v5, v12 - v14 = sext : i64 v13 - v15 = shl : i64 v14, v8 - v17 = add : ptr v4, v15 - v18 = store : mem v2, v11, v17, v6 - v19 = const : i32 2 - v20 = add : i32 nsw v5, v19 - v21 = sext : i64 v20 - v22 = shl : i64 v21, v8 - v24 = add : ptr v4, v22 - v25 = store : mem v2, v18, v24, v6 - v26 = const : i32 3 - v27 = add : i32 nsw v5, v26 - v28 = sext : i64 v27 - v29 = shl : i64 v28, v8 - v31 = add : ptr v4, v29 - v32 = store : mem v2, v25, v31, v6 - v33 = return : ctrl v2, v32 -} - -@expect -func plain(ptr, i32, i32) -> void { - v0 = start : (ctrl, mem, ptr, i32, i32) - v1 = stop : ctrl v30 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "p" of v0 - v5 = proj : i32 #3 "u" of v0 - v6 = proj : i32 #4 "v" of v0 - v7 = sext : i64 v5 - v8 = const : i64 2 - v9 = shl : i64 v7, v8 - v10 = add : ptr v4, v9 - v11 = store : mem v2, v3, v10, v6 - v12 = const : i32 1 - v13 = add : i32 v5, v12 - v14 = sext : i64 v13 - v15 = shl : i64 v14, v8 - v16 = add : ptr v4, v15 - v17 = store : mem v2, v11, v16, v6 - v18 = const : i32 2 - v19 = add : i32 v5, v18 - v20 = sext : i64 v19 - v21 = shl : i64 v20, v8 - v22 = add : ptr v4, v21 - v23 = store : mem v2, v17, v22, v6 - v24 = const : i32 3 - v25 = add : i32 v5, v24 - v26 = sext : i64 v25 - v27 = shl : i64 v26, v8 - v28 = add : ptr v4, v27 - v29 = store : mem v2, v23, v28, v6 - v30 = return : ctrl v2, v29 -} - -func flagged(ptr, i32, i32) -> void { - v0 = start : (ctrl, mem, ptr, i32, i32) - v1 = stop : ctrl v30 - v2 = proj : ctrl #0 "ctrl" of v0 - v3 = proj : mem #1 "mem" of v0 - v4 = proj : ptr #2 "p" of v0 - v5 = proj : i32 #3 "u" of v0 - v6 = proj : i32 #4 "v" of v0 - v7 = sext : i64 v5 - v8 = const : i64 2 - v9 = shl : i64 v7, v8 - v10 = add : ptr v4, v9 - v30 = return : ctrl v2, v33 - v32 = splat : <4 x i32> v6 - v33 = store : mem v2, v3, v10, v32 -} diff --git a/src/backend/test/slp_unordered_stretch.rat b/src/backend/test/slp_unordered_stretch.rat new file mode 100644 index 00000000..d4d62bb7 --- /dev/null +++ b/src/backend/test/slp_unordered_stretch.rat @@ -0,0 +1,47 @@ +@name slp: stores of a stretch in any address order pack into one wide store +@passes slp + +@input +func t(ptr, i32, i32, i32, i32) -> void { + v0 = start : (ctrl, mem, ptr, i32, i32, i32, i32) + v1 = stop : ctrl v30 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : i32 #3 "w" of v0 + v6 = proj : i32 #4 "x" of v0 + v7 = proj : i32 #5 "y" of v0 + v8 = proj : i32 #6 "z" of v0 + v9 = const : i64 4 + v10 = const : i64 8 + v11 = const : i64 12 + v12 = add : ptr v4, v9 + v13 = add : ptr v4, v10 + v14 = add : ptr v4, v11 + v15 = mul : i32 v5, v5 + v16 = mul : i32 v6, v6 + v17 = mul : i32 v7, v7 + v18 = mul : i32 v8, v8 + v20 = store : mem v2, v3, v14, v18 + v21 = store : mem v2, v20, v12, v16 + v22 = store : mem v2, v21, v4, v15 + v23 = store : mem v2, v22, v13, v17 + v30 = return : ctrl v2, v23 +} + +@expect +func t(ptr, i32, i32, i32, i32) -> void { + v0 = start : (ctrl, mem, ptr, i32, i32, i32, i32) + v1 = stop : ctrl v23 + v2 = proj : ctrl #0 "ctrl" of v0 + v3 = proj : mem #1 "mem" of v0 + v4 = proj : ptr #2 "a" of v0 + v5 = proj : i32 #3 "w" of v0 + v6 = proj : i32 #4 "x" of v0 + v7 = proj : i32 #5 "y" of v0 + v8 = proj : i32 #6 "z" of v0 + v23 = return : ctrl v2, v26 + v24 = pack : <4 x i32> v5, v6, v7, v8 + v25 = mul : <4 x i32> v24, v24 + v26 = store : mem v2, v3, v4, v25 +}