summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-lower-coopvec.cpp
blob: c8b24597c66fd5a12e4d99223b4e86c0e8c14c4f (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
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
#include "slang-ir-lower-coopvec.h"

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

namespace Slang
{
struct CoopVecLoweringContext
{
    IRModule* module;
    DiagnosticSink* sink;

    InstWorkList workList;
    InstHashSet workListSet;

    CoopVecLoweringContext(IRModule* inModule)
        : module(inModule), workList(inModule), workListSet(inModule)
    {
    }

    struct LoweredCoopVecInfo : public RefObject
    {
        IRType* coopvecType;
        IRArrayType* arrayType;
    };
    Dictionary<IRInst*, RefPtr<LoweredCoopVecInfo>> mapLoweredArrayToCoopVecInfo;
    Dictionary<IRInst*, RefPtr<LoweredCoopVecInfo>> loweredCoopVecs;

    IRType* maybeLowerCoopVecType(IRBuilder* builder, IRType* type)
    {
        if (const auto cvt = as<IRCoopVectorType>(type))
        {
            if (auto info = getLoweredCoopVecType(builder, cvt))
                return info->arrayType;
        }
        return type;
    }

    LoweredCoopVecInfo* getLoweredCoopVecType(IRBuilder* builder, IRCoopVectorType* type)
    {
        if (auto loweredInfo = loweredCoopVecs.tryGetValue(type))
            return loweredInfo->Ptr();
        if (auto loweredInfo = mapLoweredArrayToCoopVecInfo.tryGetValue(type))
            return loweredInfo->Ptr();

        if (!type)
            return nullptr;

        RefPtr<LoweredCoopVecInfo> info = new LoweredCoopVecInfo();
        info->coopvecType = (IRType*)type;
        info->arrayType = builder->getArrayType(type->getElementType(), type->getElementCount());
        builder->addNameHintDecoration(info->arrayType, UnownedStringSlice("CoopVec"));

        mapLoweredArrayToCoopVecInfo[info->arrayType] = info;
        loweredCoopVecs[type] = info;
        return info.Ptr();
    }

    void addToWorkList(IRInst* inst)
    {
        for (auto ii = inst->getParent(); ii; ii = ii->getParent())
        {
            if (as<IRGeneric>(ii))
                return;
        }

        if (workListSet.contains(inst))
            return;

        workList.add(inst);
        workListSet.add(inst);
    }

    void processMakeCoopVec(IRInst* inst)
    {
        IRBuilder builderStorage(module);
        auto builder = &builderStorage;
        builder->setInsertBefore(inst);

        const auto cvt = as<IRCoopVectorType>(inst->getDataType());
        SLANG_ASSERT(cvt);
        auto info = getLoweredCoopVecType(builder, cvt);
        List<IRInst*> operands;
        operands.setCount(Index(inst->getOperandCount()));
        UIndex i = 0;
        for (auto operand = inst->getOperands(); i < inst->getOperandCount(); operand++, i++)
            operands[Index(i)] = operand->get();
        auto makeArray =
            builder->emitMakeArray(info->arrayType, operands.getCount(), operands.begin());
        inst->replaceUsesWith(makeArray);
        inst->removeAndDeallocate();
    }

    void processGetCoopVecElement(IRGetElement*) {}

    void processGetElementPtr(IRGetElementPtr*) {}

    void processGetElement(IRGetElement*) {}

    void processCoopVecType(IRCoopVectorType* inst)
    {
        IRBuilder builderStorage(module);
        auto builder = &builderStorage;
        builder->setInsertBefore(inst);

        auto loweredCoopVecInfo = getLoweredCoopVecType(builder, inst);
        SLANG_ASSERT(loweredCoopVecInfo);
        SLANG_UNUSED(loweredCoopVecInfo);
    }

    void processUpdateElement(IRUpdateElement*) {}

    void processEntrywiseOp(IRInst* inst)
    {
        SLANG_ASSERT(inst->getOperandCount());
        if (!as<IRCoopVectorType>(inst->getDataType()))
            return;
        List<IRInst*> operands;
        IRIntegerValue width = 0;
        IRType* resultElementType = nullptr;
        UIndex opIndex = 0;
        for (auto operand = inst->getOperands(); opIndex < inst->getOperandCount();
             operand++, opIndex++)
        {
            operands.add(operand->get());
            if (const auto cv = as<IRCoopVectorType>(operand->get()->getDataType()))
            {
                width = getIntVal(cv->getElementCount());
                resultElementType = cv->getElementType();
            }
        }
        if (width == 0)
            return;
        IRBuilder builder(module);
        IRType* resultElementPtrType = builder.getPtrType(resultElementType);
        builder.setInsertBefore(inst);
        const auto result = builder.emitVar(inst->getFullType());
        List<IRInst*> entrywiseOperands;
        entrywiseOperands.setCount(operands.getCount());
        for (IRIntegerValue i = 0; i < width; ++i)
        {
            for (int j = 0; j < operands.getCount(); ++j)
            {
                if (const auto cv = as<IRCoopVectorType>(operands[j]->getDataType()))
                {
                    SLANG_ASSERT(getIntVal(cv->getElementCount()) == width);
                    const auto elementType = cv->getElementType();
                    entrywiseOperands[j] = builder.emitGetElement(elementType, operands[j], i);
                }
                else
                {
                    entrywiseOperands[j] = operands[j];
                }
            }
            const auto x = builder.emitIntrinsicInst(
                resultElementType,
                inst->getOp(),
                entrywiseOperands.getCount(),
                entrywiseOperands.begin());
            const auto d = builder.emitGetElementPtr(resultElementPtrType, result, i);
            builder.emitStore(d, x);
        }
        const auto v = builder.emitLoad(result);
        inst->replaceUsesWith(v);
        inst->removeAndDeallocate();
    }

    void processInst(IRInst* inst)
    {
        switch (inst->getOp())
        {
        case kIROp_CoopVectorType:
            processCoopVecType((IRCoopVectorType*)inst);
            break;
        case kIROp_MakeCoopVector:
            processMakeCoopVec((IRMakeCoopVector*)inst);
            break;
        case kIROp_GetElement:
            processGetElement((IRGetElement*)inst);
            break;
        case kIROp_GetElementPtr:
            processGetElementPtr((IRGetElementPtr*)inst);
            break;
        case kIROp_UpdateElement:
            processUpdateElement((IRUpdateElement*)inst);
            break;
        case kIROp_Neg:
        case kIROp_Add:
        case kIROp_Sub:
        case kIROp_Mul:
        case kIROp_Div:
        case kIROp_IntCast:
        case kIROp_FloatCast:
        case kIROp_CastIntToFloat:
        case kIROp_CastFloatToInt:
            processEntrywiseOp(inst);
            break;
        default:
            break;
        }
    }

    void processModule()
    {
        IRBuilder builder(module);

        addToWorkList(module->getModuleInst());

        while (workList.getCount() != 0)
        {
            IRInst* inst = workList.getLast();

            workList.removeLast();
            workListSet.remove(inst);

            processInst(inst);

            for (auto child = inst->getLastChild(); child; child = child->getPrevInst())
                addToWorkList(child);
        }

        // Replace all coopvec types with sized array types
        for (const auto& [key, value] : loweredCoopVecs)
            key->replaceUsesWith(value->arrayType);
    }
};

void lowerCooperativeVectors(IRModule* module, DiagnosticSink* sink)
{
    CoopVecLoweringContext context(module);
    context.sink = sink;
    context.processModule();
}
} // namespace Slang