Revision e038765d17109d6f1e4226e88809a123aa3a4a07 authored by Steven Johnson on 18 April 2022, 18:20:51 UTC, committed by Steven Johnson on 18 April 2022, 18:20:51 UTC
Technically, we should check the return code of sprintf() for failure conditions. This is a minimal fix designed to quietly ensure that encoding errors aren't overlooked. (Since our "C" output actually requires C++11 at this point, I was tempted to replace it with `ostringstream`, but decided to avoid bringing that in...)
1 parent 60a909f
lesson_03_debugging_1.cpp
// Halide tutorial lesson 3: Inspecting the generated code
// This lesson demonstrates how to inspect what the Halide compiler is producing.
// On linux, you can compile and run it like so:
// g++ lesson_03*.cpp -g -I <path/to/Halide.h> -L <path/to/libHalide.so> -lHalide -lpthread -ldl -o lesson_03 -std=c++17
// LD_LIBRARY_PATH=<path/to/libHalide.so> ./lesson_03
// On os x:
// g++ lesson_03*.cpp -g -I <path/to/Halide.h> -L <path/to/libHalide.so> -lHalide -o lesson_03 -std=c++17
// DYLD_LIBRARY_PATH=<path/to/libHalide.dylib> ./lesson_03
// If you have the entire Halide source tree, you can also build it by
// running:
// make tutorial_lesson_03_debugging_1
// in a shell with the current directory at the top of the halide
// source tree.
#include "Halide.h"
#include <stdio.h>
// This time we'll just import the entire Halide namespace
using namespace Halide;
int main(int argc, char **argv) {
// We'll start by defining the simple single-stage imaging
// pipeline from lesson 1.
// This lesson will be about debugging, but unfortunately in C++,
// objects don't know their own names, which makes it hard for us
// to understand the generated code. To get around this, you can
// pass a string to the Func and Var constructors to give them a
// name for debugging purposes.
Func gradient("gradient");
Var x("x"), y("y");
gradient(x, y) = x + y;
// Realize the function to produce an output image. We'll keep it
// very small for this lesson.
Buffer<int> output = gradient.realize({8, 8});
// That line compiled and ran the pipeline. Try running this
// lesson with the environment variable HL_DEBUG_CODEGEN set to
// 1. It will print out the various stages of compilation, and a
// pseudocode representation of the final pipeline.
// If you set HL_DEBUG_CODEGEN to a higher number, you can see
// more and more details of how Halide compiles your pipeline.
// Setting HL_DEBUG_CODEGEN=2 shows the Halide code at each stage
// of compilation, and also the llvm bitcode we generate at the
// end.
// Halide will also output an HTML version of this output, which
// supports syntax highlighting and code-folding, so it can be
// nicer to read for large pipelines. Open gradient.html with your
// browser after running this tutorial.
gradient.compile_to_lowered_stmt("gradient.html", {}, HTML);
// You can usually figure out what code Halide is generating using
// this pseudocode. In the next lesson we'll see how to snoop on
// Halide at runtime.
printf("Success!\n");
return 0;
}
Computing file changes ...