Skip to content

Commit 0ab5b5b

Browse files
committed
Fix denormal-fp-math flag and attribute interaction
Make these behave the same way unsafe-fp-math and co. The command line flag should add the attribute to functions that do not already have it, and leave existing attributes. The attribute is the actual implementation, but the flag is useful in some testing situations. AMDGPU has a variety of tests with denormals enabled/disabled that would require a painful level of test duplication without a flag. This doesn't expose setting the separate input/output modes, or add a flag for the f32 version yet. Tests will be included in future patch.
1 parent c579a5b commit 0ab5b5b

File tree

4 files changed

+55
-28
lines changed

4 files changed

+55
-28
lines changed

llvm/include/llvm/CodeGen/CommandFlags.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "llvm/ADT/FloatingPointMode.h"
1516
#include "llvm/ADT/StringExtras.h"
1617
#include "llvm/IR/Instructions.h"
1718
#include "llvm/IR/Intrinsics.h"
@@ -62,7 +63,7 @@ bool getEnableNoSignedZerosFPMath();
6263

6364
bool getEnableNoTrappingFPMath();
6465

65-
llvm::FPDenormal::DenormalMode getDenormalFPMath();
66+
DenormalMode::DenormalModeKind getDenormalFPMath();
6667

6768
bool getEnableHonorSignDependentRoundingFPMath();
6869

llvm/include/llvm/Target/TargetOptions.h

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
#ifndef LLVM_TARGET_TARGETOPTIONS_H
1515
#define LLVM_TARGET_TARGETOPTIONS_H
1616

17+
#include "llvm/ADT/FloatingPointMode.h"
1718
#include "llvm/MC/MCTargetOptions.h"
1819

1920
#include <memory>
2021

2122
namespace llvm {
23+
struct fltSemantics;
2224
class MachineFunction;
2325
class MemoryBuffer;
2426
class Module;
@@ -57,15 +59,6 @@ namespace llvm {
5759
};
5860
}
5961

60-
namespace FPDenormal {
61-
enum DenormalMode {
62-
IEEE, // IEEE 754 denormal numbers
63-
PreserveSign, // the sign of a flushed-to-zero number is preserved in
64-
// the sign of 0
65-
PositiveZero // denormals are flushed to positive zero
66-
};
67-
}
68-
6962
enum class BasicBlockSection {
7063
All, // Use Basic Block Sections for all basic blocks. A section
7164
// for every basic block can significantly bloat object file sizes.
@@ -134,8 +127,7 @@ namespace llvm {
134127
EmulatedTLS(false), ExplicitEmulatedTLS(false), EnableIPRA(false),
135128
EmitStackSizeSection(false), EnableMachineOutliner(false),
136129
SupportsDefaultOutlining(false), EmitAddrsig(false),
137-
EmitCallSiteInfo(false), SupportsDebugEntryValues(false),
138-
EnableDebugEntryValues(false), ForceDwarfFrameSection(false) {}
130+
FPDenormalMode(DenormalMode::IEEE, DenormalMode::IEEE) {}
139131

140132
/// PrintMachineCode - This flag is enabled when the -print-machineinstrs
141133
/// option is specified on the command line, and should enable debugging
@@ -336,9 +328,32 @@ namespace llvm {
336328
/// Which debugger to tune for.
337329
DebuggerKind DebuggerTuning = DebuggerKind::Default;
338330

339-
/// FPDenormalMode - This flags specificies which denormal numbers the code
340-
/// is permitted to require.
341-
FPDenormal::DenormalMode FPDenormalMode = FPDenormal::IEEE;
331+
private:
332+
/// Flushing mode to assume in default FP environment.
333+
DenormalMode FPDenormalMode;
334+
335+
/// Flushing mode to assume in default FP environment, for float/vector of
336+
/// float.
337+
DenormalMode FP32DenormalMode;
338+
339+
public:
340+
void setFPDenormalMode(DenormalMode Mode) {
341+
FPDenormalMode = Mode;
342+
}
343+
344+
void setFP32DenormalMode(DenormalMode Mode) {
345+
FP32DenormalMode = Mode;
346+
}
347+
348+
DenormalMode getRawFPDenormalMode() const {
349+
return FPDenormalMode;
350+
}
351+
352+
DenormalMode getRawFP32DenormalMode() const {
353+
return FP32DenormalMode;
354+
}
355+
356+
DenormalMode getDenormalMode(const fltSemantics &FPType) const;
342357

343358
/// What exception model to use
344359
ExceptionHandling ExceptionModel = ExceptionHandling::None;

llvm/lib/CodeGen/CommandFlags.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ CGOPT(bool, EnableNoInfsFPMath)
5454
CGOPT(bool, EnableNoNaNsFPMath)
5555
CGOPT(bool, EnableNoSignedZerosFPMath)
5656
CGOPT(bool, EnableNoTrappingFPMath)
57-
CGOPT(FPDenormal::DenormalMode, DenormalFPMath)
57+
CGOPT(DenormalMode::DenormalModeKind, DenormalFPMath)
5858
CGOPT(bool, EnableHonorSignDependentRoundingFPMath)
5959
CGOPT(FloatABI::ABIType, FloatABIForCalls)
6060
CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps)
@@ -212,17 +212,17 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
212212
cl::init(false));
213213
CGBINDOPT(EnableNoTrappingFPMath);
214214

215-
static cl::opt<FPDenormal::DenormalMode> DenormalFPMath(
215+
static cl::opt<DenormalMode::DenormalModeKind> DenormalFPMath(
216216
"denormal-fp-math",
217217
cl::desc(
218218
"Select which denormal numbers the code is permitted to require"),
219-
cl::init(FPDenormal::IEEE),
219+
cl::init(DenormalMode::IEEE),
220220
cl::values(
221-
clEnumValN(FPDenormal::IEEE, "ieee", "IEEE 754 denormal numbers"),
222-
clEnumValN(FPDenormal::PreserveSign, "preserve-sign",
221+
clEnumValN(DenormalMode::IEEE, "ieee", "IEEE 754 denormal numbers"),
222+
clEnumValN(DenormalMode::PreserveSign, "preserve-sign",
223223
"the sign of a flushed-to-zero number is preserved "
224224
"in the sign of 0"),
225-
clEnumValN(FPDenormal::PositiveZero, "positive-zero",
225+
clEnumValN(DenormalMode::PositiveZero, "positive-zero",
226226
"denormals are flushed to positive zero")));
227227
CGBINDOPT(DenormalFPMath);
228228

@@ -425,7 +425,12 @@ TargetOptions codegen::InitTargetOptionsFromCodeGenFlags() {
425425
Options.NoNaNsFPMath = getEnableNoNaNsFPMath();
426426
Options.NoSignedZerosFPMath = getEnableNoSignedZerosFPMath();
427427
Options.NoTrappingFPMath = getEnableNoTrappingFPMath();
428-
Options.FPDenormalMode = getDenormalFPMath();
428+
429+
DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
430+
431+
// FIXME: Should have separate input and output flags
432+
Options.setFPDenormalMode(DenormalMode(DenormKind, DenormKind));
433+
429434
Options.HonorSignDependentRoundingFPMathOption =
430435
getEnableHonorSignDependentRoundingFPMath();
431436
if (getFloatABIForCalls() != FloatABI::Default)
@@ -563,6 +568,15 @@ void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
563568
HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView, "no-nans-fp-math");
564569
HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView, "no-signed-zeros-fp-math");
565570

571+
if (DenormalFPMathView->getNumOccurrences() > 0 &&
572+
!F.hasFnAttribute("denormal-fp-math")) {
573+
DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
574+
575+
// FIXME: Command line flag should expose separate input/output modes.
576+
NewAttrs.addAttribute("denormal-fp-math",
577+
DenormalMode(DenormKind, DenormKind).str());
578+
}
579+
566580
if (TrapFuncNameView->getNumOccurrences() > 0)
567581
for (auto &B : F)
568582
for (auto &I : B)

llvm/lib/Target/ARM/ARMAsmPrinter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -652,16 +652,13 @@ void ARMAsmPrinter::emitAttributes() {
652652
}
653653

654654
// Set FP Denormals.
655-
if (checkDenormalAttributeConsistency(*MMI->getModule(),
656-
"denormal-fp-math",
657-
DenormalMode::getPreserveSign()) ||
658-
TM.Options.FPDenormalMode == FPDenormal::PreserveSign)
655+
if (checkDenormalAttributeConsistency(*MMI->getModule(), "denormal-fp-math",
656+
DenormalMode::getPreserveSign()))
659657
ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
660658
ARMBuildAttrs::PreserveFPSign);
661659
else if (checkDenormalAttributeConsistency(*MMI->getModule(),
662660
"denormal-fp-math",
663-
DenormalMode::getPositiveZero()) ||
664-
TM.Options.FPDenormalMode == FPDenormal::PositiveZero)
661+
DenormalMode::getPositiveZero()))
665662
ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
666663
ARMBuildAttrs::PositiveZero);
667664
else if (!TM.Options.UnsafeFPMath)

0 commit comments

Comments
 (0)