Skip to content

Latest commit

 

History

History
224 lines (204 loc) · 9.41 KB

File metadata and controls

224 lines (204 loc) · 9.41 KB

Search Clang Doc

LLVM ENABLE CRASH DIAGNOSTIC

  • export LLVM_ENABLE_DUMP=1

Print All passes enabled

$opt=-O0
echo 'int;' | clang -xc $opt  - -o /dev/null -###

Print clang options, default values

echo 'int;' | clang -xc - -o /dev/null -mllvm -print-all-options
clang -help
clang -cc1 -help
clang -cc1 -mllvm -help
clang -cc1 -mllvm -help-list-hidden
clang -cc1as -help

preprocessor

  • Dump all macros set by different options
clang -E -dM  <source>   # does not need emit-llvm etc.
-E: stop before compiling, after preprocessing, produces .i
-S: stop before assembling, produces .s
-c: stop before linking, produces .o
-v: print commands executed and run
-###: print commands executed, don't run
-o -: print to stdout rather than write output to file
-fno-discard-value-names: identifiers from source rather than numbers.
-g0: less debug info in IR.

IR

clang -O0 -emit-llvm -Xclang -disable-llvm-passes -S  bug.c -o bug.ll
opt bug.ll -o bug.opt.ll -passes='sroa,instcombine,loop(loop-rotate,indvars),dot-cfg' -S
  • Add debug location as comment
 -print-inst-debug-locs -emit-llvm
 %mul = mul nsw i32 %0, %1, !dbg !19 ; example.c:3:16
-print-inst-addrs
  %mul = mul nsw i32 %0, %1, !dbg !19 ; 0x21bbe1d0
-print-sdnode-addrs
-print-mi-addrs

CLANG/LLC

  • To pass target specific option to clang and llc
clang -mavx/-mavx512f test.c -S
llc -mattr=avx512f/-mattr=+avx512f test.ll -S
  • MCPU / MARCH / MTUNE

    https://maskray.me/blog/2022-08-28-march-mcpu-mtune
    -march=X: (execution domain) Generate code that can use instructions available in the architecture X
    -mtune=X: (optimization domain) Optimize for the microarchitecture X, but does not change the ABI or make assumptions about available instructions
    -mcpu=X: Specify both -march= and -mtune= but can be overridden by the two options. The supported values are generally the same as -mtune=. The architecture name is inferred from X
     simplest case where only one option is desired, use -march= for x86 and -mcpu= for other targets.
    When optimizing for the local machine, just use -march=native for x86 and -mcpu=native for other targets.
    When the architecture and microarchitecture are both specified, i.e. when both the execution domain and the optimization domain need to be specified, specify -march= and -mtune=, and avoid -mcpu=.
    https://community.arm.com/arm-community-blogs/b/tools-software-ides-blog/posts/compiler-flags-across-architectures-march-mtune-and-mcpu
    
  • Compiler Driver and cross compilation https://maskray.me/blog/2021-03-28-compiler-driver-and-cross-compilation#clang

Assembly

DUMP AST

clang -Xclang -ast-dump -ast-dump -ast-dump-filter=func
clang -cc1 -ast-dump -ast-dump -ast-dump-filter=func

Export DOT for CFG/Callgraph

opt test2.ll -passes=dot-callgraph -callgraph-dot-filename-prefix=callgraph -disable-output
opt -passes=dot-cfg test.ll -cfg-dot-filename-prefix=temp -o /dev/null
dot -Tpng temp.foo.dot -o file.png && sxiv file.png
dot -Tpdf cfg.dot -o cfg.pdf

Graphviz

Generate ASM from Compiler

Remove cfi and noise

clang -fno-asynchronous-unwind-tables -fno-exceptions -fno-rtti

OPT

  • How Pass manager executes and dependent passes (New PM), old PM uses -debug-pass=Structure opt -debug-pass-manager

Run IR CFG DOT

opt -passes="view-cfg" -cfg-func-name=<func> <ll filename> -disable-output

  • LLVM IR naming

Xlinker Options

IR debug and print-after-all from clang

clang -mllvm --print-before-all -mllvm --filter-print-funcs=KERNEL_FUNC

IR-LTO on, dump debug and print-after-all

clang -Xlinker -plugin-opt=-print-before-all -Xlinker -plugin-opt=-filter-print-funcs=KERNEL_FUNC

Same as above but list

clang -Wl,-mllvm,-print-after-all -Wl,-mllvm,-filter-print-funcs=KERNEL_FUNC

Opt Reports

clang -O3 -foptimization-record-file=kernel.yaml; opt-viewer.py kernel.yaml
clang -O3 -ftime-trace
opt -O3 –passes="my-opt" –pass-remarks-output=remark.yaml -disable-output

Print IR Debug Options

clang test.c -O2 -mllvm -print-changed
opt test.ll -O2 -print-changed
llc test.ll  -print-changed
  • llvm/lib/IR/OptBisect.cpp
  • Turn off BISECT message -opt-bisect-verbose=false otherwise writes to error stream
    # When using lld, or ld64 (macOS)
    clang -flto -Wl,-mllvm,-opt-bisect-limit=256 my_file.o my_other_file.o
    # When using Gold
    clang -flto -Wl,-plugin-opt,-opt-bisect-limit=256 my_file.o my_other_file.o
    opt -O2 -o test-opt.bc -opt-bisect-limit=16 test.ll
    llc -O2 -o test-opt.bc -opt-bisect-limit=16  -o test.o
    clang  -Wl,-plugin-opt,-Paramter=Val
           -Wl,-plugin-opt,-filter-print-funcs=FUNC
           -Wl,-plugin-opt,-print-before=passname
           -Wl,-plugin-opt,-print-after=passname
           -Wl,-plugin-opt=-opt-bisect-verbose=false
           -Wl,-plugin-opt=save-temps
           -Xlinker -plugin-opt=-opt-bisect-limit=-1
           -flto
    
  • Disable opt pass -opt-disable=passname,passname1

DEBUG MIR

  • For "LLVM: Machine Code Errors"
  • Error take form
    • Register / liveness issues use of undefined physreg/virtreg subregister lane mismatch killed flag incorrect

    • Illegal instruction operands wrong operand type (imm vs reg) constraints violated (target-specific)

    • Broken CFG / PHI PHI operands not matching predecessor blocks illegal critical edge assumptions

    • Stack frame / call convention mismatches bad regmask around calls wrong callee-saved restoration

    • Debug-info related verifier errors invalid DBG_VALUE location after regalloc

    • Print Verify Log with Faulty MIR llc input.ll -o /dev/null -verify-machineinstrs -debug-only=machineverifier

    • Verify Each llc input.ll -o /dev/null -verify-machineinstrs -verify-each

    • Print MIR llc input.ll -o /dev/null -print-machineinstrs

    • Run MIR Verify on file llc -run-pass=none -o /dev/null -verify-machineinstrs something.mir

    • Use Symbols for stack LLVM_SYMBOLIZER_PATH=/path/to/llvm-symbolizer llc input.ll -o /dev/null -verify-machineinstrs -debug-only=machineverifier

    • LLVM Reduce llvm-reduce --test="llc reduced.ll -o /dev/null -verify-machineinstrs" input.ll

    • bugpoint bugpoint --run-llc --llc-args="-verify-machineinstrs" input.ll

PERF ISSUE OPTIONS

FE Alignment Options

-malign-branch-boundary=16
-mbranches-within-32B-boundaries
-mpad-max-prefix-size=5
-malign-branch=fused,jcc,jmp
-mllvm -align-all-functions=5
-mllvm -align-all-blocks=5

Machine Block Placement

LTO Debugging LD + plugin GOLD/BFD (TODO)

  • Dump LTO IRs clang -flto -Wl,-plugin-opt=save-temps
  • Make the crash produce a reproducible artifact clang++ -flto -fuse-ld=lld -Wl,--reproduce=./lto-repro.tar
  • Thin LTO -Wl,--plugin-opt=thinlto-save-temps

Windows Specific

lld-linker Diagnostics

export FORCE_LLD_DIAGNOSTICS_CRASH=1

CRASH LOG SYMBOLIZE (TODO)

export LLVM_SYMBOLIZER_PATH=/path/to/llvm-symbolizer

LLVM-MCA

Using Tablegen

  • like gen-global-isel etc describe in llvm/lib/Target/X86/CMakeLists.txt
  • llvm-tblgen -gen-global-isel -I$LLVM_TOP/llvm/lib/Target/X86 -I$LLVM_TOP/llvm/include -I$LLVM_TOP/llvm/lib/Target $LLVM_TOP/llvm/lib/Target/X86/X86.td --write-if-changed -o <OUTPUT>
  • add '--debug-only=gisel-emitterand--debug`
  • Supports statistics

LLVM BUILD DEBUG

  • export VERBOSE=1
  • Find command in ninja : ninja -t commands program | grep filename

GCC Options

  • Check Default options
    • Check default target options/features enabled or disabled -Q --help=target
    • Check enabled optimization configurations -Q --help=optimizers
    • Tool flow how GCC invokes all its subprocesses (preprocessor, compiler) -dumpspecs
    • https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html
  • Debugging Options for GCC (Opt reports, dumps after stage)