Skip to content

Commit

Permalink
[FuncSpec] Avoid crashing when SwitchInst doesn't see ConstantInt
Browse files Browse the repository at this point in the history
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.

Reviewed By: ChuanqiXu

Differential Revision: https://reviews.llvm.org/D154159
  • Loading branch information
thevinster committed Jul 1, 2023
1 parent afb2743 commit 2fc0d17
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ Cost InstCostVisitor::estimateSwitchInst(SwitchInst &I) {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

@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 1
}

0 comments on commit 2fc0d17

Please sign in to comment.