LLVM开发常见调试方法

一、llvm_unreachable

用来描述某个不支持的特性,会比assert(0) 更优雅,dump出来的信息也会友好一些
官方注释,llvm-project/llvm/include/llvm/Support/ErrorHandling.h:125

/// Marks that the current location is not supposed to be reachable.
/// In !NDEBUG builds, prints the message and location info to stderr.
/// In NDEBUG builds, if the platform does not support a builtin unreachable
/// then we call an internal LLVM runtime function. Otherwise the behavior is
/// controlled by the CMake flag
///   -DLLVM_UNREACHABLE_OPTIMIZE
/// * When "ON" (default) llvm_unreachable() becomes an optimizer hint
///   that the current location is not supposed to be reachable: the hint
///   turns such code path into undefined behavior.  On compilers that don't
///   support such hints, prints a reduced message instead and aborts the
///   program.
/// * When "OFF", a builtin_trap is emitted instead of an
//    optimizer hint or printing a reduced message.
///
/// Use this instead of assert(0). It conveys intent more clearly, suppresses
/// diagnostics for unreachable code paths, and allows compilers to omit
/// unnecessary code.

二、 LLVM_DEBUG

LLVM Debug信息输出,需要使用-g选项的Debug版本,可以使用--debug输出所有LLVM_DEBUG内容,也可以使用--debug-only控制输出某个DEBUG_TYPE的输出

三、dump

基本所有的LLVM数据都有dump方法,是一个调试非常好用的东西

四、官方提供的debug方法

比如MLIR的debug文档,就有--mlir-print-ir-after-failure,可以在MLIR报错时把整个moduledump出来。

附、测试

测试的话会使用llvm-lit和FileCheck,其中还有一些他的单元测试
LLVM Testing官方文档
lit官方文档
LLVM中FileCheck开发者工具–2--入门教程
CFG可视化

posted @ 2023-11-02 08:30  暴力都不会的蒟蒻  阅读(235)  评论(1编辑  收藏  举报