File: MergeAllocas.cpp

package info (click to toggle)
intel-graphics-compiler2 2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 106,644 kB
  • sloc: cpp: 805,640; lisp: 287,672; ansic: 16,414; python: 3,952; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 35
file content (248 lines) | stat: -rw-r--r-- 9,164 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*========================== begin_copyright_notice ============================

Copyright (C) 2024 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "MergeAllocas.h"
#include "Compiler/IGCPassSupport.h"
#include "common/igc_regkeys.hpp"
#include "Probe/Assertion.h"
#include "debug/DebugMacros.hpp"

#include "common/LLVMWarningsPush.hpp"
#include <llvm/ADT/SetOperations.h>
#include <llvm/ADT/SetVector.h>
#include <llvm/ADT/SmallSet.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/Analysis/LoopInfo.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Dominators.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/Instructions.h>
#include "common/LLVMWarningsPop.hpp"

using namespace llvm;
using namespace IGC;

// Get size of bytes allocated for type including padding.
static size_t GetByteSize(Type *T, const DataLayout *DL) {
  if (T->isSized())
    return static_cast<size_t>(DL->getTypeAllocSize(T));
  return 0;
}

static MergeAllocas::AllocaInfo GetAllocaInfo(AllocaInst *allocaI, AllocationLivenessAnalyzer::LivenessData *LD,
                                              const DataLayout *DL) {
  size_t allocationSize = GetByteSize(allocaI->getAllocatedType(), DL);
  return {{},
          allocaI,
          LD,
          allocaI->getAddressSpace(),
          allocationSize,
          allocationSize,
          static_cast<size_t>(DL->getPrefTypeAlign(allocaI->getAllocatedType()).value()),
          0,
          allocaI->getMetadata("uniform") != nullptr};
}

static size_t GetStartingOffset(size_t startOffset, size_t alignment) {
  size_t remainder = startOffset % alignment;
  if (remainder == 0) {
    return startOffset;
  }
  return startOffset + (alignment - remainder);
}

static bool AddNonOverlappingAlloca(MergeAllocas::AllocaInfo *MergableAlloca, MergeAllocas::AllocaInfo *NewAlloca) {
  if (MergableAlloca->isUniform != NewAlloca->isUniform) {
    return false;
  }
  if (MergableAlloca->addressSpace != NewAlloca->addressSpace) {
    return false;
  }
  if (MergableAlloca->allocationSize < NewAlloca->allocationSize) {
    return false;
  }
  if (MergableAlloca->livenessData->OverlapsWith(*NewAlloca->livenessData)) {
    return false;
  }

  if (IGC_IS_FLAG_ENABLED(DisableMergingOfAllocasWithDifferentType)) {
    auto *AllocatedType = MergableAlloca->allocaI->getAllocatedType();
    auto *AllocatedTypeNew = NewAlloca->allocaI->getAllocatedType();
    bool IsArray = AllocatedType->isArrayTy() ? true : false;
    bool IsArrayNew = AllocatedTypeNew->isArrayTy() ? true : false;
    bool AreBothArrays = IsArray && IsArrayNew;
    if (AreBothArrays && AllocatedType->getArrayElementType() != AllocatedTypeNew->getArrayElementType()) {
      return false;
    }
    if (!AreBothArrays && AllocatedType != AllocatedTypeNew) {
      return false;
    }
  }

  // Check if we can merge alloca to one of existing non-overlapping allocas.
  for (auto *NonOverlappingAlloca : MergableAlloca->nonOverlapingAllocas) {
    bool added = AddNonOverlappingAlloca(NonOverlappingAlloca, NewAlloca);
    if (added) {
      return true;
    }
  }

  // Check if we have still space in existing alloca to add new alloca
  if (MergableAlloca->remainingSize >= NewAlloca->allocationSize) {
    size_t currentOffset = MergableAlloca->allocationSize - MergableAlloca->remainingSize;
    size_t newStartingOffset = GetStartingOffset(currentOffset, NewAlloca->alignment);
    size_t sizeWithPadding = NewAlloca->allocationSize + (newStartingOffset - currentOffset);
    // When adding alignment in consideration we can't fit new alloca.
    if (sizeWithPadding > MergableAlloca->remainingSize) {
      return false;
    }
    size_t newAllocaOffset = newStartingOffset + MergableAlloca->offset;
    if (newAllocaOffset != 0 && IGC_IS_FLAG_ENABLED(DisableMergingOfMultipleAllocasWithOffset)) {
      return false;
    }
    NewAlloca->offset = newAllocaOffset;
    MergableAlloca->nonOverlapingAllocas.push_back(NewAlloca);
    MergableAlloca->remainingSize -= sizeWithPadding;
    return true;
  }

  return false;
}

static void ReplaceAllocas(const MergeAllocas::AllocaInfo &MergableAlloca, Function &F) {
  Instruction *topAlloca = MergableAlloca.allocaI;
  topAlloca->moveBefore(F.getEntryBlock().getFirstNonPHI());
  topAlloca->setName(VALUE_NAME("MergedAlloca"));

  IRBuilder<> Builder(topAlloca->getParent());
  Instruction *topAllocaBitcast = nullptr;

  SmallVector<MergeAllocas::AllocaInfo *> allocasToReplace;
  allocasToReplace.insert(allocasToReplace.end(), MergableAlloca.nonOverlapingAllocas.begin(),
                          MergableAlloca.nonOverlapingAllocas.end());

  while (!allocasToReplace.empty()) {
    auto *subAlloca = allocasToReplace.pop_back_val();

    auto *subInst = subAlloca->allocaI;
    auto *ReplacementValue = topAlloca;

    if (topAlloca->getType() != subInst->getType()) {
      auto *InsertionPoint = (topAllocaBitcast != nullptr) ? topAllocaBitcast : topAlloca;
      Builder.SetInsertPoint(InsertionPoint->getNextNode());

      Value *ValueToCast = nullptr;
      // If we have offset from original alloca we need to create GEP
      if (subAlloca->offset != 0) {
        // We can re-use same bitcast
        if (topAllocaBitcast == nullptr) {
          topAllocaBitcast = cast<Instruction>(
              Builder.CreateBitCast(topAlloca, Builder.getInt8PtrTy(topAlloca->getType()->getPointerAddressSpace())));
        }
        auto *Offset = Builder.getInt32(subAlloca->offset);
        auto *GEP = Builder.CreateGEP(Builder.getInt8Ty(), topAllocaBitcast, Offset);
        ValueToCast = GEP;
      } else {
        // If no offset is needed we can directly cast to target type
        ValueToCast = Builder.CreateBitCast(topAlloca, subInst->getType());
      }
      auto *CastedValue = llvm::cast<Instruction>(Builder.CreateBitCast(ValueToCast, subInst->getType()));
      ReplacementValue = CastedValue;
    }
    subInst->replaceAllUsesWith(ReplacementValue);
    subInst->eraseFromParent();

    allocasToReplace.insert(allocasToReplace.end(), subAlloca->nonOverlapingAllocas.begin(),
                            subAlloca->nonOverlapingAllocas.end());
  }
}

bool MergeAllocas::runOnFunction(Function &F) {
  if (skipFunction(F)) {
    return false;
  }

  auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();

  // in the past, the analysis pass used to be responsible for the liveness data objects
  // the pass got deleted as a part of refactor (it was leaking memory anyway),
  // so to avoid changing the logic, we create a backing storage for the liveness data objects
  // we should revisit this at some point to clean it up, but for now
  // we use std::list so it doesn't invalidate the references when inserting
  std::list<LivenessData> storage;

  llvm::SmallVector<std::pair<llvm::Instruction *, LivenessData *>> ABLA;

  for (auto &I : make_filter_range(instructions(F), [](auto &I) { return isa<AllocaInst>(&I); })) {
    storage.push_back(ProcessInstruction(&I, DT, LI));
    ABLA.push_back(std::make_pair(&I, &storage.back()));
  }

  const auto *DataLayout = &F.getParent()->getDataLayout();

  // We group non-overlapping allocas for replacements.
  SmallVector<AllocaInfo *> MergableAllocas;

  // First we sort analysis results based on allocation size, from larger to
  // smaller.
  llvm::sort(ABLA, [&](const auto &a, const auto &b) {
    return GetByteSize(cast<AllocaInst>(a.first)->getAllocatedType(), DataLayout) >
           GetByteSize(cast<AllocaInst>(b.first)->getAllocatedType(), DataLayout);
  });

  // Reserve space for all alloca infos so we can use pointers to them.
  AllAllocasInfos.resize(ABLA.size());
  size_t currentIndex = 0;

  // We iterate over analysis results collecting non-overlapping allocas.
  for (const auto &A : ABLA) {
    const auto &[currI, currLD] = A;

    if (skipInstruction(F, *currLD))
      continue;

    AllAllocasInfos[currentIndex] = GetAllocaInfo(cast<AllocaInst>(currI), currLD, DataLayout);
    AllocaInfo &AllocaInfo = AllAllocasInfos[currentIndex];
    currentIndex++;

    // We check if the current alloca overlaps with any of the previously added.
    bool added = false;
    for (auto *MergableAlloca : MergableAllocas) {
      if (AllocaInfo.livenessData->OverlapsWith(*MergableAlloca->livenessData)) {
        continue;
      }
      added = AddNonOverlappingAlloca(MergableAlloca, &AllocaInfo);
      if (added) {
        break;
      }
    }
    // Alloca overlaps with all of the current ones so it will be added as new
    // element.
    if (!added && AllocaInfo.allocationSize != 0) {
      MergableAllocas.push_back(&AllocaInfo);
    }
  }

  bool changed = false;

  // Replace alloca usages
  for (auto *MergableAlloca : MergableAllocas) {
    if (MergableAlloca->nonOverlapingAllocas.empty()) {
      continue;
    }
    changed = true;
    ReplaceAllocas(*MergableAlloca, F);
  }

  return changed;
}