llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: Aiden Grossman (boomanaiden154)

<details>
<summary>Changes</summary>

Standard NewPM pass porting.


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


4 Files Affected:

- (modified) llvm/lib/Target/DirectX/DXILPrepare.cpp (+175-151) 
- (modified) llvm/lib/Target/DirectX/DirectX.h (+9-2) 
- (modified) llvm/lib/Target/DirectX/DirectXPassRegistry.def (+1) 
- (modified) llvm/lib/Target/DirectX/DirectXTargetMachine.cpp (+2-2) 


``````````diff
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp 
b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index f31635eff13d73..7ed8cdd8286f67 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -22,10 +22,12 @@
 #include "llvm/Analysis/DXILMetadataAnalysis.h"
 #include "llvm/Analysis/DXILResource.h"
 #include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/Analysis.h"
 #include "llvm/IR/AttributeMask.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/VersionTuple.h"
@@ -68,177 +70,199 @@ static void removeStringFunctionAttributes(Function &F,
   F.removeRetAttrs(DeadAttrs);
 }
 
-class DXILPrepareModule : public ModulePass {
-
-  static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
-                                     PointerTypeMap &PointerTypes,
-                                     Instruction &Inst, Value *Operand,
-                                     Type *Ty) {
-    // Omit bitcasts if the incoming value matches the instruction type.
-    auto It = PointerTypes.find(Operand);
-    if (It != PointerTypes.end()) {
-      auto *OpTy = cast<TypedPointerType>(It->second)->getElementType();
-      if (OpTy == Ty)
-        return nullptr;
-    }
+class DXILPrepareModuleLegacy : public ModulePass {
+public:
+  DXILPrepareModuleLegacy() : ModulePass(ID) {}
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.addRequired<DXILMetadataAnalysisWrapperPass>();
 
-    Type *ValTy = Operand->getType();
-    // Also omit the bitcast for matching global array types
-    if (auto *GlobalVar = dyn_cast<GlobalVariable>(Operand))
-      ValTy = GlobalVar->getValueType();
+    AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
+    AU.addPreserved<DXILResourceWrapperPass>();
+    AU.addPreserved<RootSignatureAnalysisWrapper>();
+    AU.addPreserved<SignatureAnalysisWrapper>();
+    AU.addPreserved<ShaderFlagsAnalysisWrapper>();
+  }
+  bool runOnModule(Module &M) override;
+  static char ID; // Pass identification.
+};
+char DXILPrepareModuleLegacy::ID = 0;
 
-    if (auto *AI = dyn_cast<AllocaInst>(Operand))
-      ValTy = AI->getAllocatedType();
+} // end anonymous namespace
 
-    if (auto *ArrTy = dyn_cast<ArrayType>(ValTy)) {
-      Type *ElTy = ArrTy->getElementType();
-      if (ElTy == Ty)
-        return nullptr;
-    }
+static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
+                                   PointerTypeMap &PointerTypes,
+                                   Instruction &Inst, Value *Operand,
+                                   Type *Ty) {
+  // Omit bitcasts if the incoming value matches the instruction type.
+  auto It = PointerTypes.find(Operand);
+  if (It != PointerTypes.end()) {
+    auto *OpTy = cast<TypedPointerType>(It->second)->getElementType();
+    if (OpTy == Ty)
+      return nullptr;
+  }
 
-    // finally, drill down GEP instructions until we get the array
-    // that is being accessed, and compare element types
-    if (ConstantExpr *GEPInstr = dyn_cast<ConstantExpr>(Operand)) {
-      while (GEPInstr->getOpcode() == Instruction::GetElementPtr) {
-        Value *OpArg = GEPInstr->getOperand(0);
-        if (ConstantExpr *NewGEPInstr = dyn_cast<ConstantExpr>(OpArg)) {
-          GEPInstr = NewGEPInstr;
-          continue;
-        }
+  Type *ValTy = Operand->getType();
+  // Also omit the bitcast for matching global array types
+  if (auto *GlobalVar = dyn_cast<GlobalVariable>(Operand))
+    ValTy = GlobalVar->getValueType();
 
-        if (auto *GlobalVar = dyn_cast<GlobalVariable>(OpArg))
-          ValTy = GlobalVar->getValueType();
-        if (auto *AI = dyn_cast<AllocaInst>(Operand))
-          ValTy = AI->getAllocatedType();
-        if (auto *ArrTy = dyn_cast<ArrayType>(ValTy)) {
-          Type *ElTy = ArrTy->getElementType();
-          if (ElTy == Ty)
-            return nullptr;
-        }
-        break;
+  if (auto *AI = dyn_cast<AllocaInst>(Operand))
+    ValTy = AI->getAllocatedType();
+
+  if (auto *ArrTy = dyn_cast<ArrayType>(ValTy)) {
+    Type *ElTy = ArrTy->getElementType();
+    if (ElTy == Ty)
+      return nullptr;
+  }
+
+  // finally, drill down GEP instructions until we get the array
+  // that is being accessed, and compare element types
+  if (ConstantExpr *GEPInstr = dyn_cast<ConstantExpr>(Operand)) {
+    while (GEPInstr->getOpcode() == Instruction::GetElementPtr) {
+      Value *OpArg = GEPInstr->getOperand(0);
+      if (ConstantExpr *NewGEPInstr = dyn_cast<ConstantExpr>(OpArg)) {
+        GEPInstr = NewGEPInstr;
+        continue;
       }
-    }
 
-    // Insert bitcasts where we are removing the instruction.
-    Builder.SetInsertPoint(&Inst);
-    // This code only gets hit in opaque-pointer mode, so the type of the
-    // pointer doesn't matter.
-    PointerType *PtrTy = cast<PointerType>(Operand->getType());
-    return Builder.Insert(
-        CastInst::Create(Instruction::BitCast, Operand,
-                         Builder.getPtrTy(PtrTy->getAddressSpace())));
+      if (auto *GlobalVar = dyn_cast<GlobalVariable>(OpArg))
+        ValTy = GlobalVar->getValueType();
+      if (auto *AI = dyn_cast<AllocaInst>(Operand))
+        ValTy = AI->getAllocatedType();
+      if (auto *ArrTy = dyn_cast<ArrayType>(ValTy)) {
+        Type *ElTy = ArrTy->getElementType();
+        if (ElTy == Ty)
+          return nullptr;
+      }
+      break;
+    }
   }
 
-public:
-  bool runOnModule(Module &M) override {
-    PointerTypeMap PointerTypes = PointerTypeAnalysis::run(M);
-    const AttributeMask &AttrMask = getNonDXILAttributeMask();
-
-    const dxil::ModuleMetadataInfo MetadataInfo =
-        getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
-    VersionTuple ValVer = MetadataInfo.ValidatorVersion;
-    bool AllowExperimental = ValVer.getMajor() == 0 && ValVer.getMinor() == 0;
-
-    for (auto &F : M.functions()) {
-      F.removeFnAttrs(AttrMask);
-      F.removeRetAttrs(AttrMask);
-      // Only remove string attributes if we are not skipping validation.
-      // This will reserve the experimental attributes when validation version
-      // is 0.0 for experiment mode.
-      removeStringFunctionAttributes(F, AllowExperimental);
-      for (size_t Idx = 0, End = F.arg_size(); Idx < End; ++Idx)
-        F.removeParamAttrs(Idx, AttrMask);
-
-      for (auto &BB : F) {
-        IRBuilder<> Builder(&BB);
-        for (auto &I : make_early_inc_range(BB)) {
-
-          if (auto *CB = dyn_cast<CallBase>(&I)) {
-            CB->removeFnAttrs(AttrMask);
-            CB->removeRetAttrs(AttrMask);
-            for (size_t Idx = 0, End = CB->arg_size(); Idx < End; ++Idx)
-              CB->removeParamAttrs(Idx, AttrMask);
-            continue;
-          }
+  // Insert bitcasts where we are removing the instruction.
+  Builder.SetInsertPoint(&Inst);
+  // This code only gets hit in opaque-pointer mode, so the type of the
+  // pointer doesn't matter.
+  PointerType *PtrTy = cast<PointerType>(Operand->getType());
+  return Builder.Insert(
+      CastInst::Create(Instruction::BitCast, Operand,
+                       Builder.getPtrTy(PtrTy->getAddressSpace())));
+}
 
-          // Emtting NoOp bitcast instructions allows the ValueEnumerator to be
-          // unmodified as it reserves instruction IDs during contruction.
-          if (auto *LI = dyn_cast<LoadInst>(&I)) {
-            if (Value *NoOpBitcast = maybeGenerateBitcast(
-                    Builder, PointerTypes, I, LI->getPointerOperand(),
-                    LI->getType())) {
-              LI->replaceAllUsesWith(
-                  Builder.CreateLoad(LI->getType(), NoOpBitcast));
-              LI->eraseFromParent();
-            }
-            continue;
-          }
-          if (auto *SI = dyn_cast<StoreInst>(&I)) {
-            if (Value *NoOpBitcast = maybeGenerateBitcast(
-                    Builder, PointerTypes, I, SI->getPointerOperand(),
-                    SI->getValueOperand()->getType())) {
-
-              SI->replaceAllUsesWith(
-                  Builder.CreateStore(SI->getValueOperand(), NoOpBitcast));
-              SI->eraseFromParent();
-            }
-            continue;
-          }
-          if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
-            if (Value *NoOpBitcast = maybeGenerateBitcast(
-                    Builder, PointerTypes, I, GEP->getPointerOperand(),
-                    GEP->getSourceElementType()))
-              GEP->setOperand(0, NoOpBitcast);
-            continue;
-          }
-          // An atomic on a float allocation exchanges the bit pattern as an
-          // integer, so the pointer element type does not match the value
-          // type. Typed pointers need a cast to keep the two in agreement.
-          if (auto *RMW = dyn_cast<AtomicRMWInst>(&I)) {
-            if (Value *NoOpBitcast = maybeGenerateBitcast(
-                    Builder, PointerTypes, I, RMW->getPointerOperand(),
-                    RMW->getValOperand()->getType()))
-              RMW->setOperand(AtomicRMWInst::getPointerOperandIndex(),
-                              NoOpBitcast);
-            continue;
+static bool prepareModule(
+    Module &M,
+    function_ref<dxil::ModuleMetadataInfo &()> GetModuleMetadataInfo) {
+  PointerTypeMap PointerTypes = PointerTypeAnalysis::run(M);
+  const AttributeMask &AttrMask = getNonDXILAttributeMask();
+
+  const dxil::ModuleMetadataInfo MetadataInfo = GetModuleMetadataInfo();
+  VersionTuple ValVer = MetadataInfo.ValidatorVersion;
+  bool AllowExperimental = ValVer.getMajor() == 0 && ValVer.getMinor() == 0;
+
+  for (auto &F : M.functions()) {
+    F.removeFnAttrs(AttrMask);
+    F.removeRetAttrs(AttrMask);
+    // Only remove string attributes if we are not skipping validation.
+    // This will reserve the experimental attributes when validation version
+    // is 0.0 for experiment mode.
+    removeStringFunctionAttributes(F, AllowExperimental);
+    for (size_t Idx = 0, End = F.arg_size(); Idx < End; ++Idx)
+      F.removeParamAttrs(Idx, AttrMask);
+
+    for (auto &BB : F) {
+      IRBuilder<> Builder(&BB);
+      for (auto &I : make_early_inc_range(BB)) {
+
+        if (auto *CB = dyn_cast<CallBase>(&I)) {
+          CB->removeFnAttrs(AttrMask);
+          CB->removeRetAttrs(AttrMask);
+          for (size_t Idx = 0, End = CB->arg_size(); Idx < End; ++Idx)
+            CB->removeParamAttrs(Idx, AttrMask);
+          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)) {
+          if (Value *NoOpBitcast = maybeGenerateBitcast(
+                  Builder, PointerTypes, I, LI->getPointerOperand(),
+                  LI->getType())) {
+            LI->replaceAllUsesWith(
+                Builder.CreateLoad(LI->getType(), NoOpBitcast));
+            LI->eraseFromParent();
           }
-          if (auto *CmpXchg = dyn_cast<AtomicCmpXchgInst>(&I)) {
-            if (Value *NoOpBitcast = maybeGenerateBitcast(
-                    Builder, PointerTypes, I, CmpXchg->getPointerOperand(),
-                    CmpXchg->getNewValOperand()->getType()))
-              CmpXchg->setOperand(AtomicCmpXchgInst::getPointerOperandIndex(),
-                                  NoOpBitcast);
-            continue;
+          continue;
+        }
+        if (auto *SI = dyn_cast<StoreInst>(&I)) {
+          if (Value *NoOpBitcast = maybeGenerateBitcast(
+                  Builder, PointerTypes, I, SI->getPointerOperand(),
+                  SI->getValueOperand()->getType())) {
+
+            SI->replaceAllUsesWith(
+                Builder.CreateStore(SI->getValueOperand(), NoOpBitcast));
+            SI->eraseFromParent();
           }
+          continue;
+        }
+        if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
+          if (Value *NoOpBitcast = maybeGenerateBitcast(
+                  Builder, PointerTypes, I, GEP->getPointerOperand(),
+                  GEP->getSourceElementType()))
+            GEP->setOperand(0, NoOpBitcast);
+          continue;
+        }
+        // An atomic on a float allocation exchanges the bit pattern as an
+        // integer, so the pointer element type does not match the value
+        // type. Typed pointers need a cast to keep the two in agreement.
+        if (auto *RMW = dyn_cast<AtomicRMWInst>(&I)) {
+          if (Value *NoOpBitcast = maybeGenerateBitcast(
+                  Builder, PointerTypes, I, RMW->getPointerOperand(),
+                  RMW->getValOperand()->getType()))
+            RMW->setOperand(AtomicRMWInst::getPointerOperandIndex(),
+                            NoOpBitcast);
+          continue;
+        }
+        if (auto *CmpXchg = dyn_cast<AtomicCmpXchgInst>(&I)) {
+          if (Value *NoOpBitcast = maybeGenerateBitcast(
+                  Builder, PointerTypes, I, CmpXchg->getPointerOperand(),
+                  CmpXchg->getNewValOperand()->getType()))
+            CmpXchg->setOperand(AtomicCmpXchgInst::getPointerOperandIndex(),
+                                NoOpBitcast);
+          continue;
         }
       }
     }
-
-    return true;
   }
 
-  DXILPrepareModule() : ModulePass(ID) {}
-  void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.addRequired<DXILMetadataAnalysisWrapperPass>();
+  return true;
+}
 
-    AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
-    AU.addPreserved<DXILResourceWrapperPass>();
-    AU.addPreserved<RootSignatureAnalysisWrapper>();
-    AU.addPreserved<SignatureAnalysisWrapper>();
-    AU.addPreserved<ShaderFlagsAnalysisWrapper>();
-  }
-  static char ID; // Pass identification.
-};
-char DXILPrepareModule::ID = 0;
+bool DXILPrepareModuleLegacy::runOnModule(Module &M) {
+  return prepareModule(M, [&]() -> dxil::ModuleMetadataInfo & {
+    return getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
+  });
+}
 
-} // end anonymous namespace
+PreservedAnalyses DXILPrepareModulePass::run(Module &M,
+                                             ModuleAnalysisManager &MAM) {
+  bool Changed = prepareModule(M, [&]() -> dxil::ModuleMetadataInfo & {
+    return MAM.getResult<DXILMetadataAnalysis>(M);
+  });
+  if (!Changed)
+    return PreservedAnalyses::all();
+  return PreservedAnalyses::none()
+      .preserve<DXILMetadataAnalysis>()
+      .preserve<DXILResourceAnalysis>()
+      .preserve<RootSignatureAnalysis>()
+      .preserve<SignatureAnalysis>()
+      .preserve<ShaderFlagsAnalysis>();
+}
 
-INITIALIZE_PASS_BEGIN(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module",
-                      false, false)
+INITIALIZE_PASS_BEGIN(DXILPrepareModuleLegacy, DEBUG_TYPE,
+                      "DXIL Prepare Module", false, false)
 INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
-INITIALIZE_PASS_END(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module", 
false,
-                    false)
+INITIALIZE_PASS_END(DXILPrepareModuleLegacy, DEBUG_TYPE, "DXIL Prepare Module",
+                    false, false)
 
-ModulePass *llvm::createDXILPrepareModulePass() {
-  return new DXILPrepareModule();
+ModulePass *llvm::createDXILPrepareModuleLegacyPass() {
+  return new DXILPrepareModuleLegacy();
 }
diff --git a/llvm/lib/Target/DirectX/DirectX.h 
b/llvm/lib/Target/DirectX/DirectX.h
index a619929fa16e27..537681a6d1d916 100644
--- a/llvm/lib/Target/DirectX/DirectX.h
+++ b/llvm/lib/Target/DirectX/DirectX.h
@@ -11,6 +11,7 @@
 #ifndef LLVM_LIB_TARGET_DIRECTX_DIRECTX_H
 #define LLVM_LIB_TARGET_DIRECTX_DIRECTX_H
 
+#include "llvm/IR/Analysis.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Support/CommandLine.h"
 
@@ -34,10 +35,16 @@ void initializeWriteDXILPassPass(PassRegistry &);
 void initializeEmbedDXILPassPass(PassRegistry &);
 
 /// Initializer for DXIL-prepare
-void initializeDXILPrepareModulePass(PassRegistry &);
+void initializeDXILPrepareModuleLegacyPass(PassRegistry &);
 
 /// Pass to convert modules into DXIL-compatable modules
-ModulePass *createDXILPrepareModulePass();
+class DXILPrepareModulePass
+    : public RequiredPassInfoMixin<DXILPrepareModulePass> {
+public:
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
+};
+
+ModulePass *createDXILPrepareModuleLegacyPass();
 
 /// Pass to downgrade debug information to forms supported by DXIL.
 class DXILDebugInfoPass : public OptionalPassInfoMixin<DXILDebugInfoPass> {
diff --git a/llvm/lib/Target/DirectX/DirectXPassRegistry.def 
b/llvm/lib/Target/DirectX/DirectXPassRegistry.def
index 222f19a3a4afb7..6e140e5a8f8f5f 100644
--- a/llvm/lib/Target/DirectX/DirectXPassRegistry.def
+++ b/llvm/lib/Target/DirectX/DirectXPassRegistry.def
@@ -32,6 +32,7 @@ MODULE_PASS("dxil-debug-info", DXILDebugInfoPass())
 MODULE_PASS("dxil-intrinsic-expansion", DXILIntrinsicExpansionPass())
 MODULE_PASS("dxil-mem-intrinsics", DXILMemIntrinsicsPass())
 MODULE_PASS("dxil-op-lower", DXILOpLoweringPass())
+MODULE_PASS("dxil-prepare-module", DXILPrepareModulePass())
 MODULE_PASS("dxil-pretty-printer", DXILPrettyPrinterPass(dbgs()))
 MODULE_PASS("dxil-translate-metadata", DXILTranslateMetadataPass())
 MODULE_PASS("dxil-resource-implicit-binding", 
DXILResourceImplicitBindingPass())
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp 
b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index f3f8814766362e..433f4ba81ffd0a 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -55,7 +55,7 @@ LLVMInitializeDirectXTarget() {
   initializeDXILFlattenArraysLegacyPass(*PR);
   initializeScalarizerLegacyPassPass(*PR);
   initializeDXILLegalizeLegacyPass(*PR);
-  initializeDXILPrepareModulePass(*PR);
+  initializeDXILPrepareModuleLegacyPass(*PR);
   initializeEmbedDXILPassPass(*PR);
   initializeWriteDXILPassPass(*PR);
   initializeDXContainerGlobalsPass(*PR);
@@ -134,7 +134,7 @@ class DirectXPassConfig : public TargetPassConfig {
     addPass(createDXILTranslateMetadataLegacyPass());
     addPass(createDXILPostOptimizationValidationLegacyPass());
     addPass(createDXILOpLoweringLegacyPass());
-    addPass(createDXILPrepareModulePass());
+    addPass(createDXILPrepareModuleLegacyPass());
     addPass(createDXILDebugInfoLegacyPass());
   }
 };

``````````

</details>


https://github.com/llvm/llvm-project/pull/238156
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to