Skip to content

Commit b3a5e18

Browse files
authored
[TVMScript] Improved error message for unexpected top frame (#14399)
Previously, the error messasge from `FindPrimFuncFrame`, `FindBlockFrame`, and `FindIfFrame` stated that they could not find the requested frame when the top-most frame did not match the requested type. This error message could be misinterpreted by a user as stating that the frame didn't exist at all. This commit updates the error message to distinguish between the case of a missing frame (e.g. `T.reads()` occurring outside of any `T.block()` frame) and a frame not being top-most (e.g. `T.reads()` occurring inside a `T.block()`, but inside an `if` conditional instead of the top of the block).
1 parent 14ddb37 commit b3a5e18

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/script/ir_builder/tir/utils.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ inline tvm::tir::Stmt AsStmt(const Array<tvm::tir::Stmt>& stmt) {
6969
inline PrimFuncFrame FindPrimFuncFrame(const String& method) {
7070
if (Optional<PrimFuncFrame> frame = IRBuilder::Current()->GetLastFrame<PrimFuncFrame>()) {
7171
return frame.value();
72+
} else if (Optional<PrimFuncFrame> frame = IRBuilder::Current()->FindFrame<PrimFuncFrame>()) {
73+
LOG(FATAL) << "ValueError: " << method << " must be called at the top of a PrimFunc. "
74+
<< "While " << method << " did occur within the PrimFunc \"" << frame.value()->name
75+
<< "\", other frames (e.g. block/if/else/let) had been introduced since the "
76+
<< "PrimFunc's frame";
77+
} else {
78+
LOG(FATAL) << "ValueError: " << method << " must be called at the top of a PrimFunc, "
79+
<< "but " << method << " occurred outside of any T.prim_func() frame";
7280
}
73-
LOG(FATAL) << "ValueError: PrimFunc frame not find. Please ensure '" << method
74-
<< "' is called under T.prim_func()";
7581
throw;
7682
}
7783

@@ -83,9 +89,15 @@ inline PrimFuncFrame FindPrimFuncFrame(const String& method) {
8389
inline BlockFrame FindBlockFrame(const String& method) {
8490
if (Optional<BlockFrame> frame = IRBuilder::Current()->GetLastFrame<BlockFrame>()) {
8591
return frame.value();
92+
} else if (Optional<BlockFrame> frame = IRBuilder::Current()->FindFrame<BlockFrame>()) {
93+
LOG(FATAL) << "ValueError: " << method << " must be called at the top of a T.block(). "
94+
<< "While " << method << " did occur within the block \"" << frame.value()->name
95+
<< "\", other frames (e.g. if/else/let) had been introduced since the T.block(\""
96+
<< frame.value()->name << "\") frame";
97+
} else {
98+
LOG(FATAL) << "ValueError: " << method << " must be called at the top of a T.block(), "
99+
<< "but " << method << " occurred outside of any T.block() frame";
86100
}
87-
LOG(FATAL) << "ValueError: Block frame not find. Please ensure '" << method
88-
<< "' is called under T.block()";
89101
throw;
90102
}
91103

@@ -97,6 +109,12 @@ inline BlockFrame FindBlockFrame(const String& method) {
97109
inline IfFrame FindIfFrame(const String& method) {
98110
if (Optional<IfFrame> frame = IRBuilder::Current()->GetLastFrame<IfFrame>()) {
99111
return frame.value();
112+
} else if (Optional<IfFrame> frame = IRBuilder::Current()->FindFrame<IfFrame>()) {
113+
LOG(FATAL) << "ValueError: " << method << " must be called at the top of a T.if_(). "
114+
<< "While " << method << " did occur within the conditional based on ("
115+
<< frame.value()->condition
116+
<< "), other frames (e.g. if/else/let) had been introduced since the "
117+
<< "IfThenElse frame";
100118
} else {
101119
LOG(FATAL) << "ValueError: IfThenElse frame not find. Please ensure '" << method
102120
<< "' is called under T.if_()";

0 commit comments

Comments
 (0)