DxilRemoveDiscards.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilRemoveDiscards.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides a pass to remove all instances of the discard instruction //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/DXIL/DxilModule.h"
  12. #include "dxc/DXIL/DxilOperations.h"
  13. #include "dxc/DxilPIXPasses/DxilPIXPasses.h"
  14. #include "dxc/HLSL/DxilGenerationPass.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/PassManager.h"
  17. using namespace llvm;
  18. using namespace hlsl;
  19. class DxilRemoveDiscards : public ModulePass {
  20. public:
  21. static char ID; // Pass identification, replacement for typeid
  22. explicit DxilRemoveDiscards() : ModulePass(ID) {}
  23. const char *getPassName() const override {
  24. return "DXIL Remove all discard instructions";
  25. }
  26. bool runOnModule(Module &M) override;
  27. };
  28. bool DxilRemoveDiscards::runOnModule(Module &M) {
  29. // This pass removes all instances of the discard instruction within the
  30. // shader.
  31. DxilModule &DM = M.GetOrCreateDxilModule();
  32. LLVMContext &Ctx = M.getContext();
  33. OP *HlslOP = DM.GetOP();
  34. Function *DiscardFunction =
  35. HlslOP->GetOpFunc(DXIL::OpCode::Discard, Type::getVoidTy(Ctx));
  36. auto DiscardFunctionUses = DiscardFunction->uses();
  37. bool Modified = false;
  38. for (auto FI = DiscardFunctionUses.begin();
  39. FI != DiscardFunctionUses.end();) {
  40. auto &FunctionUse = *FI++;
  41. auto FunctionUser = FunctionUse.getUser();
  42. auto instruction = cast<Instruction>(FunctionUser);
  43. instruction->eraseFromParent();
  44. Modified = true;
  45. }
  46. return Modified;
  47. }
  48. char DxilRemoveDiscards::ID = 0;
  49. ModulePass *llvm::createDxilRemoveDiscardsPass() {
  50. return new DxilRemoveDiscards();
  51. }
  52. INITIALIZE_PASS(DxilRemoveDiscards, "hlsl-dxil-remove-discards",
  53. "HLSL DXIL Remove all discard instructions", false, false)