[FRONTEND] Emit warning if the result of tl.advance is unused (#2155)

https://github.com/openai/triton/issues/2138
This commit is contained in:
Keren Zhou
2023-08-22 21:02:00 -04:00
committed by GitHub
parent c4a9006340
commit 5fa1fa1b27

View File

@@ -54,6 +54,7 @@
#include <fstream>
#include <optional>
#include <pybind11/buffer_info.h>
#include <pybind11/embed.h>
#include <pybind11/functional.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
@@ -172,6 +173,30 @@ private:
bool lineInfoEnabled = !triton::tools::getBoolEnv("TRITON_DISABLE_LINE_INFO");
};
static std::string locationToString(mlir::Location loc) {
std::string str;
llvm::raw_string_ostream os(str);
loc.print(os);
os.flush(); // Make sure all the content is dumped into the 'str' string
return str;
}
static void outputWarning(mlir::Location loc, const std::string &msg) {
std::string locStr = locationToString(loc);
py::exec(
R"(
import warnings
def custom_showwarning(message, category, filename, lineno, file=None, line=None):
print(f"UserWarning: {message}")
warnings.showwarning = custom_showwarning
warnings.warn(f"{loc}: {msg}")
)",
py::globals(), py::dict(py::arg("loc") = locStr, py::arg("msg") = msg));
}
/*****************************************************************************/
/* Python bindings for triton::ir */
/*****************************************************************************/
@@ -596,6 +621,17 @@ void init_triton_ir(py::module &&m) {
newBlock->erase();
}
});
// 2. Check if the result of tl.advance is used
self.walk([&](mlir::Operation *op) {
if (mlir::isa<mlir::triton::AdvanceOp>(op) &&
op->getResult(0).use_empty())
outputWarning(op->getLoc(), "The result of tl.advance is not "
"being used. Note that tl.advance "
"does not have any side effects. "
"To move the block pointer, you "
"need to assign the result of "
"tl.advance to a variable.");
});
})
.def_property_readonly("type", &mlir::triton::FuncOp::getFunctionType)
.def("reset_type", &mlir::triton::FuncOp::setType);