|
| 1 | +#include "slang-ir-specialize-dispatch.h" |
| 2 | + |
| 3 | +#include "slang-ir-generics-lowering-context.h" |
| 4 | +#include "slang-ir-insts.h" |
| 5 | +#include "slang-ir.h" |
| 6 | + |
| 7 | +namespace Slang |
| 8 | +{ |
| 9 | + |
| 10 | +struct AssociatedTypeLookupSpecializationContext |
| 11 | +{ |
| 12 | + SharedGenericsLoweringContext* sharedContext; |
| 13 | + |
| 14 | + IRFunc* createWitnessTableLookupFunc(IRInterfaceType* interfaceType, IRInst* key) |
| 15 | + { |
| 16 | + IRBuilder builder; |
| 17 | + builder.sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 18 | + builder.setInsertBefore(interfaceType); |
| 19 | + |
| 20 | + auto inputWitnessTableIDType = builder.getWitnessTableIDType(interfaceType); |
| 21 | + auto requirementEntry = sharedContext->findInterfaceRequirementVal(interfaceType, key); |
| 22 | + |
| 23 | + auto resultWitnessTableType = cast<IRWitnessTableType>(requirementEntry); |
| 24 | + auto resultWitnessTableIDType = |
| 25 | + builder.getWitnessTableIDType((IRType*)resultWitnessTableType->getConformanceType()); |
| 26 | + |
| 27 | + auto funcType = |
| 28 | + builder.getFuncType(1, (IRType**)&inputWitnessTableIDType, resultWitnessTableIDType); |
| 29 | + auto func = builder.createFunc(); |
| 30 | + func->setFullType(funcType); |
| 31 | + |
| 32 | + if (auto linkage = key->findDecoration<IRLinkageDecoration>()) |
| 33 | + builder.addNameHintDecoration(func, linkage->getMangledName()); |
| 34 | + |
| 35 | + builder.setInsertInto(func); |
| 36 | + |
| 37 | + auto block = builder.emitBlock(); |
| 38 | + auto witnessTableParam = builder.emitParam(inputWitnessTableIDType); |
| 39 | + |
| 40 | + // Collect all witness tables of `witnessTableType` in current module. |
| 41 | + List<IRWitnessTable*> witnessTables = |
| 42 | + sharedContext->getWitnessTablesFromInterfaceType(interfaceType); |
| 43 | + |
| 44 | + // Generate case blocks for each possible witness table. |
| 45 | + IRBlock* defaultBlock = nullptr; |
| 46 | + List<IRInst*> caseBlocks; |
| 47 | + for (Index i = 0; i < witnessTables.getCount(); i++) |
| 48 | + { |
| 49 | + auto witnessTable = witnessTables[i]; |
| 50 | + auto seqIdDecoration = witnessTable->findDecoration<IRSequentialIDDecoration>(); |
| 51 | + SLANG_ASSERT(seqIdDecoration); |
| 52 | + |
| 53 | + if (i != witnessTables.getCount() - 1) |
| 54 | + { |
| 55 | + // Create a case block if we are not the last case. |
| 56 | + caseBlocks.add(seqIdDecoration->getSequentialIDOperand()); |
| 57 | + builder.setInsertInto(func); |
| 58 | + auto caseBlock = builder.emitBlock(); |
| 59 | + caseBlocks.add(caseBlock); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + // Generate code for the last possible value in the `default` block. |
| 64 | + builder.setInsertInto(func); |
| 65 | + defaultBlock = builder.emitBlock(); |
| 66 | + builder.setInsertInto(defaultBlock); |
| 67 | + } |
| 68 | + |
| 69 | + auto resultWitnessTable = sharedContext->findWitnessTableEntry(witnessTable, key); |
| 70 | + auto resultWitnessTableIDDecoration = |
| 71 | + resultWitnessTable->findDecoration<IRSequentialIDDecoration>(); |
| 72 | + SLANG_ASSERT(resultWitnessTableIDDecoration); |
| 73 | + builder.emitReturn(resultWitnessTableIDDecoration->getSequentialIDOperand()); |
| 74 | + } |
| 75 | + |
| 76 | + // Emit a switch statement to return the correct witness table ID based on |
| 77 | + // the witness table ID passed in. |
| 78 | + builder.setInsertInto(func); |
| 79 | + auto breakBlock = builder.emitBlock(); |
| 80 | + builder.setInsertInto(breakBlock); |
| 81 | + builder.emitUnreachable(); |
| 82 | + |
| 83 | + builder.setInsertInto(block); |
| 84 | + builder.emitSwitch( |
| 85 | + witnessTableParam, |
| 86 | + breakBlock, |
| 87 | + defaultBlock, |
| 88 | + caseBlocks.getCount(), |
| 89 | + caseBlocks.getBuffer()); |
| 90 | + |
| 91 | + return func; |
| 92 | + } |
| 93 | + |
| 94 | + // Retrieves the conformance type from a WitnessTableType or a WitnessTableIDType. |
| 95 | + IRInterfaceType* getInterfaceTypeFromWitnessTableTypes(IRInst* witnessTableType) |
| 96 | + { |
| 97 | + switch (witnessTableType->op) |
| 98 | + { |
| 99 | + case kIROp_WitnessTableType: |
| 100 | + return cast<IRInterfaceType>( |
| 101 | + cast<IRWitnessTableType>(witnessTableType)->getConformanceType()); |
| 102 | + case kIROp_WitnessTableIDType: |
| 103 | + return cast<IRInterfaceType>( |
| 104 | + cast<IRWitnessTableIDType>(witnessTableType)->getConformanceType()); |
| 105 | + default: |
| 106 | + return nullptr; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + void processLookupInterfaceMethodInst(IRLookupWitnessMethod* inst) |
| 111 | + { |
| 112 | + // Ignore lookups for RTTI objects for now, since they are not used anywhere. |
| 113 | + if (!as<IRWitnessTableType>(inst->getDataType())) |
| 114 | + return; |
| 115 | + |
| 116 | + // Replace all witness table lookups with calls to specialized functions that directly |
| 117 | + // returns the sequential ID of the resulting witness table, effectively getting rid |
| 118 | + // of actual witness table objects in the target code (they all become IDs). |
| 119 | + auto witnessTableType = inst->getWitnessTable()->getDataType(); |
| 120 | + IRInterfaceType* interfaceType = getInterfaceTypeFromWitnessTableTypes(witnessTableType); |
| 121 | + if (!interfaceType) |
| 122 | + return; |
| 123 | + auto key = inst->getRequirementKey(); |
| 124 | + IRFunc* func = nullptr; |
| 125 | + if (!sharedContext->mapInterfaceRequirementKeyToDispatchMethods.TryGetValue(key, func)) |
| 126 | + { |
| 127 | + func = createWitnessTableLookupFunc(interfaceType, key); |
| 128 | + sharedContext->mapInterfaceRequirementKeyToDispatchMethods[key] = func; |
| 129 | + } |
| 130 | + IRBuilder builder; |
| 131 | + builder.sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 132 | + builder.setInsertBefore(inst); |
| 133 | + auto witnessTableArg = inst->getWitnessTable(); |
| 134 | + if (witnessTableArg->getDataType()->op == kIROp_WitnessTableType) |
| 135 | + { |
| 136 | + witnessTableArg = builder.emitGetSequentialIDInst(witnessTableArg); |
| 137 | + } |
| 138 | + auto callInst = builder.emitCallInst( |
| 139 | + builder.getWitnessTableIDType(interfaceType), func, witnessTableArg); |
| 140 | + inst->replaceUsesWith(callInst); |
| 141 | + inst->removeAndDeallocate(); |
| 142 | + } |
| 143 | + |
| 144 | + void cleanUpWitnessTableIDType() |
| 145 | + { |
| 146 | + List<IRInst*> instsToRemove; |
| 147 | + for (auto inst : sharedContext->module->getGlobalInsts()) |
| 148 | + { |
| 149 | + if (inst->op == kIROp_WitnessTableIDType) |
| 150 | + { |
| 151 | + IRBuilder builder; |
| 152 | + builder.sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 153 | + builder.setInsertBefore(inst); |
| 154 | + inst->replaceUsesWith(builder.getUIntType()); |
| 155 | + instsToRemove.add(inst); |
| 156 | + } |
| 157 | + } |
| 158 | + for (auto inst : instsToRemove) |
| 159 | + inst->removeAndDeallocate(); |
| 160 | + } |
| 161 | + |
| 162 | + void processGetSequentialIDInst(IRGetSequentialID* inst) |
| 163 | + { |
| 164 | + if (inst->getRTTIOperand()->getDataType()->op == kIROp_WitnessTableIDType) |
| 165 | + { |
| 166 | + inst->replaceUsesWith(inst->getRTTIOperand()); |
| 167 | + inst->removeAndDeallocate(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + void processModule() |
| 172 | + { |
| 173 | + SharedIRBuilder* sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 174 | + sharedBuilder->module = sharedContext->module; |
| 175 | + sharedBuilder->session = sharedContext->module->session; |
| 176 | + |
| 177 | + sharedContext->addToWorkList(sharedContext->module->getModuleInst()); |
| 178 | + |
| 179 | + while (sharedContext->workList.getCount() != 0) |
| 180 | + { |
| 181 | + IRInst* inst = sharedContext->workList.getLast(); |
| 182 | + |
| 183 | + sharedContext->workList.removeLast(); |
| 184 | + sharedContext->workListSet.Remove(inst); |
| 185 | + |
| 186 | + if (inst->op == kIROp_lookup_interface_method) |
| 187 | + { |
| 188 | + processLookupInterfaceMethodInst(cast<IRLookupWitnessMethod>(inst)); |
| 189 | + } |
| 190 | + |
| 191 | + for (auto child = inst->getLastChild(); child; child = child->getPrevInst()) |
| 192 | + { |
| 193 | + sharedContext->addToWorkList(child); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // `GetSequentialID(WitnessTableIDOperand)` becomes just `WitnessTableIDOperand`. |
| 198 | + sharedContext->addToWorkList(sharedContext->module->getModuleInst()); |
| 199 | + while (sharedContext->workList.getCount() != 0) |
| 200 | + { |
| 201 | + IRInst* inst = sharedContext->workList.getLast(); |
| 202 | + |
| 203 | + sharedContext->workList.removeLast(); |
| 204 | + sharedContext->workListSet.Remove(inst); |
| 205 | + |
| 206 | + if (inst->op == kIROp_GetSequentialID) |
| 207 | + { |
| 208 | + processGetSequentialIDInst(cast<IRGetSequentialID>(inst)); |
| 209 | + } |
| 210 | + |
| 211 | + for (auto child = inst->getLastChild(); child; child = child->getPrevInst()) |
| 212 | + { |
| 213 | + sharedContext->addToWorkList(child); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + cleanUpWitnessTableIDType(); |
| 218 | + } |
| 219 | +}; |
| 220 | + |
| 221 | +void specializeDynamicAssociatedTypeLookup(SharedGenericsLoweringContext* sharedContext) |
| 222 | +{ |
| 223 | + AssociatedTypeLookupSpecializationContext context; |
| 224 | + context.sharedContext = sharedContext; |
| 225 | + context.processModule(); |
| 226 | +} |
| 227 | + |
| 228 | +} // namespace Slang |
0 commit comments