aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
blob: 837624935c5faedd5ff0c72982b84dd600657379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//===- DXILResourceAccess.cpp - Resource access via load/store ------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "DXILResourceAccess.h"
#include "DirectX.h"
#include "llvm/Analysis/DXILResource.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsDirectX.h"
#include "llvm/InitializePasses.h"

#define DEBUG_TYPE "dxil-resource-access"

using namespace llvm;

static void replaceTypedBufferAccess(IntrinsicInst *II,
                                     dxil::ResourceTypeInfo &RTI) {
  const DataLayout &DL = II->getDataLayout();

  auto *HandleType = cast<TargetExtType>(II->getOperand(0)->getType());
  assert(HandleType->getName() == "dx.TypedBuffer" &&
         "Unexpected typed buffer type");
  Type *ContainedType = HandleType->getTypeParameter(0);

  Type *LoadType =
      StructType::get(ContainedType, Type::getInt1Ty(II->getContext()));

  // We need the size of an element in bytes so that we can calculate the offset
  // in elements given a total offset in bytes later.
  Type *ScalarType = ContainedType->getScalarType();
  uint64_t ScalarSize = DL.getTypeSizeInBits(ScalarType) / 8;

  // Process users keeping track of indexing accumulated from GEPs.
  struct AccessAndIndex {
    User *Access;
    Value *Index;
  };
  SmallVector<AccessAndIndex> Worklist;
  for (User *U : II->users())
    Worklist.push_back({U, nullptr});

  SmallVector<Instruction *> DeadInsts;
  while (!Worklist.empty()) {
    AccessAndIndex Current = Worklist.back();
    Worklist.pop_back();

    if (auto *GEP = dyn_cast<GetElementPtrInst>(Current.Access)) {
      IRBuilder<> Builder(GEP);

      Value *Index;
      APInt ConstantOffset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
      if (GEP->accumulateConstantOffset(DL, ConstantOffset)) {
        APInt Scaled = ConstantOffset.udiv(ScalarSize);
        Index = ConstantInt::get(Builder.getInt32Ty(), Scaled);
      } else {
        auto IndexIt = GEP->idx_begin();
        assert(cast<ConstantInt>(IndexIt)->getZExtValue() == 0 &&
               "GEP is not indexing through pointer");
        ++IndexIt;
        Index = *IndexIt;
        assert(++IndexIt == GEP->idx_end() && "Too many indices in GEP");
      }

      for (User *U : GEP->users())
        Worklist.push_back({U, Index});
      DeadInsts.push_back(GEP);

    } else if (auto *SI = dyn_cast<StoreInst>(Current.Access)) {
      assert(SI->getValueOperand() != II && "Pointer escaped!");
      IRBuilder<> Builder(SI);

      Value *V = SI->getValueOperand();
      if (V->getType() == ContainedType) {
        // V is already the right type.
      } else if (V->getType() == ScalarType) {
        // We're storing a scalar, so we need to load the current value and only
        // replace the relevant part.
        auto *Load = Builder.CreateIntrinsic(
            LoadType, Intrinsic::dx_resource_load_typedbuffer,
            {II->getOperand(0), II->getOperand(1)});
        auto *Struct = Builder.CreateExtractValue(Load, {0});

        // If we have an offset from seeing a GEP earlier, use it.
        Value *IndexOp = Current.Index
                             ? Current.Index
                             : ConstantInt::get(Builder.getInt32Ty(), 0);
        V = Builder.CreateInsertElement(Struct, V, IndexOp);
      } else {
        llvm_unreachable("Store to typed resource has invalid type");
      }

      auto *Inst = Builder.CreateIntrinsic(
          Builder.getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
          {II->getOperand(0), II->getOperand(1), V});
      SI->replaceAllUsesWith(Inst);
      DeadInsts.push_back(SI);

    } else if (auto *LI = dyn_cast<LoadInst>(Current.Access)) {
      IRBuilder<> Builder(LI);
      Value *V = Builder.CreateIntrinsic(
          LoadType, Intrinsic::dx_resource_load_typedbuffer,
          {II->getOperand(0), II->getOperand(1)});
      V = Builder.CreateExtractValue(V, {0});

      if (Current.Index)
        V = Builder.CreateExtractElement(V, Current.Index);

      LI->replaceAllUsesWith(V);
      DeadInsts.push_back(LI);

    } else
      llvm_unreachable("Unhandled instruction - pointer escaped?");
  }

  // Traverse the now-dead instructions in RPO and remove them.
  for (Instruction *Dead : llvm::reverse(DeadInsts))
    Dead->eraseFromParent();
  II->eraseFromParent();
}

static bool transformResourcePointers(Function &F, DXILResourceTypeMap &DRTM) {
  bool Changed = false;
  SmallVector<std::pair<IntrinsicInst *, dxil::ResourceTypeInfo>> Resources;
  for (BasicBlock &BB : F)
    for (Instruction &I : BB)
      if (auto *II = dyn_cast<IntrinsicInst>(&I))
        if (II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) {
          auto *HandleTy = cast<TargetExtType>(II->getArgOperand(0)->getType());
          Resources.emplace_back(II, DRTM[HandleTy]);
        }

  for (auto &[II, RI] : Resources) {
    if (RI.isTyped()) {
      Changed = true;
      replaceTypedBufferAccess(II, RI);
    }

    // TODO: handle other resource types. We should probably have an
    // `unreachable` here once we've added support for all of them.
  }

  return Changed;
}

PreservedAnalyses DXILResourceAccess::run(Function &F,
                                          FunctionAnalysisManager &FAM) {
  auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
  DXILResourceTypeMap *DRTM =
      MAMProxy.getCachedResult<DXILResourceTypeAnalysis>(*F.getParent());
  assert(DRTM && "DXILResourceTypeAnalysis must be available");

  bool MadeChanges = transformResourcePointers(F, *DRTM);
  if (!MadeChanges)
    return PreservedAnalyses::all();

  PreservedAnalyses PA;
  PA.preserve<DXILResourceTypeAnalysis>();
  PA.preserve<DominatorTreeAnalysis>();
  return PA;
}

namespace {
class DXILResourceAccessLegacy : public FunctionPass {
public:
  bool runOnFunction(Function &F) override {
    DXILResourceTypeMap &DRTM =
        getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();

    return transformResourcePointers(F, DRTM);
  }
  StringRef getPassName() const override { return "DXIL Resource Access"; }
  DXILResourceAccessLegacy() : FunctionPass(ID) {}

  static char ID; // Pass identification.
  void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
    AU.addRequired<DXILResourceTypeWrapperPass>();
    AU.addPreserved<DominatorTreeWrapperPass>();
  }
};
char DXILResourceAccessLegacy::ID = 0;
} // end anonymous namespace

INITIALIZE_PASS_BEGIN(DXILResourceAccessLegacy, DEBUG_TYPE,
                      "DXIL Resource Access", false, false)
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
INITIALIZE_PASS_END(DXILResourceAccessLegacy, DEBUG_TYPE,
                    "DXIL Resource Access", false, false)

FunctionPass *llvm::createDXILResourceAccessLegacyPass() {
  return new DXILResourceAccessLegacy();
}