summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-strip-legalization-insts.cpp
blob: f55d3f11974b54cbdaf69c6ed3e781cb85536f36 (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
// slang-ir-strip-legalization-insts.cpp
#include "slang-ir-strip-legalization-insts.h"

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

namespace Slang
{

void stripLegalizationOnlyInstructions(IRModule* module)
{
    for (auto inst : module->getGlobalInsts())
    {
        switch (inst->getOp())
        {
        // Remove cached dictionaries.
        case kIROp_GenericSpecializationDictionary:
        case kIROp_ExistentialFuncSpecializationDictionary:
        case kIROp_ExistentialTypeSpecializationDictionary:
            {
                inst->removeAndDeallocate();
                break;
            }

        // Remove global param entry point param decoration.
        case kIROp_GlobalParam:
            {
                if (const auto entryPointParamDecoration =
                        inst->findDecoration<IREntryPointParamDecoration>())
                    entryPointParamDecoration->removeAndDeallocate();
                break;
            }

        // Remove witness tables.
        // Our goal here is to empty out any witness tables in
        // the IR so that they don't keep other symbols alive
        // further into compilation. Luckily we expect all
        // witness tables to live directly at the global scope
        // (or inside of a generic, which we can ignore for
        // now because the emit logic also ignores generics),
        // and there is a single function we can call to
        // remove all of the content from the witness tables
        // (since the key-value associations are stored as
        // children of each table).
        case kIROp_WitnessTable:
            {
                auto witnessTable = as<IRWitnessTable>(inst);
                auto conformanceType = witnessTable->getConformanceType();
                if (!conformanceType ||
                    !conformanceType->findDecoration<IRComInterfaceDecoration>())
                {
                    witnessTable->removeAndDeallocateAllDecorationsAndChildren();
                }
                break;
            }

        default:
            break;
        }
    }
}

void unpinWitnessTables(IRModule* module)
{
    for (auto inst : module->getGlobalInsts())
    {
        auto witnessTable = as<IRWitnessTable>(inst);
        if (!witnessTable)
            continue;

        // If a witness table is not used for dynamic dispatch, unpin it.
        if (!witnessTable->findDecoration<IRDynamicDispatchWitnessDecoration>())
        {
            while (auto decor = witnessTable->findDecoration<IRKeepAliveDecoration>())
                decor->removeAndDeallocate();
        }
    }
}

} // namespace Slang