The goal of this RFC is to codify how we would like to write error messages and when we want to use them. The results of this discussion will be included into https://tvm.apache.org/docs/contribute/error_handling.html. Below I’ve included what I think the document should look like. Please give feedback on the content, the wording can be improved when committing.
My Proposal
ICHECK vs CHECK
TVM’s C++ codebase contains two different types of assert statements: ICHECK and CHECK. ICHECK should be used in places where an internal compiler invariant was violated. ICHECK will print a stack trace along with an error message asking users to report this internal compiler error to the TVM github or discuss. CHECK is used in places where the error is the users fault. For example, when the input shapes were incorrect or when data type do not match. These errors are displayed in a friendly manner to the user.
Writing good error messages
ICHECK and CHECK must always contain a useful error message. Explicit error messages make it easier for both developers and users to understand what went wrong. This error message should be actionable. Whoever encounters an error should be able to take steps to fix the error based off of the error message. For example, if a function requires an integer parameter, instead of saying “unexpected float” when a floating point value is passed, try saying “MyFunc requires an integer argument, but a floating point one was provided”. Error messages should contain all useful context that is available. For example, “Expected float, got int”, say “MyFunc expects an int argument, but a float was provided” (the new error message now include the function name). If working with TIR/TE/Relay nodes, you should include the span information from these nodes in the error message to help the user locate the error in their code.
Here are my reasons for requiring messages on all assert statements:
- Error messages serve as both internal (for developers) and external (for users) documentation. Error messages are like an improved comment.
- The current codebase lacks error messages in a lot of places. Requiring them on all incoming commits helps build a culture of documenting the code base.
- Adding an error message requires little time commitment vs the amount of benefit it produces.
Please give feedback on what other guidance we should add for writing error messages.