forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tir] Add line level debug info (apache#13012)
* TIR debug info * Fix location emission * Comments 1/N (docs, cleanups) * Remove leaky macro usage * Add unit test * Remove dead code * Add accuracy test Co-authored-by: driazati <[email protected]>
1 parent
f2bbb7e
commit 391b659
Showing
16 changed files
with
762 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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 tir_text_printer.cc | ||
* \brief Printer to print out the IR text format | ||
* that can be parsed by a parser. | ||
*/ | ||
|
||
#include "tir_text_printer_debug.h" | ||
|
||
#include <optional> | ||
#include <string> | ||
|
||
namespace tvm { | ||
namespace tir { | ||
|
||
std::optional<std::string> span_text(const Span& span) { | ||
if (!span.defined()) { | ||
return std::nullopt; | ||
} | ||
|
||
std::string source("main.tir"); | ||
if (span->source_name.defined() && span->source_name->name.get()) { | ||
source = span->source_name->name; | ||
} | ||
return source + ":" + std::to_string(span->line) + ":" + std::to_string(span->column); | ||
} | ||
|
||
template <typename ObjectPtr> | ||
void add_all_relevant_lines(const std::vector<std::tuple<const ObjectPtr*, size_t>>& data, | ||
size_t current_line, Doc* output) { | ||
ICHECK(output) << "output must be a valid Doc"; | ||
for (const auto& item : data) { | ||
if (std::get<1>(item) != current_line - 1) { | ||
// Item is not relevant for this line, skip it | ||
continue; | ||
} | ||
|
||
// Print out the item's span info if present | ||
auto text = span_text(std::get<0>(item)->span); | ||
if (text.has_value()) { | ||
*output << *text; | ||
} else { | ||
*output << "missing"; | ||
} | ||
*output << ", "; | ||
} | ||
} | ||
|
||
Doc TIRTextPrinterDebug::NewLine() { | ||
current_line_ += 1; | ||
|
||
if (!show_spans_) { | ||
return TIRTextPrinter::NewLine(); | ||
} | ||
|
||
Doc output; | ||
|
||
output << " ["; | ||
|
||
add_all_relevant_lines(exprs_by_line_, current_line_, &output); | ||
add_all_relevant_lines(stmts_by_line_, current_line_, &output); | ||
|
||
output << "]" << TIRTextPrinter::NewLine(); | ||
|
||
return output; | ||
} | ||
|
||
Doc TIRTextPrinterDebug::VisitStmt(const tvm::tir::Stmt& n) { | ||
stmts_by_line_.push_back(std::make_tuple(n.get(), current_line_)); | ||
return TIRTextPrinter::VisitStmt(n); | ||
} | ||
|
||
Doc TIRTextPrinterDebug::VisitExpr(const PrimExpr& e) { | ||
exprs_by_line_.push_back(std::make_tuple(e.get(), current_line_)); | ||
return TIRTextPrinter::VisitExpr(e); | ||
} | ||
|
||
} // namespace tir | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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 text_printer.h | ||
* \brief Printer to print out the unified IR text format | ||
* that can be parsed by a parser. | ||
*/ | ||
|
||
#ifndef TVM_PRINTER_TIR_TEXT_PRINTER_DEBUG_H_ | ||
#define TVM_PRINTER_TIR_TEXT_PRINTER_DEBUG_H_ | ||
|
||
#include <tuple> | ||
#include <vector> | ||
|
||
#include "text_printer.h" | ||
|
||
namespace tvm { | ||
namespace tir { | ||
|
||
class TIRTextPrinterDebug : public TIRTextPrinter { | ||
public: | ||
explicit TIRTextPrinterDebug(bool show_spans) | ||
: TIRTextPrinter(false, &meta_), current_line_(1), show_spans_(show_spans) {} | ||
|
||
std::vector<std::tuple<const PrimExprNode*, size_t>> GetExprsByLine() const { | ||
return exprs_by_line_; | ||
} | ||
|
||
std::vector<std::tuple<const StmtNode*, size_t>> GetStmtsByLine() const { return stmts_by_line_; } | ||
|
||
private: | ||
Doc NewLine() override; | ||
|
||
Doc VisitStmt(const tvm::tir::Stmt& n) override; | ||
Doc VisitExpr(const PrimExpr& e) override; | ||
|
||
TextMetaDataContext meta_; | ||
|
||
// Line that the printer is currently printing | ||
size_t current_line_; | ||
|
||
// Whether to include spans relevant to each line before a newline or not | ||
bool show_spans_; | ||
|
||
// Record of all stmts and exprs and their corresponding line | ||
std::vector<std::tuple<const StmtNode*, size_t>> stmts_by_line_; | ||
std::vector<std::tuple<const PrimExprNode*, size_t>> exprs_by_line_; | ||
}; | ||
|
||
} // namespace tir | ||
} // namespace tvm | ||
|
||
#endif // TVM_PRINTER_TIR_TEXT_PRINTER_DEBUG_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { \ | ||
auto new_expr = StmtExprMutator::VisitExpr_(op); \ | ||
auto new_type = Downcast<TypeName>(new_expr); \ | ||
auto new_node = new_type.CopyOnWrite(); \ | ||
new_node->span = MaybeSpan(op); \ | ||
return new_type; \ | ||
} | ||
TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_SUPPORTED_EXPRS | ||
#undef X | ||
|
||
#define X(TypeName) \ | ||
Stmt DebugInfoInstaller::VisitStmt_(const TypeName##Node* op) { \ | ||
Stmt new_stmt = StmtExprMutator::VisitStmt_(op); \ | ||
auto new_type = Downcast<TypeName>(new_stmt); \ | ||
auto new_node = new_type.CopyOnWrite(); \ | ||
new_node->span = MaybeSpan(op); \ | ||
return new_type; \ | ||
} | ||
TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_SUPPORTED_STMTS | ||
#undef X | ||
|
||
namespace transform { | ||
|
||
Pass InstallDebugSpans() { | ||
auto pass_func = [](PrimFunc f, IRModule m, PassContext ctx) { | ||
ICHECK(m->functions.size() == 1) | ||
<< "Debug info can only be added to IRModules with a single function"; | ||
// There is known to be only 1 function in the module at this point | ||
auto entry = m->functions.begin(); | ||
auto name = std::get<0>(*entry)->name_hint; | ||
auto* n = f.CopyOnWrite(); | ||
|
||
n->body = DebugInfoInstaller::InstallInfo(std::move(name), std::move(f->body)); | ||
|
||
return f; | ||
}; | ||
return CreatePrimFuncPass(pass_func, 0, "tir.InstallDebugSpans", {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("tir.transform.InstallDebugSpans").set_body_typed(InstallDebugSpans); | ||
|
||
} // namespace transform | ||
} // namespace tir | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* 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> | ||
|
||
#ifndef TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_OPS_H_ | ||
#define TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_OPS_H_ | ||
|
||
#define TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_SUPPORTED_EXPRS \ | ||
X(Call) \ | ||
X(Add) \ | ||
X(Sub) \ | ||
X(Mul) \ | ||
X(Div) \ | ||
X(Mod) \ | ||
X(FloorDiv) \ | ||
X(FloorMod) \ | ||
X(Min) \ | ||
X(Max) \ | ||
X(EQ) \ | ||
X(NE) \ | ||
X(LT) \ | ||
X(LE) \ | ||
X(GT) \ | ||
X(GE) \ | ||
X(And) \ | ||
X(Or) \ | ||
X(Reduce) \ | ||
X(Cast) \ | ||
X(Not) \ | ||
X(Select) \ | ||
X(Ramp) \ | ||
X(Broadcast) \ | ||
X(Shuffle) \ | ||
X(IntImm) \ | ||
X(FloatImm) \ | ||
X(StringImm) | ||
|
||
#define TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_SUPPORTED_STMTS \ | ||
X(AttrStmt) \ | ||
X(IfThenElse) \ | ||
X(LetStmt) \ | ||
X(For) \ | ||
X(While) \ | ||
X(Allocate) \ | ||
X(AllocateConst) \ | ||
X(DeclBuffer) \ | ||
X(Store) \ | ||
X(BufferStore) \ | ||
X(BufferRealize) \ | ||
X(AssertStmt) \ | ||
X(ProducerStore) \ | ||
X(ProducerRealize) \ | ||
X(Prefetch) \ | ||
X(SeqStmt) \ | ||
X(Evaluate) \ | ||
X(BlockRealize) | ||
|
||
#endif // TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_OPS_H_ | ||
|
||
namespace tvm { | ||
namespace tir { | ||
|
||
/*! | ||
* \brief This Pass prints out the provided 'stmt' through the TIR debug printer | ||
while recording the statements and expressions printed on each line. Running | ||
this pass uses the per-line information to change the Spans attached to each | ||
statement and expression to the source location in the printed TIR. This pass | ||
also writes to a file called '<name>.tir' so the line information used is | ||
saved to disk. | ||
*/ | ||
class DebugInfoInstaller : public StmtExprMutator { | ||
public: | ||
static Stmt InstallInfo(const std::string& name, const Stmt& stmt); | ||
|
||
PrimExpr VisitExpr(const PrimExpr& expr) override; | ||
Stmt VisitStmt(const Stmt& stmt) override; | ||
|
||
protected: | ||
DebugInfoInstaller(const Stmt& stmt, const std::string& filename); | ||
|
||
#define X(TypeName) PrimExpr VisitExpr_(const TypeName##Node* op) override; | ||
TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_SUPPORTED_EXPRS | ||
#undef X | ||
|
||
#define X(TypeName) Stmt VisitStmt_(const TypeName##Node* op) override; | ||
TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_SUPPORTED_STMTS | ||
#undef X | ||
|
||
private: | ||
std::unordered_map<const StmtNode*, size_t> stmt_lines_; | ||
std::unordered_map<const PrimExprNode*, size_t> expr_lines_; | ||
std::string filename_; | ||
|
||
Span MaybeSpan(const StmtNode* op); | ||
Span MaybeSpan(const PrimExprNode* op); | ||
}; | ||
|
||
} // namespace tir | ||
} // namespace tvm | ||
|
||
#endif // TVM_TIR_TRANSFORMS_INSTALL_DEBUG_SPANS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# 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. | ||
"""Test line-level debug info for TIR""" | ||
import tvm | ||
import tvm.testing | ||
from tvm import tir | ||
from tvm import relay | ||
from tvm.script import tir as T | ||
|
||
from typing import List, Dict | ||
import re | ||
|
||
|
||
def find_di_locations(source: str) -> Dict[int, int]: | ||
""" | ||
Parse out DILocation references in printed LLVM IR | ||
""" | ||
result = {} | ||
|
||
for line in source.splitlines(): | ||
m = re.match(r"!(\d+) = !DILocation\(line: (\d+).*", line) | ||
if m: | ||
debug_id, line = m.groups() | ||
result[debug_id] = line | ||
|
||
return result | ||
|
||
|
||
def _module(): | ||
@tvm.script.ir_module | ||
class MyModule: | ||
@T.prim_func | ||
def main(a: T.handle, b: T.handle): | ||
# We exchange data between function by handles, which are similar to pointer. | ||
T.func_attr({"global_symbol": "main", "tir.noalias": True}) | ||
# Create buffer from handles. | ||
A = T.match_buffer(a, (8,), dtype="float32") | ||
B = T.match_buffer(b, (8,), dtype="float32") | ||
for i in range(8): | ||
# A block is an abstraction for computation. | ||
with T.block("B"): | ||
# Define a spatial block iterator and bind it to value i. | ||
vi = T.axis.spatial(8, i) | ||
assert 1 == 0, "Some numbers" | ||
B[vi] = A[vi] + 1.0 | ||
|
||
return MyModule | ||
|
||
|
||
def test_tir_debug_info(): | ||
""" | ||
Test that Spans are correctly replaced with debug spans that reference | ||
the printed TIR | ||
""" | ||
|
||
def find_span(m): | ||
func = next(m.functions.values()) | ||
return func.body.block.body.span | ||
|
||
module_before = _module() | ||
span_before = find_span(module_before) | ||
assert span_before is None | ||
|
||
module_after = tir.transform.InstallDebugSpans()(module_before) | ||
span_after = find_span(module_after) | ||
|
||
# Check that the module name has been added and a line number is present | ||
assert span_after.source_name.name == "main.tir" | ||
assert span_after.line == 4 | ||
|
||
|
||
def test_llvm_ir_debug_info(): | ||
""" | ||
Check that the right amount of debug locations are present | ||
""" | ||
MyModule = _module() | ||
with tvm.transform.PassContext(opt_level=3, config={"tir.enable_debug": True}): | ||
runtime_module = tvm.build(MyModule, target="llvm") | ||
|
||
source = runtime_module.get_source() | ||
|
||
locations = find_di_locations(source) | ||
assert len(locations) == 34 | ||
|
||
|
||
def test_llvm_ir_debug_accuracy(): | ||
""" | ||
Check that the debug location on an assert is correct | ||
""" | ||
MyModule = _module() | ||
with tvm.transform.PassContext(opt_level=3, config={"tir.enable_debug": True}): | ||
runtime_module = tvm.build(MyModule, target="llvm") | ||
source = runtime_module.get_source() | ||
locations = find_di_locations(source) | ||
|
||
# Find the 'assert' from MyModule | ||
debug_dir_match = re.search( | ||
r"tail call void %0\(i8\* getelementptr inbounds .* !dbg !(\d+)\n", source | ||
) | ||
|
||
# Extract out the debug directive line | ||
directive_idx = debug_dir_match.groups()[0] | ||
|
||
# Check that it matches the expected line number (in main.tir) | ||
debug_line_no = int(locations[directive_idx]) | ||
assert debug_line_no == 42 | ||
|
||
|
||
if __name__ == "__main__": | ||
tvm.testing.main() |