Unverified Commit 5275e727 authored by Kazu Hirata's avatar Kazu Hirata Committed by GitHub
Browse files

[InlineOrder] Fix assertion failure in CostBenefitPriority (#195564)

InlineCost::getStaticBonusApplied() triggers an assertion failure
if the CostBenefitPriority constructor calls it when
IC.isVariable() is false. This is because
getStaticBonusApplied() expects isVariable() to be true.
Unconditionally populating CostBenefit also incorrectly prioritizes
a NeverInline candidate with a cost-benefit pair over other
valid variable-cost sites.
  
This patch fixes the crash and the sorting issue by calling
getStaticBonusApplied() and populating CostBenefit only when
IC.isVariable() is true. For AlwaysInline and NeverInline costs,
CostBenefit is explicitly set to std::nullopt.
parent 7e44c07e
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -114,12 +114,15 @@ public:
  CostBenefitPriority(const CallBase *CB, FunctionAnalysisManager &FAM,
                      const InlineParams &Params) {
    auto IC = getInlineCostWrapper(const_cast<CallBase &>(*CB), FAM, Params);
    if (IC.isVariable())
    if (IC.isVariable()) {
      Cost = IC.getCost();
    else
      Cost = IC.isNever() ? INT_MAX : INT_MIN;
      StaticBonusApplied = IC.getStaticBonusApplied();
      CostBenefit = IC.getCostBenefit();
    } else {
      Cost = IC.isNever() ? INT_MAX : INT_MIN;
      StaticBonusApplied = 0;
      CostBenefit = std::nullopt;
    }
  }

  static bool isMoreDesirable(const CostBenefitPriority &P1,
+23 −0
Original line number Diff line number Diff line
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
; Test that getStaticBonusApplied() does not trigger an assertion failure
; when CostBenefitPriority encounters a non-variable InlineCost.
; RUN: opt -passes='module-inline' -inline-priority-mode=cost-benefit -S < %s | FileCheck %s

define i1 @foo() {
; CHECK-LABEL: define i1 @foo() {
; CHECK-NEXT:    [[TMP1:%.*]] = call ptr @bar(ptr null)
; CHECK-NEXT:    ret i1 true
;
  call ptr @bar(ptr null)
  ret i1 true
}

define ptr @bar(ptr %0) {
; CHECK-LABEL: define ptr @bar(
; CHECK-SAME: ptr [[TMP0:%.*]]) {
; CHECK-NEXT:    [[TMP2:%.*]] = call ptr @bar(ptr null)
; CHECK-NEXT:    ret ptr null
;
  call ptr @bar(ptr null)
  ret ptr null
}