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

#include "slang-ir-any-value-inference.h"
#include "slang-ir-any-value-marshalling.h"
#include "slang-ir-insts.h"
#include "slang-ir-layout.h"
#include "slang-ir.h"

namespace Slang
{

struct ReinterpretLoweringContext
{
    TargetProgram* targetProgram;
    DiagnosticSink* sink;
    IRModule* module;
    OrderedHashSet<IRInst*> workList;

    void addToWorkList(IRInst* inst)
    {
        if (workList.contains(inst))
            return;

        workList.add(inst);
    }

    void processInst(IRInst* inst)
    {
        switch (inst->getOp())
        {
        case kIROp_Reinterpret:
            processReinterpret(inst);
            break;
        default:
            break;
        }
    }

    void processModule()
    {
        addToWorkList(module->getModuleInst());

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

            workList.removeLast();

            processInst(inst);

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

    void processReinterpret(IRInst* inst)
    {
        auto operand = inst->getOperand(0);
        auto fromType = operand->getDataType();
        auto toType = inst->getDataType();
        SlangInt fromTypeSize = getAnyValueSize(fromType);
        if (fromTypeSize < 0)
        {
            sink->diagnose(
                inst->sourceLoc,
                Slang::Diagnostics::typeCannotBePackedIntoAnyValue,
                fromType);
        }
        SlangInt toTypeSize = getAnyValueSize(toType);
        if (toTypeSize < 0)
        {
            sink->diagnose(
                inst->sourceLoc,
                Slang::Diagnostics::typeCannotBePackedIntoAnyValue,
                toType);
        }
        SlangInt anyValueSize = Math::Max(fromTypeSize, toTypeSize);

        IRBuilder builder(module);
        builder.setInsertBefore(inst);
        auto anyValueType =
            builder.getAnyValueType(builder.getIntValue(builder.getUIntType(), anyValueSize));
        auto packInst = builder.emitPackAnyValue(anyValueType, operand);
        auto unpackInst = builder.emitUnpackAnyValue(toType, packInst);
        inst->replaceUsesWith(unpackInst);
        inst->removeAndDeallocate();
    }
};

void lowerReinterpret(TargetProgram* target, IRModule* module, DiagnosticSink* sink)
{
    // Before processing reinterpret insts, ensure that existential types without
    // user-defined sizes have inferred sizes where possible.
    //
    inferAnyValueSizeWhereNecessary(target, module);

    ReinterpretLoweringContext context;
    context.module = module;
    context.targetProgram = target;
    context.sink = sink;
    context.processModule();
}

} // namespace Slang