Skip to content

Commit c378e52

Browse files
committed
Set some fast math attributes in setFunctionAttributes
This will provide a more consistent view to codegen for these attributes. The current system is somewhat awkward, and the fields in TargetOptions are reset based on the command line flag if the attribute isn't set. By forcing these attributes with the flag, there can never be an inconsistency in the behavior if code directly inspects the attribute on the function without considering the command line flags.
1 parent b54a50f commit c378e52

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

llvm/include/llvm/CodeGen/CommandFlags.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,17 @@ LLVM_ATTRIBUTE_UNUSED static std::vector<std::string> getFeatureList() {
377377
return Features.getFeatures();
378378
}
379379

380+
static void renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) {
381+
B.addAttribute(Name, Val ? "true" : "false");
382+
}
383+
384+
#define HANDLE_BOOL_ATTR(CL, AttrName) \
385+
do { \
386+
if (CL.getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName)) \
387+
renderBoolStringAttr(NewAttrs, AttrName, CL); \
388+
} while (0)
389+
390+
380391
/// Set function attributes of function \p F based on CPU, Features, and command
381392
/// line flags.
382393
LLVM_ATTRIBUTE_UNUSED static void
@@ -415,6 +426,11 @@ setFunctionAttributes(StringRef CPU, StringRef Features, Function &F) {
415426
if (StackRealign)
416427
NewAttrs.addAttribute("stackrealign");
417428

429+
HANDLE_BOOL_ATTR(EnableUnsafeFPMath, "unsafe-fp-math");
430+
HANDLE_BOOL_ATTR(EnableNoInfsFPMath, "no-infs-fp-math");
431+
HANDLE_BOOL_ATTR(EnableNoNaNsFPMath, "no-nans-fp-math");
432+
HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMath, "no-signed-zeros-fp-math");
433+
418434
if (TrapFuncName.getNumOccurrences() > 0)
419435
for (auto &B : F)
420436
for (auto &I : B)

llvm/lib/Target/TargetMachine.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ bool TargetMachine::isPositionIndependent() const {
4646
}
4747

4848
/// Reset the target options based on the function's attributes.
49+
/// setFunctionAttributes should have made the raw attribute value consistent
50+
/// with the command line flag if used.
51+
//
4952
// FIXME: This function needs to go away for a number of reasons:
5053
// a) global state on the TargetMachine is terrible in general,
5154
// b) these target options should be passed only on the function
5255
// and not on the TargetMachine (via TargetOptions) at all.
5356
void TargetMachine::resetTargetOptions(const Function &F) const {
54-
#define RESET_OPTION(X, Y) \
55-
do { \
56-
if (F.hasFnAttribute(Y)) \
57-
Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
58-
else \
59-
Options.X = DefaultOptions.X; \
57+
#define RESET_OPTION(X, Y) \
58+
do { \
59+
Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
6060
} while (0)
6161

6262
RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");

0 commit comments

Comments
 (0)