[llvm] [HLSL] Move FNeg legalization to the DXILLegalization pass (PR #140942)
Sumit Agarwal via llvm-commits
llvm-commits at lists.llvm.org
Wed May 21 10:56:19 PDT 2025
https://github.com/sumitsays created https://github.com/llvm/llvm-project/pull/140942
This solves #137685
>From a3fb01d8043604435ed8a0fe66e3362731494e61 Mon Sep 17 00:00:00 2001
From: Sumit Agarwal <sumitagarwal at microsoft.com>
Date: Wed, 21 May 2025 10:43:08 -0700
Subject: [PATCH] Move fneg to fsub to DXILLegalizePass
---
llvm/lib/Target/DirectX/DXILLegalizePass.cpp | 15 +++++++++++++
llvm/lib/Target/DirectX/DXILPrepare.cpp | 9 --------
llvm/test/CodeGen/DirectX/fneg-conversion.ll | 16 --------------
llvm/test/CodeGen/DirectX/legalize-fneg.ll | 23 ++++++++++++++++++++
4 files changed, 38 insertions(+), 25 deletions(-)
delete mode 100644 llvm/test/CodeGen/DirectX/fneg-conversion.ll
create mode 100644 llvm/test/CodeGen/DirectX/legalize-fneg.ll
diff --git a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
index be77a70fa46ba..48267830aebef 100644
--- a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
+++ b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
@@ -317,6 +317,20 @@ static void removeMemSet(Instruction &I,
ToRemove.push_back(CI);
}
+static void updateFnegToFsub(Instruction &I,
+ SmallVectorImpl<Instruction *> &ToRemove,
+ DenseMap<Value *, Value *> &ReplacedValues) {
+ 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 {
@@ -349,6 +363,7 @@ class DXILLegalizationPipeline {
LegalizationPipeline.push_back(downcastI64toI32InsertExtractElements);
LegalizationPipeline.push_back(legalizeFreeze);
LegalizationPipeline.push_back(removeMemSet);
+ LegalizationPipeline.push_back(updateFnegToFsub);
}
};
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index e9a05a7b90aca..e0068787f5e5a 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -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)) {
diff --git a/llvm/test/CodeGen/DirectX/fneg-conversion.ll b/llvm/test/CodeGen/DirectX/fneg-conversion.ll
deleted file mode 100644
index 3acf4790de4b1..0000000000000
--- a/llvm/test/CodeGen/DirectX/fneg-conversion.ll
+++ /dev/null
@@ -1,16 +0,0 @@
-; RUN: llc %s --filetype=asm -o - | FileCheck %s
-target triple = "dxil-unknown-shadermodel6.7-library"
-
-define float @negateF(float %0) #0 {
-; CHECK: %2 = fsub float -0.000000e+00, %0
- %2 = fneg float %0
- ret float %2
-}
-
-define double @negateD(double %0) #0 {
-; CHECK: %2 = fsub double -0.000000e+00, %0
- %2 = fneg double %0
- ret double %2
-}
-
-attributes #0 = { convergent norecurse nounwind "hlsl.export"}
\ No newline at end of file
diff --git a/llvm/test/CodeGen/DirectX/legalize-fneg.ll b/llvm/test/CodeGen/DirectX/legalize-fneg.ll
new file mode 100644
index 0000000000000..996cbbfc59e0f
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/legalize-fneg.ll
@@ -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
+}
\ No newline at end of file
More information about the llvm-commits
mailing list