Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/tvm/relax/analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,21 @@ TVM_DLL bool WellFormed(IRModule m, bool check_struct_info = true);
TVM_DLL Map<tir::Block, Map<ObjectRef, tir::IndexMap>> SuggestLayoutTransforms(
const Function& fn, Array<tir::IndexMap> write_buffer_transformations);

/* \brief Collect variables whose value can be computed at compile-time
*
* If a function has the `kNumInput` attribute, then the first
* `kNumInput` parameters are provided at run-time, while all
* remaining parameters may be known at compile-time. This utility
* collects all variable bindings that only depend, directly or
* indirectly, on the parameters known at compile-time.
*
* \param func The relax::Function to analyze
*
* \return The set of variables that can be computed at compile-time,
* in order of their occurrence within the function.
*/
TVM_DLL Array<Var> ComputableAtCompileTime(const Function& func);

} // namespace relax
} // namespace tvm

Expand Down
1 change: 1 addition & 0 deletions python/tvm/relax/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
all_global_vars,
all_vars,
bound_vars,
computable_at_compile_time,
contains_impure_call,
definable_tir_vars_in_struct_info,
defined_symbolic_vars,
Expand Down
25 changes: 25 additions & 0 deletions python/tvm/relax/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,28 @@ def detect_recursion(mod: tvm.IRModule) -> List[List[GlobalVar]]:
with any other, it will be a singleton in this list.
"""
return _ffi_api.detect_recursion(mod) # type: ignore


def computable_at_compile_time(func: Function) -> List[Var]:
"""Collect variables whose value can be computed at compile-time

If a function has the `kNumInput` attribute, then the first
`kNumInput` parameters are provided at run-time, while all
remaining parameters may be known at compile-time. This utility
collects all variable bindings that only depend, directly or
indirectly, on the parameters known at compile-time.

Parameters
----------
func: Function

The `relax.Function` to analyze

Returns
-------
ret: List[Var]

The set of variables that can be computed at compile-time, in
order of their occurrence within the function.
"""
return _ffi_api.computable_at_compile_time(func) # type: ignore
99 changes: 99 additions & 0 deletions src/relax/analysis/computable_at_compile_time.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 computable_at_compile_time.cc
*
* \brief Utilities for identifying potentially compile-time variables
*/

#include <tvm/relax/analysis.h>
#include <tvm/relax/expr_functor.h>

#include "../../support/ordered_set.h"

namespace tvm {
namespace relax {

namespace {
class CompileTimeCollector : ExprVisitor {
public:
static Array<Var> Collect(const Function& func) {
CompileTimeCollector visitor;
visitor(func);
return Array<Var>(visitor.known_relax_vars_.begin(), visitor.known_relax_vars_.end());
}

private:
void VisitExpr_(const FunctionNode* func) override {
if (auto opt_num_input = func->attrs.GetAttr<Integer>(attr::kNumInput)) {
size_t num_input = opt_num_input.value()->value;
for (size_t i = num_input; i < func->params.size(); i++) {
MarkAsKnown(func->params[i]);
}
}

ExprVisitor::VisitExpr_(func);
}

void VisitBinding(const Binding& binding) override {
Expr value = GetBoundValue(binding);
bool can_compute_at_compile_time = [&]() {
for (const auto& relax_var : FreeVars(value)) {
if (!known_relax_vars_.count(relax_var)) {
return false;
}
}
for (const auto& tir_var : FreeSymbolicVars(value)) {
if (!known_tir_vars_.count(tir_var)) {
return false;
}
}

return true;
}();

if (can_compute_at_compile_time) {
MarkAsKnown(binding->var);
}

ExprVisitor::VisitBinding(binding);
}

void MarkAsKnown(const Var& var) {
known_relax_vars_.insert(var);
for (const auto& tir_var : DefinableTIRVarsInStructInfo(GetStructInfo(var))) {
known_tir_vars_.insert(tir_var);
}
}

support::OrderedSet<Var> known_relax_vars_;
std::unordered_set<tir::Var, ObjectPtrHash, ObjectPtrEqual> known_tir_vars_;
};
} // namespace

Array<Var> ComputableAtCompileTime(const Function& func) {
return CompileTimeCollector::Collect(func);
}

TVM_REGISTER_GLOBAL("relax.analysis.computable_at_compile_time")
.set_body_typed(ComputableAtCompileTime);

} // namespace relax
} // namespace tvm
Loading