llvm.txt 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ===========
  2. QBE vs LLVM
  3. ===========
  4. Both QBE and LLVM are compiler backends using an SSA
  5. representation. This document will explain why LLVM
  6. does not make QBE a redundant project. Obviously,
  7. everything following is biased, because written by me.
  8. - Scope
  9. -------
  10. QBE is a much smaller scale project with different goals
  11. than LLVM.
  12. * QBE is for amateur language designers.
  13. It does not address all the problems faced when
  14. conceiving an industry-grade language. If you are
  15. toying with some language ideas, using LLVM will
  16. be like hauling your backpack with a truck, but
  17. using QBE will feel more like riding a bicycle.
  18. * QBE is about the first 70%, not the last 30%.
  19. It attempts to pinpoint, in the extremely vast
  20. compilation literature, the optimizations that get
  21. you 70% of the performance in 10% of the code of
  22. full blown compilers.
  23. For example, copy propagation on SSA form is
  24. implemented in 160 lines of code in QBE!
  25. * QBE is extremely hackable.
  26. First, it is, and will remain, a small project
  27. (less than 8 kloc). Second, it is programmed in
  28. non-fancy C99 without any dependencies. Third,
  29. it is able to dump the IL and debug information in
  30. a uniform format after each pass.
  31. On my Core 2 Duo machine, QBE compiles in half a
  32. second (without optimizations).
  33. - Features
  34. ----------
  35. LLVM is definitely more packed with features, but there
  36. are a few things provided in QBE to consider.
  37. * LLVM does NOT provide full C compatibility for you.
  38. In more technical terms, any language that provides
  39. good C compatibility and uses LLVM as a backend
  40. needs to reimplement large chunks of the ABI in
  41. its frontend! This well known issue in the LLVM
  42. community causes a great deal of duplication
  43. and bugs.
  44. Implementing a complete C ABI (with struct arguments
  45. and returns) is incredibly tricky, and not really
  46. a lot of fun. QBE provides you with IL operations
  47. to call in (and be called by) C with no pain.
  48. Moreover the ABI implementation in QBE has been
  49. thoroughly tested by fuzzing and manual tests.
  50. * LLVM IL is more cluttered with memory operations.
  51. Implementing SSA construction is hard. To save its
  52. users from having to implement it, LLVM provides
  53. stack slots. This means that one increment of
  54. a variable `v` will be composed of three LLVM
  55. instructions: one load, one add, and one store.
  56. QBE provides simple non-SSA temporaries, so
  57. incrementing `v` is simply done with one instruction
  58. `%v =w add %v, 1`.
  59. This could seem cosmetic, but dividing the size of
  60. the IL by three makes it easier for the frontend
  61. writers to spot bugs in the generated code.
  62. * LLVM IL is more cluttered with type annotations and
  63. casts.
  64. For the sake of advanced optimizations and
  65. correctness, LLVM has complex IL types. However,
  66. only a few types are really first class and many
  67. operations of source languages require casts to be
  68. compiled.
  69. Because QBE makes a much lighter use of types, the
  70. IL is more readable and shorter. It can of course be
  71. argued back that the correctness of QBE is jeopardized,
  72. but remember that, in practice, the large amount
  73. of casts necessary in LLVM IL is undermining the
  74. overall effectiveness of the type system.