HLExpandStoreIntrinsics.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // HLExpandStoreIntrinsics.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/Support/Global.h"
  10. #include "dxc/HLSL/HLOperations.h"
  11. #include "dxc/HLSL/HLMatrixType.h"
  12. #include "dxc/HlslIntrinsicOp.h"
  13. #include "llvm/IR/InstIterator.h"
  14. #include "llvm/IR/Instruction.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/IR/IRBuilder.h"
  19. #include "llvm/IR/Intrinsics.h"
  20. #include "llvm/Transforms/Scalar.h"
  21. using namespace hlsl;
  22. using namespace llvm;
  23. namespace {
  24. // Expands buffer stores of aggregate value types
  25. // into stores of its individual elements,
  26. // before SROA happens and we lose the layout information.
  27. class HLExpandStoreIntrinsics : public FunctionPass {
  28. public:
  29. static char ID;
  30. explicit HLExpandStoreIntrinsics() : FunctionPass(ID) {}
  31. const char *getPassName() const override {
  32. return "Expand HLSL store intrinsics";
  33. }
  34. bool runOnFunction(Function& Func) override;
  35. private:
  36. bool expand(CallInst *StoreCall);
  37. void emitElementStores(CallInst &OriginalCall,
  38. SmallVectorImpl<Value*>& GEPIndicesStack, Type *StackTopTy,
  39. unsigned OffsetFromBase);
  40. };
  41. char HLExpandStoreIntrinsics::ID = 0;
  42. bool HLExpandStoreIntrinsics::runOnFunction(Function& Func) {
  43. bool changed = false;
  44. for (auto InstIt = inst_begin(Func), InstEnd = inst_end(Func); InstIt != InstEnd;) {
  45. CallInst *Call = dyn_cast<CallInst>(&*(InstIt++));
  46. if (Call == nullptr
  47. || GetHLOpcodeGroup(Call->getCalledFunction()) != HLOpcodeGroup::HLIntrinsic
  48. || static_cast<IntrinsicOp>(GetHLOpcode(Call)) != IntrinsicOp::MOP_Store) {
  49. continue;
  50. }
  51. changed |= expand(Call);
  52. }
  53. return changed;
  54. }
  55. bool HLExpandStoreIntrinsics::expand(CallInst* StoreCall) {
  56. Value *OldStoreValueArg = StoreCall->getArgOperand(HLOperandIndex::kStoreValOpIdx);
  57. Type *OldStoreValueArgTy = OldStoreValueArg->getType();
  58. // Only expand if the value argument is by pointer, which means it's an aggregate.
  59. if (!OldStoreValueArgTy->isPointerTy()) return false;
  60. IRBuilder<> Builder(StoreCall);
  61. SmallVector<Value*, 4> GEPIndicesStack;
  62. GEPIndicesStack.emplace_back(Builder.getInt32(0));
  63. emitElementStores(*StoreCall, GEPIndicesStack, OldStoreValueArgTy->getPointerElementType(), /* OffsetFromBase */ 0);
  64. DXASSERT(StoreCall->getType()->isVoidTy() && StoreCall->use_empty(),
  65. "Buffer store intrinsic is expected to return void and hence not have uses.");
  66. StoreCall->eraseFromParent();
  67. return true;
  68. }
  69. void HLExpandStoreIntrinsics::emitElementStores(CallInst &OriginalCall,
  70. SmallVectorImpl<Value*>& GEPIndicesStack, Type *StackTopTy,
  71. unsigned OffsetFromBase) {
  72. llvm::Module &Module = *OriginalCall.getModule();
  73. IRBuilder<> Builder(&OriginalCall);
  74. StructType* StructTy = dyn_cast<StructType>(StackTopTy);
  75. if (StructTy != nullptr && !HLMatrixType::isa(StructTy)) {
  76. const StructLayout* Layout = Module.getDataLayout().getStructLayout(StructTy);
  77. for (unsigned i = 0; i < StructTy->getNumElements(); ++i) {
  78. Type *ElemTy = StructTy->getElementType(i);
  79. unsigned ElemOffsetFromBase = OffsetFromBase + Layout->getElementOffset(i);
  80. GEPIndicesStack.emplace_back(Builder.getInt32(i));
  81. emitElementStores(OriginalCall, GEPIndicesStack, ElemTy, ElemOffsetFromBase);
  82. GEPIndicesStack.pop_back();
  83. }
  84. }
  85. else if (ArrayType *ArrayTy = dyn_cast<ArrayType>(StackTopTy)) {
  86. unsigned ElemSize = (unsigned)Module.getDataLayout().getTypeAllocSize(ArrayTy->getElementType());
  87. for (int i = 0; i < (int)ArrayTy->getNumElements(); ++i) {
  88. unsigned ElemOffsetFromBase = OffsetFromBase + ElemSize * i;
  89. GEPIndicesStack.emplace_back(Builder.getInt32(i));
  90. emitElementStores(OriginalCall, GEPIndicesStack, ArrayTy->getElementType(), ElemOffsetFromBase);
  91. GEPIndicesStack.pop_back();
  92. }
  93. }
  94. else {
  95. // Scalar or vector
  96. Value* OpcodeVal = OriginalCall.getArgOperand(HLOperandIndex::kOpcodeIdx);
  97. Value* BufHandle = OriginalCall.getArgOperand(HLOperandIndex::kHandleOpIdx);
  98. Value* OffsetVal = OriginalCall.getArgOperand(HLOperandIndex::kStoreOffsetOpIdx);
  99. if (OffsetFromBase > 0)
  100. OffsetVal = Builder.CreateAdd(OffsetVal, Builder.getInt32(OffsetFromBase));
  101. Value* AggPtr = OriginalCall.getArgOperand(HLOperandIndex::kStoreValOpIdx);
  102. Value *ElemPtr = Builder.CreateGEP(AggPtr, GEPIndicesStack);
  103. Value *ElemVal = Builder.CreateLoad(ElemPtr); // We go from memory to memory so no special bool handling needed
  104. FunctionType *NewCalleeType = FunctionType::get(Builder.getVoidTy(),
  105. { OpcodeVal->getType(), BufHandle->getType(), OffsetVal->getType(), ElemVal->getType() },
  106. /* isVarArg */ false);
  107. Function *NewCallee = GetOrCreateHLFunction(Module, NewCalleeType,
  108. HLOpcodeGroup::HLIntrinsic, (unsigned)IntrinsicOp::MOP_Store);
  109. Builder.CreateCall(NewCallee, { OpcodeVal, BufHandle, OffsetVal, ElemVal });
  110. }
  111. }
  112. } // namespace
  113. FunctionPass *llvm::createHLExpandStoreIntrinsicsPass() { return new HLExpandStoreIntrinsics(); }
  114. INITIALIZE_PASS(HLExpandStoreIntrinsics, "hl-expand-store-intrinsics",
  115. "Expand HLSL store intrinsics", false, false)