From 0a0fc740fab0533199ab3412e798073da0a5bc55 Mon Sep 17 00:00:00 2001 From: Martijn Courteaux Date: Sun, 22 Nov 2026 14:34:17 +0100 Subject: [PATCH 1/3] Bind let names in a scope in CodeGen_C instead of substituting CodeGen_C printed a Let or LetStmt by substituting a variable named after the id of the value into the body, which rebuilds the body once for every let around it. It now remembers the id in a scope, where the Variable looks it up, so the IR that is printed is the IR that was passed in. A let of a handle and a For loop bind their own name, which hides a let of the same name around them, as the substitution did. The output is the same, except where an expression is simplified while it is printed (e.g. the lowering of lerp): the simplifier orders the operands of commutative operators by the names of variables, which are no longer the ids. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011FRemhS639vq75urwV44oe --- src/CodeGen_C.cpp | 34 +++++++++++++++++----------------- src/CodeGen_C.h | 5 +++++ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/CodeGen_C.cpp b/src/CodeGen_C.cpp index 3e201d82d63a..ba79bb68c2b3 100644 --- a/src/CodeGen_C.cpp +++ b/src/CodeGen_C.cpp @@ -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" @@ -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); } } } @@ -2136,7 +2138,6 @@ 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. @@ -2144,11 +2145,10 @@ void CodeGen_C::visit(const Let *op) { 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 bind(bound_ids, op->name, id_value); + print_expr(op->body); } void CodeGen_C::visit(const Select *op) { @@ -2230,8 +2230,6 @@ 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. @@ -2239,11 +2237,10 @@ void CodeGen_C::visit(const LetStmt *op) { 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 bind(bound_ids, op->name, id_value); + op->body.accept(this); } // Halide asserts have different semantics to C asserts. They're @@ -2350,7 +2347,10 @@ void CodeGen_C::visit(const For *op) { << "++)\n"; open_scope(); - op->body.accept(this); + { + ScopedBinding bind(bound_ids, op->name, op->name); + op->body.accept(this); + } close_scope("for " + print_name(op->name)); } diff --git a/src/CodeGen_C.h b/src/CodeGen_C.h index 7947a5332339..3d84588045d6 100644 --- a/src/CodeGen_C.h +++ b/src/CodeGen_C.h @@ -102,6 +102,11 @@ class CodeGen_C : public IRPrinter { /** A cache of generated values in scope */ std::map 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 bound_ids; + /** Emit an expression as an assignment, then return the id of the * resulting var */ std::string print_expr(const Expr &); From e6e8fe2f1cbea1b5b2abb7f69a13297497fac3cb Mon Sep 17 00:00:00 2001 From: Martijn Courteaux Date: Sun, 22 Nov 2026 13:21:32 +0100 Subject: [PATCH 2/3] Print the stmt html with IRPrinter The html output is written by HTMLStmtPrinter, a subclass of IRPrinter, through new hooks: begin_style()/end_style() for styled text, and begin_node()/end_node() around each Expr and Stmt. It shows the IR exactly as the stmt output prints it, which replaces the separate html printer. IRPrinter is simplified along the way: the colors of the styles come from a table, Modules and LoweredFuncs are printed by member functions that operator<< uses, the binary operators share a helper, and chained ifs are printed recursively. Colored output ends some styles by resetting only the color where it reset all attributes; plain output is unchanged. The variables of the loops over GPU threads (.thread_id_x, .thread_id_y and .thread_id_z) are red, green and blue, in colored output and in the html. The assembly is linked to the loops and produce/consume blocks by the names of their basic blocks. It is written as plain text into a diff --git a/test/correctness/stmt_to_html.cpp b/test/correctness/stmt_to_html.cpp index dae0cdc95527..9c017c65bc04 100644 --- a/test/correctness/stmt_to_html.cpp +++ b/test/correctness/stmt_to_html.cpp @@ -1,10 +1,137 @@ #include "Halide.h" #include "halide_test_dirs.h" +#include #include +#include +#include using namespace Halide; +namespace { + +std::string read_file(const std::string &path) { + std::ifstream in(path); + std::stringstream ss; + ss << in.rdbuf(); + return ss.str(); +} + +std::vector split_lines(const std::string &text) { + std::vector lines; + std::istringstream in(text); + std::string line; + while (std::getline(in, line)) { + lines.push_back(line); + } + return lines; +} + +// The text of each line of IR in an html output, in order of their line +// numbers, which must be consecutive from 1. +bool html_lines(const std::string &html, std::vector &lines) { + // A line may have more classes than 'l', e.g. when it is shaded. + const std::string line_open = "
0) { + if (html.compare(c, 5, "', c) + 1; + } else if (html.compare(c, 7, "") == 0) { + depth--; + c += 7; + } else if (html.compare(c, 5, "&") == 0) { + text += '&'; + c += 5; + } else if (html.compare(c, 4, "<") == 0) { + text += '<'; + c += 4; + } else if (html.compare(c, 4, ">") == 0) { + text += '>'; + c += 4; + } else { + text += html[c++]; + } + } + lines.push_back(text); + } + return true; +} + +bool check_html(const std::string &html_path, const std::string &stmt_path) { + const std::string html = read_file(html_path); + const std::vector stmt_lines = split_lines(read_file(stmt_path)); + std::vector lines; + if (!html_lines(html, lines)) { + return false; + } + if (lines != stmt_lines) { + for (size_t i = 0; i < std::max(lines.size(), stmt_lines.size()); i++) { + std::string a = i < lines.size() ? lines[i] : "(none)"; + std::string b = i < stmt_lines.size() ? stmt_lines[i] : "(none)"; + if (a != b) { + printf("%s line %d is\n %s\ninstead of\n %s\n", html_path.c_str(), (int)i + 1, a.c_str(), b.c_str()); + break; + } + } + return false; + } + // Some loops are linked to their assembly. + const std::string asm_lines = "const asmIRLines = ["; + size_t at = html.find(asm_lines); + if (html.find("data-asm='1'") == std::string::npos || at == std::string::npos) { + printf("%s links no IR to assembly\n", html_path.c_str()); + return false; + } + // ... and the assembly is linked to lines that exist. + std::istringstream in(html.substr(at + asm_lines.size(), html.find(']', at) - at - asm_lines.size())); + int linked = 0, mapped = 0; + for (std::string n; std::getline(in, n, ',');) { + mapped++; + int line = std::atoi(n.c_str()); + if (line < 0 || line > (int)lines.size()) { + printf("%s links assembly to line %d of %d\n", html_path.c_str(), line, (int)lines.size()); + return false; + } + linked += line > 0; + } + if (linked == 0) { + printf("%s links no assembly to IR\n", html_path.c_str()); + return false; + } + // The assembly is there as text, with a link for each of its lines. + const std::string asm_open = "