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
19*9ffdc930SRiver 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"
25*9ffdc930SRiver Riddle #include "mlir/IR/Operation.h"
26*9ffdc930SRiver Riddle using namespace mlir;
27a7215a90SAlex Zinenko 
28*9ffdc930SRiver Riddle //===----------------------------------------------------------------------===//
29*9ffdc930SRiver Riddle // OperationState
30*9ffdc930SRiver 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> &&region) {
63a7215a90SAlex Zinenko   regions.push_back(std::move(region));
64a7215a90SAlex Zinenko }
65a7215a90SAlex Zinenko 
66*9ffdc930SRiver Riddle //===----------------------------------------------------------------------===//
67*9ffdc930SRiver Riddle // OperandStorage
68*9ffdc930SRiver Riddle //===----------------------------------------------------------------------===//
69*9ffdc930SRiver Riddle 
70*9ffdc930SRiver Riddle /// Replace the operands contained in the storage with the ones provided in
71*9ffdc930SRiver Riddle /// 'operands'.
72*9ffdc930SRiver Riddle void detail::OperandStorage::setOperands(Operation *owner,
73*9ffdc930SRiver Riddle                                          ArrayRef<Value *> operands) {
74*9ffdc930SRiver Riddle   // If the number of operands is less than or equal to the current amount, we
75*9ffdc930SRiver Riddle   // can just update in place.
76*9ffdc930SRiver Riddle   if (operands.size() <= numOperands) {
77*9ffdc930SRiver Riddle     auto instOperands = getInstOperands();
78*9ffdc930SRiver Riddle 
79*9ffdc930SRiver Riddle     // If the number of new operands is less than the current count, then remove
80*9ffdc930SRiver Riddle     // any extra operands.
81*9ffdc930SRiver Riddle     for (unsigned i = operands.size(); i != numOperands; ++i)
82*9ffdc930SRiver Riddle       instOperands[i].~InstOperand();
83*9ffdc930SRiver Riddle 
84*9ffdc930SRiver Riddle     // Set the operands in place.
85*9ffdc930SRiver Riddle     numOperands = operands.size();
86*9ffdc930SRiver Riddle     for (unsigned i = 0; i != numOperands; ++i)
87*9ffdc930SRiver Riddle       instOperands[i].set(operands[i]);
88*9ffdc930SRiver Riddle     return;
89*9ffdc930SRiver Riddle   }
90*9ffdc930SRiver Riddle 
91*9ffdc930SRiver Riddle   // Otherwise, we need to be resizable.
92*9ffdc930SRiver Riddle   assert(resizable && "Only resizable operations may add operands");
93*9ffdc930SRiver Riddle 
94*9ffdc930SRiver Riddle   // Grow the capacity if necessary.
95*9ffdc930SRiver Riddle   auto &resizeUtil = getResizableStorage();
96*9ffdc930SRiver Riddle   if (resizeUtil.capacity < operands.size())
97*9ffdc930SRiver Riddle     grow(resizeUtil, operands.size());
98*9ffdc930SRiver Riddle 
99*9ffdc930SRiver Riddle   // Set the operands.
100*9ffdc930SRiver Riddle   InstOperand *opBegin = getRawOperands();
101*9ffdc930SRiver Riddle   for (unsigned i = 0; i != numOperands; ++i)
102*9ffdc930SRiver Riddle     opBegin[i].set(operands[i]);
103*9ffdc930SRiver Riddle   for (unsigned e = operands.size(); numOperands != e; ++numOperands)
104*9ffdc930SRiver Riddle     new (&opBegin[numOperands]) InstOperand(owner, operands[numOperands]);
105*9ffdc930SRiver Riddle }
106*9ffdc930SRiver Riddle 
107*9ffdc930SRiver Riddle /// Erase an operand held by the storage.
108*9ffdc930SRiver Riddle void detail::OperandStorage::eraseOperand(unsigned index) {
109*9ffdc930SRiver Riddle   assert(index < size());
110*9ffdc930SRiver Riddle   auto Operands = getInstOperands();
111*9ffdc930SRiver Riddle   --numOperands;
112*9ffdc930SRiver Riddle 
113*9ffdc930SRiver Riddle   // Shift all operands down by 1 if the operand to remove is not at the end.
114*9ffdc930SRiver Riddle   if (index != numOperands)
115*9ffdc930SRiver Riddle     std::rotate(&Operands[index], &Operands[index + 1], &Operands[numOperands]);
116*9ffdc930SRiver Riddle   Operands[numOperands].~InstOperand();
117*9ffdc930SRiver Riddle }
118*9ffdc930SRiver Riddle 
119*9ffdc930SRiver Riddle /// Grow the internal operand storage.
120*9ffdc930SRiver Riddle void detail::OperandStorage::grow(ResizableStorage &resizeUtil,
121*9ffdc930SRiver Riddle                                   size_t minSize) {
122*9ffdc930SRiver Riddle   // Allocate a new storage array.
123*9ffdc930SRiver Riddle   resizeUtil.capacity =
124*9ffdc930SRiver Riddle       std::max(size_t(llvm::NextPowerOf2(resizeUtil.capacity + 2)), minSize);
125*9ffdc930SRiver Riddle   InstOperand *newStorage = static_cast<InstOperand *>(
126*9ffdc930SRiver Riddle       llvm::safe_malloc(resizeUtil.capacity * sizeof(InstOperand)));
127*9ffdc930SRiver Riddle 
128*9ffdc930SRiver Riddle   // Move the current operands to the new storage.
129*9ffdc930SRiver Riddle   auto operands = getInstOperands();
130*9ffdc930SRiver Riddle   std::uninitialized_copy(std::make_move_iterator(operands.begin()),
131*9ffdc930SRiver Riddle                           std::make_move_iterator(operands.end()), newStorage);
132*9ffdc930SRiver Riddle 
133*9ffdc930SRiver Riddle   // Destroy the original operands and update the resizable storage pointer.
134*9ffdc930SRiver Riddle   for (auto &operand : operands)
135*9ffdc930SRiver Riddle     operand.~InstOperand();
136*9ffdc930SRiver Riddle   resizeUtil.setDynamicStorage(newStorage);
137*9ffdc930SRiver Riddle }
138