Skip to content

Commit 077f8fb

Browse files
author
Justin Lebar
committed
[NVPTX] Move getDivF32Level, usePrecSqrtF32, and useF32FTZ into out of DAGToDAG and into TargetLowering.
Summary: DADToDAG has access to TargetLowering, but not vice versa, so this is the more general location for these functions. NFC Reviewers: tra Subscribers: jholewinski, llvm-commits Differential Revision: https://reviews.llvm.org/D28795 llvm-svn: 292693
1 parent 8b18a34 commit 077f8fb

File tree

3 files changed

+75
-46
lines changed

3 files changed

+75
-46
lines changed

llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@ using namespace llvm;
2626

2727
#define DEBUG_TYPE "nvptx-isel"
2828

29-
static cl::opt<int> UsePrecDivF32(
30-
"nvptx-prec-divf32", cl::ZeroOrMore, cl::Hidden,
31-
cl::desc("NVPTX Specifies: 0 use div.approx, 1 use div.full, 2 use"
32-
" IEEE Compliant F32 div.rnd if available."),
33-
cl::init(2));
34-
35-
static cl::opt<bool>
36-
UsePrecSqrtF32("nvptx-prec-sqrtf32", cl::Hidden,
37-
cl::desc("NVPTX Specific: 0 use sqrt.approx, 1 use sqrt.rn."),
38-
cl::init(true));
39-
40-
static cl::opt<bool>
41-
FtzEnabled("nvptx-f32ftz", cl::ZeroOrMore, cl::Hidden,
42-
cl::desc("NVPTX Specific: Flush f32 subnormals to sign-preserving zero."),
43-
cl::init(false));
44-
4529
/// createNVPTXISelDag - This pass converts a legalized DAG into a
4630
/// NVPTX-specific DAG, ready for instruction scheduling.
4731
FunctionPass *llvm::createNVPTXISelDag(NVPTXTargetMachine &TM,
@@ -56,45 +40,20 @@ NVPTXDAGToDAGISel::NVPTXDAGToDAGISel(NVPTXTargetMachine &tm,
5640
}
5741

5842
bool NVPTXDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
59-
Subtarget = &static_cast<const NVPTXSubtarget &>(MF.getSubtarget());
60-
return SelectionDAGISel::runOnMachineFunction(MF);
43+
Subtarget = &static_cast<const NVPTXSubtarget &>(MF.getSubtarget());
44+
return SelectionDAGISel::runOnMachineFunction(MF);
6145
}
6246

6347
int NVPTXDAGToDAGISel::getDivF32Level() const {
64-
if (UsePrecDivF32.getNumOccurrences() > 0) {
65-
// If nvptx-prec-div32=N is used on the command-line, always honor it
66-
return UsePrecDivF32;
67-
} else {
68-
// Otherwise, use div.approx if fast math is enabled
69-
if (TM.Options.UnsafeFPMath)
70-
return 0;
71-
else
72-
return 2;
73-
}
48+
return Subtarget->getTargetLowering()->getDivF32Level();
7449
}
7550

7651
bool NVPTXDAGToDAGISel::usePrecSqrtF32() const {
77-
if (UsePrecSqrtF32.getNumOccurrences() > 0) {
78-
// If nvptx-prec-sqrtf32 is used on the command-line, always honor it
79-
return UsePrecSqrtF32;
80-
} else {
81-
// Otherwise, use sqrt.approx if fast math is enabled
82-
return !TM.Options.UnsafeFPMath;
83-
}
52+
return Subtarget->getTargetLowering()->usePrecSqrtF32();
8453
}
8554

8655
bool NVPTXDAGToDAGISel::useF32FTZ() const {
87-
if (FtzEnabled.getNumOccurrences() > 0) {
88-
// If nvptx-f32ftz is used on the command-line, always honor it
89-
return FtzEnabled;
90-
} else {
91-
const Function *F = MF->getFunction();
92-
// Otherwise, check for an nvptx-f32ftz attribute on the function
93-
if (F->hasFnAttribute("nvptx-f32ftz"))
94-
return F->getFnAttribute("nvptx-f32ftz").getValueAsString() == "true";
95-
else
96-
return false;
97-
}
56+
return Subtarget->getTargetLowering()->useF32FTZ(*MF);
9857
}
9958

10059
bool NVPTXDAGToDAGISel::allowFMA() const {

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,60 @@ FMAContractLevelOpt("nvptx-fma-level", cl::ZeroOrMore, cl::Hidden,
7979
" 1: do it 2: do it aggressively"),
8080
cl::init(2));
8181

82+
static cl::opt<int> UsePrecDivF32(
83+
"nvptx-prec-divf32", cl::ZeroOrMore, cl::Hidden,
84+
cl::desc("NVPTX Specifies: 0 use div.approx, 1 use div.full, 2 use"
85+
" IEEE Compliant F32 div.rnd if available."),
86+
cl::init(2));
87+
88+
static cl::opt<bool> UsePrecSqrtF32(
89+
"nvptx-prec-sqrtf32", cl::Hidden,
90+
cl::desc("NVPTX Specific: 0 use sqrt.approx, 1 use sqrt.rn."),
91+
cl::init(true));
92+
93+
static cl::opt<bool> FtzEnabled(
94+
"nvptx-f32ftz", cl::ZeroOrMore, cl::Hidden,
95+
cl::desc("NVPTX Specific: Flush f32 subnormals to sign-preserving zero."),
96+
cl::init(false));
97+
98+
int NVPTXTargetLowering::getDivF32Level() const {
99+
if (UsePrecDivF32.getNumOccurrences() > 0) {
100+
// If nvptx-prec-div32=N is used on the command-line, always honor it
101+
return UsePrecDivF32;
102+
} else {
103+
// Otherwise, use div.approx if fast math is enabled
104+
if (getTargetMachine().Options.UnsafeFPMath)
105+
return 0;
106+
else
107+
return 2;
108+
}
109+
}
110+
111+
bool NVPTXTargetLowering::usePrecSqrtF32() const {
112+
if (UsePrecSqrtF32.getNumOccurrences() > 0) {
113+
// If nvptx-prec-sqrtf32 is used on the command-line, always honor it
114+
return UsePrecSqrtF32;
115+
} else {
116+
// Otherwise, use sqrt.approx if fast math is enabled
117+
return !getTargetMachine().Options.UnsafeFPMath;
118+
}
119+
}
120+
121+
bool NVPTXTargetLowering::useF32FTZ(const MachineFunction &MF) const {
122+
// TODO: Get rid of this flag; there can be only one way to do this.
123+
if (FtzEnabled.getNumOccurrences() > 0) {
124+
// If nvptx-f32ftz is used on the command-line, always honor it
125+
return FtzEnabled;
126+
} else {
127+
const Function *F = MF.getFunction();
128+
// Otherwise, check for an nvptx-f32ftz attribute on the function
129+
if (F->hasFnAttribute("nvptx-f32ftz"))
130+
return F->getFnAttribute("nvptx-f32ftz").getValueAsString() == "true";
131+
else
132+
return false;
133+
}
134+
}
135+
82136
static bool IsPTXVectorType(MVT VT) {
83137
switch (VT.SimpleTy) {
84138
default:

llvm/lib/Target/NVPTX/NVPTXISelLowering.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,22 @@ class NVPTXTargetLowering : public TargetLowering {
510510
TargetLoweringBase::LegalizeTypeAction
511511
getPreferredVectorAction(EVT VT) const override;
512512

513+
// Get the degree of precision we want from 32-bit floating point division
514+
// operations.
515+
//
516+
// 0 - Use ptx div.approx
517+
// 1 - Use ptx.div.full (approximate, but less so than div.approx)
518+
// 2 - Use IEEE-compliant div instructions, if available.
519+
int getDivF32Level() const;
520+
521+
// Get whether we should use a precise or approximate 32-bit floating point
522+
// sqrt instruction.
523+
bool usePrecSqrtF32() const;
524+
525+
// Get whether we should use instructions that flush floating-point denormals
526+
// to sign-preserving zero.
527+
bool useF32FTZ(const MachineFunction &MF) const;
528+
513529
bool allowFMA(MachineFunction &MF, CodeGenOpt::Level OptLevel) const;
514530
bool allowUnsafeFPMath(MachineFunction &MF) const;
515531

0 commit comments

Comments
 (0)