[PATCH] D62186: [SimplifyCFG] Fix prof branch_weights MD while removing unreachable switch cases

Yevgeny Rouban via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 21 04:01:41 PDT 2019


yrouban created this revision.
yrouban added reviewers: davidxl, eraman, reames.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
yrouban added a parent revision: D62122: [NFC] Introduce SwitchInst wrapper for prof branch_weights handling.

SimplifyCFG has a bug that results in inconsistent prof branch_weights metadata if unreachable switch cases are removed.
This patch fixes this bug by making use of the newly introduced //SwitchInstProfBranchWeightsWrapper// class (see D62122 <https://reviews.llvm.org/D62122>).
A new test is created.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D62186

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/switch-profmd.ll


Index: llvm/test/Transforms/SimplifyCFG/switch-profmd.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SimplifyCFG/switch-profmd.ll
@@ -0,0 +1,35 @@
+; RUN: opt -S -simplifycfg < %s | FileCheck %s
+
+declare i32 @f(i32 %val)
+
+; Check that eliminating cases with unreachable branches keeps
+; prof branch_weights metadata consistent with switch instruction.
+define i32 @test_switch_to_unreachable(i32 %val) {
+; CHECK-LABEL: test_switch_to_unreachable
+; CHECK: switch
+; CHECK-NOT: i32 0, label %on0
+  switch i32 %val, label %otherwise [
+    i32 0, label %on0
+    i32 1, label %on1
+    i32 2, label %on2
+  ], !prof !{!"branch_weights", i32 99, i32 0, i32 1, i32 2}
+; CHECK: !prof ![[MD0:[0-9]+]]
+
+otherwise:
+  %result = call i32 @f(i32 -1)
+  ret i32 %result
+
+on0:
+  unreachable
+  ret i32 125
+
+on1:
+  %result1 = call i32 @f(i32 -2)
+  ret i32 %result1
+
+on2:
+  %result2 = call i32 @f(i32 -3)
+  ret i32 %result2
+}
+
+; CHECK: ![[MD0]] = !{!"branch_weights", i32 99, i32 2, i32 1}
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4214,14 +4214,15 @@
           Changed = true;
         }
       }
-    } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
+    } else if (auto *S = dyn_cast<SwitchInst>(TI)) {
+      SwitchInstProfBranchWeightsWrapper SI(*S);
       for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) {
         if (i->getCaseSuccessor() != BB) {
           ++i;
           continue;
         }
         BB->removePredecessor(SI->getParent());
-        i = SI->removeCase(i);
+        i = SI.removeCase(i);
         e = SI->case_end();
         Changed = true;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62186.200455.patch
Type: text/x-patch
Size: 1839 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190521/af888d89/attachment.bin>


More information about the llvm-commits mailing list