[PATCH] D83101: [Scalarizer] Constant ExtractElement Hanlding
Roman Lebedev via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 2 15:41:15 PDT 2020
lebedev.ri created this revision.
lebedev.ri added reviewers: jdoerfert, bjope, arsenm, cameron.mcinally.
lebedev.ri added a project: LLVM.
Herald added subscribers: hiraditya, wdng.
lebedev.ri added a child revision: D83102: [Scalarizer] Constant InsertElement Hanlding.
lebedev.ri added a parent revision: D82970: [Scalarizer] Variable insert handling (PR46524).
lebedev.ri marked an inline comment as done.
jdoerfert accepted this revision.
jdoerfert added a comment.
This revision is now accepted and ready to land.
LGTM.
================
Comment at: llvm/test/Transforms/Scalarizer/basic.ll:611
+; Test that constant extracts are nicely scalarized
+define i32 @f24(<4 x i32> *%src, i32 %index) {
+; CHECK-LABEL: @f24(
----------------
Previously we'd end up with https://godbolt.org/z/HDKdqh
```
%val0 = load <4 x i32>, <4 x i32>* %src, align 16
%val0.i0 = extractelement <4 x i32> %val0, i32 0
%val1.i0 = shl i32 1, %val0.i0
%val0.i1 = extractelement <4 x i32> %val0, i32 1
%val1.i1 = shl i32 2, %val0.i1
%val0.i2 = extractelement <4 x i32> %val0, i32 2
%val1.i2 = shl i32 3, %val0.i2
%val0.i3 = extractelement <4 x i32> %val0, i32 3
%val1.i3 = shl i32 4, %val0.i3
%val1.upto0 = insertelement <4 x i32> undef, i32 %val1.i0, i32 0
%val1.upto1 = insertelement <4 x i32> %val1.upto0, i32 %val1.i1, i32 1
%val1.upto2 = insertelement <4 x i32> %val1.upto1, i32 %val1.i2, i32 2
%val1 = insertelement <4 x i32> %val1.upto2, i32 %val1.i3, i32 3
%val2 = extractelement <4 x i32> %val1, i32 3
ret i32 %val2
```
It appears to be better IR-wise to aggressively scalarize it,
rather than relying on gathering it, and leaving it as-is.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D83101
Files:
llvm/lib/Transforms/Scalar/Scalarizer.cpp
llvm/test/Transforms/Scalarizer/basic.ll
llvm/test/Transforms/Scalarizer/phi-unreachable-pred.ll
Index: llvm/test/Transforms/Scalarizer/phi-unreachable-pred.ll
===================================================================
--- llvm/test/Transforms/Scalarizer/phi-unreachable-pred.ll
+++ llvm/test/Transforms/Scalarizer/phi-unreachable-pred.ll
@@ -15,12 +15,7 @@
; CHECK-NEXT: [[PHI_I1:%.*]] = phi i16 [ 1, [[ENTRY]] ], [ undef, [[FOR_COND]] ]
; CHECK-NEXT: [[PHI_I2:%.*]] = phi i16 [ 1, [[ENTRY]] ], [ undef, [[FOR_COND]] ]
; CHECK-NEXT: [[PHI_I3:%.*]] = phi i16 [ 1, [[ENTRY]] ], [ undef, [[FOR_COND]] ]
-; CHECK-NEXT: [[PHI_UPTO0:%.*]] = insertelement <4 x i16> undef, i16 [[PHI_I0]], i32 0
-; CHECK-NEXT: [[PHI_UPTO1:%.*]] = insertelement <4 x i16> [[PHI_UPTO0]], i16 [[PHI_I1]], i32 1
-; CHECK-NEXT: [[PHI_UPTO2:%.*]] = insertelement <4 x i16> [[PHI_UPTO1]], i16 [[PHI_I2]], i32 2
-; CHECK-NEXT: [[PHI:%.*]] = insertelement <4 x i16> [[PHI_UPTO2]], i16 [[PHI_I3]], i32 3
-; CHECK-NEXT: [[EXTRACT:%.*]] = extractelement <4 x i16> [[PHI]], i32 0
-; CHECK-NEXT: ret i16 [[EXTRACT]]
+; CHECK-NEXT: ret i16 [[PHI_I0]]
;
entry:
br label %for.end
Index: llvm/test/Transforms/Scalarizer/basic.ll
===================================================================
--- llvm/test/Transforms/Scalarizer/basic.ll
+++ llvm/test/Transforms/Scalarizer/basic.ll
@@ -607,6 +607,20 @@
ret i32 %val2
}
+; Test that constant extracts are nicely scalarized
+define i32 @f24(<4 x i32> *%src, i32 %index) {
+; CHECK-LABEL: @f24(
+; CHECK: %src.i0 = bitcast <4 x i32>* %src to i32*
+; CHECK: %src.i3 = getelementptr i32, i32* %src.i0, i32 3
+; CHECK: %val0.i3 = load i32, i32* %src.i3, align 4
+; CHECK: %val2 = shl i32 4, %val0.i3
+; CHECK: ret i32 %val2
+ %val0 = load <4 x i32> , <4 x i32> *%src
+ %val1 = shl <4 x i32> <i32 1, i32 2, i32 3, i32 4>, %val0
+ %val2 = extractelement <4 x i32> %val1, i32 3
+ ret i32 %val2
+}
+
!0 = !{ !"root" }
!1 = !{ !"set1", !0 }
!2 = !{ !"set2", !0 }
Index: llvm/lib/Transforms/Scalar/Scalarizer.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/Scalarizer.cpp
+++ llvm/lib/Transforms/Scalar/Scalarizer.cpp
@@ -778,9 +778,6 @@
}
bool ScalarizerVisitor::visitExtractElementInst(ExtractElementInst &EEI) {
- if (!ScalarizeVariableInsertExtract)
- return false;
-
VectorType *VT = dyn_cast<VectorType>(EEI.getOperand(0)->getType());
if (!VT)
return false;
@@ -789,7 +786,14 @@
IRBuilder<> Builder(&EEI);
Scatterer Op0 = scatter(&EEI, EEI.getOperand(0));
Value *ExtIdx = EEI.getOperand(1);
- if (isa<ConstantInt>(ExtIdx))
+
+ if (auto *CI = dyn_cast<ConstantInt>(ExtIdx)) {
+ Value *Res = Op0[CI->getValue().getZExtValue()];
+ gather(&EEI, {Res});
+ return true;
+ }
+
+ if (!ScalarizeVariableInsertExtract)
return false;
Value *Res = UndefValue::get(VT->getElementType());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83101.275249.patch
Type: text/x-patch
Size: 2852 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200702/dcc6ac80/attachment.bin>
More information about the llvm-commits
mailing list