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 32a7215a90SAlex Zinenko OperationState::OperationState(MLIRContext *context, Location location, 33a7215a90SAlex Zinenko StringRef name) 34a7215a90SAlex Zinenko : context(context), location(location), name(name, context) {} 35a7215a90SAlex Zinenko 36a7215a90SAlex Zinenko OperationState::OperationState(MLIRContext *context, Location location, 37a7215a90SAlex Zinenko OperationName name) 38a7215a90SAlex Zinenko : context(context), location(location), name(name) {} 39a7215a90SAlex Zinenko 40a7215a90SAlex Zinenko OperationState::OperationState(MLIRContext *context, Location location, 41a7215a90SAlex Zinenko StringRef name, ArrayRef<Value *> operands, 42a7215a90SAlex Zinenko ArrayRef<Type> types, 43a7215a90SAlex Zinenko ArrayRef<NamedAttribute> attributes, 44a7215a90SAlex Zinenko ArrayRef<Block *> successors, 45a7215a90SAlex Zinenko MutableArrayRef<std::unique_ptr<Region>> regions, 46a7215a90SAlex Zinenko bool resizableOperandList) 47a7215a90SAlex Zinenko : context(context), location(location), name(name, context), 48a7215a90SAlex Zinenko operands(operands.begin(), operands.end()), 49a7215a90SAlex Zinenko types(types.begin(), types.end()), 50a7215a90SAlex Zinenko attributes(attributes.begin(), attributes.end()), 51a7215a90SAlex Zinenko successors(successors.begin(), successors.end()) { 52a7215a90SAlex Zinenko for (std::unique_ptr<Region> &r : regions) { 53a7215a90SAlex Zinenko this->regions.push_back(std::move(r)); 54a7215a90SAlex Zinenko } 55a7215a90SAlex Zinenko } 56a7215a90SAlex Zinenko 57a7215a90SAlex Zinenko Region *OperationState::addRegion() { 58a7215a90SAlex Zinenko regions.emplace_back(new Region); 59a7215a90SAlex Zinenko return regions.back().get(); 60a7215a90SAlex Zinenko } 61a7215a90SAlex Zinenko 62a7215a90SAlex Zinenko void OperationState::addRegion(std::unique_ptr<Region> &®ion) { 63a7215a90SAlex Zinenko regions.push_back(std::move(region)); 64a7215a90SAlex Zinenko } 65a7215a90SAlex Zinenko 669ffdc930SRiver Riddle //===----------------------------------------------------------------------===// 679ffdc930SRiver Riddle // OperandStorage 689ffdc930SRiver Riddle //===----------------------------------------------------------------------===// 699ffdc930SRiver Riddle 709ffdc930SRiver Riddle /// Replace the operands contained in the storage with the ones provided in 719ffdc930SRiver Riddle /// 'operands'. 729ffdc930SRiver Riddle void detail::OperandStorage::setOperands(Operation *owner, 739ffdc930SRiver Riddle ArrayRef<Value *> operands) { 749ffdc930SRiver Riddle // If the number of operands is less than or equal to the current amount, we 759ffdc930SRiver Riddle // can just update in place. 769ffdc930SRiver Riddle if (operands.size() <= numOperands) { 77*213b8d4dSRiver Riddle auto opOperands = getOperands(); 789ffdc930SRiver Riddle 799ffdc930SRiver Riddle // If the number of new operands is less than the current count, then remove 809ffdc930SRiver Riddle // any extra operands. 819ffdc930SRiver Riddle for (unsigned i = operands.size(); i != numOperands; ++i) 82*213b8d4dSRiver Riddle opOperands[i].~OpOperand(); 839ffdc930SRiver Riddle 849ffdc930SRiver Riddle // Set the operands in place. 859ffdc930SRiver Riddle numOperands = operands.size(); 869ffdc930SRiver Riddle for (unsigned i = 0; i != numOperands; ++i) 87*213b8d4dSRiver Riddle opOperands[i].set(operands[i]); 889ffdc930SRiver Riddle return; 899ffdc930SRiver Riddle } 909ffdc930SRiver Riddle 919ffdc930SRiver Riddle // Otherwise, we need to be resizable. 929ffdc930SRiver Riddle assert(resizable && "Only resizable operations may add operands"); 939ffdc930SRiver Riddle 949ffdc930SRiver Riddle // Grow the capacity if necessary. 959ffdc930SRiver Riddle auto &resizeUtil = getResizableStorage(); 969ffdc930SRiver Riddle if (resizeUtil.capacity < operands.size()) 979ffdc930SRiver Riddle grow(resizeUtil, operands.size()); 989ffdc930SRiver Riddle 999ffdc930SRiver Riddle // Set the operands. 100*213b8d4dSRiver Riddle OpOperand *opBegin = getRawOperands(); 1019ffdc930SRiver Riddle for (unsigned i = 0; i != numOperands; ++i) 1029ffdc930SRiver Riddle opBegin[i].set(operands[i]); 1039ffdc930SRiver Riddle for (unsigned e = operands.size(); numOperands != e; ++numOperands) 104*213b8d4dSRiver Riddle new (&opBegin[numOperands]) OpOperand(owner, operands[numOperands]); 1059ffdc930SRiver Riddle } 1069ffdc930SRiver Riddle 1079ffdc930SRiver Riddle /// Erase an operand held by the storage. 1089ffdc930SRiver Riddle void detail::OperandStorage::eraseOperand(unsigned index) { 1099ffdc930SRiver Riddle assert(index < size()); 110*213b8d4dSRiver Riddle auto operands = getOperands(); 1119ffdc930SRiver Riddle --numOperands; 1129ffdc930SRiver Riddle 1139ffdc930SRiver Riddle // Shift all operands down by 1 if the operand to remove is not at the end. 1149ffdc930SRiver Riddle if (index != numOperands) 115*213b8d4dSRiver Riddle std::rotate(&operands[index], &operands[index + 1], &operands[numOperands]); 116*213b8d4dSRiver 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); 125*213b8d4dSRiver Riddle OpOperand *newStorage = static_cast<OpOperand *>( 126*213b8d4dSRiver Riddle llvm::safe_malloc(resizeUtil.capacity * sizeof(OpOperand))); 1279ffdc930SRiver Riddle 1289ffdc930SRiver Riddle // Move the current operands to the new storage. 129*213b8d4dSRiver 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) 135*213b8d4dSRiver Riddle operand.~OpOperand(); 1369ffdc930SRiver Riddle resizeUtil.setDynamicStorage(newStorage); 1379ffdc930SRiver Riddle } 138