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
7 changes: 7 additions & 0 deletions doc/DebugSymbols.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ 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.

The `conceptual_stmt_html` output also links the device code of GPU kernels to
the conceptual stmt it was made from, for CUDA (PTX) and for the APIs whose
device code is C-like source (OpenCL, Metal, D3D12, WebGPU), in the same way as
the assembly: by line of IR, and within a line by IR node. The device code
itself is not changed for this, and has no line information in it. Device code
that isn't text to read, like SPIR-V, is not linked.

## Caveats

- The code is still fully optimized, so many variables are `<optimized out>`,
Expand Down
36 changes: 28 additions & 8 deletions src/CodeGen_C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,18 @@ void CodeGen_C::compile(const Buffer<> &buffer) {

string CodeGen_C::print_expr(const Expr &e) {
id = "$$ BAD ID $$";
// An Expr that was rewritten since line_source was printed is not noted:
// what is printed for it goes to what it is printed within.
const StmtSourceMap::Location *l = line_source ? line_source->find(e.get(), 0) : nullptr;
if (!l) {
e.accept(this);
return id;
}
const size_t i = printed_nodes.size();
const uint32_t start = (uint32_t)stream.tellp();
printed_nodes.push_back({start, start, IRPosition{l->line, l->order}});
e.accept(this);
printed_nodes[i].end = (uint32_t)stream.tellp();
return id;
}

Expand All @@ -1334,7 +1345,16 @@ string CodeGen_C::print_cast_expr(const Type &t, const Expr &e) {
}

void CodeGen_C::print_stmt(const Stmt &s) {
if (!line_source) {
s.accept(this);
return;
}
const StmtSourceMap::Location *l = line_source->find_within(s.get());
const size_t i = printed_nodes.size();
const uint32_t start = (uint32_t)stream.tellp();
printed_nodes.push_back({start, start, l ? IRPosition{l->line, l->order} : IRPosition()});
s.accept(this);
printed_nodes[i].end = (uint32_t)stream.tellp();
}

string CodeGen_C::print_assignment(Type t, const std::string &rhs) {
Expand Down Expand Up @@ -2240,7 +2260,7 @@ void CodeGen_C::visit(const LetStmt *op) {
id_value = op->name;
}
ScopedBinding<string> bind(bound_ids, op->name, id_value);
op->body.accept(this);
print_stmt(op->body);
}

// Halide asserts have different semantics to C asserts. They're
Expand Down Expand Up @@ -2308,7 +2328,7 @@ void CodeGen_C::visit(const Acquire *op) {
open_scope();
stream << get_indent() << "#pragma omp taskyield\n";
close_scope("");
op->body.accept(this);
print_stmt(op->body);
close_scope("");
}

Expand All @@ -2317,11 +2337,11 @@ void CodeGen_C::visit(const Atomic *op) {
internal_assert(!inside_atomic_mutex_node)
<< "Nested atomic mutex locks detected. This might causes a deadlock.\n";
ScopedValue<bool> old_inside_atomic_mutex_node(inside_atomic_mutex_node, true);
op->body.accept(this);
print_stmt(op->body);
} else {
// Issue atomic stores.
ScopedValue<bool> old_emit_atomic_stores(emit_atomic_stores, true);
op->body.accept(this);
print_stmt(op->body);
}
}

Expand Down Expand Up @@ -2349,7 +2369,7 @@ void CodeGen_C::visit(const For *op) {
open_scope();
{
ScopedBinding<string> bind(bound_ids, op->name, op->name);
op->body.accept(this);
print_stmt(op->body);
}
close_scope("for " + print_name(op->name));
}
Expand Down Expand Up @@ -2517,7 +2537,7 @@ void CodeGen_C::visit(const Allocate *op) {
emit_halide_free_helper(op_name, free_function);
}

op->body.accept(this);
print_stmt(op->body);

// Free the memory if it was allocated on the heap and there is no matching
// Free node.
Expand Down Expand Up @@ -2554,13 +2574,13 @@ void CodeGen_C::visit(const IfThenElse *op) {

stream << get_indent() << "if (" << cond_id << ")\n";
open_scope();
op->then_case.accept(this);
print_stmt(op->then_case);
close_scope("if " + cond_id);

if (op->else_case.defined()) {
stream << get_indent() << "else\n";
open_scope();
op->else_case.accept(this);
print_stmt(op->else_case);
close_scope("if " + cond_id + " else");
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/CodeGen_C.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ class CodeGen_C : public IRPrinter {
CPlusPlusFunctionInfoHeader,
};

/** If set, print_stmt() and print_expr() note in printed_nodes where
* they print each Stmt and Expr, and where that is in this printing of
* the IR, or of IR that it was lowered from. The stream must support
* tellp(). */
const StmtSourceMap *line_source = nullptr;

/** Where a Stmt or an Expr was printed, as byte offsets into the stream,
* and where it is in line_source. They are in the order they began
* printing. */
struct PrintedNode {
uint32_t start, end;
IRPosition position;
};
std::vector<PrintedNode> printed_nodes;

/** Initialize a C code generator pointing at a particular output
* stream (e.g. a file, or std::cout) */
CodeGen_C(std::ostream &dest,
Expand Down
11 changes: 7 additions & 4 deletions src/CodeGen_D3D12Compute_Dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const For *loop) {
stream << get_indent() << print_type(Int(32)) << " " << print_name(loop->name)
<< " = " << simt_intrinsic(loop->name) << ";\n";

loop->body.accept(this);
print_stmt(loop->body);
}

void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Ramp *op) {
Expand Down Expand Up @@ -1437,7 +1437,7 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Allocate *op)
// Already handled
internal_assert(!groupshared_allocations.contains(op->name));
groupshared_allocations.push(op->name);
op->body.accept(this);
print_stmt(op->body);
} else {
open_scope();

Expand Down Expand Up @@ -1467,7 +1467,7 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Allocate *op)
alloc.type = op->type;
allocations.push(op->name, alloc);

op->body.accept(this);
print_stmt(op->body);

// Should have been freed internally
internal_assert(!allocations.contains(op->name));
Expand Down Expand Up @@ -2141,7 +2141,7 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::add_kernel(Stmt s,
// Emit the kernel code:

open_scope();
print(s);
print_stmt(s);
close_scope("kernel " + name);

for (const auto &arg : args) {
Expand All @@ -2161,6 +2161,8 @@ void CodeGen_D3D12Compute_Dev::init_module() {
// wipe the internal kernel source
src_stream.str("");
src_stream.clear();
d3d12compute_c.line_source = line_source;
d3d12compute_c.printed_nodes.clear();

// Emit SM version marker for runtime to select FXC (SM 5.1) vs DXC (SM 6.x).
// Emitted as a real #define (not a comment) so generated shader code can
Expand Down Expand Up @@ -2270,6 +2272,7 @@ void CodeGen_D3D12Compute_Dev::init_module() {

vector<char> CodeGen_D3D12Compute_Dev::compile_to_src() {
string str = src_stream.str();
source_lines = line_source ? lines_of_printed_nodes(str, d3d12compute_c.printed_nodes) : std::vector<IRPosition>();
debug(1) << "D3D12Compute kernel:\n"
<< str << "\n";
vector<char> buffer(str.begin(), str.end());
Expand Down
26 changes: 26 additions & 0 deletions src/CodeGen_GPU_Dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "IRMutator.h"
#include "IROperator.h"
#include "IRVisitor.h"
#include <algorithm>

namespace Halide {
namespace Internal {
Expand Down Expand Up @@ -278,5 +279,30 @@ std::string CodeGen_GPU_C::print_extern_call(const Call *op) {
return rhs.str();
}

std::vector<IRPosition> lines_of_printed_nodes(const std::string &text, const std::vector<CodeGen_C::PrintedNode> &nodes) {
std::vector<uint32_t> line_starts{0};
for (uint32_t i = 0; i < (uint32_t)text.size(); i++) {
if (text[i] == '\n') {
line_starts.push_back(i + 1);
}
}
// The nodes are in the order they began printing, so those within a node
// come after it, and overwrite its lines. What is left to a node is what
// was printed for it alone, like the line that assigns the value of an
// Expr after the lines of its operands.
std::vector<IRPosition> result(line_starts.size());
for (const CodeGen_C::PrintedNode &n : nodes) {
if (n.position.line == 0 || n.end <= n.start) {
continue;
}
const size_t first = std::upper_bound(line_starts.begin(), line_starts.end(), n.start) - line_starts.begin() - 1;
const size_t last = std::upper_bound(line_starts.begin(), line_starts.end(), n.end - 1) - line_starts.begin() - 1;
for (size_t i = first; i <= last; i++) {
result[i] = n.position;
}
}
return result;
}

} // namespace Internal
} // namespace Halide
15 changes: 15 additions & 0 deletions src/CodeGen_GPU_Dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@
namespace Halide {
namespace Internal {

struct StmtSourceMap;

/** For each line of text, C-like source that a CodeGen_C printed: where the
* innermost Stmt or Expr that it was printed for is (see
* CodeGen_C::line_source), of those that it is known of. */
std::vector<IRPosition> lines_of_printed_nodes(const std::string &text, const std::vector<CodeGen_C::PrintedNode> &nodes);

/** A code generator that emits GPU code from a given Halide stmt. */
struct CodeGen_GPU_Dev {
virtual ~CodeGen_GPU_Dev();

/** If set before init_module(): a printing of IR that holds the Stmts of
* the kernels, or that they were lowered from. compile_to_src() then says
* in source_lines where in it each line of the source it returns came
* from. That stays empty for code generators that don't keep track, or
* whose output isn't text. */
const StmtSourceMap *line_source = nullptr;
std::vector<IRPosition> source_lines;

/** Compile a GPU kernel into the module. This may be called many times
* with different kernels, which will all be accumulated into a single
* source module shared by a given Halide pipeline. */
Expand Down
Loading
Loading