Skip to content

[RISCV] Transform fcmp to is.fpclass #120242

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/Local.h"

using namespace llvm;

Expand Down Expand Up @@ -58,6 +60,7 @@ class RISCVCodeGenPrepare : public FunctionPass,
bool visitAnd(BinaryOperator &BO);
bool visitIntrinsicInst(IntrinsicInst &I);
bool expandVPStrideLoad(IntrinsicInst &I);
bool visitFCmpInst(FCmpInst &I);
};

} // end anonymous namespace
Expand Down Expand Up @@ -196,6 +199,42 @@ bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &II) {
return true;
}

// The 'fcmp uno/ord/oeq/une/ueq/one/ogt/oge/olt/ole x, 0.0' instructions are
// equivalent to an FP class test. If the fcmp instruction would be custom
Comment on lines +202 to +203
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't true depending on denormal handling and fp exceptions

// lowered or lowered to a libcall, use the is.fpclass intrinsic instead, which
// is lowered by the back-end without a libcall.
//
// This basically reverts the transformations of
// InstCombinerImpl::foldIntrinsicIsFPClass.
bool RISCVCodeGenPrepare::visitFCmpInst(FCmpInst &Fcmp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

CodeGenPrepare already has this transform

const auto *TLI = ST->getTargetLowering();
const EVT VT = TLI->getValueType(*DL, Fcmp.getOperand(0)->getType());
const int ISDOpcode = TLI->InstructionOpcodeToISD(Fcmp.getOpcode());

auto LegalizeTypeAction = TLI->getTypeAction(Fcmp.getContext(), VT);
auto OperationAction = TLI->getOperationAction(ISDOpcode, VT);
if ((LegalizeTypeAction != TargetLoweringBase::TypeSoftenFloat &&
LegalizeTypeAction != TargetLoweringBase::TypeSoftPromoteHalf) ||
OperationAction == TargetLowering::Custom)
return false;
Comment on lines +214 to +219
Copy link
Contributor

Choose a reason for hiding this comment

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

This level of logic really belongs directly in the legalizer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I understand. Moving it to the legalizer. @topperc is it ok to implement it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually the logic in CodeGenPrepare uses TargetLoweringBase::isFAbsFree. When I started to implement this, I was wondering if there should be a similar function for FCmp, and the whole thing should go into CodegenPrepare instead. Is this a valid approach? Or the right place to do it is in the legalizer.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really like having it in codegenprepare in the first place. It really belongs in some combination of DAGCombiner or legalizer, depending on the purpose. The only nice thing is codegenprepare has access to better utilities, like an existing fcmpToClassTest helper and computeKnownFPClass. In principle those should be reimplemented in codegen

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. Regarding the InstCombinerImpl::foldIntrinsicIsFPClass. For a back-end, where lowering the fcmp is not cheap, why is it profitable to do this transformation? It is done unconditionally as far as I see it.

Copy link
Contributor

Choose a reason for hiding this comment

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

fcmp is a better canonical form. More code will always understand fcmp than is.fpclass.

fcmp is not cheap

This is certainly not universally true, and I would say is not the common case. If the target wants something else, that's for the backend to undo for its preferred form.


auto [ClassVal, ClassTest] =
fcmpToClassTest(Fcmp.getPredicate(), *Fcmp.getParent()->getParent(),
Fcmp.getOperand(0), Fcmp.getOperand(1));

// FIXME: For some conditions (e.g ole, olt, oge, ogt) the output is quite
// verbose compared to the libcall. Should we do the tranformation
// only if we are optimizing for speed?
if (!ClassVal)
return false;

IRBuilder<> Builder(&Fcmp);
Value *IsFPClass = Builder.createIsFPClass(ClassVal, ClassTest);
Fcmp.replaceAllUsesWith(IsFPClass);
RecursivelyDeleteTriviallyDeadInstructions(&Fcmp);
return true;
}

bool RISCVCodeGenPrepare::runOnFunction(Function &F) {
if (skipFunction(F))
return false;
Expand Down
Loading
Loading