DxilUtil.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilUtil.h //
  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. // DXIL helper functions. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include <unordered_set>
  13. #include <string>
  14. #include <memory>
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/IR/Constants.h"
  18. namespace llvm {
  19. class Type;
  20. class GlobalVariable;
  21. class Function;
  22. class Module;
  23. class MemoryBuffer;
  24. class LLVMContext;
  25. class DiagnosticInfo;
  26. class Value;
  27. class Instruction;
  28. class CallInst;
  29. class BasicBlock;
  30. class raw_ostream;
  31. class ModulePass;
  32. class PassRegistry;
  33. class DebugInfoFinder;
  34. class DebugLoc;
  35. class DIGlobalVariable;
  36. ModulePass *createDxilLoadMetadataPass();
  37. void initializeDxilLoadMetadataPass(llvm::PassRegistry&);
  38. }
  39. namespace hlsl {
  40. class DxilFieldAnnotation;
  41. class DxilModule;
  42. class DxilTypeSystem;
  43. class OP;
  44. namespace dxilutil {
  45. extern const char ManglingPrefix[];
  46. extern const char EntryPrefix[];
  47. extern const char *kResourceMapErrorMsg;
  48. unsigned
  49. GetLegacyCBufferFieldElementSize(DxilFieldAnnotation &fieldAnnotation,
  50. llvm::Type *Ty, DxilTypeSystem &typeSys);
  51. llvm::Type *GetArrayEltTy(llvm::Type *Ty);
  52. bool HasDynamicIndexing(llvm::Value *V);
  53. // Find alloca insertion point, given instruction
  54. llvm::Instruction *FindAllocaInsertionPt(llvm::Instruction* I); // Considers entire parent function
  55. llvm::Instruction *FindAllocaInsertionPt(llvm::BasicBlock* BB); // Only considers provided block
  56. llvm::Instruction *FindAllocaInsertionPt(llvm::Function* F);
  57. llvm::Instruction *SkipAllocas(llvm::Instruction *I);
  58. // Get first non-alloca insertion point, to avoid inserting non-allocas before alloca
  59. llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Instruction* I); // Considers entire parent function
  60. llvm::Instruction *FirstNonAllocaInsertionPt(llvm::BasicBlock* BB); // Only considers provided block
  61. llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Function* F);
  62. bool IsStaticGlobal(llvm::GlobalVariable *GV);
  63. bool IsSharedMemoryGlobal(llvm::GlobalVariable *GV);
  64. bool RemoveUnusedFunctions(llvm::Module &M, llvm::Function *EntryFunc,
  65. llvm::Function *PatchConstantFunc, bool IsLib);
  66. llvm::DIGlobalVariable *FindGlobalVariableDebugInfo(llvm::GlobalVariable *GV,
  67. llvm::DebugInfoFinder &DbgInfoFinder);
  68. void EmitErrorOnInstruction(llvm::Instruction *I, llvm::Twine Msg);
  69. void EmitWarningOnInstruction(llvm::Instruction *I, llvm::Twine Msg);
  70. void EmitErrorOnFunction(llvm::Function *F, llvm::Twine Msg);
  71. void EmitWarningOnFunction(llvm::Function *F, llvm::Twine Msg);
  72. void EmitErrorOnGlobalVariable(llvm::GlobalVariable *GV, llvm::Twine Msg);
  73. void EmitWarningOnGlobalVariable(llvm::GlobalVariable *GV, llvm::Twine Msg);
  74. void EmitResMappingError(llvm::Instruction *Res);
  75. std::string FormatMessageAtLocation(const llvm::DebugLoc &DL, const llvm::Twine& Msg);
  76. llvm::Twine FormatMessageWithoutLocation(const llvm::Twine& Msg);
  77. // Simple demangle just support case "\01?name@" pattern.
  78. llvm::StringRef DemangleFunctionName(llvm::StringRef name);
  79. // ReplaceFunctionName replaces the undecorated portion of originalName with undecorated newName
  80. std::string ReplaceFunctionName(llvm::StringRef originalName, llvm::StringRef newName);
  81. void PrintEscapedString(llvm::StringRef Name, llvm::raw_ostream &Out);
  82. void PrintUnescapedString(llvm::StringRef Name, llvm::raw_ostream &Out);
  83. // Change select/phi on operands into select/phi on operation.
  84. // phi0 = phi a0, b0, c0
  85. // phi1 = phi a1, b1, c1
  86. // Inst = Add(phi0, phi1);
  87. // into
  88. // A = Add(a0, a1);
  89. // B = Add(b0, b1);
  90. // C = Add(c0, c1);
  91. // NewInst = phi A, B, C
  92. // Only support 1 operand now, other oerands should be Constant.
  93. llvm::Value * SelectOnOperation(llvm::Instruction *Inst, unsigned operandIdx);
  94. // Collect all select operand used by Inst.
  95. void CollectSelect(llvm::Instruction *Inst,
  96. std::unordered_set<llvm::Instruction *> &selectSet);
  97. // If all operands are the same for a select inst, replace it with the operand.
  98. // Returns replacement value if successful
  99. llvm::Value *MergeSelectOnSameValue(llvm::Instruction *SelInst,
  100. unsigned startOpIdx,
  101. unsigned numOperands);
  102. bool SimplifyTrivialPHIs(llvm::BasicBlock *BB);
  103. void MigrateDebugValue(llvm::Value *Old, llvm::Value *New);
  104. void TryScatterDebugValueToVectorElements(llvm::Value *Val);
  105. std::unique_ptr<llvm::Module> LoadModuleFromBitcode(llvm::StringRef BC,
  106. llvm::LLVMContext &Ctx, std::string &DiagStr);
  107. std::unique_ptr<llvm::Module> LoadModuleFromBitcode(llvm::MemoryBuffer *MB,
  108. llvm::LLVMContext &Ctx, std::string &DiagStr);
  109. std::unique_ptr<llvm::Module> LoadModuleFromBitcodeLazy(std::unique_ptr<llvm::MemoryBuffer> &&MB,
  110. llvm::LLVMContext &Ctx, std::string &DiagStr);
  111. void PrintDiagnosticHandler(const llvm::DiagnosticInfo &DI, void *Context);
  112. bool IsIntegerOrFloatingPointType(llvm::Type *Ty);
  113. // Returns true if type contains HLSL Object type (resource)
  114. bool ContainsHLSLObjectType(llvm::Type *Ty);
  115. bool IsHLSLResourceType(llvm::Type *Ty);
  116. bool IsHLSLObjectType(llvm::Type *Ty);
  117. bool IsHLSLRayQueryType(llvm::Type *Ty);
  118. bool IsHLSLResourceDescType(llvm::Type *Ty);
  119. bool IsResourceSingleComponent(llvm::Type *Ty);
  120. bool IsSplat(llvm::ConstantDataVector *cdv);
  121. llvm::Type* StripArrayTypes(llvm::Type *Ty, llvm::SmallVectorImpl<unsigned> *OuterToInnerLengths = nullptr);
  122. llvm::Type* WrapInArrayTypes(llvm::Type *Ty, llvm::ArrayRef<unsigned> OuterToInnerLengths);
  123. llvm::CallInst *TranslateCallRawBufferLoadToBufferLoad(
  124. llvm::CallInst *CI, llvm::Function *newFunction, hlsl::OP *op);
  125. void ReplaceRawBufferLoadWithBufferLoad(llvm::Function *F, hlsl::OP *op);
  126. llvm::CallInst *TranslateCallRawBufferStoreToBufferStore(
  127. llvm::CallInst *CI, llvm::Function *newFunction, hlsl::OP *op);
  128. void ReplaceRawBufferStoreWithBufferStore(llvm::Function *F, hlsl::OP *op);
  129. void ReplaceRawBufferLoad64Bit(llvm::Function *F, llvm::Type *EltTy, hlsl::OP *hlslOP);
  130. void ReplaceRawBufferStore64Bit(llvm::Function *F, llvm::Type *ETy, hlsl::OP *hlslOP);
  131. }
  132. }