Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 603b625

Browse files
[Analysis] Add Intrinsics::CLMUL case to cost calculations to getIntrinsicInstrCost / getTypeBasedIntrinsicInstrCost (llvm#176552)
This patch adds a case in getIntrinsicInstrCost and getTypeBasedIntrinsicInstrCost in llvm/include/llvm/CodeGen/BasicTTIImpl.h for Intrinsic::clmul. This patch uses TLI->isOperationLegalOrCustom to check if the instruction is cheap. If not cheap, it sums up the cost of the arithmetic operations (AND, SHIFT, XOR) multiplied by the bit width. Fixes llvm#176354
1 parent d1e477b commit 603b625

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,9 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
27022702
case Intrinsic::scmp:
27032703
ISD = ISD::SCMP;
27042704
break;
2705+
case Intrinsic::clmul:
2706+
ISD = ISD::CLMUL;
2707+
break;
27052708
}
27062709

27072710
auto *ST = dyn_cast<StructType>(RetTy);
@@ -3017,6 +3020,15 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
30173020
}
30183021
break;
30193022
}
3023+
case Intrinsic::clmul: {
3024+
// This cost model should match the expansion in
3025+
// TargetLowering::expandCLMUL.
3026+
InstructionCost PerBitCost =
3027+
thisT()->getArithmeticInstrCost(Instruction::And, RetTy, CostKind) +
3028+
thisT()->getArithmeticInstrCost(Instruction::Mul, RetTy, CostKind) +
3029+
thisT()->getArithmeticInstrCost(Instruction::Xor, RetTy, CostKind);
3030+
return RetTy->getScalarSizeInBits() * PerBitCost;
3031+
}
30203032
default:
30213033
break;
30223034
}

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8428,6 +8428,9 @@ SDValue TargetLowering::expandCLMUL(SDNode *Node, SelectionDAG &DAG) const {
84288428

84298429
switch (Opcode) {
84308430
case ISD::CLMUL: {
8431+
// NOTE: If you change this expansion, please update the cost model
8432+
// calculation in BasicTTIImpl::getTypeBasedIntrinsicInstrCost for
8433+
// Intrinsic::clmul.
84318434
SDValue Res = DAG.getConstant(0, DL, VT);
84328435
for (unsigned I = 0; I < BW; ++I) {
84338436
SDValue Mask = DAG.getConstant(APInt::getOneBitSet(BW, I), DL, VT);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2+
; RUN: opt < %s -passes="print<cost-model>" 2>&1 -disable-output -mtriple=aarch64-- | FileCheck %s
3+
4+
define void @clmul(i128 %a128, i128 %b128, i64 %a64, i64 %b64, i32 %a32, i32 %b32, i8 %a8, i8 %b8) {
5+
; CHECK-LABEL: 'clmul'
6+
; CHECK-NEXT: Cost Model: Found an estimated cost of 768 for instruction: %call_i128 = call i128 @llvm.clmul.i128(i128 %a128, i128 %b128)
7+
; CHECK-NEXT: Cost Model: Found an estimated cost of 192 for instruction: %call_i64 = call i64 @llvm.clmul.i64(i64 %a64, i64 %b64)
8+
; CHECK-NEXT: Cost Model: Found an estimated cost of 96 for instruction: %call_i32 = call i32 @llvm.clmul.i32(i32 %a32, i32 %b32)
9+
; CHECK-NEXT: Cost Model: Found an estimated cost of 24 for instruction: %call_i8 = call i8 @llvm.clmul.i8(i8 %a8, i8 %b8)
10+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
11+
;
12+
%call_i128 = call i128 @llvm.clmul.i128(i128 %a128, i128 %b128)
13+
%call_i64 = call i64 @llvm.clmul.i64(i64 %a64, i64 %b64)
14+
%call_i32 = call i32 @llvm.clmul.i32(i32 %a32, i32 %b32)
15+
%call_i8 = call i8 @llvm.clmul.i8(i8 %a8, i8 %b8)
16+
ret void
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2+
; RUN: opt < %s -passes="print<cost-model>" 2>&1 -disable-output -mtriple=x86_64-unknown-linux-gnu -mattr=+pclmul | FileCheck %s --check-prefix=PCLMUL
3+
; RUN: opt < %s -passes="print<cost-model>" 2>&1 -disable-output -mtriple=x86_64-unknown-linux-gnu -mattr=-pclmul | FileCheck %s --check-prefix=NO-PCLMUL
4+
5+
6+
define void @clmul(i128 %a128, i128 %b128, i64 %a64, i64 %b64, i32 %a32, i32 %b32, i8 %a8, i8 %b8) {
7+
; PCLMUL-LABEL: 'clmul'
8+
; PCLMUL-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %call_i128 = call i128 @llvm.clmul.i128(i128 %a128, i128 %b128)
9+
; PCLMUL-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %call_i64 = call i64 @llvm.clmul.i64(i64 %a64, i64 %b64)
10+
; PCLMUL-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %call_i32 = call i32 @llvm.clmul.i32(i32 %a32, i32 %b32)
11+
; PCLMUL-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %call_i8 = call i8 @llvm.clmul.i8(i8 %a8, i8 %b8)
12+
; PCLMUL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
13+
;
14+
; NO-PCLMUL-LABEL: 'clmul'
15+
; NO-PCLMUL-NEXT: Cost Model: Found an estimated cost of 1024 for instruction: %call_i128 = call i128 @llvm.clmul.i128(i128 %a128, i128 %b128)
16+
; NO-PCLMUL-NEXT: Cost Model: Found an estimated cost of 256 for instruction: %call_i64 = call i64 @llvm.clmul.i64(i64 %a64, i64 %b64)
17+
; NO-PCLMUL-NEXT: Cost Model: Found an estimated cost of 96 for instruction: %call_i32 = call i32 @llvm.clmul.i32(i32 %a32, i32 %b32)
18+
; NO-PCLMUL-NEXT: Cost Model: Found an estimated cost of 40 for instruction: %call_i8 = call i8 @llvm.clmul.i8(i8 %a8, i8 %b8)
19+
; NO-PCLMUL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
20+
;
21+
%call_i128 = call i128 @llvm.clmul.i128(i128 %a128, i128 %b128)
22+
%call_i64 = call i64 @llvm.clmul.i64(i64 %a64, i64 %b64)
23+
%call_i32 = call i32 @llvm.clmul.i32(i32 %a32, i32 %b32)
24+
%call_i8 = call i8 @llvm.clmul.i8(i8 %a8, i8 %b8)
25+
ret void
26+
}

0 commit comments

Comments
 (0)