tvm
Loading...
Searching...
No Matches
block_builder.h
Go to the documentation of this file.
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
24#ifndef TVM_RELAX_BLOCK_BUILDER_H_
25#define TVM_RELAX_BLOCK_BUILDER_H_
26
27#include <tvm/base.h>
29#include <tvm/relax/expr.h>
30#include <tvm/relax/utils.h>
31#include <tvm/runtime/base.h>
32#include <tvm/sym/analyzer.h>
33#include <tvm/target/target.h>
34
35namespace tvm {
36namespace relax {
37
67class BlockBuilderObj : public ffi::Object {
68 public:
70 ffi::Optional<Target> target;
71
72 //-------------------------------
73 // Global Context management
74 //-------------------------------
81
88 virtual IRModule GetContextIRModule() const = 0;
89
101 virtual IRModule Finalize() = 0;
102
111 virtual GlobalVar AddFunction(const BaseFunc& func, ffi::String func_name_hint) = 0;
112
118 virtual void UpdateFunction(const GlobalVar& gv, BaseFunc function) = 0;
119
120 //-------------------------------
121 // Scope management
122 //-------------------------------
129 virtual ffi::Optional<Expr> LookupBinding(const Var& var) = 0;
130
145 virtual void BeginScope(ffi::Optional<ffi::Array<Var>> params) = 0;
146
160 virtual void BeginInnerScope() = 0;
161
174 virtual void AddDefinitionToScope(Var var) = 0;
175
177 virtual void EndScope() = 0;
178
180 virtual void BeginDataflowBlock() = 0;
181
183 virtual void BeginBindingBlock() = 0;
188 virtual BindingBlock EndBlock() = 0;
189
194 virtual bool CurrentBlockIsDataFlow() = 0;
195
205 virtual Var Emit(Expr expr, ffi::String name_hint = "") = 0;
206
214 virtual Var EmitMatchCast(Expr value, Type ty, ffi::String name_hint = "") = 0;
215
222 virtual Var EmitOutput(Expr output, ffi::String name_hint = "") = 0;
223
231 virtual void EmitNormalized(Binding normalized_binding) = 0;
232
241 virtual Expr Normalize(const Expr& expr) = 0;
242
250 virtual Expr NormalizeArgument(const Expr& expr) = 0;
251
257
258 static constexpr const bool _type_mutable = true;
259 TVM_FFI_DECLARE_OBJECT_INFO("relax.BlockBuilder", BlockBuilderObj, ffi::Object);
260};
261
262class BlockBuilder : public ffi::ObjectRef {
263 public:
275 TVM_DLL static BlockBuilder Create(ffi::Optional<IRModule> ctx_mod);
276
316 TVM_DLL static BlockBuilder Create(ffi::Optional<IRModule> ctx_mod,
318
319 explicit BlockBuilder(ffi::UnsafeInit tag, ffi::ObjectPtr<BlockBuilderObj> n)
320 : ffi::ObjectRef(tag) {
321 data_ = std::move(n);
322 }
324};
325
326} // namespace relax
327} // namespace tvm
328
329#endif // TVM_RELAX_BLOCK_BUILDER_H_
Algebra expression simplifications.
Managed reference to BaseFuncObj.
Definition function.h:202
Managed reference to ExprObj.
Definition expr.h:101
Managed reference to GlobalVarObj.
Definition expr.h:709
Managed reference class to IRModuleObj.
Definition module.h:292
Managed reference to TypeObj.
Definition type.h:75
Managed reference class to UniqueNameSupplyObj.
Definition unique_name_supply.h:115
Managed reference to VarObj.
Definition expr.h:571
Definition expr.h:229
Definition expr.h:123
A builder to build Relax binding blocks.
Definition block_builder.h:67
virtual void BeginBindingBlock()=0
Begin to build a BindingBlock.
virtual sym::Analyzer GetAnalyzer()=0
Get the analyzer of the BlockBuilder.
ffi::Optional< Target > target
Explicit target for the operation being legalized, if one was supplied.
Definition block_builder.h:70
virtual void UpdateFunction(const GlobalVar &gv, BaseFunc function)=0
Update a Relax function or a TIR tirx::Function in the internal context module.
virtual ffi::Optional< Expr > LookupBinding(const Var &var)=0
Lookup the binding value that var binds to in the current emitted sequences.
virtual Expr Normalize(const Expr &expr)=0
Convert an expression to normal form, and try to eagerly infer types and shapes.
static constexpr const bool _type_mutable
Definition block_builder.h:258
virtual void BeginInnerScope()=0
Begin a new scope, which inherits visible parameters from its parent scope.
virtual void BeginScope(ffi::Optional< ffi::Array< Var > > params)=0
Begin a new scope, with optional parameters that are visible within the scope.
virtual Var EmitOutput(Expr output, ffi::String name_hint="")=0
Generate an output for the current dataflow block.
virtual void EmitNormalized(Binding normalized_binding)=0
Emit a binding that is already normalized.
virtual BindingBlock EndBlock()=0
End building a BindingBlock.
virtual IRModule Finalize()=0
Finalize the building process and return the result IRModule. Possibly rename GlobalVars in the IRMod...
virtual bool CurrentBlockIsDataFlow()=0
Check if the block being built is DataflowBlock or not.
virtual void AddDefinitionToScope(Var var)=0
Append a definition to the current scope.
TVM_FFI_DECLARE_OBJECT_INFO("relax.BlockBuilder", BlockBuilderObj, ffi::Object)
virtual Var Emit(Expr expr, ffi::String name_hint="")=0
Emits an Expr, and returns the variable it is bound to.
virtual void BeginDataflowBlock()=0
Begin to build a DataflowBlock.
virtual UniqueNameSupply name_supply()=0
Get the unique name supply for generating unique names.
virtual GlobalVar AddFunction(const BaseFunc &func, ffi::String func_name_hint)=0
Add a Relax function or a TIR tirx::Function to internal context module.
virtual Expr NormalizeArgument(const Expr &expr)=0
Normalize argument to a call or another IRNode.
virtual IRModule GetContextIRModule() const =0
Get the context IRModule in this builder.
virtual Var EmitMatchCast(Expr value, Type ty, ffi::String name_hint="")=0
Emit a MatchCast.
virtual void EndScope()=0
End the previously defined scope.
Definition block_builder.h:262
BlockBuilder(ffi::UnsafeInit tag, ffi::ObjectPtr< BlockBuilderObj > n)
Definition block_builder.h:319
static BlockBuilder Create(ffi::Optional< IRModule > ctx_mod, DisableOperatorSpecificNormalizationForTVMScript tag)
Create a BlockBuilder.
static BlockBuilder Create(ffi::Optional< IRModule > ctx_mod)
Create a BlockBuilder.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BlockBuilder, ffi::ObjectRef, BlockBuilderObj)
Managed reference to AnalyzerObj.
Definition analyzer.h:750
Parent compiler symbol visibility.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Utility classes and functions for working with the Relax IR.
A marker struct to disable FNormalize.
Definition block_builder.h:300
Compilation target object.
UniqueNameSupply that can be used to generate unique variable names.