MCPcopy Index your code
hub / github.com/apache/tvm / parse_macro

Method parse_macro

python/tvm/relax/script/parser/entry.py:89–111  ·  view source on GitHub ↗
(self, parser: Parser)

Source from the content-addressed store, hash-verified

87 """Specialization of the ScriptMacro class for Relax."""
88
89 def parse_macro(self, parser: Parser) -> Expr:
90 macro_def = self.get_macro_def()
91 ret_value = None
92
93 with R.SeqExpr() as seq:
94 for idx, stmt in enumerate(macro_def.body):
95 # Normally, a "return" statement is only allowed in a R.function. We don't
96 # want to parse the macro's body as if it was a body of a function, because
97 # the latter imposes some constraints that we want to avoid.
98 # At the same time, we want to use "return" to indicate the value of the
99 # macro (since in Relax everything is an expression), so add special handling
100 # of "return".
101 if isinstance(stmt, doc.Return):
102 ret_value = parser.eval_expr(stmt.value)
103 if idx + 1 != len(macro_def.body):
104 parser.report_error(macro_def, "'return' should be the last statement")
105 break
106 parser.visit(stmt)
107
108 if ret_value is None:
109 parser.report_error(macro_def, "Macros must end with a return statement")
110
111 return SeqExpr(seq.binding_blocks, ret_value)
112
113
114def macro(*args, hygienic: bool = True) -> _Callable:

Callers

nothing calls this directly

Calls 5

SeqExprFunction · 0.90
get_macro_defMethod · 0.80
eval_exprMethod · 0.80
report_errorMethod · 0.80
visitMethod · 0.45

Tested by

no test coverage detected