File: SinkCommonOffsetFromGEP.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (443 lines) | stat: -rw-r--r-- 13,425 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*========================== begin_copyright_notice ============================

Copyright (C) 2022 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "common/LLVMUtils.h"
#include "Compiler/CodeGenPublic.h"
#include "Compiler/CISACodeGen/SinkCommonOffsetFromGEP.h"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Probe/Assertion.h"
#include "llvm/IR/DerivedTypes.h"
#include <vector>
#include <algorithm>

using namespace llvm;
using namespace IGC;

namespace {
// This is to sink to the successor block the common constant offset from several GEP
// e.g
// ************************************
// bb1:
//    %0 = gep(a, 0)
//    %1 = gep(a, 1)
// bb2:
//    %2 = gep(b, 0)
//    %3 = gep(b, 1)
//bb3:
//    %4 = phi( [%0, bb1], [%2, bb2] )
//    %5 = phi( [%1, bb1], [%3, bb2] )
//    load [%4]
//    load [%5]
// ************************************
//
// converts to
//
// ************************************
// bb1:
//    %0 = a
// bb2:
//    %1 = b
// bb3:
//    %2 = phi( [%0, bb1], [%1, bb2] )
//    %3 = gep(%2, 0)
//    %4 = gep(%2, 1)
//    load[%3]
//    load[%4]
// ************************************
// This have two goals:
// 1) Could decrease register pressure
// 2) The MemOpt pass will merge loads

class SinkCommonOffsetFromGEP : public llvm::FunctionPass
{

public:
    static char ID;

    SinkCommonOffsetFromGEP();
    virtual bool runOnFunction(llvm::Function& F) override;

    virtual void getAnalysisUsage(llvm::AnalysisUsage& AU) const override {
        AU.setPreservesCFG();
    }
};

// Struct for save GEP and corresponding addrespace cast instruction if exist
struct GepPointer
{
    GetElementPtrInst* GEP;
    AddrSpaceCastInst* Cast;

    static GepPointer get(Value* V);
};

// Struct for save divergent pointer phi node and corresponding GEP instructions
struct DivergentPointer
{
    PHINode* Phi = nullptr;
    MapVector<BasicBlock*, GepPointer> Geps;

    static DivergentPointer create(PHINode* PN);
};

// CommonBaseGroup is aimed to save all divergent pointers with common base of GEP
// e.g:
//
// bb1:
//   %0 = gep(a1, 0)
//   %1 = gep(a1, 1)
//
//   %3 = gep(a2, 0)
//   %4 = gep(a2, 1)
// bb2:
//   %5 = gep(b1, 0)
//   %6 = gep(b1, 1)
//
//   %7 = gep(b2, 0)
//   %8 = gep(b2, 1)
//
// CommonBaseGroupArray[0] = { { %0, %5}, {%1, %6} }
// CommonBaseGroupArray[1] = { { %3, %7}, {%4, %8} }
struct CommonBaseGroup
{
    SmallVector<DivergentPointer, 2> DivergentPointers;
    MapVector<BasicBlock*, std::pair<Value*, SmallVector<Value*, 4>>> GroupFactor;
    MapVector<BasicBlock*, AddrSpaceCastInst*> Casts;
    BasicBlock* Successor;
};

class DivergentPointersGroups
{
public:
    DivergentPointersGroups() = default;

    void add(const DivergentPointer& DP);
    const std::vector<CommonBaseGroup>& getGroups() const { return PointerGroups; }

private:
    std::vector<CommonBaseGroup> PointerGroups;

    bool addToExistingGroup(const DivergentPointer& DP);
    bool createAddNewGroups(const DivergentPointer& DP);
    static bool isBelongedToGroup(const CommonBaseGroup& Group, const DivergentPointer& DP);
};

} // End anonymous namespace

char SinkCommonOffsetFromGEP::ID = 0;

#define PASS_FLAG "igc-sink-gep-constant"
#define PASS_DESCRIPTION "IGC Sink Common Offset From GEP"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false

namespace IGC {
IGC_INITIALIZE_PASS_BEGIN(SinkCommonOffsetFromGEP, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(SinkCommonOffsetFromGEP, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
} // End IGC namespace

FunctionPass* IGC::createSinkCommonOffsetFromGEPPass()  {
    return new SinkCommonOffsetFromGEP();
}

SinkCommonOffsetFromGEP::SinkCommonOffsetFromGEP() : FunctionPass(ID) {
    initializeSinkCommonOffsetFromGEPPass(*PassRegistry::getPassRegistry());
}

GepPointer GepPointer::get(Value* V) {
    GepPointer GP{nullptr, nullptr};

    Value* CurV = V;
    if (auto ASC = dyn_cast<AddrSpaceCastInst>(CurV)) {
        GP.Cast = ASC;
        CurV = ASC->getPointerOperand();
    }

    GP.GEP = dyn_cast<GetElementPtrInst>(CurV);
    return GP;
}

// Create DivergentPointer for phi node. If cannot find GEP for any incoming value Phi field is nullptr
DivergentPointer DivergentPointer::create(PHINode* PN) {
    DivergentPointer DP;
    for (Use& IV : PN->incoming_values()) {

        auto GP = GepPointer::get(IV.get());
        if (GP.GEP == nullptr)
            return DP;

        DP.Geps[PN->getIncomingBlock(IV)] = GP;
    }

    DP.Phi = PN;
    return DP;
}

// Get pure pointer skipping the cast operations
static Value* getPointer(Value* V) {
    if (auto ASC = dyn_cast<AddrSpaceCastInst>(V))
        return getPointer(ASC->getPointerOperand());
    if (auto BC = dyn_cast<BitCastInst>(V))
        return getPointer(BC->getOperand(0));
    return V;
}

// Get pointer for load/store
static Value* getPointerForMemRef(Instruction* I) {
    Value* Pointer = nullptr;
    if (auto LD = dyn_cast<LoadInst>(I))
        Pointer = getPointer(LD->getPointerOperand());
    if (auto ST = dyn_cast<StoreInst>(I))
        Pointer = getPointer(ST->getPointerOperand());

    return Pointer;
}

static std::vector<DivergentPointer> findDivergentPointers(BasicBlock* BB) {
    std::vector<DivergentPointer> DivergentPointers;
    for (auto II = BB->begin(), IE = BB->end(); II != IE; ++II) {

        Instruction* I = &(*II);

        if (!isa<LoadInst>(I) && !isa<StoreInst>(I))
            continue;

        auto Pointer = getPointerForMemRef(I);
        if (Pointer == nullptr)
            continue;

        auto PhiPointer = dyn_cast<PHINode>(Pointer);
        if (PhiPointer == nullptr)
            continue;

        auto DP = DivergentPointer::create(PhiPointer);
        if (DP.Phi == nullptr)
            continue;

        DivergentPointers.push_back(DP);
    }
    return DivergentPointers;
}

// Check if current DivergentPointer belonng to any CommonBaseGroup
bool DivergentPointersGroups::isBelongedToGroup(const CommonBaseGroup& Group, const DivergentPointer& DP) {
    DenseMap<BasicBlock*, std::pair<Value*, SmallVector<Value*, 4>>> CurGroupFactor;

    for (auto& IT : DP.Geps) {
        BasicBlock* BB          = IT.first;
        GetElementPtrInst* GEP  = IT.second.GEP;

        auto PtrOperand = GEP->getPointerOperand();
        SmallVector<Value*, 4> BaseIndices(GEP->idx_begin(), GEP->idx_end() - 1);
        CurGroupFactor[BB] = std::make_pair(PtrOperand, BaseIndices);
    }

    BasicBlock* Successor = DP.Phi->getParent();
    if (Successor != Group.Successor)
        return false;

    for (auto& IT : Group.GroupFactor) {
        BasicBlock* BB      = IT.first;
        Value* PtrOperand   = IT.second.first;
        auto& Indices       = IT.second.second;

        if (CurGroupFactor[BB].first != PtrOperand)
            return false;
        if (CurGroupFactor[BB].second != Indices)
            return false;
    }
    return true;
}

bool DivergentPointersGroups::addToExistingGroup(const DivergentPointer& DP) {
    for (auto& Group : PointerGroups) {
        if (isBelongedToGroup(Group, DP)) {
            Group.DivergentPointers.push_back(DP);
            return true;
        }
    }
    return false;
}

bool DivergentPointersGroups::createAddNewGroups(const DivergentPointer& DP) {
    CommonBaseGroup Group;
    Group.DivergentPointers.push_back(DP);
    for (auto& IT : DP.Geps) {
        BasicBlock* BB          = IT.first;
        AddrSpaceCastInst* ASC  = IT.second.Cast;
        GetElementPtrInst* GEP  = IT.second.GEP;

        if (GEP->getNumIndices() < 1)
            return false;

        SmallVector<Value*, 4> Indices(GEP->idx_begin(), GEP->idx_end() - 1);
        Value* PtrOperand = GEP->getPointerOperand();
        Group.GroupFactor[BB] = std::make_pair(PtrOperand, Indices);
        Group.Casts[BB] = ASC;
    }
    Group.Successor = DP.Phi->getParent();
    PointerGroups.push_back(Group);
    return true;
}

void DivergentPointersGroups::add(const DivergentPointer& DP) {
    // Check all GEPs without last index have the same type
    SmallVector<Type*, 2> Types;
    for (auto IT : DP.Geps) {
        GetElementPtrInst* GEP = IT.second.GEP;
        SmallVector<Value*, 2> IdxList { GEP->idx_begin(), GEP->idx_end() - 1 };
        auto Type = GetElementPtrInst::getIndexedType(GEP->getSourceElementType(), IdxList);
        Types.push_back(Type);
    }
    auto predicate = [&Types](Type* T) {
        return T == Types[0];
    };
    if (!std::all_of(Types.begin(), Types.end(), predicate))
        return;

    if (!addToExistingGroup(DP))
        createAddNewGroups(DP);
}

static Value* getLastIdx(GetElementPtrInst* GEP) {
    if (GEP->getNumIndices() == 0)
        return nullptr;

    auto IdxIt = GEP->idx_end() - 1;
    return IdxIt->get();;
};

static ConstantInt* getCommonConstantOffset(const DivergentPointer& DP) {
    SmallVector<Value*, 2> LastIndices;
    for (auto& IT : DP.Geps) {
        GetElementPtrInst* GEP = IT.second.GEP;
        LastIndices.push_back(getLastIdx(GEP));
    }

    auto IB = LastIndices.begin();
    auto ConstantOffset = dyn_cast<ConstantInt>(*IB);
    if (ConstantOffset == nullptr)
        return nullptr;

    for (auto II = IB + 1, IE = LastIndices.end(); II != IE; II++) {
        auto CI = dyn_cast<ConstantInt>(*II);
        if (CI == nullptr)
            return nullptr;

        if (CI != ConstantOffset)
            return nullptr;
    }

    return ConstantOffset;
}

static bool sinkCommonOffsetForGroup(const CommonBaseGroup& Group) {
    // Find pointers with common constant offset
    SmallVector<std::pair<DivergentPointer, ConstantInt*>, 2> PointersToSink;
    for (auto& Pointer : Group.DivergentPointers) {
        auto ConstantOffset = getCommonConstantOffset(Pointer);
        if (ConstantOffset == nullptr)
            continue;

        PointersToSink.push_back(std::make_pair(Pointer, ConstantOffset));
    }

    // If pointers to split and sink are less than 2 than do nothing
    if (PointersToSink.size() < 2)
        return false;

    SmallVector<std::pair<Value*, BasicBlock*>, 2> NewPointers;
    bool NewGEP = false;
    // Create GEP for the common base in every basic block
    for (auto& IT : Group.GroupFactor) {
        BasicBlock* BB      = IT.first;
        Value* PtrOperand   = IT.second.first;
        auto BaseIndices    = IT.second.second;

        auto EType = dyn_cast<PointerType>(PtrOperand->getType())->getPointerElementType();
        Value * NewPointer = PtrOperand;
        if (NewPointer == nullptr)
            return false;

        // If we have gep(A, constant) just take pure A without creating new GEP
        auto NewPointerType = PtrOperand->getType();
        if (BaseIndices.size() > 0) {
            auto BaseGEP = GetElementPtrInst::Create(EType, PtrOperand, BaseIndices, "", BB->getTerminator());
            NewPointer = BaseGEP;
            NewPointerType = BaseGEP->getResultElementType();
            NewGEP = true;
        }


        if (auto ASC = Group.Casts.find(BB)->second) {
            auto PType = PointerType::get(NewPointerType, ASC->getDestAddressSpace());
            auto BaseASC = new AddrSpaceCastInst(NewPointer, PType, "", BB->getTerminator());
            NewPointer = BaseASC;
        }
        NewPointers.push_back(std::make_pair(NewPointer, BB));
    }

    // Create and fill new phi node for base GEPs
    auto PhiType = NewPointers.front().first->getType();
    auto BasePhi = PHINode::Create(PhiType, 2, "", Group.Successor->getFirstNonPHI());
    for (auto& NP : NewPointers)
        BasePhi->addIncoming(NP.first, NP.second);

    // Create offset GEPs
    for (auto& IT : PointersToSink) {
        ConstantInt* Offset = IT.second;
        PHINode* PN         = IT.first.Phi;
        auto Geps           = IT.first.Geps;

        SmallVector<Value*, 4> Indices;
        if (NewGEP)
            Indices.push_back(ConstantInt::get(Offset->getType(), 0));
        Indices.push_back(Offset);

        auto OffsetType = dyn_cast<PointerType>(BasePhi->getType())->getPointerElementType();
        auto OffsetGEP = GetElementPtrInst::Create(OffsetType, BasePhi, Indices, "", BasePhi->getNextNonDebugInstruction());

        bool isInBounds = false;
        for (auto Gep : Geps)
            isInBounds |= Gep.second.GEP->isInBounds();
        OffsetGEP->setIsInBounds(isInBounds);

        PN->replaceAllUsesWith(OffsetGEP);
    }
    return true;
}

static bool sinkCommonOffset(const std::vector<DivergentPointer>& DivergentPointers) {
    DivergentPointersGroups Groups;
    for (auto& DP : DivergentPointers)
        Groups.add(DP);

    bool changed = false;
    for (auto& Group : Groups.getGroups())
        changed |= sinkCommonOffsetForGroup(Group);

    return changed;
}

bool SinkCommonOffsetFromGEP::runOnFunction(Function& F) {
    bool changed = false;
    for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) {

        // Collect phi node pointers for basic block
        auto DivergentPointers = findDivergentPointers(&(*BB));

        // Does not make sense operate with less than 2 divergent pointers
        if (DivergentPointers.size() < 2)
            continue;

        changed |= sinkCommonOffset(DivergentPointers);
    }
    return changed;
}