the recent switch lowering improvements
play

The recent switch lowering improvements Hans Wennborg - PowerPoint PPT Presentation

The recent switch lowering improvements Hans Wennborg hwennborg@google.com A Switch C: switch (x) { LLVM IR: case 0: // foo switch i32 %x, label %baz [ case 1: i32 0, label %foo // bar i32 1, label %bar ... ... default: ] // baz


  1. The recent switch lowering improvements Hans Wennborg hwennborg@google.com

  2. A Switch C: switch (x) { LLVM IR: case 0: // foo switch i32 %x, label %baz [ case 1: i32 0, label %foo // bar i32 1, label %bar ... ... default: ] // baz }

  3. A Switch C: if (x == 0) { LLVM IR: // foo } else if (x == 1) { switch i32 %x, label %baz [ // bar i32 0, label %foo } else { i32 1, label %bar // baz ... } ]

  4. Lowering LowerSwitch SelectionDAGBuilder::visitSwitch

  5. Lowering LowerSwitch SelectionDAGBuilder::visitSwitch

  6. Step 0: Cluster adjacent cases 1 5 2 3 0 4 B C B B A C

  7. Step 0: Cluster adjacent cases 1 5 2 3 0 4 B C B B A C 0 1-3 4-5

  8. Lowering strategies 1. Straight comparisons 2. Jump tables 3. Bit tests 4. Binary search tree

  9. 1. Straight comparisons x = 0 A 1 ≤ x ≤ 3 B 4 ≤ x ≤ 5 C Number of clusters ≤ 3 ● Default

  10. 2. Bit tests x ≤ 8 2 0 +2 3 +2 6 = 73 bt x, $73 A 0 3 6 A 2 1 +2 4 +2 7 = 146 bt x, $146 B 1 4 7 B 2 2 +2 5 +2 8 = 292 bt x, $292 C 2 5 8 C Default Number of destinations ≤ 3 ● ● Range fits in machine word

  11. 3. Jump table table: 1 A 0 A 1 B 2 B 1≤ x ≤ 5 table[x-1] 2 C 3 C 3 Default 4 D Default 5 D Number of clusters ≥ 4 ● ● Table density ≥ 40%

  12. 4. Binary search tree 101 1000 0 3 6 D H A 102 2000 E I 1 4 7 B 103 3000 F J 2 5 8 C 104 G Bit tests Jump table Straight comparisons

  13. 4. Binary search tree x ≤ 100 x ≤ 999 x ≤ 8 bt x, $73 A x = 1000 H 101≤ x ≤ 104 table[x-101] bt x, $146 B x=2000 I bt x, $292 C x=3000 J Default Default Default

  14. What changed?

  15. Old algorithm: top-down ● Consider the range of cases Lower by cmps, bit tests or jump table? If yes, done ● ● Split the range in two*, creating BST Repeat for both sides ●

  16. Old algorithm: pivot selection is hard x < 10000 x < 1000 x < 100 x < 10 Heuristic helps find jump tables * Pivot heuristic: maximize gap size But trees might not be balanced and sum density of LHS and RHS. (PR22262)

  17. New algorithm: bottom-up ● Consider the whole range of cases Find case clusters suitable for bit tests ● ● Find case clusters suitable for jump tables Build binary search tree ●

  18. New algorithm: benefits ● Lowering strategies decoupled a. Code is easier to follow b. Can do less work at -O0 ● Jump table extraction is optimal* BST will be balanced** ● * For our size and density criteria ** Next slide!

  19. Balanced by node count 0 10 20 30 40 50 60 70 x < 40 x < 20 x < 60 x = 0 x = 20 x = 40 x = 60 x = 10 x = 30 x = 50 x = 70

  20. Balanced by node weight 0 10 20 30 40 50 60 70 10 1 1 1 1 1 1 1000 x < 50 x = 70 x < 10 x = 50 x = 0 x < 30 x = 60 x = 10 x = 30 x = 20 x = 40

  21. Balanced by node weight 0 10 20 30 40 50 60 70 10 1 1 1 1 1 1 1000 x Branches x weight x < 50 0 3 30 10 4 4 x = 70 x < 10 20 5 5 x = 50 30 4 4 x = 0 x < 30 40 5 5 x = 60 50 3 3 x = 10 x = 30 60 4 4 x = 20 x = 40 70 2 2000 Sum: 2055 (Without weight balancing: 3052)

  22. Summary ● Trees are balanced Jump tables are found ● Uses profile info ●

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend