[llvm] ffd79b3 - [LoopUnroll] Consider simplified operands while retrieving TTI instruction cost (#70929)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 6 06:01:42 PST 2024
Author: Sergey Kachkov
Date: 2024-02-06T17:01:38+03:00
New Revision: ffd79b3312cea51c0787aad479ce285771470397
URL: https://github.com/llvm/llvm-project/commit/ffd79b3312cea51c0787aad479ce285771470397
DIFF: https://github.com/llvm/llvm-project/commit/ffd79b3312cea51c0787aad479ce285771470397.diff
LOG: [LoopUnroll] Consider simplified operands while retrieving TTI instruction cost (#70929)
Get more precise cost of instruction after LoopUnroll considering that
some operands of it can be simplified, e.g. induction variable will be
replaced by constant after full unrolling.
Added:
llvm/test/Transforms/LoopUnroll/RISCV/unroll-Os.ll
Modified:
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
llvm/test/Transforms/LoopUnroll/AMDGPU/unroll-cost-addrspacecast.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 7dfe4aca6fe400..75fb8765061edf 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -450,7 +450,15 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
// First accumulate the cost of this instruction.
if (!Cost.IsFree) {
- UnrolledCost += TTI.getInstructionCost(I, CostKind);
+ // Consider simplified operands in instruction cost.
+ SmallVector<Value *, 4> Operands;
+ transform(I->operands(), std::back_inserter(Operands),
+ [&](Value *Op) {
+ if (auto Res = SimplifiedValues.lookup(Op))
+ return Res;
+ return Op;
+ });
+ UnrolledCost += TTI.getInstructionCost(I, Operands, CostKind);
LLVM_DEBUG(dbgs() << "Adding cost of instruction (iteration "
<< Iteration << "): ");
LLVM_DEBUG(I->dump());
diff --git a/llvm/test/Transforms/LoopUnroll/AMDGPU/unroll-cost-addrspacecast.ll b/llvm/test/Transforms/LoopUnroll/AMDGPU/unroll-cost-addrspacecast.ll
index 6ec64bfba89596..d189f917048522 100644
--- a/llvm/test/Transforms/LoopUnroll/AMDGPU/unroll-cost-addrspacecast.ll
+++ b/llvm/test/Transforms/LoopUnroll/AMDGPU/unroll-cost-addrspacecast.ll
@@ -1,4 +1,6 @@
-; RUN: opt -S -mtriple=amdgcn-unknown-amdhsa -mcpu=hawaii -passes=loop-unroll -unroll-threshold=49 -unroll-peel-count=0 -unroll-allow-partial=false -unroll-max-iteration-count-to-analyze=16 < %s | FileCheck %s
+; RUN: opt -S -mtriple=amdgcn-unknown-amdhsa -mcpu=hawaii -passes=loop-unroll -unroll-threshold=57 -unroll-peel-count=0 -unroll-allow-partial=false -unroll-max-iteration-count-to-analyze=16 < %s | FileCheck %s
+
+ at indices = external global [16 x i32]
; CHECK-LABEL: @test_func_addrspacecast_cost_noop(
; CHECK-NOT: br i1
@@ -9,8 +11,10 @@ entry:
for.body:
%indvars.iv = phi i32 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
%sum.02 = phi float [ %fmul, %for.body ], [ 0.0, %entry ]
- %arrayidx.in = getelementptr inbounds float, ptr addrspace(1) %in, i32 %indvars.iv
- %arrayidx.out = getelementptr inbounds float, ptr addrspace(1) %out, i32 %indvars.iv
+ %idx.ptr = getelementptr inbounds [16 x i32], ptr @indices, i32 0, i32 %indvars.iv
+ %index = load i32, ptr %idx.ptr
+ %arrayidx.in = getelementptr inbounds float, ptr addrspace(1) %in, i32 %index
+ %arrayidx.out = getelementptr inbounds float, ptr addrspace(1) %out, i32 %index
%cast.in = addrspacecast ptr addrspace(1) %arrayidx.in to ptr
%cast.out = addrspacecast ptr addrspace(1) %arrayidx.out to ptr
%load = load float, ptr %cast.in
@@ -34,8 +38,10 @@ entry:
for.body:
%indvars.iv = phi i32 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
%sum.02 = phi float [ %fmul, %for.body ], [ 0.0, %entry ]
- %arrayidx.in = getelementptr inbounds float, ptr %in, i32 %indvars.iv
- %arrayidx.out = getelementptr inbounds float, ptr %out, i32 %indvars.iv
+ %idx.ptr = getelementptr inbounds [16 x i32], ptr @indices, i32 0, i32 %indvars.iv
+ %index = load i32, ptr %idx.ptr
+ %arrayidx.in = getelementptr inbounds float, ptr %in, i32 %index
+ %arrayidx.out = getelementptr inbounds float, ptr %out, i32 %index
%cast.in = addrspacecast ptr %arrayidx.in to ptr addrspace(3)
%cast.out = addrspacecast ptr %arrayidx.out to ptr addrspace(3)
%load = load float, ptr addrspace(3) %cast.in
@@ -58,8 +64,10 @@ entry:
for.body:
%indvars.iv = phi i32 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
%sum.02 = phi float [ %fmul, %for.body ], [ 0.0, %entry ]
- %arrayidx.in = getelementptr inbounds float, ptr addrspace(3) %in, i32 %indvars.iv
- %arrayidx.out = getelementptr inbounds float, ptr addrspace(3) %out, i32 %indvars.iv
+ %idx.ptr = getelementptr inbounds [16 x i32], ptr @indices, i32 0, i32 %indvars.iv
+ %index = load i32, ptr %idx.ptr
+ %arrayidx.in = getelementptr inbounds float, ptr addrspace(3) %in, i32 %index
+ %arrayidx.out = getelementptr inbounds float, ptr addrspace(3) %out, i32 %index
%cast.in = addrspacecast ptr addrspace(3) %arrayidx.in to ptr
%cast.out = addrspacecast ptr addrspace(3) %arrayidx.out to ptr
%load = load float, ptr %cast.in
diff --git a/llvm/test/Transforms/LoopUnroll/RISCV/unroll-Os.ll b/llvm/test/Transforms/LoopUnroll/RISCV/unroll-Os.ll
new file mode 100644
index 00000000000000..26de40bf1dc13e
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnroll/RISCV/unroll-Os.ll
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt < %s -S -mtriple=riscv64 -passes=loop-unroll | FileCheck %s
+
+; Function Attrs: optsize
+define void @foo(ptr %array, i32 %x) #0 {
+; CHECK-LABEL: define void @foo
+; CHECK-SAME: (ptr [[ARRAY:%.*]], i32 [[X:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_BODY:%.*]]
+; CHECK: for.body:
+; CHECK-NEXT: store i32 [[X]], ptr [[ARRAY]], align 4
+; CHECK-NEXT: [[ARRAYIDX_1:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 1
+; CHECK-NEXT: store i32 [[X]], ptr [[ARRAYIDX_1]], align 4
+; CHECK-NEXT: [[ARRAYIDX_2:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 2
+; CHECK-NEXT: store i32 [[X]], ptr [[ARRAYIDX_2]], align 4
+; CHECK-NEXT: [[ARRAYIDX_3:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 3
+; CHECK-NEXT: store i32 [[X]], ptr [[ARRAYIDX_3]], align 4
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %for.body
+
+for.body:
+ %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
+ %arrayidx = getelementptr inbounds i32, ptr %array, i64 %indvars.iv
+ store i32 %x, ptr %arrayidx, align 4
+ %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+ %exitcond.not = icmp eq i64 %indvars.iv.next, 4
+ br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+
+for.cond.cleanup:
+ ret void
+}
+
+attributes #0 = { optsize }
More information about the llvm-commits
mailing list