Skip to content

Commit 218c46d

Browse files
committed
[Transforms][Utils][PromoteMem2Reg] Propagate nnan flag on par with the nsz flag
Following the change introduced by the PR llvm#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 92af82a commit 218c46d

File tree

2 files changed

+90
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)