Lunderberg commented on a change in pull request #9727: URL: https://github.com/apache/tvm/pull/9727#discussion_r770081720
########## File path: include/tvm/tir/index_map.h ########## @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file tvm/tir/index_map.h + * \brief Defines a remapping of buffer indices + * + * For use with tvm::tir::Buffer. + */ +#ifndef TVM_TIR_INDEX_MAP_H_ +#define TVM_TIR_INDEX_MAP_H_ + +#include <tvm/ir/expr.h> +#include <tvm/runtime/container/array.h> +#include <tvm/runtime/object.h> +#include <tvm/tir/var.h> + +namespace tvm { +namespace tir { + +/*! + * \brief Defines the mapping from logical layout of a tensor to + * physical layout of a buffer. + */ +class IndexMapNode : public Object { Review comment: This could go into the `Buffer` along-side the pre-flattened shapes/strides, rather than its current location as a `PrimFunc` attribute, but wouldn't entirely replace them. The `IndexMap` represents transformations that occur during the [ApplyLayoutTransforms](https://github.com/apache/tvm/blob/6e9e4e6b0/src/tir/transforms/storage_flatten.cc#L1079) pass (TE schedules), or at the time when applied by a user (schedulable TIR). The constraints that I ran into were as follows: 1. Buffer flattening occurs during `tir.lower`, and is a prerequisite to several other passes. 2. For a given pre-flattened shape, there exist more than one way to flatten it. 3. The checks in defined [MakePackedAPI](https://github.com/apache/tvm/blob/6e9e4e6b0/src/driver/driver_api.cc#L627), during [the call](https://github.com/apache/tvm/blob/6e9e4e6b0/src/tir/transforms/make_packed_api.cc#L247) to [ArgBinder::BindDLTensor](https://github.com/apache/tvm/blob/6e9e4e6b0/src/tir/transforms/arg_binder.cc#L149), are on the pre-flattened shape/strides. 4. The low-level TIR should have direct access to the flattened buffer shape, and flattened indices. Any one of those constraints, if broken, would avoid the need for the pre-flattened shape/strides. However, each one came with tradeoffs that I wanted to avoid. 1. If buffer flattening occurs after `MakePackedAPI` call, then it wouldn't need separate storage, because the pre-flattened shape/strides would still be present as the the buffer's shape/strides. However, this would require significant changes to all passes in-between. 2. If there were only a single way to flatten a buffer, then the Buffer could always store the pre-flattened shape/strides, and the flattened shape could be generated as needed. This was how the previous version of the code worked, since the only flattening was flattening to a single flat memory buffer. 3. If the checks were based on the post-flattened shape/strides, then MakePackedAPI wouldn't need the pre-flattened shape/strides. However, since these are checked against the array shape passed by the user, that would be a significant breaking change. 4. If the buffer maintained the pre-flattened shape/strides, and generated the post-flattened shape as needed, then the buffer could maintain the pre-flattened shape through to the low-level TIR. However, this would require passes to know whether they are before/after flattening, and introduce different semantics based on that location. (e.g. Before flattening, `buffer_load->indices.size() == buffer_load->buffer.shape.size()`. After flattening, `buffer_load->indices.size() == buffer_load->buffer.GetFlattenedBuffer().shape.size()`.) I didn't want to introduce that dependency on location in the lowering flow. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org