Skip to content

[InstCombine] Handle scalable splats of constants in getMinimumFPType #132960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,11 +1685,12 @@ static Type *getMinimumFPType(Value *V, bool PreferBFloat) {
return T;

// We can only correctly find a minimum type for a scalable vector when it is
// a splat. For splats of constant values the fpext is wrapped up as a
// ConstantExpr.
if (auto *FPCExt = dyn_cast<ConstantExpr>(V))
if (FPCExt->getOpcode() == Instruction::FPExt)
return FPCExt->getOperand(0)->getType();
// a splat.
if (auto *FPCE = dyn_cast<ConstantExpr>(V))
if (isa<ScalableVectorType>(V->getType()))
if (auto *Splat = dyn_cast<ConstantFP>(FPCE->getSplatValue()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In InstCombinerImpl::visitCallInst there is a similar example of using splats:

    // Handle mul by one:
    if (Constant *CV1 = dyn_cast<Constant>(Arg1))
      if (ConstantInt *Splat =
              dyn_cast_or_null<ConstantInt>(CV1->getSplatValue()))

and I wondered if it was worth doing the same here. Perhaps I'm wrong, but I thought splats aren't restricted to scalable vectors I think and would apply to fixed-width too? For example,

  if (auto *CFP = dyn_cast<Constant>(V))
    if (auto *Splat = dyn_cast_or_null<ConstantFP>(CFP->getSplatValue()))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed-width vectors are handled by shrinkFPConstantVector below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, shrinkFPConstantVector will handle splatted and non-splatted fixed vectors too.

But I think it should be NFC if we also handle fixed splats here, I guess it would act as a sort of fast path to prevent having to check shrinkFPConstant on each element? I've added it to this PR but let me know if you want to leave it out to keep it closer to the original

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. I personally think it looks neater to handle all splats in one place.

if (Type *T = shrinkFPConstant(Splat, PreferBFloat))
return T;

// Try to shrink a vector of FP constants. This returns nullptr on scalable
// vectors
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Transforms/InstCombine/scalable-const-fp-splat.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ define <vscale x 2 x float> @shrink_splat_scalable_extend(<vscale x 2 x float> %
%5 = fptrunc <vscale x 2 x double> %4 to <vscale x 2 x float>
ret <vscale x 2 x float> %5
}

define <vscale x 2 x float> @shrink_splat_scalable_extend_rhs_constexpr(<vscale x 2 x float> %a) {
; CHECK-LABEL: define <vscale x 2 x float> @shrink_splat_scalable_extend_rhs_constexpr(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have a fixed-width variant of this too, if there isn't one already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to fpextend.ll in d26e672

; CHECK-SAME: <vscale x 2 x float> [[A:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc <vscale x 2 x double> splat (double -1.000000e+00) to <vscale x 2 x float>
; CHECK-NEXT: [[TMP3:%.*]] = fadd <vscale x 2 x float> [[A]], [[TMP1]]
; CHECK-NEXT: ret <vscale x 2 x float> [[TMP3]]
;
%2 = fpext <vscale x 2 x float> %a to <vscale x 2 x double>
%4 = fadd <vscale x 2 x double> %2, splat (double -1.000000e+00)
%5 = fptrunc <vscale x 2 x double> %4 to <vscale x 2 x float>
ret <vscale x 2 x float> %5
}
Loading