Skip to content

Commit

Permalink
[TVMScript] Improved error message for unexpected top frame (#14399)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Lunderberg authored Mar 28, 2023
1 parent 14ddb37 commit b3a5e18
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/script/ir_builder/tir/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ inline tvm::tir::Stmt AsStmt(const Array<tvm::tir::Stmt>& stmt) {
inline PrimFuncFrame FindPrimFuncFrame(const String& method) {
if (Optional<PrimFuncFrame> frame = IRBuilder::Current()->GetLastFrame<PrimFuncFrame>()) {
return frame.value();
} else if (Optional<PrimFuncFrame> frame = IRBuilder::Current()->FindFrame<PrimFuncFrame>()) {
LOG(FATAL) << "ValueError: " << method << " must be called at the top of a PrimFunc. "
<< "While " << method << " did occur within the PrimFunc \"" << frame.value()->name
<< "\", other frames (e.g. block/if/else/let) had been introduced since the "
<< "PrimFunc's frame";
} else {
LOG(FATAL) << "ValueError: " << method << " must be called at the top of a PrimFunc, "
<< "but " << method << " occurred outside of any T.prim_func() frame";
}
LOG(FATAL) << "ValueError: PrimFunc frame not find. Please ensure '" << method
<< "' is called under T.prim_func()";
throw;
}

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

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

0 comments on commit b3a5e18

Please sign in to comment.