Skip to content

[DirectX] Address PR comments to #131221 #131706

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
Mar 21, 2025
Merged

Conversation

@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-backend-directx

Author: Farzon Lotfi (farzonl)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXILLegalizePass.cpp (+20-35)
diff --git a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
index f9a494ce63dd3..317bff40caf7e 100644
--- a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
+++ b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
@@ -5,12 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===---------------------------------------------------------------------===//
-//===---------------------------------------------------------------------===//
-///
-/// \file This file contains a pass to remove i8 truncations and i64 extract
-/// and insert elements.
-///
-//===----------------------------------------------------------------------===//
+
 #include "DXILLegalizePass.h"
 #include "DirectX.h"
 #include "llvm/IR/Function.h"
@@ -20,31 +15,27 @@
 #include "llvm/Pass.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include <functional>
-#include <map>
-#include <stack>
-#include <vector>
 
 #define DEBUG_TYPE "dxil-legalize"
 
 using namespace llvm;
 namespace {
 
-static void fixI8TruncUseChain(Instruction &I,
-                               std::stack<Instruction *> &ToRemove,
-                               std::map<Value *, Value *> &ReplacedValues) {
+void fixI8TruncUseChain(Instruction &I, SmallVector<Instruction *> &ToRemove,
+                        DenseMap<Value *, Value *> &ReplacedValues) {
 
   auto *Cmp = dyn_cast<CmpInst>(&I);
 
   if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
     if (Trunc->getDestTy()->isIntegerTy(8)) {
       ReplacedValues[Trunc] = Trunc->getOperand(0);
-      ToRemove.push(Trunc);
+      ToRemove.push_back(Trunc);
     }
   } else if (I.getType()->isIntegerTy(8) ||
              (Cmp && Cmp->getOperand(0)->getType()->isIntegerTy(8))) {
     IRBuilder<> Builder(&I);
 
-    std::vector<Value *> NewOperands;
+    SmallVector<Value *> NewOperands;
     Type *InstrType = IntegerType::get(I.getContext(), 32);
     for (unsigned OpIdx = 0; OpIdx < I.getNumOperands(); ++OpIdx) {
       Value *Op = I.getOperand(OpIdx);
@@ -88,20 +79,19 @@ static void fixI8TruncUseChain(Instruction &I,
 
     if (NewInst) {
       ReplacedValues[&I] = NewInst;
-      ToRemove.push(&I);
+      ToRemove.push_back(&I);
     }
   } else if (auto *Cast = dyn_cast<CastInst>(&I)) {
     if (Cast->getSrcTy()->isIntegerTy(8)) {
-      ToRemove.push(Cast);
+      ToRemove.push_back(Cast);
       Cast->replaceAllUsesWith(ReplacedValues[Cast->getOperand(0)]);
     }
   }
 }
 
-static void
-downcastI64toI32InsertExtractElements(Instruction &I,
-                                      std::stack<Instruction *> &ToRemove,
-                                      std::map<Value *, Value *> &) {
+void downcastI64toI32InsertExtractElements(Instruction &I,
+                                           SmallVector<Instruction *> &ToRemove,
+                                           DenseMap<Value *, Value *> &) {
 
   if (auto *Extract = dyn_cast<ExtractElementInst>(&I)) {
     Value *Idx = Extract->getIndexOperand();
@@ -115,7 +105,7 @@ downcastI64toI32InsertExtractElements(Instruction &I,
           Extract->getVectorOperand(), Idx32, Extract->getName());
 
       Extract->replaceAllUsesWith(NewExtract);
-      ToRemove.push(Extract);
+      ToRemove.push_back(Extract);
     }
   }
 
@@ -132,7 +122,7 @@ downcastI64toI32InsertExtractElements(Instruction &I,
           Insert->getName());
 
       Insert->replaceAllUsesWith(Insert32Index);
-      ToRemove.push(Insert);
+      ToRemove.push_back(Insert);
     }
   }
 }
@@ -143,27 +133,22 @@ class DXILLegalizationPipeline {
   DXILLegalizationPipeline() { initializeLegalizationPipeline(); }
 
   bool runLegalizationPipeline(Function &F) {
-    std::stack<Instruction *> ToRemove;
-    std::map<Value *, Value *> ReplacedValues;
+    SmallVector<Instruction *> ToRemove;
+    DenseMap<Value *, Value *> ReplacedValues;
     for (auto &I : instructions(F)) {
-      for (auto &LegalizationFn : LegalizationPipeline) {
+      for (auto &LegalizationFn : LegalizationPipeline)
         LegalizationFn(I, ToRemove, ReplacedValues);
-      }
     }
-    bool MadeChanges = !ToRemove.empty();
 
-    while (!ToRemove.empty()) {
-      Instruction *I = ToRemove.top();
-      I->eraseFromParent();
-      ToRemove.pop();
-    }
+    for (auto *Inst : reverse(ToRemove))
+      Inst->eraseFromParent();
 
-    return MadeChanges;
+    return !ToRemove.empty();
   }
 
 private:
-  std::vector<std::function<void(Instruction &, std::stack<Instruction *> &,
-                                 std::map<Value *, Value *> &)>>
+  SmallVector<std::function<void(Instruction &, SmallVector<Instruction *> &,
+                                 DenseMap<Value *, Value *> &)>>
       LegalizationPipeline;
 
   void initializeLegalizationPipeline() {

static void fixI8TruncUseChain(Instruction &I,
std::stack<Instruction *> &ToRemove,
std::map<Value *, Value *> &ReplacedValues) {
void fixI8TruncUseChain(Instruction &I, SmallVector<Instruction *> &ToRemove,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you missed the intent here. These functions should be static, the namespace should be restricted to just class definitions:

...we have a simple guideline: make anonymous namespaces as small as possible, and only use them for class declarations.

source: https://llvm.org/docs/CodingStandards.html#restrict-visibility

static void fixI8TruncUseChain(Instruction &I,
std::stack<Instruction *> &ToRemove,
std::map<Value *, Value *> &ReplacedValues) {
void fixI8TruncUseChain(Instruction &I, SmallVector<Instruction *> &ToRemove,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For function arguments SmallVectorImpl rather than SmallVector.

@farzonl farzonl moved this to Needs Review in HLSL Support Mar 19, 2025
@farzonl farzonl self-assigned this Mar 19, 2025
if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
if (Trunc->getDestTy()->isIntegerTy(8)) {
ReplacedValues[Trunc] = Trunc->getOperand(0);
ToRemove.push_back(Trunc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would make sense to return here - none of the following code is possible if the instruction is a trunc.

if (OBO->hasNoSignedWrap())
cast<BinaryOperator>(NewInst)->setHasNoSignedWrap();
if (OBO->hasNoUnsignedWrap())
cast<BinaryOperator>(NewInst)->setHasNoUnsignedWrap();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I return here i need to do

     ReplacedValues[&I] = NewInst;
    ToRemove.push_back(&I);

twice. once for cmp and once for BinaryOperator

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess im already doing that for the trunc case.

@farzonl farzonl merged commit 8d825cb into llvm:main Mar 21, 2025
12 checks passed
@damyanp damyanp moved this from Needs Review to Closed in HLSL Support Apr 22, 2025
@damyanp damyanp removed this from HLSL Support Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants