[PATCH] D154159: [FuncSpec] Avoid crashing when SwitchInst doesn't see ConstantInt

Vincent Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 29 15:14:08 PDT 2023


thevinster created this revision.
Herald added subscribers: ormris, hiraditya.
Herald added a project: All.
thevinster edited the summary of this revision.
thevinster added reviewers: smeenai, lanza, yozhu, labrinea, ChuanqiXu, SjoerdMeijer, chill.
thevinster published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

D150464 <https://reviews.llvm.org/D150464> updated the cost model for function specialization. Unfortunately, this 
also crashes when trying to build stage2 LLD with thinLTO and assertions. It looks
like the issue is caused by a mishandling of the Constant in a SwitchInst since the 
Constant cannot always be assumed to safely casted to a ConstantInt. In the case 
of the crash, Constant was a ConstantExpr which triggered the assertion.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154159

Files:
  llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
  llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression6.ll


Index: llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression6.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression6.ll
@@ -0,0 +1,22 @@
+; RUN: opt -passes="ipsccp<func-spec>" -force-specialization -S < %s
+; Check that we don't crash when SwitchInst Constant is not ConstantInt.
+
+ at S = external constant [1 x i8]
+
+define i1 @foo() {
+entry:
+  %tmp = call i32 @bar(ptr @S)
+  ret i1 0
+}
+
+define i32 @bar(ptr %arg) {
+entry:
+  %magicptr = ptrtoint ptr %arg to i64
+  switch i64 %magicptr, label %bb2 [
+    i64 0, label %bb1
+  ]
+bb1:
+  ret i32 0
+bb2:
+  ret i32 0
+}
Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
===================================================================
--- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -190,7 +190,10 @@
   if (I.getCondition() != LastVisited->first)
     return 0;
 
-  auto *C = cast<ConstantInt>(LastVisited->second);
+  auto *C = dyn_cast<ConstantInt>(LastVisited->second);
+  if (!C)
+    return 0;
+
   BasicBlock *Succ = I.findCaseValue(C)->getCaseSuccessor();
   // Initialize the worklist with the dead basic blocks. These are the
   // destination labels which are different from the one corresponding


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154159.536023.patch
Type: text/x-patch
Size: 1401 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230629/f1b34c47/attachment.bin>


More information about the llvm-commits mailing list