1 of 5

A5 and Debugging

2 of 5

Debugging Tips

  • Generate Assembly from C/C++
    • gcc -S foo.c
    • gcc -S -o foo.S foo.c
  • Could use gdb
    • layout asm
  • Make Optimizations Modular
    • Should be considered “Black box functions”
    • Be able to toggle on or off (CLI argument)
  • Optimization Effectiveness
    • time and timeout commands
    • Keep intermediate IR metrics
    • Keep lines of assembly metrics

3 of 5

Measuring Memory Usage

  • Massif: heap profiler in Valgrind
  • Use to find memory leaks
    • Run a loop with garbage collection
    • See if the heap grows over time
  • Use to measure memory usage
    • Quantify how much better/worse optimizations make heap usage

4 of 5

A5 Tips

  • Minimal failing test cases become super important
  • Debug C++ and asm independently
    • Use your VM as ground truth
  • x64asm can be quirky
    • Read the docs and examples
  • When to generate asm?
    • Consider optimizing your VM, only compiling “hot” functions to asm
    • After first call, after N calls, etc.
  • Set up your measurement infrastructure ahead of time

5 of 5

C++ helpers in asm

# helpers.h

uint64_t helper_add(uint64_t left, uint64_t right);

# helpers.cpp

uint64_t helper_add(uint64_t left, uint64_t right) {� return vm_add(Value(left), Value(right)).value;� }

# compiler.cpp

void* fn = (void *)(&helper_add);� asm.mov(reg, Imm64{(uint64_t)fn});� asm.call(reg);