Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -1032,8 +1032,25 @@ } // Otherwise, we have to make a token factor node. - SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, - PendingLoads); + // If we have >= 2^16 loads then split across multiple token factors as + // there's a 64k limit on the number of SDNode operands. + SDValue Root; + size_t Limit = (1 << 16) - 1; + if (PendingLoads.size() <= Limit) { + Root = + DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, PendingLoads); + } else { + SmallVector TokenFactors; + for (unsigned i = 0; i < PendingLoads.size(); i += Limit) { + auto LoadSlice = ArrayRef(PendingLoads) + .slice(i, std::min(Limit, PendingLoads.size() - i)); + assert(LoadSlice.size() <= Limit && "Too many loads in slice"); + TokenFactors.emplace_back( + DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, LoadSlice)); + } + Root = + DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, TokenFactors); + } PendingLoads.clear(); DAG.setRoot(Root); return Root;