Skip to content

Commit 1e48dc5

Browse files
bokrzesiigcbot
authored andcommitted
[LLVM16] [LITS] Adjustment of getInstructionCost, so its calculations are consistent with getUserCost
Porting IGC code to LLVM16 Refactor and adjustment of getUserCost (LLVM 14) and getInstructionCost (LLVM16), so their calculations use the same algorithm. This fixes SimplifyCFG passes for expensive_math_intrinsics LITs Because without those adjustments the instruction cost of "declare float @llvm.sqrt.f32(float) #0" was 1 instead of 4. This caused `if (Cost > Budget ...)` check in `dominatesMergePoint` method to behave differently, thus different transformations were applied. This commit originally introduced the "extra cost" for cos/sin/sqrt instructions. a7bc5c8 Fixes: SimplifyCFG/expensive_math_intrinsics-typed-pointers.ll SimplifyCFG/expensive_math_intrinsics.ll
1 parent 9a5fda5 commit 1e48dc5

File tree

2 files changed

+50
-77
lines changed

2 files changed

+50
-77
lines changed

IGC/Compiler/GenTTI.cpp

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace llvm {
153153

154154
void GenIntrinsicsTTIImpl::getUnrollingPreferences(Loop* L,
155155
ScalarEvolution& SE,
156-
TTI::UnrollingPreferences& UP,
156+
TTI::UnrollingPreferences& UP,
157157
OptimizationRemarkEmitter* ORE
158158
)
159159
{
@@ -608,90 +608,56 @@ namespace llvm {
608608
return BaseT::isProfitableToHoist(I);
609609
}
610610

611-
#if LLVM_VERSION_MAJOR <= 10
612-
unsigned GenIntrinsicsTTIImpl::getCallCost(const Function* F,
613-
ArrayRef<const Value*> Arguments, const User* U)
611+
// TODO: Upon the complete removal of pre-LLVM 14 conditions, move to 'getInstructionCost' per LLVM 16 API
612+
llvm::InstructionCost GenIntrinsicsTTIImpl::getUserCost(const User* U, ArrayRef<const Value*> Operands, TTI::TargetCostKind CostKind)
614613
{
615-
// The extra cost of speculative execution for math intrinsics
616-
if (auto *II = dyn_cast_or_null<IntrinsicInst>(U)) {
617-
if (Intrinsic::ID IID = II->getIntrinsicID()) {
618-
switch (IID) {
619-
case Intrinsic::cos:
620-
case Intrinsic::sin:
621-
case Intrinsic::sqrt:
622-
return TTI::TCC_Expensive;
623-
default:
624-
break;
625-
}
626-
}
627-
}
628-
629-
IGC::CodeGenContext* CGC = this->ctx;
630-
if (!CGC->enableFunctionCall() && !GenISAIntrinsic::isIntrinsic(F) &&
631-
!F->isIntrinsic()) {
632-
// If subroutine call is not enabled but we have function call. They
633-
// are not inlined. e.g. due to two-phase inlining. Return function
634-
// size instead of to avoid under-estimating the cost of function call.
635-
//
636-
// FIXME: We need to collect the cost following calling graph. However,
637-
// as LLVM's ininer only support bottom-up inlining currently. That's
638-
// not a big issue so far.
639-
//
640-
// FIXME: We also need to consider the case where sub-routine call is
641-
// enabled.
642-
unsigned FuncSize = countTotalInstructions(F, false);
643-
return TargetTransformInfo::TCC_Basic * FuncSize;
644-
}
645-
return BaseT::getCallCost(F, Arguments, U);
614+
return GenIntrinsicsTTIImpl::internalCalculateCost(U, Operands, CostKind);
646615
}
647-
#else
648-
// [LLVM-UPGRADE] moved from getCallCost to getUserCost
649-
// https://github.com/llvm/llvm-project/commit/2641a19981e71c887bece92074e00d1af3e716c9#diff-dd4bd65dc55d754674d9a945a0d22911
650616

651-
#if LLVM_VERSION_MAJOR <= 12
652-
int GenIntrinsicsTTIImpl::getUserCost(const User* U, ArrayRef<const Value*> Operands, TTI::TargetCostKind CostKind)
653-
#else
654-
// TODO: Upon the complete removal of pre-LLVM 14 conditions, move to 'getInstructionCost' per LLVM 16 API
655-
llvm::InstructionCost GenIntrinsicsTTIImpl::getUserCost(const User* U, ArrayRef<const Value*> Operands, TTI::TargetCostKind CostKind)
617+
#if LLVM_VERSION_MAJOR >= 16
618+
llvm::InstructionCost GenIntrinsicsTTIImpl::getInstructionCost(const User* U, ArrayRef<const Value*> Operands, TTI::TargetCostKind CostKind)
619+
{
620+
return GenIntrinsicsTTIImpl::internalCalculateCost(U, Operands, CostKind);
621+
}
656622
#endif
623+
624+
llvm::InstructionCost GenIntrinsicsTTIImpl::internalCalculateCost(const User* U, ArrayRef<const Value*> Operands, TTI::TargetCostKind CostKind)
657625
{
658-
// The extra cost of speculative execution for math intrinsics
659-
if (auto *II = dyn_cast_or_null<IntrinsicInst>(U)) {
660-
if (Intrinsic::ID IID = II->getIntrinsicID()) {
661-
switch (IID) {
662-
case Intrinsic::cos:
663-
case Intrinsic::sin:
664-
case Intrinsic::sqrt:
665-
return TTI::TCC_Expensive;
666-
default:
667-
break;
668-
}
669-
}
626+
// The extra cost of speculative execution for math intrinsics
627+
if (auto* II = dyn_cast_or_null<IntrinsicInst>(U)) {
628+
if (Intrinsic::ID IID = II->getIntrinsicID()) {
629+
switch (IID) {
630+
case Intrinsic::cos:
631+
case Intrinsic::sin:
632+
case Intrinsic::sqrt:
633+
return TTI::TCC_Expensive;
634+
default:
635+
break;
636+
}
670637
}
638+
}
671639

672-
const Function* F = dyn_cast<Function>(U);
673-
if (F != nullptr)
674-
{
675-
IGC::CodeGenContext* CGC = this->ctx;
676-
if (!CGC->enableFunctionCall() && !GenISAIntrinsic::isIntrinsic(F) &&
677-
!F->isIntrinsic()) {
678-
// If subroutine call is not enabled but we have function call. They
679-
// are not inlined. e.g. due to two-phase inlining. Return function
680-
// size instead of to avoid under-estimating the cost of function call.
681-
//
682-
// FIXME: We need to collect the cost following calling graph. However,
683-
// as LLVM's ininer only support bottom-up inlining currently. That's
684-
// not a big issue so far.
685-
//
686-
// FIXME: We also need to consider the case where sub-routine call is
687-
// enabled.
688-
unsigned FuncSize = countTotalInstructions(F, false);
689-
return TargetTransformInfo::TCC_Basic * FuncSize;
690-
}
640+
const Function* F = dyn_cast<Function>(U);
641+
if (F != nullptr)
642+
{
643+
IGC::CodeGenContext* CGC = this->ctx;
644+
if (!CGC->enableFunctionCall() && !GenISAIntrinsic::isIntrinsic(F) &&
645+
!F->isIntrinsic()) {
646+
// If subroutine call is not enabled but we have function call. They
647+
// are not inlined. e.g. due to two-phase inlining. Return function
648+
// size instead of to avoid under-estimating the cost of function call.
649+
//
650+
// FIXME: We need to collect the cost following calling graph. However,
651+
// as LLVM's ininer only support bottom-up inlining currently. That's
652+
// not a big issue so far.
653+
//
654+
// FIXME: We also need to consider the case where sub-routine call is
655+
// enabled.
656+
unsigned FuncSize = countTotalInstructions(F, false);
657+
return TargetTransformInfo::TCC_Basic * FuncSize;
691658
}
659+
}
692660

693-
return BaseT::getInstructionCost(U, Operands, CostKind);
661+
return BaseT::getInstructionCost(U, Operands, CostKind);
694662
}
695-
#endif
696-
697663
} // namespace llvm

IGC/Compiler/GenTTI.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ namespace llvm
5656
llvm::InstructionCost getUserCost(const User* U, ArrayRef<const Value*> Operands,
5757
TTI::TargetCostKind CostKind);
5858

59+
llvm::InstructionCost getInstructionCost(const User* U, ArrayRef<const Value*> Operands,
60+
TTI::TargetCostKind CostKind);
61+
62+
private:
63+
llvm::InstructionCost internalCalculateCost(const User* U, ArrayRef<const Value*> Operands,
64+
TTI::TargetCostKind CostKind);
65+
5966
};
6067

6168
}

0 commit comments

Comments
 (0)