From 876968ccadf96ff592061c61855d77c6071f89f5 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 13 Aug 2020 12:17:59 -0700 Subject: IR support for Tuple types. (#1492) * Tuple types. * Fix x86 warning * Improved deduplication Co-authored-by: Tim Foley --- source/slang/slang-ir-deduplicate.cpp | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 source/slang/slang-ir-deduplicate.cpp (limited to 'source/slang/slang-ir-deduplicate.cpp') diff --git a/source/slang/slang-ir-deduplicate.cpp b/source/slang/slang-ir-deduplicate.cpp new file mode 100644 index 000000000..81bb2371e --- /dev/null +++ b/source/slang/slang-ir-deduplicate.cpp @@ -0,0 +1,80 @@ +#include "slang-ir-insts.h" + +namespace Slang +{ + struct DeduplicateContext + { + SharedIRBuilder* builder; + IRInst* addValue(IRInst* value) + { + if (!value) return nullptr; + if (as(value)) + return addTypeValue(value); + if (auto constValue = as(value)) + return addConstantValue(constValue); + return value; + } + IRInst* addConstantValue(IRConstant* value) + { + IRConstantKey key = { value }; + if (auto newValue = builder->constantMap.TryGetValue(key)) + return *newValue; + builder->constantMap[key] = value; + return value; + } + IRInst* addTypeValue(IRInst* value) + { + // Do not deduplicate struct types. + switch (value->op) + { + case kIROp_StructType: + return value; + default: + break; + } + + IRInstKey key = { value }; + if (auto newValue = builder->globalValueNumberingMap.TryGetValue(key)) + return *newValue; + + for (UInt i = 0; i < value->getOperandCount(); i++) + { + value->setOperand(i, addValue(value->getOperand(i))); + } + value->setFullType((IRType*)addValue(value->getFullType())); + builder->globalValueNumberingMap[key] = value; + return value; + } + }; + void SharedIRBuilder::deduplicateAndRebuildGlobalNumberingMap() + { + DeduplicateContext context; + context.builder = this; + bool changed = true; + constantMap.Clear(); + for (auto inst : module->getGlobalInsts()) + { + if (auto constVal = as(inst)) + { + context.addConstantValue(constVal); + } + } + globalValueNumberingMap.Clear(); + List instToRemove; + for (auto inst : module->getGlobalInsts()) + { + if (as(inst)) + { + auto newInst = context.addTypeValue(inst); + if (newInst != inst) + { + changed = true; + inst->replaceUsesWith(newInst); + instToRemove.add(inst); + } + } + } + for (auto inst : instToRemove) + inst->removeAndDeallocate(); + } +} -- cgit v1.2.3