From 261b2f1f2bc13ccf7db5ec68c825ffc7b0781f7f Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 12 Jul 2023 16:00:05 -0700 Subject: Use scratchData on `IRInst` to replace HashSets. (#2978) * Use scratchData on `IRInst` to replace HashSets. * Update test results. * Initialize scratchData. * Update autodiff documentation. * Use enum instead of bool. --------- Co-authored-by: Yong He --- source/slang/slang-ir.h | 50 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'source/slang/slang-ir.h') diff --git a/source/slang/slang-ir.h b/source/slang/slang-ir.h index 44dc585ab..e7c7f4fb2 100644 --- a/source/slang/slang-ir.h +++ b/source/slang/slang-ir.h @@ -548,6 +548,12 @@ private: IRInst* m_inst = nullptr; }; +enum class SideEffectAnalysisOptions +{ + None, + UseDominanceTree, +}; + // Every value in the IR is an instruction (even things // like literal values). // @@ -733,6 +739,11 @@ struct IRInst getOperands()[index].init(this, value); } + // Reserved memory space for use by individual IR passes. + // This field is not supposed to be valid outside an IR pass, + // and each IR pass should always treat it as uninitialized + // upon entry. + UInt64 scratchData = 0; // @@ -785,7 +796,7 @@ struct IRInst /// It is possible that this instruction has side effects? /// /// This is a conservative test, and will return `true` if an exact answer can't be determined. - bool mightHaveSideEffects(); + bool mightHaveSideEffects(SideEffectAnalysisOptions options = SideEffectAnalysisOptions::None); // RTTI support static bool isaImpl(IROp) { return true; } @@ -1971,12 +1982,41 @@ struct IRModule; // Description of an instruction to be used for global value numbering struct IRInstKey { - IRInst* inst; +private: + IRInst* inst = nullptr; + HashCode hashCode = 0; + HashCode _getHashCode(); - HashCode getHashCode(); -}; +public: + IRInstKey() = default; + IRInstKey(const IRInstKey& key) = default; + IRInstKey(IRInst* i) + : inst(i) + { + hashCode = _getHashCode(); + } + HashCode getHashCode() const { return hashCode; } + IRInst* getInst() const { return inst; } -bool operator==(IRInstKey const& left, IRInstKey const& right); + bool operator==(IRInstKey const& right) const + { + if (hashCode != right.getHashCode()) return false; + if (getInst()->getOp() != right.getInst()->getOp()) return false; + if (getInst()->getFullType() != right.getInst()->getFullType()) return false; + if (getInst()->operandCount != right.getInst()->operandCount) return false; + + auto argCount = getInst()->operandCount; + auto leftArgs = getInst()->getOperands(); + auto rightArgs = right.getInst()->getOperands(); + for (UInt aa = 0; aa < argCount; ++aa) + { + if (leftArgs[aa].get() != rightArgs[aa].get()) + return false; + } + + return true; + } +}; struct IRConstantKey { -- cgit v1.2.3