|
| 1 | +#include "../utils.h" |
| 2 | + |
| 3 | +namespace tvm { |
| 4 | +namespace tir { |
| 5 | + |
| 6 | +class AnnotateRegionRewriter : public StmtExprMutator { |
| 7 | + public: |
| 8 | + AnnotateRegionRewriter(Buffer buffer, int buffer_index, BufferRegion new_region, |
| 9 | + BufferIndexType buffer_index_type) |
| 10 | + : buffer_(buffer), |
| 11 | + buffer_index_(buffer_index), |
| 12 | + new_region_(new_region), |
| 13 | + buffer_index_type_(buffer_index_type) {} |
| 14 | + |
| 15 | + Stmt VisitStmt_(const BlockNode* op) final { |
| 16 | + Block block = Downcast<Block>(StmtExprMutator::VisitStmt_(op)); |
| 17 | + |
| 18 | + Array<BufferRegion> regions = |
| 19 | + buffer_index_type_ == BufferIndexType::kWrite ? block->writes : block->reads; |
| 20 | + ICHECK_GE(buffer_index_, 0) << "Buffer index must be non-negative"; |
| 21 | + ICHECK_LT(buffer_index_, static_cast<int>(regions.size())) << "Buffer index out of range"; |
| 22 | + regions.Set(buffer_index_, new_region_); |
| 23 | + |
| 24 | + ObjectPtr<BlockNode> n = CopyOnWrite(block.get()); |
| 25 | + if (buffer_index_type_ == BufferIndexType::kWrite) { |
| 26 | + n->writes = std::move(regions); |
| 27 | + } else { |
| 28 | + n->reads = std::move(regions); |
| 29 | + } |
| 30 | + |
| 31 | + // Annotate the block with explicit_read_region or explicit_write_region |
| 32 | + Map<String, ObjectRef> new_annotations = n->annotations; |
| 33 | + String annotation_key = buffer_index_type_ == BufferIndexType::kWrite |
| 34 | + ? attr::explicit_write_region |
| 35 | + : attr::explicit_read_region; |
| 36 | + if (new_annotations.count(annotation_key)) { |
| 37 | + Array<Integer> buffer_indices = Downcast<Array<Integer>>(new_annotations[annotation_key]); |
| 38 | + bool found = false; |
| 39 | + for (const Integer& index : buffer_indices) { |
| 40 | + if (index->value == buffer_index_) { |
| 41 | + found = true; |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + if (!found) { |
| 46 | + buffer_indices.push_back(Integer(buffer_index_)); |
| 47 | + new_annotations.Set(annotation_key, buffer_indices); |
| 48 | + } |
| 49 | + } else { |
| 50 | + new_annotations.Set(annotation_key, Array<Integer>{Integer(buffer_index_)}); |
| 51 | + } |
| 52 | + n->annotations = std::move(new_annotations); |
| 53 | + |
| 54 | + return Block(n); |
| 55 | + } |
| 56 | + |
| 57 | + private: |
| 58 | + Buffer buffer_; |
| 59 | + int buffer_index_; |
| 60 | + BufferRegion new_region_; |
| 61 | + BufferIndexType buffer_index_type_; |
| 62 | +}; |
| 63 | + |
| 64 | +void AnnotateBufferAccess(ScheduleState self, const StmtSRef& block_sref, int buffer_index, |
| 65 | + BufferIndexType buffer_index_type, const IndexMap& index_map) { |
| 66 | + const BlockNode* block = TVM_SREF_TO_BLOCK(block_sref); |
| 67 | + Buffer buffer = GetNthAccessBuffer(self, GetRef<Block>(block), buffer_index, buffer_index_type); |
| 68 | + |
| 69 | + arith::Analyzer analyzer; |
| 70 | + Array<PrimExpr> block_iter_vars; |
| 71 | + for (const IterVar& iter_var : block->iter_vars) { |
| 72 | + block_iter_vars.push_back(iter_var->var); |
| 73 | + } |
| 74 | + Array<PrimExpr> new_indices = index_map->MapIndices(block_iter_vars, &analyzer); |
| 75 | + ICHECK_EQ(new_indices.size() % 2, 0) << "The size of new_indices should be even."; |
| 76 | + Array<Range> new_ranges; |
| 77 | + for (size_t i = 0; i < new_indices.size(); i += 2) { |
| 78 | + // (iter_var, iter_var) represents a single point |
| 79 | + if (analyzer.CanProveEqual(new_indices[i], new_indices[i + 1])) { |
| 80 | + new_ranges.push_back(Range::FromMinExtent(new_indices[i], 1)); |
| 81 | + } |
| 82 | + // (begin, end) represents a region |
| 83 | + else { |
| 84 | + new_ranges.push_back(Range::FromMinExtent( |
| 85 | + new_indices[i], analyzer.Simplify(new_indices[i + 1] - new_indices[i]))); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + BufferRegion new_region(buffer, new_ranges); |
| 90 | + |
| 91 | + AnnotateRegionRewriter mutator(buffer, buffer_index, new_region, buffer_index_type); |
| 92 | + Stmt new_stmt = mutator(GetRef<Stmt>(block_sref->stmt)); |
| 93 | + |
| 94 | + self->Replace(block_sref, new_stmt, {{GetRef<Block>(block), Downcast<Block>(new_stmt)}}); |
| 95 | +} |
| 96 | + |
| 97 | +struct AnnotateBufferAccessTraits : public UnpackedInstTraits<AnnotateBufferAccessTraits> { |
| 98 | + static constexpr const char* kName = "AnnotateBufferAccess"; |
| 99 | + static constexpr bool kIsPure = false; |
| 100 | + |
| 101 | + private: |
| 102 | + static constexpr size_t kNumInputs = 4; |
| 103 | + static constexpr size_t kNumAttrs = 0; |
| 104 | + static constexpr size_t kNumDecisions = 0; |
| 105 | + |
| 106 | + static void UnpackedApplyToSchedule(Schedule sch, BlockRV block, Integer buffer_index, |
| 107 | + Integer buffer_index_type, IndexMap index_map) { |
| 108 | + return sch->AnnotateBufferAccess(block, buffer_index->value, |
| 109 | + static_cast<BufferIndexType>(buffer_index_type->value), |
| 110 | + index_map); |
| 111 | + } |
| 112 | + |
| 113 | + static String IndexMap2GenNewRangesLambda(const IndexMap& index_map) { |
| 114 | + std::ostringstream oss; |
| 115 | + oss << "lambda "; |
| 116 | + for (size_t i = 0; i < index_map->initial_indices.size(); ++i) { |
| 117 | + if (i != 0) oss << ", "; |
| 118 | + oss << index_map->initial_indices[i]; |
| 119 | + } |
| 120 | + oss << ": ["; |
| 121 | + for (size_t i = 0; i < index_map->final_indices.size(); i += 2) { |
| 122 | + if (i != 0) oss << ", "; |
| 123 | + if (index_map->final_indices[i].same_as(index_map->final_indices[i + 1])) { |
| 124 | + oss << index_map->final_indices[i]; |
| 125 | + } else { |
| 126 | + oss << "(" << index_map->final_indices[i] << ", " << index_map->final_indices[i + 1] << ")"; |
| 127 | + } |
| 128 | + } |
| 129 | + oss << "]"; |
| 130 | + return String(oss.str()); |
| 131 | + } |
| 132 | + |
| 133 | + static String UnpackedAsPython(Array<String> outputs, String block, Integer buffer_index, |
| 134 | + Integer buffer_index_type, IndexMap index_map) { |
| 135 | + PythonAPICall py("annotate_buffer_access"); |
| 136 | + py.Input("block", block); |
| 137 | + py.Input("buffer_index", buffer_index->value); |
| 138 | + |
| 139 | + std::ostringstream os; |
| 140 | + os << "\"" << BufferIndexType2Str(static_cast<BufferIndexType>(buffer_index_type->value)) |
| 141 | + << "\""; |
| 142 | + py.Input("buf_type", os.str()); |
| 143 | + |
| 144 | + py.Input("gen_new_ranges", IndexMap2GenNewRangesLambda(index_map)); |
| 145 | + return py.Str(); |
| 146 | + } |
| 147 | + |
| 148 | + template <typename> |
| 149 | + friend struct ::tvm::tir::UnpackedInstTraits; |
| 150 | +}; |
| 151 | + |
| 152 | +TVM_REGISTER_INST_KIND_TRAITS(AnnotateBufferAccessTraits); |
| 153 | + |
| 154 | +} // namespace tir |
| 155 | +} // namespace tvm |
0 commit comments