junrushao1994 commented on a change in pull request #7765: URL: https://github.com/apache/tvm/pull/7765#discussion_r603803031
########## File path: src/tir/schedule/utils.h ########## @@ -0,0 +1,95 @@ +/* + * 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. + */ +#ifndef TVM_TIR_SCHEDULE_UTILS_H_ +#define TVM_TIR_SCHEDULE_UTILS_H_ + +#include <tvm/arith/analyzer.h> +#include <tvm/tir/analysis.h> +#include <tvm/tir/function.h> +#include <tvm/tir/op.h> +#include <tvm/tir/schedule/state.h> +#include <tvm/tir/stmt_functor.h> + +#include "./analysis.h" + +namespace tvm { +namespace tir { + +/*! + * \brief A helper macro to convert an sref to the statement it points to, + * then check if the downcasting succeeded. + * \param Result The result variable, used for checking + * \param SRef The SRef to be casted + * \param Type The type to be casted to, can be Block or For + * \note The `E` in the macro means `error`, which means allowing to customize error message + */ +#define TVM_SREF_TO_E(Result, SRef, Type) \ Review comment: Inlined functions are generally good, but I am not in favor of it in this particular case - I considered this before but didn't go with that idea, and here is the reason: - When an error occurs, we want to print the exact line/function/file that throws that error: if we use an inline function, then instead of rendering the caller, it throws in the inline function in utils.h, which is much less informative. - The caller should be responsible for writing the declaration of the variable. Comparing the following two, I would go for the first one, because it writes the type clearly, allows re-assignment of a variable, and makes it really clear what we are doing. - The only disadvantage is that we need to repeat the name "block" twice, which is not quite inconvenient IMO. ```C++ const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref); // compared with TVM_SREF_TO_BLOCK(block, block_sref); ``` A good alternative I considered before is to use inlined lambda function which expands like: ```C++ const auto* block = [&]() -> const BlockNode* { const BlockNode* stmt = sref->StmtAs<BlockNode>(); ICHECK(stmt != nullptr) << "Error Message"; return stmt; }(); ``` The disadvantage of this approach is that the error message is not customizable, and it really depends on the compiler to optimize the lambda out. -- 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. For queries about this service, please contact Infrastructure at: [email protected]
