Skip to content

Commit 37a57ca

Browse files
authored
[FMF] Set all bits if needed when setting individual flags. (#131321)
Currently fast() won't return true if all flags are set via setXXX, which is surprising. Update setters to set all bits if needed to make sure isFast() consistently returns the expected result. PR: #131321
1 parent 5f449b9 commit 37a57ca

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

llvm/include/llvm/IR/FMF.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ class FastMathFlags {
2323

2424
unsigned Flags = 0;
2525

26-
FastMathFlags(unsigned F) {
27-
// If all 7 bits are set, turn this into -1. If the number of bits grows,
28-
// this must be updated. This is intended to provide some forward binary
29-
// compatibility insurance for the meaning of 'fast' in case bits are added.
30-
if (F == 0x7F) Flags = ~0U;
31-
else Flags = F;
32-
}
26+
FastMathFlags(unsigned F) : Flags(F) {}
3327

3428
public:
3529
// This is how the bits are used in Value::SubclassOptionalData so they
@@ -43,9 +37,12 @@ class FastMathFlags {
4337
NoSignedZeros = (1 << 3),
4438
AllowReciprocal = (1 << 4),
4539
AllowContract = (1 << 5),
46-
ApproxFunc = (1 << 6)
40+
ApproxFunc = (1 << 6),
41+
FlagEnd = (1 << 7)
4742
};
4843

44+
constexpr static unsigned AllFlagsMask = FlagEnd - 1;
45+
4946
FastMathFlags() = default;
5047

5148
static FastMathFlags getFast() {
@@ -56,10 +53,10 @@ class FastMathFlags {
5653

5754
bool any() const { return Flags != 0; }
5855
bool none() const { return Flags == 0; }
59-
bool all() const { return Flags == ~0U; }
56+
bool all() const { return Flags == AllFlagsMask; }
6057

6158
void clear() { Flags = 0; }
62-
void set() { Flags = ~0U; }
59+
void set() { Flags = AllFlagsMask; }
6360

6461
/// Flag queries
6562
bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }

llvm/test/Transforms/LoopVectorize/AArch64/widen-call-with-intrinsic-or-libfunc.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ target triple = "arm64-apple-ios"
2626
; CHECK-NEXT: vp<[[VEC_PTR:%.+]]> = vector-pointer ir<%gep.src>
2727
; CHECK-NEXT: WIDEN ir<%l> = load vp<[[VEC_PTR]]>
2828
; CHECK-NEXT: WIDEN-CAST ir<%conv> = fpext ir<%l> to double
29-
; CHECK-NEXT: WIDEN-CALL ir<%s> = call reassoc nnan ninf nsz arcp contract afn @llvm.sin.f64(ir<%conv>) (using library function: __simd_sin_v2f64)
29+
; CHECK-NEXT: WIDEN-CALL ir<%s> = call fast @llvm.sin.f64(ir<%conv>) (using library function: __simd_sin_v2f64)
3030
; CHECK-NEXT: REPLICATE ir<%gep.dst> = getelementptr inbounds ir<%dst>, vp<[[STEPS]]>
3131
; CHECK-NEXT: REPLICATE store ir<%s>, ir<%gep.dst>
3232
; CHECK-NEXT: EMIT vp<[[CAN_IV_NEXT:%.+]]> = add nuw vp<[[CAN_IV]]>, vp<[[VFxUF]]>
@@ -72,7 +72,7 @@ target triple = "arm64-apple-ios"
7272
; CHECK-NEXT: vp<[[VEC_PTR:%.+]]> = vector-pointer ir<%gep.src>
7373
; CHECK-NEXT: WIDEN ir<%l> = load vp<[[VEC_PTR]]>
7474
; CHECK-NEXT: WIDEN-CAST ir<%conv> = fpext ir<%l> to double
75-
; CHECK-NEXT: WIDEN-INTRINSIC ir<%s> = call reassoc nnan ninf nsz arcp contract afn llvm.sin(ir<%conv>)
75+
; CHECK-NEXT: WIDEN-INTRINSIC ir<%s> = call fast llvm.sin(ir<%conv>)
7676
; CHECK-NEXT: REPLICATE ir<%gep.dst> = getelementptr inbounds ir<%dst>, vp<[[STEPS]]>
7777
; CHECK-NEXT: REPLICATE store ir<%s>, ir<%gep.dst>
7878
; CHECK-NEXT: EMIT vp<[[CAN_IV_NEXT:%.+]]> = add nuw vp<[[CAN_IV]]>, vp<[[VFxUF]]>

llvm/test/Transforms/LoopVectorize/vplan-printing.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ define void @print_fast_math_flags(i64 %n, ptr noalias %y, ptr noalias %x, ptr %
800800
; CHECK-NEXT: vp<[[VEC_PTR:%.+]]> = vector-pointer ir<%gep.y>
801801
; CHECK-NEXT: WIDEN ir<%lv> = load vp<[[VEC_PTR]]>
802802
; CHECK-NEXT: WIDEN ir<%add> = fadd nnan ir<%lv>, ir<1.000000e+00>
803-
; CHECK-NEXT: WIDEN ir<%mul> = fmul reassoc nnan ninf nsz arcp contract afn ir<%add>, ir<2.000000e+00>
803+
; CHECK-NEXT: WIDEN ir<%mul> = fmul fast ir<%add>, ir<2.000000e+00>
804804
; CHECK-NEXT: WIDEN ir<%div> = fdiv reassoc nsz contract ir<%mul>, ir<2.000000e+00>
805805
; CHECK-NEXT: CLONE ir<%gep.x> = getelementptr inbounds ir<%x>, vp<[[STEPS]]>
806806
; CHECK-NEXT: vp<[[VEC_PTR:%.+]]> = vector-pointer ir<%gep.x>
@@ -1224,8 +1224,8 @@ define void @print_select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, pt
12241224
; CHECK-NEXT: vp<[[PTR2:%.+]]> = vector-pointer ir<[[GEP2]]>
12251225
; CHECK-NEXT: WIDEN ir<[[LD2:%.+]]> = load vp<[[PTR2]]>
12261226
; CHECK-NEXT: WIDEN ir<[[FCMP:%.+]]> = fcmp ogt ir<[[LD1]]>, ir<[[LD2]]>
1227-
; CHECK-NEXT: WIDEN ir<[[FADD:%.+]]> = fadd reassoc nnan ninf nsz arcp contract afn ir<[[LD1]]>, ir<1.000000e+01>
1228-
; CHECK-NEXT: WIDEN-SELECT ir<[[SELECT:%.+]]> = select reassoc nnan ninf nsz arcp contract afn ir<[[FCMP]]>, ir<[[FADD]]>, ir<[[LD2]]>
1227+
; CHECK-NEXT: WIDEN ir<[[FADD:%.+]]> = fadd fast ir<[[LD1]]>, ir<1.000000e+01>
1228+
; CHECK-NEXT: WIDEN-SELECT ir<[[SELECT:%.+]]> = select fast ir<[[FCMP]]>, ir<[[FADD]]>, ir<[[LD2]]>
12291229
; CHECK-NEXT: CLONE ir<[[GEP3:%.+]]> = getelementptr inbounds nuw ir<%a>, vp<[[ST]]>
12301230
; CHECK-NEXT: vp<[[PTR3:%.+]]> = vector-pointer ir<[[GEP3]]>
12311231
; CHECK-NEXT: WIDEN store vp<[[PTR3]]>, ir<[[SELECT]]>

0 commit comments

Comments
 (0)