|
| 1 | +// slang-ir-lower-generic-existential.cpp |
| 2 | + |
| 3 | +#include "slang-ir-lower-existential.h" |
| 4 | +#include "slang-ir-generics-lowering-context.h" |
| 5 | +#include "slang-ir.h" |
| 6 | +#include "slang-ir-insts.h" |
| 7 | + |
| 8 | +namespace Slang |
| 9 | +{ |
| 10 | + struct ExistentialLoweringContext |
| 11 | + { |
| 12 | + SharedGenericsLoweringContext* sharedContext; |
| 13 | + |
| 14 | + void processMakeExistential(IRMakeExistential* inst) |
| 15 | + { |
| 16 | + IRBuilder builderStorage; |
| 17 | + auto builder = &builderStorage; |
| 18 | + builder->sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 19 | + builder->setInsertBefore(inst); |
| 20 | + |
| 21 | + auto value = inst->getWrappedValue(); |
| 22 | + auto valueType = sharedContext->lowerType(builder, value->getDataType()); |
| 23 | + auto witnessTableType = cast<IRWitnessTableType>(inst->getWitnessTable()->getDataType()); |
| 24 | + auto interfaceType = witnessTableType->getConformanceType(); |
| 25 | + auto anyValueSize = sharedContext->getInterfaceAnyValueSize(interfaceType, inst->sourceLoc); |
| 26 | + auto anyValueType = builder->getAnyValueType(anyValueSize); |
| 27 | + auto rttiType = builder->getPtrType(builder->getRTTIType()); |
| 28 | + auto tupleType = builder->getTupleType(anyValueType, witnessTableType, rttiType); |
| 29 | + |
| 30 | + IRInst* rttiObject = nullptr; |
| 31 | + if (valueType->op != kIROp_AnyValueType) |
| 32 | + { |
| 33 | + rttiObject = sharedContext->maybeEmitRTTIObject(valueType); |
| 34 | + rttiObject = builder->emitGetAddress( |
| 35 | + builder->getPtrType(builder->getRTTIType()), |
| 36 | + rttiObject); |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + rttiObject = valueType; |
| 41 | + } |
| 42 | + IRInst* packedValue = value; |
| 43 | + if (valueType->op != kIROp_AnyValueType) |
| 44 | + packedValue = builder->emitPackAnyValue(anyValueType, value); |
| 45 | + IRInst* tupleArgs[] = { packedValue, inst->getWitnessTable(), rttiObject }; |
| 46 | + auto tuple = builder->emitMakeTuple(tupleType, 3, tupleArgs); |
| 47 | + inst->replaceUsesWith(tuple); |
| 48 | + inst->removeAndDeallocate(); |
| 49 | + } |
| 50 | + |
| 51 | + IRInst* extractTupleElement(IRBuilder* builder, IRInst* value, UInt index) |
| 52 | + { |
| 53 | + auto tupleType = cast<IRTupleType>(sharedContext->lowerType(builder, value->getDataType())); |
| 54 | + auto getElement = builder->emitGetTupleElement( |
| 55 | + (IRType*)tupleType->getOperand(index), |
| 56 | + value, |
| 57 | + index); |
| 58 | + return getElement; |
| 59 | + } |
| 60 | + |
| 61 | + void processExtractExistentialElement(IRInst* extractInst, UInt elementId) |
| 62 | + { |
| 63 | + IRBuilder builderStorage; |
| 64 | + auto builder = &builderStorage; |
| 65 | + builder->sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 66 | + builder->setInsertBefore(extractInst); |
| 67 | + |
| 68 | + auto element = extractTupleElement(builder, extractInst->getOperand(0), elementId); |
| 69 | + extractInst->replaceUsesWith(element); |
| 70 | + extractInst->removeAndDeallocate(); |
| 71 | + } |
| 72 | + |
| 73 | + void processExtractExistentialValue(IRExtractExistentialValue* inst) |
| 74 | + { |
| 75 | + processExtractExistentialElement(inst, 0); |
| 76 | + } |
| 77 | + |
| 78 | + void processExtractExistentialWitnessTable(IRExtractExistentialWitnessTable* inst) |
| 79 | + { |
| 80 | + processExtractExistentialElement(inst, 1); |
| 81 | + } |
| 82 | + |
| 83 | + void processExtractExistentialType(IRExtractExistentialType* inst) |
| 84 | + { |
| 85 | + processExtractExistentialElement(inst, 2); |
| 86 | + } |
| 87 | + |
| 88 | + void processInst(IRInst* inst) |
| 89 | + { |
| 90 | + if (auto makeExistential = as<IRMakeExistential>(inst)) |
| 91 | + { |
| 92 | + processMakeExistential(makeExistential); |
| 93 | + } |
| 94 | + else if (auto extractExistentialVal = as<IRExtractExistentialValue>(inst)) |
| 95 | + { |
| 96 | + processExtractExistentialValue(extractExistentialVal); |
| 97 | + } |
| 98 | + else if (auto extractExistentialType = as<IRExtractExistentialType>(inst)) |
| 99 | + { |
| 100 | + processExtractExistentialType(extractExistentialType); |
| 101 | + } |
| 102 | + else if (auto extractExistentialWitnessTable = as<IRExtractExistentialWitnessTable>(inst)) |
| 103 | + { |
| 104 | + processExtractExistentialWitnessTable(extractExistentialWitnessTable); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + void processModule() |
| 109 | + { |
| 110 | + // We start by initializing our shared IR building state, |
| 111 | + // since we will re-use that state for any code we |
| 112 | + // generate along the way. |
| 113 | + // |
| 114 | + SharedIRBuilder* sharedBuilder = &sharedContext->sharedBuilderStorage; |
| 115 | + sharedBuilder->module = sharedContext->module; |
| 116 | + sharedBuilder->session = sharedContext->module->session; |
| 117 | + |
| 118 | + sharedContext->addToWorkList(sharedContext->module->getModuleInst()); |
| 119 | + |
| 120 | + while (sharedContext->workList.getCount() != 0) |
| 121 | + { |
| 122 | + // We will then iterate until our work list goes dry. |
| 123 | + // |
| 124 | + while (sharedContext->workList.getCount() != 0) |
| 125 | + { |
| 126 | + IRInst* inst = sharedContext->workList.getLast(); |
| 127 | + |
| 128 | + sharedContext->workList.removeLast(); |
| 129 | + sharedContext->workListSet.Remove(inst); |
| 130 | + |
| 131 | + processInst(inst); |
| 132 | + |
| 133 | + for (auto child = inst->getLastChild(); child; child = child->getPrevInst()) |
| 134 | + { |
| 135 | + sharedContext->addToWorkList(child); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + |
| 143 | + void lowerExistentials(SharedGenericsLoweringContext* sharedContext) |
| 144 | + { |
| 145 | + ExistentialLoweringContext context; |
| 146 | + context.sharedContext = sharedContext; |
| 147 | + context.processModule(); |
| 148 | + } |
| 149 | +} |
0 commit comments