LLVM开发常见调试方法

发布时间 2023-11-02 08:35:20作者: 暴力都不会的蒟蒻

一、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方法,是一个调试非常好用的东西