Skip to content

Commit 234b4c3

Browse files
committed
[Transforms][Utils][PromoteMem2Reg] Propagate nnan flag on par with the nsz flag
Following the change introduced by the PR #83381, this patch extends it with the same treatment of the nnan fast-math flag. This is to address the performance drop caused by PR#83200 which prevented vital InstCombine transformation due to the lack of relevant fast-math flags. The PromoteMem2Reg utility is used by the SROA pass, where Phi nodes are being created. Proposed change allows propagation of the nnan flag down to these Phi nodes.
1 parent cf6ca98 commit 234b4c3

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ struct PromoteMem2Reg {
394394
/// Whether the function has the no-signed-zeros-fp-math attribute set.
395395
bool NoSignedZeros = false;
396396

397+
/// Whether the function has the no-nans-fp-math attribute set.
398+
bool NoNaNs = false;
399+
397400
public:
398401
PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
399402
AssumptionCache *AC)
@@ -752,6 +755,7 @@ void PromoteMem2Reg::run() {
752755
ForwardIDFCalculator IDF(DT);
753756

754757
NoSignedZeros = F.getFnAttribute("no-signed-zeros-fp-math").getValueAsBool();
758+
NoNaNs = F.getFnAttribute("no-nans-fp-math").getValueAsBool();
755759

756760
for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
757761
AllocaInst *AI = Allocas[AllocaNum];
@@ -1140,6 +1144,11 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
11401144
if (isa<FPMathOperator>(APN) && NoSignedZeros)
11411145
APN->setHasNoSignedZeros(true);
11421146

1147+
// This allows select instruction folding relevant to floating point
1148+
// reductions whose operand is a PHI.
1149+
if (isa<FPMathOperator>(APN) && NoNaNs)
1150+
APN->setHasNoNaNs(true);
1151+
11431152
// The currently active variable for this block is now the PHI.
11441153
IncomingVals[AllocaNo] = APN;
11451154
AllocaATInfo[AllocaNo].updateForNewPhi(APN, DIB);

llvm/test/Transforms/SROA/propagate-fast-math-flags-on-phi.ll

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,81 @@ return: ; preds = %entry,%if.then
7777
%retval = load double, ptr %x.addr
7878
ret double %retval
7979
}
80+
81+
define double @phi_with_nnan(double %x) "no-nans-fp-math"="true" {
82+
; CHECK-LABEL: define double @phi_with_nnan(
83+
; CHECK-SAME: double [[X:%.*]]) #[[ATTR0:[0-9]+]] {
84+
; CHECK-NEXT: entry:
85+
; CHECK-NEXT: [[CMP:%.*]] = fcmp olt double [[X]], 0.000000e+00
86+
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RETURN:%.*]]
87+
; CHECK: if.then:
88+
; CHECK-NEXT: [[FNEG:%.*]] = fneg double [[X]]
89+
; CHECK-NEXT: br label [[RETURN]]
90+
; CHECK: return:
91+
; CHECK-NEXT: [[X_ADDR_0:%.*]] = phi nnan double [ [[FNEG]], [[IF_THEN]] ], [ undef, [[ENTRY:%.*]] ]
92+
; CHECK-NEXT: ret double [[X_ADDR_0]]
93+
entry:
94+
%x.addr = alloca double
95+
%cmp = fcmp olt double %x, 0.0
96+
br i1 %cmp, label %if.then, label %return
97+
98+
if.then: ; preds = %entry
99+
%fneg = fneg double %x
100+
store double %fneg, ptr %x.addr
101+
br label %return
102+
103+
return: ; preds = %entry,%if.then
104+
%retval = load double, ptr %x.addr
105+
ret double %retval
106+
}
107+
108+
define <2 x double> @vector_phi_with_nnan(<2 x double> %x, i1 %cmp, <2 x double> %a, <2 x double> %b) "no-nans-fp-math"="true" {
109+
; CHECK-LABEL: define <2 x double> @vector_phi_with_nnan(
110+
; CHECK-SAME: <2 x double> [[X:%.*]], i1 [[CMP:%.*]], <2 x double> [[A:%.*]], <2 x double> [[B:%.*]]) #[[ATTR0]] {
111+
; CHECK-NEXT: entry:
112+
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RETURN:%.*]]
113+
; CHECK: if.then:
114+
; CHECK-NEXT: br label [[RETURN]]
115+
; CHECK: return:
116+
; CHECK-NEXT: [[X_ADDR_0:%.*]] = phi nnan <2 x double> [ [[B]], [[IF_THEN]] ], [ [[A]], [[ENTRY:%.*]] ]
117+
; CHECK-NEXT: ret <2 x double> [[X_ADDR_0]]
118+
entry:
119+
%x.addr = alloca <2 x double>
120+
store <2 x double> %a, ptr %x.addr
121+
br i1 %cmp, label %if.then, label %return
122+
123+
if.then: ; preds = %entry
124+
store <2 x double> %b, ptr %x.addr
125+
br label %return
126+
127+
return: ; preds = %entry,%if.then
128+
%retval = load <2 x double>, ptr %x.addr
129+
ret <2 x double> %retval
130+
}
131+
132+
define double @phi_without_nnan(double %x) "no-nans-fp-math"="false" {
133+
; CHECK-LABEL: define double @phi_without_nnan(
134+
; CHECK-SAME: double [[X:%.*]]) #[[ATTR1:[0-9]+]] {
135+
; CHECK-NEXT: entry:
136+
; CHECK-NEXT: [[CMP:%.*]] = fcmp olt double [[X]], 0.000000e+00
137+
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RETURN:%.*]]
138+
; CHECK: if.then:
139+
; CHECK-NEXT: [[FNEG:%.*]] = fneg double [[X]]
140+
; CHECK-NEXT: br label [[RETURN]]
141+
; CHECK: return:
142+
; CHECK-NEXT: [[X_ADDR_0:%.*]] = phi double [ [[FNEG]], [[IF_THEN]] ], [ undef, [[ENTRY:%.*]] ]
143+
; CHECK-NEXT: ret double [[X_ADDR_0]]
144+
entry:
145+
%x.addr = alloca double
146+
%cmp = fcmp olt double %x, 0.0
147+
br i1 %cmp, label %if.then, label %return
148+
149+
if.then: ; preds = %entry
150+
%fneg = fneg double %x
151+
store double %fneg, ptr %x.addr
152+
br label %return
153+
154+
return: ; preds = %entry,%if.then
155+
%retval = load double, ptr %x.addr
156+
ret double %retval
157+
}

0 commit comments

Comments
 (0)