Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ LLVM_STATIC_LIBFILES = \
ipo \
passes \
orcjit \
orcdebugging \
$(X86_LLVM_CONFIG_LIB) \
$(ARM_LLVM_CONFIG_LIB) \
$(OPENCL_LLVM_CONFIG_LIB) \
Expand Down Expand Up @@ -641,7 +642,6 @@ SOURCE_FILES = \
CodeGen_C_vectors

HTML_TEMPLATE_FILES = \
StmtToHTML_dependencies.html \
StmtToHTML.js \
StmtToHTML.css

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ We have more documentation in `doc/`, the following links might be helpful:
| --------------------------------------------- | ------------------------------------------------------------------------- |
| [CMake build](doc/BuildingHalideWithCMake.md) | How to configure and build Halide using CMake. |
| [CMake package](doc/HalideCMakePackage.md) | How to use the Halide CMake package to build your code. |
| [Debug symbols](doc/DebugSymbols.md) | How to step through compiled pipelines in a debugger, line by line. |
| [Generator cache](doc/GeneratorCache.md) | How to speed up rebuilds with the opt-in generator compile cache. |
| [Hexagon](doc/Hexagon.md) | How to use the Hexagon backend. |
| [Python](doc/Python.md) | Documentation for the Python bindings. |
Expand Down
2 changes: 1 addition & 1 deletion cmake/FindHalide_LLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ if (Halide_LLVM_FOUND)
if (Halide_LLVM_SHARED_LIBS)
target_link_libraries(Halide_LLVM::Core INTERFACE LLVM ${CMAKE_DL_LIBS})
else ()
_Halide_LLVM_link(Halide_LLVM::Core orcjit bitwriter linker passes)
_Halide_LLVM_link(Halide_LLVM::Core orcjit orcdebugging bitwriter linker passes)
endif ()
endif ()

Expand Down
94 changes: 94 additions & 0 deletions doc/DebugSymbols.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Debug symbols

The `debug_symbols` target feature makes Halide emit debug info for the code it
generates. The "source" that debug info refers to is the `.stmt` output: the
lowered IR exactly as it was handed to code generation. This lets you set
breakpoints on lines of the `.stmt`, step through it, inspect variables, and get
profilers to attribute time to its lines.

## Ahead-of-time compilation

Add the feature to the target, and also request the `stmt` output, so that the
debugger can find the file:

```shell
$ ./my_generator -g my_pipeline -o out -e static_library,h,stmt target=host-debug_symbols
```

or, with CMake:

```cmake
add_halide_library(my_pipeline FROM my_generators
FEATURES debug_symbols
STMT my_pipeline_stmt)
```

Without the `stmt` output, the debug info still refers to the file it would have
been written to, next to the compiled code.

## JIT compilation

With the feature in the JIT target (e.g. `HL_JIT_TARGET=host-debug_symbols`),
Halide writes the `.stmt` of each JIT module to the temp directory, as
`halide-jit-<name>-<hash>.stmt`, and registers the compiled code with the
debugger through the GDB JIT interface. Set `HL_DEBUG_CODEGEN=1` to see which
file a module refers to.

## What you get

- Every instruction is attributed to the line and column of the IR node it was
generated for. Binary operators point at the operator itself, calls and loads
at their name.
- Every function in the `.stmt`, including the closures of parallel loops, is a
function to the debugger.
- `let` variables, loop variables and function arguments are visible as local
variables. Loop variables are scoped to their loop. Buffer arguments
(`*.buffer`) are typed as `halide_buffer_t *`, so their fields and dimensions
can be printed.

Halide's names contain dots, so quote them in GDB:

```
(gdb) break my_pipeline.stmt:42
(gdb) info locals
(gdb) print 'f.s0.x'
(gdb) print *'input.buffer'
(gdb) print 'input.buffer'->dim[1]
```

## Pointing into the conceptual stmt

With the `debug_symbols_conceptual` feature instead, the debug info points into
the conceptual stmt (the `conceptual_stmt` output): the IR before it is lowered
to device code offloading, closures for parallel loops, and the like, which is
usually easier to read. Code made from IR that is not in the conceptual stmt,
e.g. the call that runs a parallel loop, is attributed to the nearest enclosing
IR that is, which can be the start of the function. A target can't have both
features.

## The html outputs

The `stmt_html` and `conceptual_stmt_html` outputs show the IR exactly as the
`stmt` and `conceptual_stmt` outputs print it: the line numbers and columns are
those of these files. The host assembly is compiled with debug info for them
(even without the `debug_symbols` feature, in which case the other outputs get
none), and that debug info links each line of IR to the assembly generated for
it. Hover IR to see its assembly, double-click it to keep that shown and go
there, and double-click it again (or press Escape) to let go of it; the same
goes for assembly and the IR it came from (the toolbar can make going there
scroll smoothly). A line of IR is marked faintly. Within the line, the debug
info says which IR node each instruction was generated for: on an operator, a
keyword or a name, the assembly of that expression and of all that is nested in
it is marked strongly. With rainbow parentheses on, the assembly that is
selected is colored by the node each instruction was generated for, in the
colors of the punctuation of those nodes. The assembly pane shows the line
numbers of the assembly file, without the lines that hold the debug info.

This needs every IR node to be printed in one place only, so lowering copies the
IR nodes that the stmt uses in more than one place.

## Caveats

- The code is still fully optimized, so many variables are `<optimized out>`,
and stepping can jump back and forth between lines.
- DWARF version 4 is emitted, or CodeView on Windows.
2 changes: 2 additions & 0 deletions python_bindings/halide/src/halide_/PyEnums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ void define_enums(py::module_ &m) {
.value("HLSL_SM69", Target::Feature::HLSL_SM69)
.value("NoMetadata", Target::Feature::NoMetadata)
.value("ARM64e", Target::Feature::ARM64e)
.value("DebugSymbols", Target::Feature::DebugSymbols)
.value("DebugSymbolsConceptual", Target::Feature::DebugSymbolsConceptual)
.value("FeatureEnd", Target::Feature::FeatureEnd);

py::enum_<halide_type_code_t>(m, "TypeCode")
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ target_sources(Halide PRIVATE "${Halide_SOURCE_DIR}/tools/Halide.natvis")

set(C_TEMPLATE_FILES CodeGen_C_prologue CodeGen_C_vectors)

set(HTML_TEMPLATE_FILES StmtToHTML_dependencies.html StmtToHTML.js StmtToHTML.css)
set(HTML_TEMPLATE_FILES StmtToHTML.js StmtToHTML.css)

##
# Build and import the runtime.
Expand Down
11 changes: 6 additions & 5 deletions src/CodeGen_ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,9 @@ llvm::Function *CodeGen_ARM::define_intrin_wrapper(const std::string &inner_name
llvm::Function::Create(wrapper_ty, llvm::GlobalValue::InternalLinkage, wrapper_name, module.get());
llvm::BasicBlock *block =
llvm::BasicBlock::Create(module->getContext(), "entry", wrapper);
IRBuilderBase::InsertPoint here = builder->saveIP();
IRBuilderBase::InsertPointGuard insert_point_guard(*builder);
// This code belongs to another function, with no debug info.
builder->SetCurrentDebugLocation(DebugLoc());
builder->SetInsertPoint(block);

if (split_arg0) {
Expand Down Expand Up @@ -993,8 +995,6 @@ llvm::Function *CodeGen_ARM::define_intrin_wrapper(const std::string &inner_name
// Available regardless of SME streaming mode
wrapper->addFnAttr("aarch64_pstate_sm_compatible");

builder->restoreIP(here);

llvm::verifyFunction(*wrapper);
return wrapper;
}
Expand Down Expand Up @@ -2674,7 +2674,9 @@ void CodeGen_ARM::visit(const Call *op) {
fn = llvm::Function::Create(func_t, llvm::Function::InternalLinkage, op->name, module.get());

llvm::BasicBlock *block = llvm::BasicBlock::Create(module->getContext(), "entry", fn);
IRBuilderBase::InsertPoint here = builder->saveIP();
IRBuilderBase::InsertPointGuard insert_point_guard(*builder);
// This code belongs to another function, with no debug info.
builder->SetCurrentDebugLocation(DebugLoc());
builder->SetInsertPoint(block);

// Body
Expand All @@ -2690,7 +2692,6 @@ void CodeGen_ARM::visit(const Call *op) {
fn->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(*context, 1, 16));
fn->addFnAttr(llvm::Attribute::NoInline);
internal_assert(!verifyFunction(*fn, &llvm::errs()));
builder->restoreIP(here);
}
value = builder->CreateCall(fn, {});
return;
Expand Down
34 changes: 17 additions & 17 deletions src/CodeGen_C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "Param.h"
#include "Simplify.h"
#include "StrictifyFloat.h"
#include "Substitute.h"
#include "Type.h"
#include "Util.h"
#include "Var.h"
Expand Down Expand Up @@ -1376,15 +1375,18 @@ void CodeGen_C::close_scope(const std::string &comment) {
}

void CodeGen_C::visit(const Variable *op) {
if (starts_with(op->name, "::")) {
// A variable that a let defines is the id of the value of the let.
const string *bound = bound_ids.find(op->name);
const string &name = bound ? *bound : op->name;
if (starts_with(name, "::")) {
// This is the name of a global, so we can't modify it.
id = op->name;
id = name;
} else {
// This substitution ensures const correctness for all calls
if (op->name == "__user_context") {
if (name == "__user_context") {
id = "_ucon";
} else {
id = print_name(op->name);
id = print_name(name);
}
}
}
Expand Down Expand Up @@ -2136,19 +2138,17 @@ void CodeGen_C::visit(const Store *op) {

void CodeGen_C::visit(const Let *op) {
string id_value = print_expr(op->value);
Expr body = op->body;
if (op->value.type().is_handle() && op->name != "__user_context") {
// The body might contain a Load that references this directly
// by name, so we can't rewrite the name.
std::string name = print_name(op->name);
stream << get_indent() << "auto "
<< name << " = " << id_value << ";\n";
stream << get_indent() << "halide_maybe_unused(" << name << ");\n";
} else {
Expr new_var = Variable::make(op->value.type(), id_value);
body = substitute(op->name, new_var, body);
id_value = op->name;
}
print_expr(body);
ScopedBinding<string> bind(bound_ids, op->name, id_value);
print_expr(op->body);
}

void CodeGen_C::visit(const Select *op) {
Expand Down Expand Up @@ -2230,20 +2230,17 @@ void CodeGen_C::visit(const VectorReduce *op) {

void CodeGen_C::visit(const LetStmt *op) {
string id_value = print_expr(op->value);
Stmt body = op->body;

if (op->value.type().is_handle() && op->name != "__user_context") {
// The body might contain a Load or Store that references this
// directly by name, so we can't rewrite the name.
std::string name = print_name(op->name);
stream << get_indent() << "auto "
<< name << " = " << id_value << ";\n";
stream << get_indent() << "halide_maybe_unused(" << name << ");\n";
} else {
Expr new_var = Variable::make(op->value.type(), id_value);
body = substitute(op->name, new_var, body);
id_value = op->name;
}
body.accept(this);
ScopedBinding<string> bind(bound_ids, op->name, id_value);
op->body.accept(this);
}

// Halide asserts have different semantics to C asserts. They're
Expand Down Expand Up @@ -2350,7 +2347,10 @@ void CodeGen_C::visit(const For *op) {
<< "++)\n";

open_scope();
op->body.accept(this);
{
ScopedBinding<string> bind(bound_ids, op->name, op->name);
op->body.accept(this);
}
close_scope("for " + print_name(op->name));
}

Expand Down
5 changes: 5 additions & 0 deletions src/CodeGen_C.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class CodeGen_C : public IRPrinter {
/** A cache of generated values in scope */
std::map<std::string, std::string> cache;

/** What the Variables with the names that the enclosing Lets, LetStmts
* and For loops define are printed as: the id of the value of a let, or
* else the name itself. */
Scope<std::string> bound_ids;

/** Emit an expression as an assignment, then return the id of the
* resulting var */
std::string print_expr(const Expr &);
Expand Down
6 changes: 3 additions & 3 deletions src/CodeGen_Hexagon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,9 @@ llvm::Function *CodeGen_Hexagon::define_hvx_intrinsic(llvm::Function *intrin,
"halide.hexagon." + name, module.get());
llvm::BasicBlock *block =
llvm::BasicBlock::Create(module->getContext(), "entry", wrapper);
IRBuilderBase::InsertPoint here = builder->saveIP();
IRBuilderBase::InsertPointGuard insert_point_guard(*builder);
// This code belongs to another function, with no debug info.
builder->SetCurrentDebugLocation(DebugLoc());
builder->SetInsertPoint(block);

vector<Value *> args;
Expand Down Expand Up @@ -977,8 +979,6 @@ llvm::Function *CodeGen_Hexagon::define_hvx_intrinsic(llvm::Function *intrin,
// Always inline these wrappers.
wrapper->addFnAttr(llvm::Attribute::AlwaysInline);

builder->restoreIP(here);

llvm::verifyFunction(*wrapper);
return wrapper;
}
Expand Down
Loading
Loading