diff options
| author | Yong He <yonghe@outlook.com> | 2020-08-18 13:08:45 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-18 13:08:45 -0700 |
| commit | b820f34a1b6336af184458c5b1dfe2273c99f1ff (patch) | |
| tree | ca9986891127cfe07eda18bfbdb5094eef5677cc /source/slang/slang-ir-augment-make-existential.cpp | |
| parent | 9abcb6ea24dbc7184c3a2ad9f4458f63f8901928 (diff) | |
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.
Diffstat (limited to 'source/slang/slang-ir-augment-make-existential.cpp')
| -rw-r--r-- | source/slang/slang-ir-augment-make-existential.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
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<IRInst*> workList; + HashSet<IRInst*> 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 |
