From b820f34a1b6336af184458c5b1dfe2273c99f1ff Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 18 Aug 2020 13:08:45 -0700 Subject: Support initializing an existential value from a generic value. (#1503) * Support initializing an existential value from a generic value. * Remove trailing spaces and clean up debugging code. --- source/slang/slang-ir-augment-make-existential.cpp | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 source/slang/slang-ir-augment-make-existential.cpp (limited to 'source/slang/slang-ir-augment-make-existential.cpp') diff --git a/source/slang/slang-ir-augment-make-existential.cpp b/source/slang/slang-ir-augment-make-existential.cpp new file mode 100644 index 000000000..18d061276 --- /dev/null +++ b/source/slang/slang-ir-augment-make-existential.cpp @@ -0,0 +1,84 @@ +#include "slang-ir-augment-make-existential.h" +#include "slang-ir-insts.h" +#include "slang-ir.h" + +namespace Slang +{ +struct AugmentMakeExistentialContext +{ + IRModule* module; + + SharedIRBuilder sharedBuilderStorage; + + List workList; + HashSet workListSet; + + void addToWorkList(IRInst* inst) + { + if (workListSet.Contains(inst)) + return; + + workList.add(inst); + workListSet.Add(inst); + } + + void processMakeExistential(IRMakeExistential* inst) + { + IRBuilder builderStorage; + auto builder = &builderStorage; + builder->sharedBuilder = &sharedBuilderStorage; + builder->setInsertBefore(inst); + + auto augInst = builder->emitMakeExistentialWithRTTI( + inst->getFullType(), + inst->getWrappedValue(), + inst->getWitnessTable(), + inst->getWrappedValue()->getDataType()); + inst->replaceUsesWith(augInst); + inst->removeAndDeallocate(); + } + + void processInst(IRInst* inst) + { + switch (inst->op) + { + case kIROp_MakeExistential: + processMakeExistential((IRMakeExistential*)inst); + break; + default: + break; + } + } + + void processModule() + { + SharedIRBuilder* sharedBuilder = &sharedBuilderStorage; + sharedBuilder->module = module; + sharedBuilder->session = module->session; + + addToWorkList(module->getModuleInst()); + + while (workList.getCount() != 0) + { + IRInst* inst = workList.getLast(); + + workList.removeLast(); + workListSet.Remove(inst); + + processInst(inst); + + for (auto child = inst->getLastChild(); child; child = child->getPrevInst()) + { + addToWorkList(child); + } + } + } +}; + +void augmentMakeExistentialInsts(IRModule* module) +{ + AugmentMakeExistentialContext context; + context.module = module; + context.processModule(); +} +} // namespace Slang -- cgit v1.2.3