File: AllocaTracking.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 (164 lines) | stat: -rw-r--r-- 4,743 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2020-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

//===----------------------------------------------------------------------===//
//
// Simple utility functions to track the usage of allocas and rewrite their
// types.  This is useful for promoting global memory allocas to either
// stateful or scratch space.
//
//===----------------------------------------------------------------------===//

#include "AllocaTracking.h"
#include "RTBuilder.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Instruction.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvmWrapper/IR/Instructions.h>
#include "common/LLVMWarningsPop.hpp"

using namespace llvm;

namespace AllocaTracking {

// recursively trace all the users of 'I' and check whether they would be safe
// to transform.  Build up the list in 'Insts'.
bool processAlloca(
    Instruction* I,
    bool AllowCapture,
    SmallVector<Instruction*, 4> &Insts,
    DenseSet<CallInst*> &DeferredInsts)
{
    Insts.push_back(I);
    for (auto* U : I->users())
    {
        auto* UI = cast<Instruction>(U);
        switch (UI->getOpcode())
        {
        case Instruction::GetElementPtr:
        case Instruction::BitCast:
            if (!processAlloca(UI, AllowCapture, Insts, DeferredInsts))
                return false;
            break;
        case Instruction::Load:
            break;
        case Instruction::Store:
            if (I == cast<StoreInst>(UI)->getValueOperand() && !AllowCapture)
                return false;
            break;
        case Instruction::Call:
            if (auto* GII = dyn_cast<GenIntrinsicInst>(UI);
                     GII && AllowCapture)
            {
                bool Legal = false;
                switch (GII->getIntrinsicID())
                {
                case GenISAIntrinsic::GenISA_TraceRayAsyncHL:
                case GenISAIntrinsic::GenISA_CallShaderHL:
                    Legal = true;
                    break;
                default:
                    return false;
                }
                if (Legal)
                {
                    DeferredInsts.insert(GII);
                    break;
                }
            }
            else if (auto *II = dyn_cast<IntrinsicInst>(UI))
            {
                bool Legal = false;
                switch (II->getIntrinsicID())
                {
                case Intrinsic::memcpy:
                case Intrinsic::lifetime_start:
                case Intrinsic::lifetime_end:
                    Legal = true;
                    break;
                default:
                    return false;
                }
                if (Legal)
                {
                    DeferredInsts.insert(II);
                    break;
                }
            }
            return false;
        default:
            return false;
        }
    }

    return true;
}

// Given a safe list, transform each of their types.
void rewriteTypes(
    uint32_t NewAddrSpace,
    SmallVector<Instruction*, 4> &Insts,
    DenseSet<CallInst*> &DeferredInsts)
{
    for (auto* I : Insts)
    {
        switch (I->getOpcode())
        {
        case Instruction::GetElementPtr:
        case Instruction::BitCast:
        case Instruction::Alloca:
        {
            auto* EltTy = I->getType()->getPointerElementType();
            auto* NewTy = PointerType::get(EltTy, NewAddrSpace);
            I->mutateType(NewTy);
            break;
        }
        default:
            break;
        }
    }

    for (auto* II : DeferredInsts)
    {
        auto* FTy = II->getFunctionType();
        IGC_ASSERT_MESSAGE(FTy->getReturnType()->isVoidTy(),
            "Only handles void right now!");

        SmallVector<Type*, 4> Tys;
        for (auto &Op : IGCLLVM::args(II))
            Tys.push_back(Op->getType());

        auto* NewFTy = FunctionType::get(
            FTy->getReturnType(), Tys, FTy->isVarArg());

        Function* CurFunc = II->getCalledFunction();
        Function* NewFunc = nullptr;

        if (isa<GenIntrinsicInst>(II))
        {
            NewFunc = Function::Create(
                NewFTy,
                CurFunc->getLinkage(),
                CurFunc->getName(),
                CurFunc->getParent());
        }
        else if (isa<IntrinsicInst>(II))
        {
            NewFunc = RTBuilder::updateIntrinsicMangle(
                NewFTy, *CurFunc);
        }
        else
        {
            IGC_ASSERT_MESSAGE(0, "not yet handled!");
        }

        II->setCalledFunction(NewFunc);
    }
}

} // namespace AllocaTracking