This is an archive of the discontinued LLVM Phabricator instance.

[IR] New function llvm::createMinMaxSelectCmpOp for creating min/max operation in select-cmp form
Needs ReviewPublic

Authored by Mel-Chen on May 3 2023, 1:05 AM.

Details

Summary

This patch preserves the ability to represent min max operations in select-cmp form. This provides flexibility in choosing between intrinsic or select-cmp form for generating min max operations, depending on the optimization requirements.

Diff Detail

Event Timeline

Mel-Chen created this revision.May 3 2023, 1:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2023, 1:05 AM
Herald added subscribers: hoy, hiraditya. · View Herald Transcript
Mel-Chen requested review of this revision.May 3 2023, 1:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2023, 1:05 AM
xbolva00 added inline comments.
llvm/lib/Transforms/Utils/LoopUtils.cpp
943

We have intrinsics for min and max. Do not emit cmp select form..

952

Why we dont use intrinsic in all cases?

Mel-Chen added inline comments.May 3 2023, 2:17 AM
llvm/lib/Transforms/Utils/LoopUtils.cpp
943

I am aware that we can use intrinsic to represent min max operations, but this patch aims to preserve the ability to express min max operations in select-cmp form. This is necessary for the vectorization feature I am currently developing. It is important to emphasize that this patch does not enforce the use of select-cmp form for min max operations, but rather provides an additional option.

952

Refer to D148221.
My understanding is that for fp min max there are still some FMF issues to be handled.
@RKSimon

nikic added inline comments.May 3 2023, 2:20 AM
llvm/lib/Transforms/Utils/LoopUtils.cpp
943

Why does your patch require the non-canonical select-cmp form? Please explain this in the patch description.

xbolva00 added inline comments.May 3 2023, 2:25 AM
llvm/lib/Transforms/Utils/LoopUtils.cpp
943

This is necessary for the vectorization feature I am currently developing.

Can you please share so we can check it and possibly suggest some tips?

Mel-Chen updated this revision to Diff 519026.May 3 2023, 3:35 AM

Update commit log.

Mel-Chen edited the summary of this revision. (Show Details)May 3 2023, 3:36 AM
Mel-Chen added inline comments.May 3 2023, 4:03 AM
llvm/lib/Transforms/Utils/LoopUtils.cpp
943

@nikic @xbolva00 Sure. Here is the WIP patch: D143465
In short, we are developing a new reduction pattern, min max with index, for the vectorizer. In the function fixReduction process for interleaving, we need to generate the following IR in middle.block:

middle.block:                                     ; preds = %vector.body
  ;; Start to fix minmax reduction
  %rdx.minmax.cmp = icmp sgt i64 %12, %13
  %rdx.minmax.select = select i1 %rdx.minmax.cmp, i64 %12, i64 %13
  %rdx.minmax.cmp8 = icmp sgt i64 %rdx.minmax.select, %14
  %rdx.minmax.select9 = select i1 %rdx.minmax.cmp8, i64 %rdx.minmax.select, i64 %14
  %rdx.minmax.cmp10 = icmp sgt i64 %rdx.minmax.select9, %15
  %rdx.minmax.select11 = select i1 %rdx.minmax.cmp10, i64 %rdx.minmax.select9, i64 %15
   ;; Start to fix index reduction
  %rdx.select = select i1 %rdx.minmax.cmp, i64 %20, i64 %21
  %rdx.select12 = select i1 %rdx.minmax.cmp8, i64 %rdx.select, i64 %22
  %rdx.select13 = select i1 %rdx.minmax.cmp10, i64 %rdx.select12, i64 %23

For the handling of index reduction, we require the cmp part of the min max operation. This is the reason why I need this patch.

RKSimon added inline comments.May 4 2023, 5:31 AM
llvm/lib/Transforms/Utils/LoopUtils.cpp
952

maxnum/minnum reductions are sensitive to ordering if the values might be nan

fhahn added inline comments.May 30 2023, 8:40 AM
llvm/lib/Transforms/Utils/LoopUtils.cpp
943

To clarify, is the sequence below could be expressed using intrinsics,

%rdx.minmax.cmp = icmp sgt i64 %12, %13
%rdx.minmax.select = select i1 %rdx.minmax.cmp, i64 %12, i64 %13
%rdx.minmax.cmp8 = icmp sgt i64 %rdx.minmax.select, %14
%rdx.minmax.select9 = select i1 %rdx.minmax.cmp8, i64 %rdx.minmax.select, i64 %14
%rdx.minmax.cmp10 = icmp sgt i64 %rdx.minmax.select9, %15
%rdx.minmax.select11 = select i1 %rdx.minmax.cmp10, i64 %rdx.minmax.select9, i64 %15

but you would like to re-use the compares from above for the selcets below:

 ;; Start to fix index reduction
%rdx.select = select i1 %rdx.minmax.cmp, i64 %20, i64 %21
%rdx.select12 = select i1 %rdx.minmax.cmp8, i64 %rdx.select, i64 %22
%rdx.select13 = select i1 %rdx.minmax.cmp10, i64 %rdx.select12, i64 %23

If that's the case it would probably be better to just keep the min/max select generation code local to the code that generates the 2nd chain of selects, instead of exposing this as general API

943

but you would like to re-use the compares from above for the selcets below:

@Mel-Chen is the comment above an accurate summary?

Mel-Chen added inline comments.Jun 5 2023, 2:05 AM
llvm/lib/Transforms/Utils/LoopUtils.cpp
943

@fhahn
Exactly, that's correct. I also believe there are better ways to address it, such as changing to generate the CmpInst during fixing index reduction, or generating an additional CmpInst during fixing min max reduction. Once I fix the issue you described, this patch will be abandoned. However, until it is fixed, we will temporarily rely on this patch.