|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2024 Intel Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | + |
| 17 | +#ifndef MLIR_ANALYSIS_GLOBALANALYSIS_H |
| 18 | +#define MLIR_ANALYSIS_GLOBALANALYSIS_H |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <memory> |
| 22 | +#include <numeric> |
| 23 | + |
| 24 | +#include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 25 | +#include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 26 | +#include "mlir/Support/LLVM.h" |
| 27 | +#include "llvm/ADT/DenseMap.h" |
| 28 | + |
| 29 | +namespace mlir { |
| 30 | +namespace gc { |
| 31 | + |
| 32 | +using namespace mlir; |
| 33 | + |
| 34 | +class TensorLayout { |
| 35 | +public: |
| 36 | + TensorLayout(ArrayRef<int64_t> outerAxis, ArrayRef<int64_t> innerAxis, |
| 37 | + ArrayRef<int64_t> tileSizes) { |
| 38 | + assert(innerAxis.size() == tileSizes.size()); |
| 39 | + for (auto oa : outerAxis) { |
| 40 | + OuterAxis.push_back(oa); |
| 41 | + } |
| 42 | + for (auto ia : innerAxis) { |
| 43 | + InnerAxis.push_back(ia); |
| 44 | + } |
| 45 | + for (auto ts : tileSizes) { |
| 46 | + TileSizes.push_back(ts); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + bool isPlainLayout() const { |
| 51 | + for (int64_t i = 0; i < static_cast<int64_t>(OuterAxis.size()); ++i) { |
| 52 | + if (i != OuterAxis[i]) |
| 53 | + return false; |
| 54 | + } |
| 55 | + return TileSizes.empty() && InnerAxis.empty(); |
| 56 | + } |
| 57 | + |
| 58 | + static TensorLayout createPlainLayout(int64_t rank) { |
| 59 | + SmallVector<int64_t> outerAxis(rank, 0); |
| 60 | + std::iota(outerAxis.begin(), outerAxis.end(), 0); |
| 61 | + return TensorLayout(outerAxis, SmallVector<int64_t>{}, |
| 62 | + SmallVector<int64_t>{}); |
| 63 | + } |
| 64 | + |
| 65 | + size_t getTensorRank() const { return OuterAxis.size(); } |
| 66 | + |
| 67 | + SmallVector<int64_t> getOuterAxis() const { return OuterAxis; } |
| 68 | + |
| 69 | + SmallVector<int64_t> getInnerAxis() const { return InnerAxis; } |
| 70 | + |
| 71 | + SmallVector<int64_t> getTileSizes() const { return TileSizes; } |
| 72 | + |
| 73 | + friend std::ostream &operator<<(std::ostream &ss, const TensorLayout &layout); |
| 74 | + |
| 75 | + bool operator==(const TensorLayout &layout); |
| 76 | + |
| 77 | +private: |
| 78 | + SmallVector<int64_t> OuterAxis; |
| 79 | + SmallVector<int64_t> InnerAxis; |
| 80 | + SmallVector<int64_t> TileSizes; |
| 81 | +}; |
| 82 | + |
| 83 | +class OperatorLayout { |
| 84 | +public: |
| 85 | + OperatorLayout() {} |
| 86 | + |
| 87 | + OperatorLayout(SmallVector<TensorLayout> inputLayouts, |
| 88 | + SmallVector<TensorLayout> outputLayouts) { |
| 89 | + supportedInputLayouts = inputLayouts; |
| 90 | + supportedOutputLayouts = outputLayouts; |
| 91 | + } |
| 92 | + |
| 93 | + SmallVector<TensorLayout> getSupportedInputLayouts() const { |
| 94 | + return supportedInputLayouts; |
| 95 | + } |
| 96 | + |
| 97 | + SmallVector<TensorLayout> getSupportedOutputLayouts() const { |
| 98 | + return supportedOutputLayouts; |
| 99 | + } |
| 100 | + |
| 101 | + TensorLayout getOutputLayout(int64_t idx) const { |
| 102 | + assert(idx < static_cast<int64_t>(supportedOutputLayouts.size())); |
| 103 | + return supportedOutputLayouts[idx]; |
| 104 | + } |
| 105 | + |
| 106 | + friend std::ostream &operator<<(std::ostream &ss, |
| 107 | + const OperatorLayout &opLayout); |
| 108 | + |
| 109 | +private: |
| 110 | + SmallVector<TensorLayout> supportedInputLayouts; |
| 111 | + SmallVector<TensorLayout> supportedOutputLayouts; |
| 112 | +}; |
| 113 | + |
| 114 | +class GlobalAnalysis { |
| 115 | +public: |
| 116 | + explicit GlobalAnalysis(Operation *root); |
| 117 | + |
| 118 | + FailureOr<OperatorLayout> getOpLayout(Operation *op) { |
| 119 | + if (layout.find(op) != layout.end()) |
| 120 | + return layout[op]; |
| 121 | + else |
| 122 | + return op->emitError("Current op does not have layout information."); |
| 123 | + } |
| 124 | + |
| 125 | +private: |
| 126 | + DenseMap<Operation *, OperatorLayout> layout; |
| 127 | +}; |
| 128 | + |
| 129 | +} // namespace gc |
| 130 | +} // namespace mlir |
| 131 | + |
| 132 | +#endif |
0 commit comments