summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-deduplicate.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2020-08-13 12:17:59 -0700
committerGitHub <noreply@github.com>2020-08-13 12:17:59 -0700
commit876968ccadf96ff592061c61855d77c6071f89f5 (patch)
tree7615ff80b7db8540d4a8034e699b3f1e3a58739e /source/slang/slang-ir-deduplicate.cpp
parent09adf10f646f01e177d412ba2d86602a51579b4f (diff)
IR support for Tuple types. (#1492)
* Tuple types. * Fix x86 warning * Improved deduplication Co-authored-by: Tim Foley <tfoleyNV@users.noreply.github.com>
Diffstat (limited to 'source/slang/slang-ir-deduplicate.cpp')
-rw-r--r--source/slang/slang-ir-deduplicate.cpp80
1 files changed, 80 insertions, 0 deletions
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<IRType>(value))
+ return addTypeValue(value);
+ if (auto constValue = as<IRConstant>(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<IRConstant>(inst))
+ {
+ context.addConstantValue(constVal);
+ }
+ }
+ globalValueNumberingMap.Clear();
+ List<IRInst*> instToRemove;
+ for (auto inst : module->getGlobalInsts())
+ {
+ if (as<IRType>(inst))
+ {
+ auto newInst = context.addTypeValue(inst);
+ if (newInst != inst)
+ {
+ changed = true;
+ inst->replaceUsesWith(newInst);
+ instToRemove.add(inst);
+ }
+ }
+ }
+ for (auto inst : instToRemove)
+ inst->removeAndDeallocate();
+ }
+}