summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-composite-reg-to-mem.cpp
blob: e88de3d11980e6fac7da82433ab8381c9d34d963 (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
200
201
202
203
204
205
206
207
208
209
#include "slang-ir-composite-reg-to-mem.h"

#include "slang-ir-dce.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
#include "slang-ir.h"

namespace Slang
{
struct RegisterReplacementWorkItem
{
    IRInst* ssaValue;
    IRInst* addr;
    IRInst* initialStore;
};

void replaceRegisterUseWithAddrUse(
    List<RegisterReplacementWorkItem>& workList,
    IRInst* ssaValue,
    IRInst* addr,
    IRInst* initialStore)
{
    IRBuilder builder(ssaValue);
    traverseUses(
        ssaValue,
        [&](IRUse* use)
        {
            auto user = use->getUser();
            if (user == initialStore)
                return;
            builder.setInsertBefore(user);
            IRInst* newAddr = nullptr;
            // If the user is itself a getElement/getField inst,
            // we want to follow that chain and recursively replace
            // their users.
            if (auto getElementUser = as<IRGetElement>(user))
            {
                if (getElementUser->getOperands() == use)
                {
                    newAddr = builder.emitElementAddress(addr, getElementUser->getIndex());
                }
            }
            else if (auto getFieldUser = as<IRFieldExtract>(user))
            {
                if (getFieldUser->getOperands() == use)
                {
                    newAddr = builder.emitFieldAddress(
                        builder.getPtrType(user->getFullType()),
                        addr,
                        getFieldUser->getField());
                }
            }
            if (newAddr)
            {
                workList.add(RegisterReplacementWorkItem{user, newAddr, nullptr});
            }
            else
            {
                // For all other uses, we emit a load from addr and use it.
                auto val = builder.emitLoad(addr);
                builder.replaceOperand(use, val);
            }
        });
}

void replaceRegisterUseWithAddrUse(IRInst* ssaValue, IRInst* addr, IRInst* initialStore)
{
    List<RegisterReplacementWorkItem> workList, pendingWorkList;
    workList.add(RegisterReplacementWorkItem{ssaValue, addr, initialStore});

    while (workList.getCount())
    {
        for (auto item : workList)
        {
            replaceRegisterUseWithAddrUse(
                pendingWorkList,
                item.ssaValue,
                item.addr,
                item.initialStore);
        }
        workList.swapWith(pendingWorkList);
        pendingWorkList.clear();
    }
}

void convertCompositeTypeParametersToPointers(IRFunc* func)
{
    IRBuilder builder(func);
    List<UInt> compositeParamIds;
    UInt idx = 0;
    List<IRParam*> paramWorkList;
    if (!func->findDecoration<IREntryPointDecoration>())
    {
        // Only translate function parameters for non entry points.
        for (auto param : func->getParams())
        {
            if (as<IRArrayTypeBase>(param->getFullType()) || as<IRStructType>(param->getFullType()))
            {
                paramWorkList.add(param);
                compositeParamIds.add(idx);
            }
            idx++;
        }
    }
    for (auto param : paramWorkList)
    {
        // We have a composite type parameter, so we need to replace it with a pointer.
        //

        auto ptrCompositeType = builder.getPtrType(param->getFullType());
        auto newParam = builder.createParam(ptrCompositeType);
        newParam->insertBefore(param);
        replaceRegisterUseWithAddrUse(param, newParam, nullptr);
        param->removeAndDeallocate();
    }
    if (paramWorkList.getCount())
    {
        // The function is modified, we need to also update its type.
        List<IRType*> paramTypes;
        for (auto param : func->getParams())
        {
            paramTypes.add(param->getFullType());
        }
        auto newFuncType = builder.getFuncType(
            (UInt)paramTypes.getCount(),
            paramTypes.getBuffer(),
            func->getResultType());
        func->setFullType(newFuncType);

        // Update all the call sites to pass the composite by pointer.
        traverseUses(
            func,
            [&](IRUse* use)
            {
                if (const auto call = as<IRCall>(use->getUser()))
                {
                    builder.setInsertBefore(call);
                    for (auto paramId : compositeParamIds)
                    {
                        auto arg = call->getArg(paramId);
                        SLANG_ASSERT(as<IRPtrTypeBase>(paramTypes[paramId]));
                        auto var =
                            builder.emitVar(as<IRPtrTypeBase>(paramTypes[paramId])->getValueType());
                        builder.emitStore(var, arg);
                        call->setArg(paramId, var);
                    }
                }
            });
    }

    // Now work through all the local values and process uses of `Load(composite)`.
    for (auto block : func->getBlocks())
    {
        for (auto inst : block->getModifiableChildren())
        {
            if (!as<IRArrayTypeBase>(inst->getDataType()) && !as<IRStructType>(inst->getDataType()))
                continue;
            if (inst->getParent() != block)
                continue;
            IRInst* tempVar = nullptr;
            IRInst* initialStore = nullptr;
            builder.setInsertAfter(inst);
            switch (inst->getOp())
            {
            case kIROp_Load:
                {
                    auto ptr = inst->getOperand(0);
                    auto rootPtr = getRootAddr(ptr);
                    if (as<IRConstantBufferType>(rootPtr->getDataType()) ||
                        as<IRParameterBlockType>(rootPtr->getDataType()))
                    {
                        tempVar = ptr;
                    }
                    else
                    {
                        tempVar = builder.emitVar(inst->getFullType());
                        initialStore = builder.emitStore(tempVar, inst);
                    }
                    break;
                }
            case kIROp_Call:
                {
                    tempVar = builder.emitVar(inst->getFullType());
                    initialStore = builder.emitStore(tempVar, inst);
                    break;
                }
            default:
                break;
            }

            if (!tempVar)
                continue;
            replaceRegisterUseWithAddrUse(inst, tempVar, initialStore);
        }
    }
    eliminateDeadCode(func);
}

void convertCompositeTypeParametersToPointers(IRModule* module)
{
    for (auto inst : module->getGlobalInsts())
    {
        if (auto func = as<IRFunc>(inst))
        {
            convertCompositeTypeParametersToPointers(func);
        }
    }
}
} // namespace Slang