|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file computable_at_compile_time.cc |
| 22 | + * |
| 23 | + * \brief Utilities for identifying potentially compile-time variables |
| 24 | + */ |
| 25 | + |
| 26 | +#include <tvm/relax/analysis.h> |
| 27 | +#include <tvm/relax/expr_functor.h> |
| 28 | + |
| 29 | +#include "../../support/ordered_set.h" |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace relax { |
| 33 | + |
| 34 | +namespace { |
| 35 | +class CompileTimeCollector : ExprVisitor { |
| 36 | + public: |
| 37 | + static Array<Var> Collect(const Function& func) { |
| 38 | + CompileTimeCollector visitor; |
| 39 | + visitor(func); |
| 40 | + return Array<Var>(visitor.known_relax_vars_.begin(), visitor.known_relax_vars_.end()); |
| 41 | + } |
| 42 | + |
| 43 | + private: |
| 44 | + void VisitExpr_(const FunctionNode* func) override { |
| 45 | + if (auto opt_num_input = func->attrs.GetAttr<Integer>(attr::kNumInput)) { |
| 46 | + size_t num_input = opt_num_input.value()->value; |
| 47 | + for (size_t i = num_input; i < func->params.size(); i++) { |
| 48 | + MarkAsKnown(func->params[i]); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + ExprVisitor::VisitExpr_(func); |
| 53 | + } |
| 54 | + |
| 55 | + void VisitBinding(const Binding& binding) override { |
| 56 | + Expr value = GetBoundValue(binding); |
| 57 | + bool can_compute_at_compile_time = [&]() { |
| 58 | + for (const auto& relax_var : FreeVars(value)) { |
| 59 | + if (!known_relax_vars_.count(relax_var)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + for (const auto& tir_var : FreeSymbolicVars(value)) { |
| 64 | + if (!known_tir_vars_.count(tir_var)) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | + }(); |
| 71 | + |
| 72 | + if (can_compute_at_compile_time) { |
| 73 | + MarkAsKnown(binding->var); |
| 74 | + } |
| 75 | + |
| 76 | + ExprVisitor::VisitBinding(binding); |
| 77 | + } |
| 78 | + |
| 79 | + void MarkAsKnown(const Var& var) { |
| 80 | + known_relax_vars_.insert(var); |
| 81 | + for (const auto& tir_var : DefinableTIRVarsInStructInfo(GetStructInfo(var))) { |
| 82 | + known_tir_vars_.insert(tir_var); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + support::OrderedSet<Var> known_relax_vars_; |
| 87 | + std::unordered_set<tir::Var, ObjectPtrHash, ObjectPtrEqual> known_tir_vars_; |
| 88 | +}; |
| 89 | +} // namespace |
| 90 | + |
| 91 | +Array<Var> ComputableAtCompileTime(const Function& func) { |
| 92 | + return CompileTimeCollector::Collect(func); |
| 93 | +} |
| 94 | + |
| 95 | +TVM_REGISTER_GLOBAL("relax.analysis.computable_at_compile_time") |
| 96 | + .set_body_typed(ComputableAtCompileTime); |
| 97 | + |
| 98 | +} // namespace relax |
| 99 | +} // namespace tvm |
0 commit comments