Skip to content

Commit 7b24dd7

Browse files
committedAug 6, 2019
[Strict FP] Allow custom operation actions
This patch changes the DAG legalizer to respect the operation actions set by the target for strict floating-point operations. (Currently, the legalizer will usually fall back to mutate to the non-strict action (which is assumed to be legal), and only skip mutation if the strict operation is marked legal.) With this patch, if whenever a strict operation is marked as Legal or Custom, it is passed to the target as usual. Only if it is marked as Expand will the legalizer attempt to mutate to the non-strict operation. Note that this will now fail if the non-strict operation is itself marked as Custom -- the target will have to provide a Custom definition for the strict operation then as well. Reviewed By: hfinkel Differential Revision: https://reviews.llvm.org/D65226 llvm-svn: 368012
1 parent cb4327d commit 7b24dd7

File tree

5 files changed

+670
-256
lines changed

5 files changed

+670
-256
lines changed
 

‎llvm/include/llvm/CodeGen/TargetLowering.h

+3-8
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ class TargetLoweringBase {
930930
return Supported ? Action : Expand;
931931
}
932932

933+
// If Op is a strict floating-point operation, return the result
934+
// of getOperationAction for the equivalent non-strict operation.
933935
LegalizeAction getStrictFPOperationAction(unsigned Op, EVT VT) const {
934936
unsigned EqOpc;
935937
switch (Op) {
@@ -962,14 +964,7 @@ class TargetLoweringBase {
962964
case ISD::STRICT_FP_EXTEND: EqOpc = ISD::FP_EXTEND; break;
963965
}
964966

965-
auto Action = getOperationAction(EqOpc, VT);
966-
967-
// We don't currently handle Custom or Promote for strict FP pseudo-ops.
968-
// For now, we just expand for those cases.
969-
if (Action != Legal)
970-
Action = Expand;
971-
972-
return Action;
967+
return getOperationAction(EqOpc, VT);
973968
}
974969

975970
/// Return true if the specified operation is legal on this target or can be

‎llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

+24-33
Original file line numberDiff line numberDiff line change
@@ -1097,39 +1097,6 @@ void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
10971097
return;
10981098
}
10991099
break;
1100-
case ISD::STRICT_FADD:
1101-
case ISD::STRICT_FSUB:
1102-
case ISD::STRICT_FMUL:
1103-
case ISD::STRICT_FDIV:
1104-
case ISD::STRICT_FREM:
1105-
case ISD::STRICT_FSQRT:
1106-
case ISD::STRICT_FMA:
1107-
case ISD::STRICT_FPOW:
1108-
case ISD::STRICT_FPOWI:
1109-
case ISD::STRICT_FSIN:
1110-
case ISD::STRICT_FCOS:
1111-
case ISD::STRICT_FEXP:
1112-
case ISD::STRICT_FEXP2:
1113-
case ISD::STRICT_FLOG:
1114-
case ISD::STRICT_FLOG10:
1115-
case ISD::STRICT_FLOG2:
1116-
case ISD::STRICT_FRINT:
1117-
case ISD::STRICT_FNEARBYINT:
1118-
case ISD::STRICT_FMAXNUM:
1119-
case ISD::STRICT_FMINNUM:
1120-
case ISD::STRICT_FCEIL:
1121-
case ISD::STRICT_FFLOOR:
1122-
case ISD::STRICT_FROUND:
1123-
case ISD::STRICT_FTRUNC:
1124-
case ISD::STRICT_FP_ROUND:
1125-
case ISD::STRICT_FP_EXTEND:
1126-
// These pseudo-ops get legalized as if they were their non-strict
1127-
// equivalent. For instance, if ISD::FSQRT is legal then ISD::STRICT_FSQRT
1128-
// is also legal, but if ISD::FSQRT requires expansion then so does
1129-
// ISD::STRICT_FSQRT.
1130-
Action = TLI.getStrictFPOperationAction(Node->getOpcode(),
1131-
Node->getValueType(0));
1132-
break;
11331100
case ISD::SADDSAT:
11341101
case ISD::UADDSAT:
11351102
case ISD::SSUBSAT:
@@ -2815,6 +2782,12 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
28152782
break;
28162783
}
28172784
case ISD::STRICT_FP_ROUND:
2785+
// This expansion does not honor the "strict" properties anyway,
2786+
// so prefer falling back to the non-strict operation if legal.
2787+
if (TLI.getStrictFPOperationAction(Node->getOpcode(),
2788+
Node->getValueType(0))
2789+
== TargetLowering::Legal)
2790+
break;
28182791
Tmp1 = EmitStackConvert(Node->getOperand(1),
28192792
Node->getValueType(0),
28202793
Node->getValueType(0), dl, Node->getOperand(0));
@@ -2829,6 +2802,12 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
28292802
Results.push_back(Tmp1);
28302803
break;
28312804
case ISD::STRICT_FP_EXTEND:
2805+
// This expansion does not honor the "strict" properties anyway,
2806+
// so prefer falling back to the non-strict operation if legal.
2807+
if (TLI.getStrictFPOperationAction(Node->getOpcode(),
2808+
Node->getValueType(0))
2809+
== TargetLowering::Legal)
2810+
break;
28322811
Tmp1 = EmitStackConvert(Node->getOperand(1),
28332812
Node->getOperand(1).getValueType(),
28342813
Node->getValueType(0), dl, Node->getOperand(0));
@@ -3715,6 +3694,18 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
37153694
break;
37163695
}
37173696

3697+
if (Results.empty() && Node->isStrictFPOpcode()) {
3698+
// FIXME: We were asked to expand a strict floating-point operation,
3699+
// but there is currently no expansion implemented that would preserve
3700+
// the "strict" properties. For now, we just fall back to the non-strict
3701+
// version if that is legal on the target. The actual mutation of the
3702+
// operation will happen in SelectionDAGISel::DoInstructionSelection.
3703+
if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3704+
Node->getValueType(0))
3705+
== TargetLowering::Legal)
3706+
return true;
3707+
}
3708+
37183709
// Replace the original node with the legalized result.
37193710
if (Results.empty()) {
37203711
LLVM_DEBUG(dbgs() << "Cannot expand node\n");

‎llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,23 @@ SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
335335
case ISD::STRICT_FTRUNC:
336336
case ISD::STRICT_FP_ROUND:
337337
case ISD::STRICT_FP_EXTEND:
338-
// These pseudo-ops get legalized as if they were their non-strict
339-
// equivalent. For instance, if ISD::FSQRT is legal then ISD::STRICT_FSQRT
340-
// is also legal, but if ISD::FSQRT requires expansion then so does
341-
// ISD::STRICT_FSQRT.
342-
Action = TLI.getStrictFPOperationAction(Node->getOpcode(),
343-
Node->getValueType(0));
338+
Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
339+
// If we're asked to expand a strict vector floating-point operation,
340+
// by default we're going to simply unroll it. That is usually the
341+
// best approach, except in the case where the resulting strict (scalar)
342+
// operations would themselves use the fallback mutation to non-strict.
343+
// In that specific case, just do the fallback on the vector op.
344+
if (Action == TargetLowering::Expand &&
345+
TLI.getStrictFPOperationAction(Node->getOpcode(),
346+
Node->getValueType(0))
347+
== TargetLowering::Legal) {
348+
EVT EltVT = Node->getValueType(0).getVectorElementType();
349+
if (TLI.getOperationAction(Node->getOpcode(), EltVT)
350+
== TargetLowering::Expand &&
351+
TLI.getStrictFPOperationAction(Node->getOpcode(), EltVT)
352+
== TargetLowering::Legal)
353+
Action = TargetLowering::Legal;
354+
}
344355
break;
345356
case ISD::ADD:
346357
case ISD::SUB:

0 commit comments

Comments
 (0)
Please sign in to comment.