1a7215a90SAlex Zinenko //===- OperationSupport.cpp -----------------------------------------------===// 2a7215a90SAlex Zinenko // 3a7215a90SAlex Zinenko // Copyright 2019 The MLIR Authors. 4a7215a90SAlex Zinenko // 5a7215a90SAlex Zinenko // Licensed under the Apache License, Version 2.0 (the "License"); 6a7215a90SAlex Zinenko // you may not use this file except in compliance with the License. 7a7215a90SAlex Zinenko // You may obtain a copy of the License at 8a7215a90SAlex Zinenko // 9a7215a90SAlex Zinenko // http://www.apache.org/licenses/LICENSE-2.0 10a7215a90SAlex Zinenko // 11a7215a90SAlex Zinenko // Unless required by applicable law or agreed to in writing, software 12a7215a90SAlex Zinenko // distributed under the License is distributed on an "AS IS" BASIS, 13a7215a90SAlex Zinenko // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14a7215a90SAlex Zinenko // See the License for the specific language governing permissions and 15a7215a90SAlex Zinenko // limitations under the License. 16a7215a90SAlex Zinenko // ============================================================================= 17a7215a90SAlex Zinenko // 18a7215a90SAlex Zinenko // This file contains out-of-line implementations of the support types that 199ffdc930SRiver Riddle // Operation and related classes build on top of. 20a7215a90SAlex Zinenko // 21a7215a90SAlex Zinenko //===----------------------------------------------------------------------===// 22a7215a90SAlex Zinenko 23a7215a90SAlex Zinenko #include "mlir/IR/OperationSupport.h" 24a7215a90SAlex Zinenko #include "mlir/IR/Block.h" 259ffdc930SRiver Riddle #include "mlir/IR/Operation.h" 269ffdc930SRiver Riddle using namespace mlir; 27a7215a90SAlex Zinenko 289ffdc930SRiver Riddle //===----------------------------------------------------------------------===// 299ffdc930SRiver Riddle // OperationState 309ffdc930SRiver Riddle //===----------------------------------------------------------------------===// 31a7215a90SAlex Zinenko 3248d6cf1cSRiver Riddle OperationState::OperationState(Location location, StringRef name) 3348d6cf1cSRiver Riddle : context(location->getContext()), location(location), 3448d6cf1cSRiver Riddle name(name, location->getContext()) {} 35a7215a90SAlex Zinenko 3648d6cf1cSRiver Riddle OperationState::OperationState(Location location, OperationName name) 3748d6cf1cSRiver Riddle : context(location->getContext()), location(location), name(name) {} 38a7215a90SAlex Zinenko 3948d6cf1cSRiver Riddle OperationState::OperationState(Location location, StringRef name, 4048d6cf1cSRiver Riddle ArrayRef<Value *> operands, ArrayRef<Type> types, 41a7215a90SAlex Zinenko ArrayRef<NamedAttribute> attributes, 42a7215a90SAlex Zinenko ArrayRef<Block *> successors, 43a7215a90SAlex Zinenko MutableArrayRef<std::unique_ptr<Region>> regions, 44a7215a90SAlex Zinenko bool resizableOperandList) 4548d6cf1cSRiver Riddle : context(location->getContext()), location(location), 4648d6cf1cSRiver Riddle name(name, location->getContext()), 47a7215a90SAlex Zinenko operands(operands.begin(), operands.end()), 48a7215a90SAlex Zinenko types(types.begin(), types.end()), 49a7215a90SAlex Zinenko attributes(attributes.begin(), attributes.end()), 50a7215a90SAlex Zinenko successors(successors.begin(), successors.end()) { 51a7215a90SAlex Zinenko for (std::unique_ptr<Region> &r : regions) { 52a7215a90SAlex Zinenko this->regions.push_back(std::move(r)); 53a7215a90SAlex Zinenko } 54a7215a90SAlex Zinenko } 55a7215a90SAlex Zinenko 56a7215a90SAlex Zinenko Region *OperationState::addRegion() { 57a7215a90SAlex Zinenko regions.emplace_back(new Region); 58a7215a90SAlex Zinenko return regions.back().get(); 59a7215a90SAlex Zinenko } 60a7215a90SAlex Zinenko 61a7215a90SAlex Zinenko void OperationState::addRegion(std::unique_ptr<Region> &®ion) { 62a7215a90SAlex Zinenko regions.push_back(std::move(region)); 63a7215a90SAlex Zinenko } 64a7215a90SAlex Zinenko 659ffdc930SRiver Riddle //===----------------------------------------------------------------------===// 669ffdc930SRiver Riddle // OperandStorage 679ffdc930SRiver Riddle //===----------------------------------------------------------------------===// 689ffdc930SRiver Riddle 699ffdc930SRiver Riddle /// Replace the operands contained in the storage with the ones provided in 709ffdc930SRiver Riddle /// 'operands'. 719ffdc930SRiver Riddle void detail::OperandStorage::setOperands(Operation *owner, 729ffdc930SRiver Riddle ArrayRef<Value *> operands) { 739ffdc930SRiver Riddle // If the number of operands is less than or equal to the current amount, we 749ffdc930SRiver Riddle // can just update in place. 759ffdc930SRiver Riddle if (operands.size() <= numOperands) { 76213b8d4dSRiver Riddle auto opOperands = getOperands(); 779ffdc930SRiver Riddle 789ffdc930SRiver Riddle // If the number of new operands is less than the current count, then remove 799ffdc930SRiver Riddle // any extra operands. 809ffdc930SRiver Riddle for (unsigned i = operands.size(); i != numOperands; ++i) 81213b8d4dSRiver Riddle opOperands[i].~OpOperand(); 829ffdc930SRiver Riddle 839ffdc930SRiver Riddle // Set the operands in place. 849ffdc930SRiver Riddle numOperands = operands.size(); 859ffdc930SRiver Riddle for (unsigned i = 0; i != numOperands; ++i) 86213b8d4dSRiver Riddle opOperands[i].set(operands[i]); 879ffdc930SRiver Riddle return; 889ffdc930SRiver Riddle } 899ffdc930SRiver Riddle 909ffdc930SRiver Riddle // Otherwise, we need to be resizable. 919ffdc930SRiver Riddle assert(resizable && "Only resizable operations may add operands"); 929ffdc930SRiver Riddle 939ffdc930SRiver Riddle // Grow the capacity if necessary. 949ffdc930SRiver Riddle auto &resizeUtil = getResizableStorage(); 959ffdc930SRiver Riddle if (resizeUtil.capacity < operands.size()) 969ffdc930SRiver Riddle grow(resizeUtil, operands.size()); 979ffdc930SRiver Riddle 989ffdc930SRiver Riddle // Set the operands. 99213b8d4dSRiver Riddle OpOperand *opBegin = getRawOperands(); 1009ffdc930SRiver Riddle for (unsigned i = 0; i != numOperands; ++i) 1019ffdc930SRiver Riddle opBegin[i].set(operands[i]); 1029ffdc930SRiver Riddle for (unsigned e = operands.size(); numOperands != e; ++numOperands) 103213b8d4dSRiver Riddle new (&opBegin[numOperands]) OpOperand(owner, operands[numOperands]); 1049ffdc930SRiver Riddle } 1059ffdc930SRiver Riddle 1069ffdc930SRiver Riddle /// Erase an operand held by the storage. 1079ffdc930SRiver Riddle void detail::OperandStorage::eraseOperand(unsigned index) { 1089ffdc930SRiver Riddle assert(index < size()); 109213b8d4dSRiver Riddle auto operands = getOperands(); 1109ffdc930SRiver Riddle --numOperands; 1119ffdc930SRiver Riddle 1129ffdc930SRiver Riddle // Shift all operands down by 1 if the operand to remove is not at the end. 113*0b468b01SAlex Zinenko auto indexIt = std::next(operands.begin(), index); 1149ffdc930SRiver Riddle if (index != numOperands) 115*0b468b01SAlex Zinenko std::rotate(indexIt, std::next(indexIt), operands.end()); 116213b8d4dSRiver Riddle operands[numOperands].~OpOperand(); 1179ffdc930SRiver Riddle } 1189ffdc930SRiver Riddle 1199ffdc930SRiver Riddle /// Grow the internal operand storage. 1209ffdc930SRiver Riddle void detail::OperandStorage::grow(ResizableStorage &resizeUtil, 1219ffdc930SRiver Riddle size_t minSize) { 1229ffdc930SRiver Riddle // Allocate a new storage array. 1239ffdc930SRiver Riddle resizeUtil.capacity = 1249ffdc930SRiver Riddle std::max(size_t(llvm::NextPowerOf2(resizeUtil.capacity + 2)), minSize); 125213b8d4dSRiver Riddle OpOperand *newStorage = static_cast<OpOperand *>( 126213b8d4dSRiver Riddle llvm::safe_malloc(resizeUtil.capacity * sizeof(OpOperand))); 1279ffdc930SRiver Riddle 1289ffdc930SRiver Riddle // Move the current operands to the new storage. 129213b8d4dSRiver Riddle auto operands = getOperands(); 1309ffdc930SRiver Riddle std::uninitialized_copy(std::make_move_iterator(operands.begin()), 1319ffdc930SRiver Riddle std::make_move_iterator(operands.end()), newStorage); 1329ffdc930SRiver Riddle 1339ffdc930SRiver Riddle // Destroy the original operands and update the resizable storage pointer. 1349ffdc930SRiver Riddle for (auto &operand : operands) 135213b8d4dSRiver Riddle operand.~OpOperand(); 1369ffdc930SRiver Riddle resizeUtil.setDynamicStorage(newStorage); 1379ffdc930SRiver Riddle } 138