summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-cleanup-void.cpp
blob: ac520c1d50daedd2b0963c1e492597a80c51d7ca (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
// slang-ir-cleanup-void.cpp

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

namespace Slang
{
    struct CleanUpVoidContext
    {
        IRModule* module;

        SharedIRBuilder sharedBuilderStorage;

        List<IRInst*> workList;
        HashSet<IRInst*> workListSet;

        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 processInst(IRInst* inst)
        {
            switch (inst->getOp())
            {
            case kIROp_Call:
                {
                    // Remove void argument.
                    auto call = as<IRCall>(inst);
                    List<IRInst*> newArgs;
                    for (UInt i = 0; i < call->getArgCount(); i++)
                    {
                        auto arg = call->getArg(i);
                        if (arg->getDataType() && arg->getDataType()->getOp() == kIROp_VoidType)
                        {
                            continue;
                        }
                        newArgs.add(arg);
                    }
                    if (newArgs.getCount() != (Index)call->getArgCount())
                    {
                        IRBuilder builder(&sharedBuilderStorage);
                        builder.setInsertBefore(call);
                        auto newCall = builder.emitCallInst(call->getFullType(), call->getCallee(), newArgs);
                        call->replaceUsesWith(newCall);
                        call->removeAndDeallocate();
                        inst = newCall;
                    }
                }
                break;
            case kIROp_Func:
                {
                    // Remove void parameter.
                    List<IRParam*> paramsToRemove;
                    auto func = as<IRFunc>(inst);
                    for (auto param : func->getParams())
                    {
                        if (param->getDataType()->getOp() == kIROp_VoidType)
                        {
                            paramsToRemove.add(param);
                        }
                    }
                    IRBuilder builder(&sharedBuilderStorage);
                    builder.setInsertBefore(func);
                    for (auto param : paramsToRemove)
                    {
                        auto voidVal = builder.getVoidValue();
                        param->replaceUsesWith(voidVal);
                        param->removeAndDeallocate();
                    }
                }
                break;
            case kIROp_FuncType:
                {
                    auto funcType = as<IRFuncType>(inst);
                    List<IRInst*> newOperands;
                    for (UInt i = 1; i < funcType->getOperandCount(); i++)
                    {
                        auto operand = funcType->getOperand(i);
                        if (operand->getOp() == kIROp_VoidType)
                        {
                            continue;
                        }
                        newOperands.add(operand);
                    }
                    if (newOperands.getCount() != (Index)funcType->getParamCount())
                    {
                        IRBuilder builder(&sharedBuilderStorage);
                        builder.setInsertBefore(funcType);
                        auto newFuncType = builder.getFuncType(newOperands.getCount(), (IRType**)newOperands.getBuffer(), funcType->getResultType());
                        if (newFuncType != funcType)
                        {
                            funcType->replaceUsesWith(newFuncType);
                            funcType->removeAndDeallocate();
                        }
                        inst = newFuncType;
                    }
                }
                break;
            case kIROp_StructType:
                {
                    // TODO: cleanup void fields.
                }
                break;
            default:
                break;
            }

            // TODO: If inst has void type, all uses of it should be replaced with void val.
            // We should do this only for a subset of opcodes known to be safe.

        }

        void processModule()
        {
            SharedIRBuilder* sharedBuilder = &sharedBuilderStorage;
            sharedBuilder->init(module);

            // Deduplicate equivalent types.
            sharedBuilder->deduplicateAndRebuildGlobalNumberingMap();

            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);
                }
            }
        }
    };
    
    void cleanUpVoidType(IRModule* module)
    {
        CleanUpVoidContext context;
        context.module = module;
        context.processModule();
    }
}