From d245c72f2a92a74ccda83f41758c1948ae5132d3 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 24 Aug 2022 10:56:53 -0700 Subject: Compiler time evaluation of all int and bool operators. (#2376) * Compiler time evaluation of all int and bool operators. * Fix linux compile error. * Fix. Co-authored-by: Yong He --- source/slang/slang-ir.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) (limited to 'source/slang/slang-ir.h') diff --git a/source/slang/slang-ir.h b/source/slang/slang-ir.h index aea425a9b..c48f4b378 100644 --- a/source/slang/slang-ir.h +++ b/source/slang/slang-ir.h @@ -203,6 +203,36 @@ struct IRInstList : IRInstListBase Iterator end(); }; +template +struct IRFilteredInstList : IRInstListBase +{ + IRFilteredInstList() {} + + IRFilteredInstList(IRInst* fst, IRInst* lst); + + explicit IRFilteredInstList(IRInstListBase const& list) + : IRFilteredInstList(list.first, list.last) + {} + + T* getFirst() { return (T*)first; } + T* getLast() { return (T*)last; } + + struct Iterator : public IRInstListBase::Iterator + { + IRInst* exclusiveLast; + Iterator() {} + Iterator(IRInst* inst, IRInst* lastIter) : IRInstListBase::Iterator(inst), exclusiveLast(lastIter) {} + void operator++(); + T* operator*() + { + return (T*)inst; + } + }; + + Iterator begin(); + Iterator end(); +}; + /// A list of contiguous operands that can be iterated over as `IRInst`s. struct IROperandListBase { @@ -741,6 +771,41 @@ typename IRInstList::Iterator IRInstList::end() return Iterator(last ? last->next : nullptr); } +template +IRFilteredInstList::IRFilteredInstList(IRInst* fst, IRInst* lst) +{ + first = fst; + last = lst; + + auto lastIter = last ? last->next : nullptr; + while (first != lastIter && !as(first)) + first = first->next; + while (last && last != first && !as(last)) + last = last->prev; +} + +template +void IRFilteredInstList::Iterator::operator++() +{ + inst = inst->next; + while (inst != exclusiveLast && !as(inst)) + { + inst = inst->next; + } +} +template +typename IRFilteredInstList::Iterator IRFilteredInstList::begin() +{ + auto lastIter = last ? last->next : nullptr; + return IRFilteredInstList::Iterator(first, lastIter); +} + +template +typename IRFilteredInstList::Iterator IRFilteredInstList::end() +{ + auto lastIter = last ? last->next : nullptr; + return IRFilteredInstList::Iterator(lastIter, lastIter); +} // Types @@ -1419,14 +1484,14 @@ struct IRStructField : IRInst // struct IRStructType : IRType { - IRInstList getFields() { return IRInstList(getChildren()); } + IRFilteredInstList getFields() { return IRFilteredInstList(getChildren()); } IR_LEAF_ISA(StructType) }; struct IRClassType : IRType { - IRInstList getFields() { return IRInstList(getChildren()); } + IRFilteredInstList getFields() { return IRFilteredInstList(getChildren()); } IR_LEAF_ISA(ClassType) }; -- cgit v1.2.3