DxilRemoveDiscards.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/HLSL/DxilGenerationPass.h"
  12. #include "dxc/HLSL/DxilModule.h"
  13. #include "dxc/HLSL/DxilOperations.h"
  14. #include "dxc/HLSL/DxilPIXPasses.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 { return "DXIL Remove all discard instructions"; }
  24. bool runOnModule(Module &M) override;
  25. };
  26. bool DxilRemoveDiscards::runOnModule(Module &M)
  27. {
  28. // This pass removes all instances of the discard instruction within the shader.
  29. DxilModule &DM = M.GetOrCreateDxilModule();
  30. LLVMContext & Ctx = M.getContext();
  31. OP *HlslOP = DM.GetOP();
  32. Function * DiscardFunction = HlslOP->GetOpFunc(DXIL::OpCode::Discard, Type::getVoidTy(Ctx));
  33. auto DiscardFunctionUses = DiscardFunction->uses();
  34. bool Modified = false;
  35. for (auto FI = DiscardFunctionUses.begin(); FI != DiscardFunctionUses.end(); ) {
  36. auto & FunctionUse = *FI++;
  37. auto FunctionUser = FunctionUse.getUser();
  38. auto instruction = cast<Instruction>(FunctionUser);
  39. instruction->eraseFromParent();
  40. Modified = true;
  41. }
  42. return Modified;
  43. }
  44. char DxilRemoveDiscards::ID = 0;
  45. ModulePass *llvm::createDxilRemoveDiscardsPass() {
  46. return new DxilRemoveDiscards();
  47. }
  48. INITIALIZE_PASS(DxilRemoveDiscards, "hlsl-dxil-remove-discards", "HLSL DXIL Remove all discard instructions", false, false)