Halide学习笔记----Halide tutorial源码阅读3

Halide入门教程03


// Halide tutorial lesson 3: Inspecting the generated code
// Halide入门第三课:检测生成代码

// This lesson demonstrates how to inspect what the Halide compiler is producing.
// 本课揭示了怎样查看Halide编译器做了些什么

// On linux, you can compile and run it like so:
// linux操作系统,按如下操作编译和运行
// g++ lesson_03*.cpp -g -I ../include -L ../bin -lHalide -lpthread -ldl -o lesson_03 -std=c++11
// LD_LIBRARY_PATH=../bin ./lesson_03

// On os x:
// g++ lesson_03*.cpp -g -I ../include -L ../bin -lHalide -o lesson_03 -std=c++11
// DYLD_LIBRARY_PATH=../bin ./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.
    // 本课主要陈述调试(debugging),但c++中的对象并没有自己的名字标签,这就给理解生成代码增加了困难
    // 为了克服这个问题,你可以给Func和Var的构造函数传入一个string类型的名字,从而达到方便调试的目的
    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.
    // 设置环境变量HL_DEBUG_CODEGEN=1,此时运行程序会打印出编译的不同阶段和最终pipeline的伪代码
    // export HL_DEBUG_CODEGEN=1

    // 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.
    // 设置HL_DEBUG_CODEGEN=2,此时会输出Halide编译的各个不同阶段,而且会输出llvm最终生成的字节码
    // export HL_DEBUG_CODEGEN=2

    // 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.
    // Halide也提供HTML形式的伪代码输出,支持语法高亮,代码折叠,翻遍大规模复杂pipeline的阅读
    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;
}

在终端中编译并运行:

$ g++ lesson_03*.cpp -g -I ../include -L ../bin -lHalide -lpthread -ldl -o lesson_03 -std=c++11
$ export HL_DEBUG_CODEGEN=1
$ ./lesson_03

终端输出结果:

Inferred argument: (void *) __user_context
Creating initial loop nests...
Injecting realization of gradient
Canonicalizing GPU var names...
Skipping injecting memoization...
Injecting tracing...
Adding checks for parameters
Computing bounds of each function's value
Adding checks for images
Performing computation bounds inference...
Performing sliding window optimization...
Performing allocation bounds inference...
Removing code that depends on undef values...
Uniquifying variable names...
Performing storage folding optimization...
Injecting debug_to_file calls...
Simplifying...
Injecting prefetches...
Dynamically skipping stages...
Destructuring tuple-valued realizations...
Performing storage flattening...
Unpacking buffer arguments...
Skipping rewriting memoized allocations...
Simplifying...
Reduce prefetch dimension...
Unrolling...
Vectorizing...
Detecting vector interleavings...
Partitioning loops to simplify boundary conditions...
Trimming loops to the region over which they do something...
Injecting early frees...
Simplifying...
Lowering after final simplification:
assert((reinterpret(uint64, gradient.buffer) != (uint64)0), halide_error_buffer_argument_is_null("grad
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值