Skip to content

[HLSL] Move FNeg legalization to the DXILLegalization pass #140942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions llvm/lib/Target/DirectX/DXILLegalizePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,20 @@ static void removeMemSet(Instruction &I,
ToRemove.push_back(CI);
}

static void updateFnegToFsub(Instruction &I,
SmallVectorImpl<Instruction *> &ToRemove,
DenseMap<Value *, Value *> &) {
const Intrinsic::ID ID = I.getOpcode();
if (ID != Instruction::FNeg)
return;

IRBuilder<> Builder(&I);
Value *In = I.getOperand(0);
Value *Zero = ConstantFP::get(In->getType(), -0.0);
I.replaceAllUsesWith(Builder.CreateFSub(Zero, In));
ToRemove.push_back(&I);
}

namespace {
class DXILLegalizationPipeline {

Expand Down Expand Up @@ -438,6 +452,7 @@ class DXILLegalizationPipeline {
LegalizationPipeline.push_back(legalizeFreeze);
LegalizationPipeline.push_back(legalizeMemCpy);
LegalizationPipeline.push_back(removeMemSet);
LegalizationPipeline.push_back(updateFnegToFsub);
}
};

Expand Down
9 changes: 0 additions & 9 deletions llvm/lib/Target/DirectX/DXILPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,6 @@ class DXILPrepareModule : public ModulePass {

I.dropUnknownNonDebugMetadata(DXILCompatibleMDs);

if (I.getOpcode() == Instruction::FNeg) {
Builder.SetInsertPoint(&I);
Value *In = I.getOperand(0);
Value *Zero = ConstantFP::get(In->getType(), -0.0);
I.replaceAllUsesWith(Builder.CreateFSub(Zero, In));
I.eraseFromParent();
continue;
}

// Emtting NoOp bitcast instructions allows the ValueEnumerator to be
// unmodified as it reserves instruction IDs during contruction.
if (auto LI = dyn_cast<LoadInst>(&I)) {
Expand Down
16 changes: 0 additions & 16 deletions llvm/test/CodeGen/DirectX/fneg-conversion.ll

This file was deleted.

23 changes: 23 additions & 0 deletions llvm/test/CodeGen/DirectX/legalize-fneg.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s

define float @negateF(float %x) {
; CHECK-LABEL: define float @negateF(
; CHECK-SAME: float [[X:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[Y:%.*]] = fsub float -0.000000e+00, [[X]]
; CHECK-NEXT: ret float [[Y]]
entry:
%y = fneg float %x
ret float %y
}

define double @negateD(double %x) {
; CHECK-LABEL: define double @negateD(
; CHECK-SAME: double [[X:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[Y:%.*]] = fsub double -0.000000e+00, [[X]]
; CHECK-NEXT: ret double [[Y]]
entry:
%y = fneg double %x
ret double %y
}
Loading