|
| 1 | +/*! |
| 2 | + * \file legalize_negative_index.cc |
| 3 | + * \brief Legalize negative indices in buffer load expressions. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <tvm/ffi/reflection/registry.h> |
| 7 | +#include <tvm/runtime/logging.h> |
| 8 | +#include <tvm/tir/stmt_functor.h> |
| 9 | +#include <tvm/tir/transform.h> |
| 10 | + |
| 11 | +#include <unordered_map> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include "arith/ir_mutator_with_analyzer.h" |
| 15 | +#include "arith/ir_visitor_with_analyzer.h" |
| 16 | + |
| 17 | +namespace tvm { |
| 18 | +namespace tl { |
| 19 | + |
| 20 | +using namespace tir; |
| 21 | +using arith::IRVisitorWithAnalyzer; |
| 22 | + |
| 23 | +enum class IndexSignState { kNonNegative, kNegative, kUnknown }; |
| 24 | + |
| 25 | +class NegativeIndexAnalyzer : public IRVisitorWithAnalyzer { |
| 26 | +public: |
| 27 | + explicit NegativeIndexAnalyzer( |
| 28 | + std::unordered_map<const BufferLoadNode *, std::vector<IndexSignState>> |
| 29 | + *result) |
| 30 | + : result_(result) {} |
| 31 | + |
| 32 | + void VisitExpr_(const BufferLoadNode *op) final { |
| 33 | + auto load = tvm::ffi::GetRef<BufferLoad>(op); |
| 34 | + std::vector<IndexSignState> states; |
| 35 | + states.reserve(op->indices.size()); |
| 36 | + bool needs_record = false; |
| 37 | + |
| 38 | + for (size_t i = 0; i < op->indices.size(); ++i) { |
| 39 | + PrimExpr simplified = analyzer_.Simplify(op->indices[i]); |
| 40 | + if (analyzer_.CanProve(simplified >= 0)) { |
| 41 | + states.push_back(IndexSignState::kNonNegative); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if (analyzer_.CanProve(simplified < 0)) { |
| 46 | + states.push_back(IndexSignState::kNegative); |
| 47 | + needs_record = true; |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + states.push_back(IndexSignState::kUnknown); |
| 52 | + needs_record = true; |
| 53 | + LOG(WARNING) << "LegalizeNegativeIndex: cannot prove non-negative index " |
| 54 | + << simplified << " for buffer " << load->buffer->name |
| 55 | + << " (axis " << i << ")."; |
| 56 | + } |
| 57 | + |
| 58 | + if (needs_record) { |
| 59 | + (*result_)[op] = std::move(states); |
| 60 | + } |
| 61 | + |
| 62 | + IRVisitorWithAnalyzer::VisitExpr_(op); |
| 63 | + } |
| 64 | + |
| 65 | +private: |
| 66 | + std::unordered_map<const BufferLoadNode *, std::vector<IndexSignState>> |
| 67 | + *result_; |
| 68 | +}; |
| 69 | + |
| 70 | +class NegativeIndexRewriter : public arith::IRMutatorWithAnalyzer { |
| 71 | +public: |
| 72 | + static PrimFunc |
| 73 | + Apply(PrimFunc func, |
| 74 | + const std::unordered_map<const BufferLoadNode *, |
| 75 | + std::vector<IndexSignState>> &states) { |
| 76 | + arith::Analyzer analyzer; |
| 77 | + NegativeIndexRewriter rewriter(&analyzer, states); |
| 78 | + if (!func->body.defined()) { |
| 79 | + return func; |
| 80 | + } |
| 81 | + PrimFuncNode *func_node = func.CopyOnWrite(); |
| 82 | + func_node->body = rewriter.VisitStmt(func_node->body); |
| 83 | + return func; |
| 84 | + } |
| 85 | + |
| 86 | +private: |
| 87 | + NegativeIndexRewriter( |
| 88 | + arith::Analyzer *analyzer, |
| 89 | + const std::unordered_map<const BufferLoadNode *, |
| 90 | + std::vector<IndexSignState>> &states) |
| 91 | + : arith::IRMutatorWithAnalyzer(analyzer), states_(states) {} |
| 92 | + |
| 93 | + PrimExpr VisitExpr_(const BufferLoadNode *op) final { |
| 94 | + BufferLoad load = |
| 95 | + Downcast<BufferLoad>(arith::IRMutatorWithAnalyzer::VisitExpr_(op)); |
| 96 | + |
| 97 | + auto it = states_.find(op); |
| 98 | + if (it == states_.end()) { |
| 99 | + return load; |
| 100 | + } |
| 101 | + |
| 102 | + auto indices = load->indices; |
| 103 | + bool changed = false; |
| 104 | + |
| 105 | + const auto &state_vector = it->second; |
| 106 | + ICHECK_EQ(state_vector.size(), indices.size()) |
| 107 | + << "State vector size mismatch for buffer load " << load->buffer->name; |
| 108 | + |
| 109 | + for (size_t i = 0; i < indices.size(); ++i) { |
| 110 | + if (state_vector[i] != IndexSignState::kNegative) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + PrimExpr extent = load->buffer->shape[i]; |
| 114 | + indices.Set(i, analyzer_->Simplify(extent + indices[i])); |
| 115 | + changed = true; |
| 116 | + } |
| 117 | + |
| 118 | + if (!changed) { |
| 119 | + return load; |
| 120 | + } |
| 121 | + |
| 122 | + return BufferLoad(load->buffer, indices); |
| 123 | + } |
| 124 | + |
| 125 | + const std::unordered_map<const BufferLoadNode *, std::vector<IndexSignState>> |
| 126 | + &states_; |
| 127 | +}; |
| 128 | + |
| 129 | +PrimFunc LegalizeNegativeIndex(PrimFunc func) { |
| 130 | + if (!func->body.defined()) { |
| 131 | + return func; |
| 132 | + } |
| 133 | + |
| 134 | + std::unordered_map<const BufferLoadNode *, std::vector<IndexSignState>> |
| 135 | + states; |
| 136 | + NegativeIndexAnalyzer analyzer(&states); |
| 137 | + analyzer(func->body); |
| 138 | + if (states.empty()) { |
| 139 | + return func; |
| 140 | + } |
| 141 | + |
| 142 | + return NegativeIndexRewriter::Apply(std::move(func), states); |
| 143 | +} |
| 144 | + |
| 145 | +tvm::transform::Pass LegalizeNegativeIndexPass() { |
| 146 | + using namespace tir::transform; |
| 147 | + auto pass_func = [](PrimFunc f, const IRModule &, PassContext) { |
| 148 | + return LegalizeNegativeIndex(std::move(f)); |
| 149 | + }; |
| 150 | + return CreatePrimFuncPass(pass_func, 0, "tl.LegalizeNegativeIndex", {}); |
| 151 | +} |
| 152 | + |
| 153 | +TVM_FFI_STATIC_INIT_BLOCK() { |
| 154 | + namespace refl = tvm::ffi::reflection; |
| 155 | + refl::GlobalDef().def("tl.transform.LegalizeNegativeIndex", |
| 156 | + LegalizeNegativeIndexPass); |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace tl |
| 160 | +} // namespace tvm |
0 commit comments