Skip to content

Commit d19ff64

Browse files
committed
Simplfy addDeduplicatedInst into addInst
1 parent 34447fe commit d19ff64

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

source/slang/slang-ir.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,11 @@ static IRInst* pickLaterInstInSameParent(IRInst* left, IRInst* right)
16051605
}
16061606
}
16071607

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)
16091613
{
16101614
// Start with the assumption that we would insert this instruction
16111615
// into the global scope (the instruction that represents the module)
@@ -1639,17 +1643,6 @@ static IRInst* getParentOfHoistableInst(IRBuilder* builder, IRInst* inst)
16391643
//
16401644
SLANG_ASSERT(parent);
16411645

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-
16531646
// Once we determine the parent instruction that the
16541647
// new instruction should be inserted into, we need
16551648
// to find an appropriate place to insert it.
@@ -1714,7 +1707,6 @@ void addHoistableInst(IRBuilder* builder, IRInst* inst)
17141707
// the operands of `inst` come from the same
17151708
// block that we insert after them.
17161709
//
1717-
UInt operandCount = inst->getOperandCount();
17181710
for (UInt ii = 0; ii < operandCount; ++ii)
17191711
{
17201712
auto operand = inst->getOperand(ii);
@@ -1750,15 +1742,29 @@ void addHoistableInst(IRBuilder* builder, IRInst* inst)
17501742
}
17511743
}
17521744

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)
17581747
{
17591748
SLANG_ASSERT(nullptr == inst->parent);
17601749

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+
}
17621768

17631769
inst->insertAtEnd(parent);
17641770
}
@@ -2642,7 +2648,7 @@ IRInst* IRBuilder::_findOrEmitHoistableInst(
26422648
// In order to de-duplicate them, Witness-table is marked as Hoistable.
26432649
// But it is not exactly a hoistable type and it can be added simpler.
26442650
if (inst->getOp() == kIROp_WitnessTable)
2645-
addDeduplicatedInst(this, inst);
2651+
addInst(inst);
26462652
else
26472653
addHoistableInst(this, inst);
26482654
}

0 commit comments

Comments
 (0)