summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-lower-com-methods.cpp
blob: d51807aa1153e854149922a8f7d48e651b9f6f38 (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
// slang-ir-lower-com-methods.cpp

#include "slang-ir-lower-com-methods.h"

#include "slang-ir-inst-pass-base.h"
#include "slang-ir-insts.h"
#include "slang-ir-marshal-native-call.h"
#include "slang-ir-util.h"
#include "slang-ir.h"

namespace Slang
{

struct ComMethodLoweringContext : public InstPassBase
{
    DiagnosticSink* diagnosticSink = nullptr;

    NativeCallMarshallingContext marshal;

    OrderedHashSet<IRLookupWitnessMethod*> comCallees;

    ComMethodLoweringContext(IRModule* inModule)
        : InstPassBase(inModule)
    {
    }

    void processComCall(IRCall* comCall)
    {
        IRBuilder builder(module);
        builder.setInsertBefore(comCall);
        auto callee = as<IRLookupWitnessMethod>(comCall->getCallee());
        SLANG_ASSERT(callee);

        IRLookupWitnessMethod* innerMostCallee = callee;
        while (innerMostCallee->getOperand(0)->getOp() == kIROp_LookupWitnessMethod)
        {
            innerMostCallee = as<IRLookupWitnessMethod>(innerMostCallee->getOperand(0));
        }
        if (callee != innerMostCallee)
        {
            callee = (IRLookupWitnessMethod*)builder.emitLookupInterfaceMethodInst(
                callee->getDataType(),
                innerMostCallee->getWitnessTable(),
                callee->getRequirementKey());
        }
        comCallees.add(callee);

        auto calleeType = as<IRFuncType>(callee->getDataType());
        SLANG_ASSERT(calleeType);

        auto nativeFuncType = marshal.getNativeFuncType(builder, calleeType);
        ShortList<IRInst*> args;
        for (UInt i = 0; i < comCall->getArgCount(); i++)
            args.add(comCall->getArg(i));
        auto currentBlock = builder.getBlock();
        auto nextInst = comCall->getNextInst();
        auto newResult = marshal.marshalNativeCall(
            builder,
            calleeType,
            nativeFuncType,
            callee,
            args.getCount(),
            args.getArrayView().getBuffer());

        comCall->replaceUsesWith(newResult);
        if (builder.getBlock() != currentBlock)
        {
            // `marshalNativeCall` may have replaced the original call with branch insts.
            // If this is the case, we need to move all insts after the original call in the
            // original basic block to the new basic block.
            while (nextInst)
            {
                auto next = nextInst->getNextInst();
                nextInst->removeFromParent();
                nextInst->insertAtEnd(builder.getBlock());
                nextInst = next;
            }
        }
        comCall->removeAndDeallocate();
    }

    void processCall(IRCall* inst)
    {
        auto funcValue = inst->getOperand(0);

        // Detect if this is a call into a COM interface method.
        if (funcValue->getOp() == kIROp_LookupWitnessMethod)
        {
            const auto operand0TypeOp = funcValue->getOperand(0)->getDataType();
            if (auto tableType = as<IRWitnessTableTypeBase>(operand0TypeOp))
            {
                if (tableType->getConformanceType()->findDecoration<IRComInterfaceDecoration>())
                {
                    processComCall(inst);
                    return;
                }
            }
        }
    }

    void processInterfaceType(IRInterfaceType* interfaceType)
    {
        if (!interfaceType->findDecoration<IRComInterfaceDecoration>())
            return;
        IRBuilder builder(module);
        for (UInt i = 0; i < interfaceType->getOperandCount(); i++)
        {
            auto entry = as<IRInterfaceRequirementEntry>(interfaceType->getOperand(i));
            if (!entry)
                continue;
            if (auto funcType = as<IRFuncType>(entry->getRequirementVal()))
            {
                builder.setInsertBefore(funcType);
                entry->setRequirementVal(marshal.getNativeFuncType(builder, funcType));
            }
        }
    }

    void processWitnessTable(IRWitnessTable* witnessTable)
    {
        auto interfaceType = as<IRInterfaceType>(witnessTable->getConformanceType());
        if (!interfaceType)
            return;
        if (!interfaceType->findDecoration<IRComInterfaceDecoration>())
            return;
        auto interfaceReqDict = buildInterfaceRequirementDict(interfaceType);

        IRBuilder builder(module);
        NativeCallMarshallingContext marshalContext;
        marshalContext.diagnosticSink = diagnosticSink;
        for (auto entry : witnessTable->getEntries())
        {
            IRInst* interfaceRequirement = nullptr;
            if (!interfaceReqDict.tryGetValue(entry->getRequirementKey(), interfaceRequirement))
                continue;
            auto implFunc = as<IRFunc>(entry->getSatisfyingVal());
            if (!implFunc)
                continue;
            // If the function already has the same signature as the lowered COM interface method,
            // we don't need to do anything.
            if (isTypeEqual(
                    entry->getSatisfyingVal()->getDataType(),
                    (IRType*)interfaceRequirement))
                continue;
            // Now we need to generate a wrapper function that calls into the original one.
            auto nativeFunc = marshalContext.generateDLLExportWrapperFunc(builder, implFunc);
            entry->setOperand(1, nativeFunc);
        }

        auto classType = witnessTable->getConcreteType();
        builder.addCOMWitnessDecoration(classType, witnessTable);
    }

    void processModule()
    {
        // Translate all Calls to interface methods.
        processInstsOfType<IRCall>(kIROp_Call, [this](IRCall* inst) { processCall(inst); });

        // Update functypes of com callees.
        for (auto callee : comCallees)
        {
            IRBuilder builder(module);
            builder.setInsertBefore(callee);
            auto nativeType =
                marshal.getNativeFuncType(builder, as<IRFuncType>(callee->getDataType()));
            callee->setFullType(nativeType);
        }

        // Update func types of COM interfaces.
        processInstsOfType<IRInterfaceType>(
            kIROp_InterfaceType,
            [this](IRInterfaceType* inst) { processInterfaceType(inst); });

        // Update witness tables of classes that implement COM interfaces.
        // Generate native-to-managed wrappers for each witness table entry.
        processInstsOfType<IRWitnessTable>(
            kIROp_WitnessTable,
            [this](IRWitnessTable* table) { processWitnessTable(table); });
    }
};

void lowerComMethods(IRModule* module, DiagnosticSink* sink)
{
    ComMethodLoweringContext context(module);
    context.diagnosticSink = sink;
    context.marshal.diagnosticSink = sink;

    return context.processModule();
}
} // namespace Slang