@@ -1605,7 +1605,11 @@ static IRInst* pickLaterInstInSameParent(IRInst* left, IRInst* right)
1605
1605
}
1606
1606
}
1607
1607
1608
- static IRInst* getParentOfHoistableInst (IRBuilder* builder, IRInst* inst)
1608
+ // Given an instruction that represents a constant, a type, etc.
1609
+ // Try to "hoist" it as far toward the global scope as possible
1610
+ // to insert it at a location where it will be maximally visible.
1611
+ //
1612
+ void addHoistableInst (IRBuilder* builder, IRInst* inst)
1609
1613
{
1610
1614
// Start with the assumption that we would insert this instruction
1611
1615
// into the global scope (the instruction that represents the module)
@@ -1639,17 +1643,6 @@ static IRInst* getParentOfHoistableInst(IRBuilder* builder, IRInst* inst)
1639
1643
//
1640
1644
SLANG_ASSERT (parent);
1641
1645
1642
- return parent;
1643
- }
1644
-
1645
- // Given an instruction that represents a constant, a type, etc.
1646
- // Try to "hoist" it as far toward the global scope as possible
1647
- // to insert it at a location where it will be maximally visible.
1648
- //
1649
- void addHoistableInst (IRBuilder* builder, IRInst* inst)
1650
- {
1651
- IRInst* parent = getParentOfHoistableInst (builder, inst);
1652
-
1653
1646
// Once we determine the parent instruction that the
1654
1647
// new instruction should be inserted into, we need
1655
1648
// to find an appropriate place to insert it.
@@ -1714,7 +1707,6 @@ void addHoistableInst(IRBuilder* builder, IRInst* inst)
1714
1707
// the operands of `inst` come from the same
1715
1708
// block that we insert after them.
1716
1709
//
1717
- UInt operandCount = inst->getOperandCount ();
1718
1710
for (UInt ii = 0 ; ii < operandCount; ++ii)
1719
1711
{
1720
1712
auto operand = inst->getOperand (ii);
@@ -1750,15 +1742,29 @@ void addHoistableInst(IRBuilder* builder, IRInst* inst)
1750
1742
}
1751
1743
}
1752
1744
1753
- // This function finds where a parent should be for the given inst,
1754
- // and add the inst as a last child.
1755
- // When the inst is marked as Hoistable but the intention is only to de-duplicate it,
1756
- // the inst can be added in a simpler manner.
1757
- void addDeduplicatedInst (IRBuilder * builder, IRInst * inst)
1745
+ // Add the given inst to the parent of its operand.
1746
+ void addInst (IRInst* inst)
1758
1747
{
1759
1748
SLANG_ASSERT (nullptr == inst->parent );
1760
1749
1761
- IRInst* parent = getParentOfHoistableInst (builder, inst);
1750
+ IRInst* parent = nullptr ;
1751
+
1752
+ UInt operandCount = inst->getOperandCount ();
1753
+ for (UInt ii = 0 ; ii < operandCount; ++ii)
1754
+ {
1755
+ auto operand = inst->getOperand (ii);
1756
+ if (!operand)
1757
+ continue ;
1758
+
1759
+ auto operandParent = operand->getParent ();
1760
+
1761
+ parent = mergeCandidateParentsForHoistableInst (parent, operandParent);
1762
+ }
1763
+
1764
+ if (inst->getFullType ())
1765
+ {
1766
+ parent = mergeCandidateParentsForHoistableInst (parent, inst->getFullType ()->getParent ());
1767
+ }
1762
1768
1763
1769
inst->insertAtEnd (parent);
1764
1770
}
@@ -2642,7 +2648,7 @@ IRInst* IRBuilder::_findOrEmitHoistableInst(
2642
2648
// In order to de-duplicate them, Witness-table is marked as Hoistable.
2643
2649
// But it is not exactly a hoistable type and it can be added simpler.
2644
2650
if (inst->getOp () == kIROp_WitnessTable )
2645
- addDeduplicatedInst ( this , inst);
2651
+ addInst ( inst);
2646
2652
else
2647
2653
addHoistableInst (this , inst);
2648
2654
}
0 commit comments