博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Halide入门第3讲:如何设置环境变量以检查llvm编译生成的代码
阅读量:4188 次
发布时间:2019-05-26

本文共 2996 字,大约阅读时间需要 9 分钟。

// Halide tutorial lesson 3: Inspecting the generated code

// Halide入门第3讲:检查llvm编译生成的代码

// 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;
}

1. 设置环境变量HL_DEBUG_CODEGEN=1/2,jit即时编译打印出中间编译结果,方便调试

2. Func.compile_to_lowered_stmt("gradient.html", {}, HTML), 将Halide中间调度以html形式保存出来,方便阅读和理解中间调度过程

转载地址:http://kksoi.baihongyu.com/

你可能感兴趣的文章
当了将近十年的程序员,为什么从来没见过程序员带孩子
查看>>
程序员面试中最容易碰到的五个套路!应届生最容易上当
查看>>
三种不同的程序员,你属于哪一种?如果要裁员,你会让谁走?
查看>>
干货神总结,程序员面试技巧
查看>>
深度解析BAT三家互联网公司,为什么腾讯产品第一,百度技术第一,阿里运营第一?
查看>>
程序员发贴求助:剪短头发能缓解脱发吗?网友:我觉得秃头挺好的
查看>>
史上最难程序员的面试题!谷歌、百度、微软、阿里必答题
查看>>
为什么会出现“程序员千万不要学算法”这种言论?
查看>>
程序员如何做到快速升职?这几点你都做到了吗?
查看>>
第五届世界互联网大会重点介绍工业互联网
查看>>
凭什么程序员工资那么高?网友:某些文职坐着白领钱才让我惊奇
查看>>
程序员准时下班碰见领导,次月发工资时看到绩效莫名被扣20%
查看>>
你见过最牛逼的程序员是什么样的?拳打回车键,脚踩Emacs编辑器
查看>>
相比加班的程序员,企业更喜欢“偷懒”的程序员?程序员偷的不是懒,是高效!
查看>>
初学Java必备基础知识,编程领域你需要掌握的关键点!
查看>>
阿里五年Java程序员的总结,献给还在迷茫中的你!
查看>>
程序员身上有异味,同事为什么都不会直接告诉他?
查看>>
大数据折射算法“歧视”?王思聪微博抽奖113位,仅有一位男性
查看>>
Java、C、C+ +、PHP、Python分别用来开发什么?一篇文章告诉你!
查看>>
Linux-SHELL常用命令
查看>>