DxilUtil.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilUtil.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. // Dxil helper functions. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "llvm/IR/GlobalVariable.h"
  12. #include "dxc/HLSL/DxilTypeSystem.h"
  13. #include "dxc/HLSL/DxilUtil.h"
  14. #include "dxc/HLSL/DxilModule.h"
  15. #include "llvm/IR/Module.h"
  16. using namespace llvm;
  17. using namespace hlsl;
  18. namespace hlsl {
  19. namespace dxilutil {
  20. Type *GetArrayEltTy(Type *Ty) {
  21. if (isa<PointerType>(Ty))
  22. Ty = Ty->getPointerElementType();
  23. while (isa<ArrayType>(Ty)) {
  24. Ty = Ty->getArrayElementType();
  25. }
  26. return Ty;
  27. }
  28. unsigned
  29. GetLegacyCBufferFieldElementSize(DxilFieldAnnotation &fieldAnnotation,
  30. llvm::Type *Ty,
  31. DxilTypeSystem &typeSys) {
  32. while (isa<ArrayType>(Ty)) {
  33. Ty = Ty->getArrayElementType();
  34. }
  35. // Bytes.
  36. CompType compType = fieldAnnotation.GetCompType();
  37. unsigned compSize = compType.Is64Bit() ? 8 : compType.Is16Bit() && !typeSys.UseMinPrecision() ? 2 : 4;
  38. unsigned fieldSize = compSize;
  39. if (Ty->isVectorTy()) {
  40. fieldSize *= Ty->getVectorNumElements();
  41. } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
  42. DxilStructAnnotation *EltAnnotation = typeSys.GetStructAnnotation(ST);
  43. if (EltAnnotation) {
  44. fieldSize = EltAnnotation->GetCBufferSize();
  45. } else {
  46. // Calculate size when don't have annotation.
  47. if (fieldAnnotation.HasMatrixAnnotation()) {
  48. const DxilMatrixAnnotation &matAnnotation =
  49. fieldAnnotation.GetMatrixAnnotation();
  50. unsigned rows = matAnnotation.Rows;
  51. unsigned cols = matAnnotation.Cols;
  52. if (matAnnotation.Orientation == MatrixOrientation::ColumnMajor) {
  53. rows = cols;
  54. cols = matAnnotation.Rows;
  55. } else if (matAnnotation.Orientation != MatrixOrientation::RowMajor) {
  56. // Invalid matrix orientation.
  57. fieldSize = 0;
  58. }
  59. fieldSize = (rows - 1) * 16 + cols * 4;
  60. } else {
  61. // Cannot find struct annotation.
  62. fieldSize = 0;
  63. }
  64. }
  65. }
  66. return fieldSize;
  67. }
  68. bool IsStaticGlobal(GlobalVariable *GV) {
  69. return GV->getLinkage() == GlobalValue::LinkageTypes::InternalLinkage &&
  70. GV->getType()->getPointerAddressSpace() == DXIL::kDefaultAddrSpace;
  71. }
  72. bool IsSharedMemoryGlobal(llvm::GlobalVariable *GV) {
  73. return GV->getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace;
  74. }
  75. bool RemoveUnusedFunctions(Module &M, Function *EntryFunc,
  76. Function *PatchConstantFunc, bool IsLib) {
  77. std::vector<Function *> deadList;
  78. for (auto &F : M.functions()) {
  79. if (&F == EntryFunc || &F == PatchConstantFunc)
  80. continue;
  81. if (F.isDeclaration() || !IsLib) {
  82. if (F.user_empty())
  83. deadList.emplace_back(&F);
  84. }
  85. }
  86. bool bUpdated = deadList.size();
  87. for (Function *F : deadList)
  88. F->eraseFromParent();
  89. return bUpdated;
  90. }
  91. }
  92. }