You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/11/18 19:21:43 UTC

[GitHub] [tvm] areusch commented on a diff in pull request #13012: [tir] Add line level debug info

areusch commented on code in PR #13012:
URL: https://github.com/apache/tvm/pull/13012#discussion_r1025724409


##########
include/tvm/ir/expr.h:
##########
@@ -526,6 +526,7 @@ class IntImm : public PrimExpr {
   TVM_DLL IntImm(DataType dtype, int64_t value, Span span = Span());
 
   TVM_DEFINE_OBJECT_REF_METHODS(IntImm, PrimExpr, IntImmNode);
+  TVM_DEFINE_OBJECT_REF_COW_METHOD(IntImmNode);

Review Comment:
   just wondering why you needed this one?



##########
src/printer/tir_text_printer.cc:
##########
@@ -149,26 +149,28 @@ Doc TIRTextPrinter::PrintPrimFunc(const PrimFunc& prim_func) {
       buffer_map_doc.push_back(Print(v) << ": " << Print(buf));
     }
     doc << Doc::Indent(
-        2, Doc::NewLine() << "buffer_map = {" << PrintSep(buffer_map_doc, Doc::Text(", ")) << "}");
+        2, NewLine() << "buffer_map = {" << PrintSep(buffer_map_doc, Doc::Text(", ")) << "}");

Review Comment:
   i think we should either have Doc:: or not everywhere, thoughts?



##########
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:
   possible to leverage default dispatch or something here rather than a macro?



##########
src/tir/transforms/install_debug_spans.h:
##########
@@ -0,0 +1,80 @@
+/*

Review Comment:
   can you add a unit test for this?



##########
src/target/llvm/codegen_cpu.cc:
##########
@@ -183,57 +183,62 @@ void CodeGenCPU::Init(const std::string& module_name, LLVMTarget* llvm_target, b
   InitGlobalContext(dynamic_lookup);
 }
 
-void CodeGenCPU::AddFunction(const PrimFunc& f) {
-  CodeGenLLVM::AddFunction(f);
-  if (f_tvm_register_system_symbol_ != nullptr) {
-    auto global_symbol = f->GetAttr<String>(tvm::attr::kGlobalSymbol);
-    ICHECK(global_symbol.defined())
-        << "CodeGenLLVM: Expect PrimFunc to have the global_symbol attribute";
-    export_system_symbols_.emplace_back(
-        std::make_pair(global_symbol.value().operator std::string(), function_));
-  }
-  AddDebugInformation(f, function_);
-}
-
-// Following Glow |DebugInfo::generateFunctionDebugInfo|, https://git.io/fjadv
-void CodeGenCPU::AddDebugInformation(PrimFunc f_tir, llvm::Function* f_llvm) {
+llvm::DISubprogram* CodeGenCPU::CreateDebugFunction(const PrimFunc& f) {
 #if TVM_LLVM_VERSION >= 50
-  ICHECK(!f_llvm->getSubprogram());
   llvm::SmallVector<llvm::Metadata*, 4> paramTys;
-  // Functions in TIR can only return void or an int.
-  ICHECK(f_llvm->getReturnType() == t_void_ || f_llvm->getReturnType() == t_int_)
-      << "Unexpected return type";
-  auto ret_type_tir = f_llvm->getReturnType() == t_int_ ? DataType::Int(32) : DataType::Void();
-  llvm::DIType* returnTy =
-      GetDebugType(GetTypeFromRuntimeDataType(ret_type_tir), f_llvm->getReturnType());
-  paramTys.push_back(returnTy);
-  for (size_t i = 0; i < f_llvm->arg_size(); ++i) {
-    paramTys.push_back(
-        GetDebugType(GetType(f_tir->params[i]), f_llvm->getFunctionType()->getParamType(i)));
+
+  paramTys.push_back(GetDebugType(f->ret_type));
+  for (const auto& param : f->params) {
+    paramTys.push_back(GetDebugType(GetType(param)));
   }
+
   auto* DIFunctionTy = dbg_info_->di_builder_->createSubroutineType(
       dbg_info_->di_builder_->getOrCreateTypeArray(paramTys));
 
-  bool local_to_unit = llvm::GlobalValue::isLocalLinkage(f_llvm->getLinkage());
+  bool local_to_unit = llvm::GlobalVariable::isLocalLinkage(llvm::GlobalValue::InternalLinkage);
 
 #if TVM_LLVM_VERSION >= 80
-  auto SPFlags =
-      llvm::DISubprogram::toSPFlags(local_to_unit, /*IsDefinition=*/true, /*IsOptimized=*/true);
+  auto SPFlags = llvm::DISubprogram::toSPFlags(local_to_unit, /*IsDefinition=*/true,
+                                               /*IsOptimized=*/true);
   auto* DIFunction = dbg_info_->di_builder_->createFunction(
-      /*Scope=*/dbg_info_->file_, /*Name=*/f_llvm->getName(), /*LinkageName=*/"",
+      /*Scope=*/dbg_info_->file_, /*Name=*/"main.tir", /*LinkageName=*/"",
       /*File=*/dbg_info_->file_, /*LineNo=*/0, /*Ty=*/DIFunctionTy,
       /*ScopeLine=*/0, /*Flags=*/llvm::DINode::FlagZero, /*SPFlags=*/SPFlags);
 #else
   auto* DIFunction = dbg_info_->di_builder_->createFunction(
-      /*Scope=*/dbg_info_->file_, /*Name=*/f_llvm->getName(), /*LinkageName=*/"",
+      /*Scope=*/dbg_info_->file_, /*Name=*/"main.tir", /*LinkageName=*/"",

Review Comment:
   can you add a TODO to derive the name from a per-IRModule name?



##########
src/tir/transforms/install_debug_spans.h:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.h
+ * \brief Interface of the InstallDebugSpans pass
+ */
+
+#ifndef TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_H_
+#define TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_H_
+
+#include <tvm/tir/expr.h>
+#include <tvm/tir/expr_functor.h>
+#include <tvm/tir/stmt.h>
+#include <tvm/tir/stmt_functor.h>
+
+#include <string>
+#include <unordered_map>
+
+#include "install_debug_spans_ops.h"
+
+namespace tvm {
+namespace tir {
+
+class DebugInfoInstaller : public StmtExprMutator {

Review Comment:
   can you add some docstrings?



-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org