Switch Lowering in GCC

(xoranth.net)

130 points | by signa11 7 hours ago

7 comments

  • rurban 5 hours ago
    What a shame for clang and gcc! They still don't implement perfect hashes for large sparse switch tables. I did that recently for my rcc C compiler, and it was just a single day of work. Esp. beneficial for unicode work. The LLVM patch was rejected a decade ago. Gcc has several unicode patches sitting in branches, which would also need that
    • nh2 2 hours ago
      Maybe LLVM doesn't have time for performance improvements due to still reading through 30000 open issues, which also include miscompilations leading to incorrect results at runtime?

      For example, my bug report that `+= 2` is broken has not been acknowledged (read?) by anyone for 3 years:

          const size_t n = 13; // triggers the issue
          #pragma omp parallel for
            for (uint32_t i = 0; i < n; i += 2) {  // counts PAST 13!
      
      https://github.com/llvm/llvm-project/issues/70687
      • stefan_ 3 minutes ago
        #pragma omp

        Ahh, there it is! This issue isn't being tended to because it will resolve itself with the imminent death of OpenMP.

      • rurban 2 hours ago
        That's true. restrict is also still broken there. 30000 open issue is even worse than gcc
    • RossBencina 4 hours ago
      Would like to hear more about the performance characteristics. Do you perfect hash to a contiguous range and then use a jump table, or do you perfect hash directly to relative offsets to basic blocks?
    • tester756 5 hours ago
      I'd bet because that's probably no better, that's it.

      >The LLVM patch was rejected a decade ago

      And what was the reasoning?

  • LarsKrimi 5 hours ago
    > The switch in sparse_8 could be simplified by dividing x by eight before the jump. Interestingly, gcc doesn't find this simplification.

    It could not because that would ensure that 7 values for each case would be handled by function calls instead of the default case - 0/8 is 0, but 3/8 is also 0

    • rep_lodsb 4 hours ago
      All the values are divisible by 8, so it could do it like this:

          cmp  eax,40
          ja   <return 0>
          test eax,7
          jnz  <return 0>
          jmp  JumpTable[rax]
      • LarsKrimi 3 hours ago
        Okay. Good point, that is quite a bit shorter than I first thought of

        A rough benchmark of a version of that against the original shows it to be close to twice as fast for unpredictable input with GCC (4.5 ns vs 2.72 ns) on my old machine. With predictable input where x values often are the same the original version is quicker

            int sparse_8_jump(int x, int val) {
                unsigned int idx = (unsigned int)x >> 3;
                if (idx > 5)
                    return 0;
                if (x & 7)
                    return 0;
                
                switch (idx) {
                    case 0:
                        return f0(val);
                    case 1:
                        return f1(val);
                    case 2:
                        return f2(val);
                    case 3:
                        return f3(val);
                    case 5:
                        return f4(val);
                    default:
                        return 0;
                }
            }
    • Findecanor 3 hours ago
      Reducing the range would have been useful to reduce the size of a jump table, but when you have a decision tree then it does not matter.

      ... unless the size of immediate parameters would have affected code size. In the example code however, all constants fit in an `imm8` parameter to `cmp`.

  • tsukikage 5 hours ago
    Why the indirection? - why not have the jump table reference f0..fX directly rather than jumping to a jump?
    • Findecanor 3 hours ago
      That optimisation is simply missing.

      Each `jmp` to f0..fX is an optimisation of a `call` and a `ret`, and probably done in a separate pass after the switch-case code has already been expanded.

      The `mov edi, esi` at the top of the function is also an optimisation, most likely done during the register allocation pass. This was only possible because all functions take the same value in the same register. Otherwise, there would probably have been also a `mov` before each of the calls.

      • andikleen2 2 hours ago
        gcc unfortunately cannot do this (and also not the related optimization of using word/byte offsets for the jump table, which LLVM can do) because it only has approximate estimation of code size. If it got the code size wrong it would be a mis-compilation. This is related to it not using an integrated assembler.
    • yksalun 4 hours ago
      [flagged]
  • Andi 4 hours ago
    I remember having implemented similar things in JavaScript (by generating code), for immutable Maps and Sets - no significant performance gain in modern engines. For regular expressions of complex unicode ranges, the speedup was around 2x!
  • arsalan17 6 hours ago
    This one was interesting, especially its name :)
  • sylware 5 hours ago
    In order to peak into hardware speculation/BTB, having a look atswitch lowering seems a good way. Big switches will go into serious convolution to please that hardware since by definition this is a source of a lot of branch mispredicitions.
    • ahartmetz 3 hours ago
      There is surprisingly little about the proper feeding of advanced hardware in that code, two or three constants in cost calculations of which one or two are hardware-dependent.

      It is still possible that a later pass arranges jump targets to avoid false sharing of their entries (addresses) in prediction mechanisms and such.

      • sylware 3 hours ago
        Just look at the assembly output of significantly sized switch statements (with good random values): it's scary :)

        Obvsiously engaging in pleasing as much as it can speculative/BTB hardware (to the cost of memory tables!)

  • yarkwright 1 hour ago
    [flagged]