Skip to content

Commit b936112

Browse files
authored
[HLSL] Move FNeg legalization to the DXILLegalization pass (#140942)
Fixes #137685
1 parent 0ee40ca commit b936112

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,20 @@ static void removeMemSet(Instruction &I,
405405
ToRemove.push_back(CI);
406406
}
407407

408+
static void updateFnegToFsub(Instruction &I,
409+
SmallVectorImpl<Instruction *> &ToRemove,
410+
DenseMap<Value *, Value *> &) {
411+
const Intrinsic::ID ID = I.getOpcode();
412+
if (ID != Instruction::FNeg)
413+
return;
414+
415+
IRBuilder<> Builder(&I);
416+
Value *In = I.getOperand(0);
417+
Value *Zero = ConstantFP::get(In->getType(), -0.0);
418+
I.replaceAllUsesWith(Builder.CreateFSub(Zero, In));
419+
ToRemove.push_back(&I);
420+
}
421+
408422
namespace {
409423
class DXILLegalizationPipeline {
410424

@@ -438,6 +452,7 @@ class DXILLegalizationPipeline {
438452
LegalizationPipeline.push_back(legalizeFreeze);
439453
LegalizationPipeline.push_back(legalizeMemCpy);
440454
LegalizationPipeline.push_back(removeMemSet);
455+
LegalizationPipeline.push_back(updateFnegToFsub);
441456
}
442457
};
443458

llvm/lib/Target/DirectX/DXILPrepare.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,6 @@ class DXILPrepareModule : public ModulePass {
204204

205205
I.dropUnknownNonDebugMetadata(DXILCompatibleMDs);
206206

207-
if (I.getOpcode() == Instruction::FNeg) {
208-
Builder.SetInsertPoint(&I);
209-
Value *In = I.getOperand(0);
210-
Value *Zero = ConstantFP::get(In->getType(), -0.0);
211-
I.replaceAllUsesWith(Builder.CreateFSub(Zero, In));
212-
I.eraseFromParent();
213-
continue;
214-
}
215-
216207
// Emtting NoOp bitcast instructions allows the ValueEnumerator to be
217208
// unmodified as it reserves instruction IDs during contruction.
218209
if (auto LI = dyn_cast<LoadInst>(&I)) {

llvm/test/CodeGen/DirectX/fneg-conversion.ll

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
2+
3+
define float @negateF(float %x) {
4+
; CHECK-LABEL: define float @negateF(
5+
; CHECK-SAME: float [[X:%.*]]) {
6+
; CHECK-NEXT: [[ENTRY:.*:]]
7+
; CHECK-NEXT: [[Y:%.*]] = fsub float -0.000000e+00, [[X]]
8+
; CHECK-NEXT: ret float [[Y]]
9+
entry:
10+
%y = fneg float %x
11+
ret float %y
12+
}
13+
14+
define double @negateD(double %x) {
15+
; CHECK-LABEL: define double @negateD(
16+
; CHECK-SAME: double [[X:%.*]]) {
17+
; CHECK-NEXT: [[ENTRY:.*:]]
18+
; CHECK-NEXT: [[Y:%.*]] = fsub double -0.000000e+00, [[X]]
19+
; CHECK-NEXT: ret double [[Y]]
20+
entry:
21+
%y = fneg double %x
22+
ret double %y
23+
}

0 commit comments

Comments
 (0)