Skip to content

[DirectX] Legalize Freeze instruction #136043

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 3 commits into from
Apr 17, 2025
Merged

Conversation

farzonl
Copy link
Member

@farzonl farzonl commented Apr 16, 2025

fixes #135719

LLVM 3.7 did not have a freeze instruction
Further this instruction is really only used as syntactic sugar
in LLVM's optimizer passes to not aggressively optimize things that could be undef or poison ie x*2 to x+x.
Most backends treat it as a no-op so we will do the same
by removing it and replacing its uses with its input.

farzonl added 2 commits April 16, 2025 14:46
legalize free by emulating what freeze would have done."
@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/pr-subscribers-backend-directx

Author: Farzon Lotfi (farzonl)

Changes

fixes #135719

LLVM 3.7 did not have a freeze instruction
Further this instruction is really only used as syntactic sugar
in LLVM's optimizer passes to not aggressively optimize things
that could be undef or poison ie x*2 to x+x.
Most backends treat it as a no-op so we will do the same
by removing it and replacing its uses with its input.


Full diff: https://github.com/llvm/llvm-project/pull/136043.diff

2 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXILLegalizePass.cpp (+14)
  • (added) llvm/test/CodeGen/DirectX/legalize-freeze.ll (+17)
diff --git a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
index 395311e430fbb..c133e59d2e3c6 100644
--- a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
+++ b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
@@ -8,6 +8,7 @@
 
 #include "DXILLegalizePass.h"
 #include "DirectX.h"
+#include "llvm/Analysis/ValueTracking.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/InstIterator.h"
@@ -20,6 +21,18 @@
 
 using namespace llvm;
 
+static void legalizeFreeze(Instruction &I,
+                           SmallVectorImpl<Instruction *> &ToRemove,
+                           DenseMap<Value *, Value *>) {
+  auto *FI = dyn_cast<FreezeInst>(&I);
+  if (!FI)
+    return;
+
+  Value *Input = FI->getOperand(0);
+  FI->replaceAllUsesWith(Input);
+  ToRemove.push_back(FI);
+}
+
 static void fixI8TruncUseChain(Instruction &I,
                                SmallVectorImpl<Instruction *> &ToRemove,
                                DenseMap<Value *, Value *> &ReplacedValues) {
@@ -169,6 +182,7 @@ class DXILLegalizationPipeline {
   void initializeLegalizationPipeline() {
     LegalizationPipeline.push_back(fixI8TruncUseChain);
     LegalizationPipeline.push_back(downcastI64toI32InsertExtractElements);
+    LegalizationPipeline.push_back(legalizeFreeze);
   }
 };
 
diff --git a/llvm/test/CodeGen/DirectX/legalize-freeze.ll b/llvm/test/CodeGen/DirectX/legalize-freeze.ll
new file mode 100644
index 0000000000000..29446adeef215
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/legalize-freeze.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+
+; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+
+define i32 @test_remove_freeze(i32 %x) {
+; CHECK-LABEL: define i32 @test_remove_freeze(
+; CHECK-SAME: i32 [[X:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[Y:%.*]] = add i32 [[X]], 1
+; CHECK-NEXT:    ret i32 [[Y]]
+;
+entry:
+  %f = freeze i32 %x
+  %y = add i32 %f, 1
+  ret i32 %y
+}

@farzonl farzonl merged commit 168092e into llvm:main Apr 17, 2025
10 of 12 checks passed
@damyanp damyanp moved this to Closed in HLSL Support Apr 25, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
fixes llvm#135719

LLVM 3.7 did not have a freeze instruction
Further this instruction is really only used as syntactic sugar
in LLVM's optimizer passes to not aggressively optimize things that
could be undef or poison ie x*2 to x+x.
Most backends treat it as a no-op so we will do the same
by removing it and replacing its uses with its input.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
fixes llvm#135719

LLVM 3.7 did not have a freeze instruction
Further this instruction is really only used as syntactic sugar
in LLVM's optimizer passes to not aggressively optimize things that
could be undef or poison ie x*2 to x+x.
Most backends treat it as a no-op so we will do the same
by removing it and replacing its uses with its input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

[DirectX] DXIL bitcode writer does not handle the freeze instruction
4 participants