|
| 1 | +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/ir/operation.h" |
| 16 | +#include "paddle/ir/utils.h" |
| 17 | + |
| 18 | +namespace ir { |
| 19 | +// Allocate the required memory based on the size and number of inputs, outputs, |
| 20 | +// and operators, and construct it in the order of: OpOutlineResult, |
| 21 | +// OpInlineResult, Operation, Operand. |
| 22 | +Operation *Operation::create(const std::vector<ir::OpResult> &inputs, |
| 23 | + const std::vector<ir::Type> &output_types, |
| 24 | + ir::DictionaryAttribute attribute) { |
| 25 | + // 1. Calculate the required memory size for OpResults + Operation + |
| 26 | + // OpOperands. |
| 27 | + uint32_t num_results = output_types.size(); |
| 28 | + uint32_t num_operands = inputs.size(); |
| 29 | + uint32_t max_inline_result_num = |
| 30 | + detail::OpResultImpl::GetMaxInlineResultIndex() + 1; |
| 31 | + size_t result_mem_size = |
| 32 | + num_results > max_inline_result_num |
| 33 | + ? sizeof(detail::OpOutlineResultImpl) * |
| 34 | + (num_results - max_inline_result_num) + |
| 35 | + sizeof(detail::OpInlineResultImpl) * max_inline_result_num |
| 36 | + : sizeof(detail::OpInlineResultImpl) * num_results; |
| 37 | + size_t operand_mem_size = sizeof(detail::OpOperandImpl) * num_operands; |
| 38 | + size_t op_mem_size = sizeof(Operation); |
| 39 | + size_t base_size = result_mem_size + op_mem_size + operand_mem_size; |
| 40 | + // 2. Malloc memory. |
| 41 | + char *base_ptr = reinterpret_cast<char *>(aligned_malloc(base_size, 8)); |
| 42 | + // 3.1. Construct OpResults. |
| 43 | + for (size_t idx = num_results; idx > 0; idx--) { |
| 44 | + if (idx > max_inline_result_num) { |
| 45 | + new (base_ptr) |
| 46 | + detail::OpOutlineResultImpl(output_types[idx - 1], idx - 1); |
| 47 | + base_ptr += sizeof(detail::OpOutlineResultImpl); |
| 48 | + } else { |
| 49 | + new (base_ptr) detail::OpInlineResultImpl(output_types[idx - 1], idx - 1); |
| 50 | + base_ptr += sizeof(detail::OpInlineResultImpl); |
| 51 | + } |
| 52 | + } |
| 53 | + // 3.2. Construct Operation. |
| 54 | + Operation *op = |
| 55 | + new (base_ptr) Operation(num_results, num_operands, attribute); |
| 56 | + base_ptr += sizeof(Operation); |
| 57 | + // 3.3. Construct OpOperands. |
| 58 | + if ((reinterpret_cast<uintptr_t>(base_ptr) & 0x7) != 0) { |
| 59 | + throw("The address of OpOperandImpl must be divisible by 8."); |
| 60 | + } |
| 61 | + for (size_t idx = 0; idx < num_operands; idx++) { |
| 62 | + new (base_ptr) detail::OpOperandImpl(inputs[idx].impl_, op); |
| 63 | + base_ptr += sizeof(detail::OpOperandImpl); |
| 64 | + } |
| 65 | + VLOG(4) << "Construct an Operation: " << op->print(); |
| 66 | + return op; |
| 67 | +} |
| 68 | + |
| 69 | +// Call destructors for OpResults, Operation, and OpOperands in sequence, and |
| 70 | +// finally free memory. |
| 71 | +void Operation::destroy() { |
| 72 | + // 1. Get aligned_ptr by result_num. |
| 73 | + uint32_t max_inline_result_num = |
| 74 | + detail::OpResultImpl::GetMaxInlineResultIndex() + 1; |
| 75 | + size_t result_mem_size = |
| 76 | + num_results_ > max_inline_result_num |
| 77 | + ? sizeof(detail::OpOutlineResultImpl) * |
| 78 | + (num_results_ - max_inline_result_num) + |
| 79 | + sizeof(detail::OpInlineResultImpl) * max_inline_result_num |
| 80 | + : sizeof(detail::OpInlineResultImpl) * num_results_; |
| 81 | + char *aligned_ptr = reinterpret_cast<char *>(this) - result_mem_size; |
| 82 | + // 2.1. Deconstruct OpResult. |
| 83 | + char *base_ptr = aligned_ptr; |
| 84 | + for (size_t idx = num_results_; idx > 0; idx--) { |
| 85 | + if (!reinterpret_cast<detail::OpResultImpl *>(base_ptr)->use_empty()) { |
| 86 | + throw("Cannot destroy a value that still has uses!"); |
| 87 | + } |
| 88 | + if (idx > max_inline_result_num) { |
| 89 | + reinterpret_cast<detail::OpOutlineResultImpl *>(base_ptr) |
| 90 | + ->~OpOutlineResultImpl(); |
| 91 | + base_ptr += sizeof(detail::OpOutlineResultImpl); |
| 92 | + } else { |
| 93 | + reinterpret_cast<detail::OpInlineResultImpl *>(base_ptr) |
| 94 | + ->~OpInlineResultImpl(); |
| 95 | + base_ptr += sizeof(detail::OpInlineResultImpl); |
| 96 | + } |
| 97 | + } |
| 98 | + // 2.2. Deconstruct Operation. |
| 99 | + if (reinterpret_cast<uintptr_t>(base_ptr) != |
| 100 | + reinterpret_cast<uintptr_t>(this)) { |
| 101 | + throw("Operation address error"); |
| 102 | + } |
| 103 | + reinterpret_cast<Operation *>(base_ptr)->~Operation(); |
| 104 | + base_ptr += sizeof(Operation); |
| 105 | + // 2.3. Deconstruct OpOpOerand. |
| 106 | + for (size_t idx = 0; idx < num_operands_; idx++) { |
| 107 | + reinterpret_cast<detail::OpOperandImpl *>(base_ptr)->~OpOperandImpl(); |
| 108 | + base_ptr += sizeof(detail::OpOperandImpl); |
| 109 | + } |
| 110 | + // 3. Free memory. |
| 111 | + VLOG(4) << "Destroy an Operation: {ptr = " |
| 112 | + << reinterpret_cast<void *>(aligned_ptr) |
| 113 | + << ", size = " << result_mem_size << "}"; |
| 114 | + aligned_free(reinterpret_cast<void *>(aligned_ptr)); |
| 115 | +} |
| 116 | + |
| 117 | +Operation::Operation(uint32_t num_results, |
| 118 | + uint32_t num_operands, |
| 119 | + ir::DictionaryAttribute attribute) { |
| 120 | + if (!attribute) { |
| 121 | + throw("unexpected null attribute dictionary"); |
| 122 | + } |
| 123 | + num_results_ = num_results; |
| 124 | + num_operands_ = num_operands; |
| 125 | + attribute_ = attribute; |
| 126 | +} |
| 127 | + |
| 128 | +ir::OpResult Operation::GetResultByIndex(uint32_t index) { |
| 129 | + if (index >= num_results_) { |
| 130 | + throw("index exceeds OP output range."); |
| 131 | + } |
| 132 | + uint32_t max_inline_idx = detail::OpResultImpl::GetMaxInlineResultIndex(); |
| 133 | + char *ptr = nullptr; |
| 134 | + if (index > max_inline_idx) { |
| 135 | + ptr = reinterpret_cast<char *>(this) - |
| 136 | + (max_inline_idx + 1) * sizeof(detail::OpInlineResultImpl) - |
| 137 | + (index - max_inline_idx) * sizeof(detail::OpOutlineResultImpl); |
| 138 | + } else { |
| 139 | + ptr = reinterpret_cast<char *>(this) - |
| 140 | + (index + 1) * sizeof(detail::OpInlineResultImpl); |
| 141 | + } |
| 142 | + if (index > max_inline_idx) { |
| 143 | + detail::OpOutlineResultImpl *result_impl_ptr = |
| 144 | + reinterpret_cast<detail::OpOutlineResultImpl *>(ptr); |
| 145 | + return ir::OpResult(result_impl_ptr); |
| 146 | + } else { |
| 147 | + detail::OpInlineResultImpl *result_impl_ptr = |
| 148 | + reinterpret_cast<detail::OpInlineResultImpl *>(ptr); |
| 149 | + return ir::OpResult(result_impl_ptr); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +std::string Operation::print() { |
| 154 | + std::stringstream result; |
| 155 | + result << "{ " << num_results_ << " outputs, " << num_operands_ |
| 156 | + << " inputs } : "; |
| 157 | + result << "[ "; |
| 158 | + for (size_t idx = num_results_; idx > 0; idx--) { |
| 159 | + result << GetResultByIndex(idx - 1).impl_ << ", "; |
| 160 | + } |
| 161 | + result << "] = "; |
| 162 | + result << this << "( "; |
| 163 | + for (size_t idx = 0; idx < num_operands_; idx++) { |
| 164 | + result << reinterpret_cast<void *>(reinterpret_cast<char *>(this) + |
| 165 | + sizeof(Operation) + |
| 166 | + idx * sizeof(detail::OpOperandImpl)) |
| 167 | + << ", "; |
| 168 | + } |
| 169 | + result << ")"; |
| 170 | + return result.str(); |
| 171 | +} |
| 172 | + |
| 173 | +} // namespace ir |
0 commit comments