driazati commented on code in PR #13012: URL: https://github.com/apache/tvm/pull/13012#discussion_r1027610974
########## src/tir/transforms/install_debug_spans.cc: ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file install_debug_spans.cc + * \brief Prints TIR code in memory and replaces all spans in the module with + the location to which the ops would be printed + */ + +#include "install_debug_spans.h" + +#include <tvm/tir/transform.h> + +#include <string> +#include <utility> + +#include "../../printer/tir_text_printer_debug.h" + +namespace tvm { +namespace tir { + +Stmt DebugInfoInstaller::InstallInfo(const std::string& name, const Stmt& stmt) { + DebugInfoInstaller installer(stmt, name + ".tir"); + return installer.VisitStmt(stmt); +} + +DebugInfoInstaller::DebugInfoInstaller(const Stmt& stmt, const std::string& filename) { + // Determine the line that each stmt/expr will be printed on + tvm::tir::TIRTextPrinterDebug printer(false); + + // Fill in the stmts and exprs' line info + auto result = printer.Print(stmt).str(); + + // Create map of the stmt/expr -> its line number in the output to later + // create new spans for each stmt/expr + const auto& stmts = printer.GetStmtsByLine(); + VLOG(0) << "Debug printer found " << stmts.size() << " stmts after printing"; + for (const auto& line : stmts) { + stmt_lines_[std::get<0>(line)] = std::get<1>(line); + } + + const auto& exprs = printer.GetExprsByLine(); + VLOG(0) << "Debug printer found " << exprs.size() << " exprs after printing"; + for (const auto& line : exprs) { + expr_lines_[std::get<0>(line)] = std::get<1>(line); + } + + // Output the printed TIR to the specified file + VLOG(0) << "Outputting TIR to " << filename; + filename_ = std::move(filename); + std::ofstream out(filename_); + out << result; + out.close(); +} + +PrimExpr DebugInfoInstaller::VisitExpr(const PrimExpr& expr) { + PrimExpr result = expr; + result = StmtExprMutator::VisitExpr(result); + return result; +} + +Stmt DebugInfoInstaller::VisitStmt(const Stmt& stmt) { + Stmt result = stmt; + result = StmtExprMutator::VisitStmt(result); + return result; +} + +Span DebugInfoInstaller::MaybeSpan(const StmtNode* op) { + auto entry = stmt_lines_.find(op); + if (entry == stmt_lines_.end()) { + return Span(); + } else { + size_t column = 0; + size_t line = entry->second; + return Span(SourceName::Get(filename_), line, line, column, column); + } +} + +Span DebugInfoInstaller::MaybeSpan(const PrimExprNode* op) { + auto entry = expr_lines_.find(op); + if (entry == expr_lines_.end()) { + return Span(); + } else { + size_t column = 0; + size_t line = entry->second; + return Span(SourceName::Get(filename_), line, line, column, column); + } +} + +#define X(TypeName) \ + PrimExpr DebugInfoInstaller::VisitExpr_(const TypeName##Node* op) { \ Review Comment: For the text printer it looks like yes since that does the same thing for all types but I don't know about here since the function needs to know what type to `Downcast` to -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
