2
0

DxilTranslateRawBuffer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilTranslateRawBuffer.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. ///////////////////////////////////////////////////////////////////////////////
  9. #include "dxc/DXIL/DxilModule.h"
  10. #include "dxc/DXIL/DxilOperations.h"
  11. #include "dxc/DXIL/DxilUtil.h"
  12. #include "dxc/HLSL/DxilGenerationPass.h"
  13. #include "dxc/Support/Global.h"
  14. #include "llvm/Pass.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/IR/Constant.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/Instruction.h"
  21. #include "llvm/IR/Instructions.h"
  22. #include "llvm/IR/IRBuilder.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/Support/Casting.h"
  25. #include <vector>
  26. using namespace llvm;
  27. using namespace hlsl;
  28. // Translate RawBufferLoad/RawBufferStore
  29. // This pass is to make sure that we generate correct buffer load for DXIL
  30. // For DXIL < 1.2, rawBufferLoad will be translated to BufferLoad instruction
  31. // without mask.
  32. namespace {
  33. class DxilTranslateRawBuffer : public ModulePass {
  34. public:
  35. static char ID;
  36. explicit DxilTranslateRawBuffer() : ModulePass(ID) {}
  37. bool runOnModule(Module &M) {
  38. unsigned major, minor;
  39. DxilModule &DM = M.GetDxilModule();
  40. DM.GetDxilVersion(major, minor);
  41. OP *hlslOP = DM.GetOP();
  42. // Split 64bit for shader model less than 6.3.
  43. if (major == 1 && minor <= 2) {
  44. for (auto F = M.functions().begin(); F != M.functions().end();) {
  45. Function *func = &*(F++);
  46. DXIL::OpCodeClass opClass;
  47. if (hlslOP->GetOpCodeClass(func, opClass)) {
  48. if (opClass == DXIL::OpCodeClass::RawBufferLoad) {
  49. Type *ETy =
  50. OP::GetOverloadType(DXIL::OpCode::RawBufferLoad, func);
  51. bool is64 =
  52. ETy->isDoubleTy() || ETy == Type::getInt64Ty(ETy->getContext());
  53. if (is64) {
  54. ReplaceRawBufferLoad64Bit(func, ETy, M);
  55. func->eraseFromParent();
  56. }
  57. } else if (opClass == DXIL::OpCodeClass::RawBufferStore) {
  58. Type *ETy =
  59. OP::GetOverloadType(DXIL::OpCode::RawBufferStore, func);
  60. bool is64 =
  61. ETy->isDoubleTy() || ETy == Type::getInt64Ty(ETy->getContext());
  62. if (is64) {
  63. ReplaceRawBufferStore64Bit(func, ETy, M);
  64. func->eraseFromParent();
  65. }
  66. }
  67. }
  68. }
  69. }
  70. if (major == 1 && minor < 2) {
  71. for (auto F = M.functions().begin(), E = M.functions().end(); F != E;) {
  72. Function *func = &*(F++);
  73. if (func->hasName()) {
  74. if (func->getName().startswith("dx.op.rawBufferLoad")) {
  75. ReplaceRawBufferLoad(func, M);
  76. func->eraseFromParent();
  77. } else if (func->getName().startswith("dx.op.rawBufferStore")) {
  78. ReplaceRawBufferStore(func, M);
  79. func->eraseFromParent();
  80. }
  81. }
  82. }
  83. }
  84. return true;
  85. }
  86. private:
  87. // Replace RawBufferLoad/Store to BufferLoad/Store for DXIL < 1.2
  88. void ReplaceRawBufferLoad(Function *F, Module &M);
  89. void ReplaceRawBufferStore(Function *F, Module &M);
  90. void ReplaceRawBufferLoad64Bit(Function *F, Type *EltTy, Module &M);
  91. void ReplaceRawBufferStore64Bit(Function *F, Type *EltTy, Module &M);
  92. };
  93. } // namespace
  94. void DxilTranslateRawBuffer::ReplaceRawBufferLoad(Function *F,
  95. Module &M) {
  96. dxilutil::ReplaceRawBufferLoadWithBufferLoad(F, M.GetDxilModule().GetOP());
  97. }
  98. void DxilTranslateRawBuffer::ReplaceRawBufferLoad64Bit(Function *F, Type *EltTy, Module &M) {
  99. dxilutil::ReplaceRawBufferLoad64Bit(F, EltTy, M.GetDxilModule().GetOP());
  100. }
  101. void DxilTranslateRawBuffer::ReplaceRawBufferStore(Function *F,
  102. Module &M) {
  103. dxilutil::ReplaceRawBufferStoreWithBufferStore(F, M.GetDxilModule().GetOP());
  104. }
  105. void DxilTranslateRawBuffer::ReplaceRawBufferStore64Bit(Function *F, Type *ETy,
  106. Module &M) {
  107. dxilutil::ReplaceRawBufferStore64Bit(F, ETy, M.GetDxilModule().GetOP());
  108. }
  109. char DxilTranslateRawBuffer::ID = 0;
  110. ModulePass *llvm::createDxilTranslateRawBuffer() {
  111. return new DxilTranslateRawBuffer();
  112. }
  113. INITIALIZE_PASS(DxilTranslateRawBuffer, "hlsl-translate-dxil-raw-buffer",
  114. "Translate raw buffer load", false, false)