DxilRemoveDiscards.cpp 2.3 KB

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