Skip to content

Commit

Permalink
Remove any inlined globals
Browse files Browse the repository at this point in the history
This closes #5607.
  • Loading branch information
aleino-nv committed Dec 12, 2024
1 parent a1f011f commit f89d28a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
30 changes: 29 additions & 1 deletion source/slang/slang-ir-legalize-global-values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Slang
{

void GlobalInstInliningContextGeneric::inlineGlobalValues(IRModule * module)
void GlobalInstInliningContextGeneric::inlineGlobalValuesAndRemoveIfUnused(IRModule* module)
{
List<IRUse*> globalInstUsesToInline;

Expand All @@ -22,6 +22,7 @@ void GlobalInstInliningContextGeneric::inlineGlobalValues(IRModule * module)
}
}

HashSet<IRInst*> globalInstsToConsiderDeleting;
for (auto use : globalInstUsesToInline)
{
auto user = use->getUser();
Expand All @@ -30,7 +31,21 @@ void GlobalInstInliningContextGeneric::inlineGlobalValues(IRModule * module)
IRCloneEnv cloneEnv;
auto val = maybeInlineGlobalValue(builder, use->getUser(), use->get(), cloneEnv);
if (val != use->get())
{
// Since certain globals that appear in the IR are considered illegal for all targets,
// e.g. calls to functions, we delete the globals we've inlined.
// Note that the inlining is done such that none of the descendants of the global will
// have any uses either.
globalInstsToConsiderDeleting.add(use->usedValue);

builder.replaceOperand(use, val);
}
}

for (auto globalInst : globalInstsToConsiderDeleting)
{
if (!globalInst->hasUses())
globalInst->removeAndDeallocate();
}
}

Expand Down Expand Up @@ -124,6 +139,15 @@ bool GlobalInstInliningContextGeneric::isInlinableGlobalInst(IRInst* inst)

bool GlobalInstInliningContextGeneric::shouldInlineInstImpl(IRInst* inst)
{
// If 'inst' has an ancestor that is currently being inlined, then we
// better inline it since we'll be removing the ancestor.
bool ancestorShouldBeInlined = false;
for (IRInst* ancestor = inst->parent; ancestor != nullptr; ancestor = ancestor->parent)
if (m_mapGlobalInstToShouldInline.tryGetValue(inst, ancestorShouldBeInlined) && ancestorShouldBeInlined)
break;
if (ancestorShouldBeInlined)
return true;

if (!isInlinableGlobalInst(inst))
return false;
if (isLegalGlobalInst(inst))
Expand All @@ -148,6 +172,9 @@ bool GlobalInstInliningContextGeneric::shouldInlineInst(IRInst* inst)

IRInst* GlobalInstInliningContextGeneric::inlineInst(IRBuilder& builder, IRCloneEnv& cloneEnv, IRInst* inst)
{
// We rely on this dictionary in order to force inlining of any nodes with that should be inlined
SLANG_ASSERT(m_mapGlobalInstToShouldInline[inst]);

IRInst* result;
if (cloneEnv.mapOldValToNew.tryGetValue(inst, result))
return result;
Expand All @@ -169,6 +196,7 @@ IRInst* GlobalInstInliningContextGeneric::inlineInst(IRBuilder& builder, IRClone
}
for (auto child : inst->getChildren())
{
m_mapGlobalInstToShouldInline[child] = true;
inlineInst(subBuilder, cloneEnv, child);
}
return result;
Expand Down
3 changes: 2 additions & 1 deletion source/slang/slang-ir-legalize-global-values.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ struct GlobalInstInliningContextGeneric
virtual IRInst* getOutsideASM(IRInst* beforeInst) =0;

// Inline global values that can't represented by the target to their use sites.
void inlineGlobalValues(IRModule * module);
// If this leaves any global unused, then remove it.
void inlineGlobalValuesAndRemoveIfUnused(IRModule * module);

// Opcodes that can exist in global scope, as long as the operands are.
bool isLegalGlobalInst(IRInst* inst);
Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-ir-spirv-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ struct SPIRVLegalizationContext : public SourceEmitterBase
}
}

GlobalInstInliningContext().inlineGlobalValues(m_module);
GlobalInstInliningContext().inlineGlobalValuesAndRemoveIfUnused(m_module);

// Some legalization processing may change the function parameter types,
// so we need to update the function types to match that.
Expand Down
4 changes: 3 additions & 1 deletion source/slang/slang-ir-wgsl-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,9 @@ void legalizeIRForWGSL(IRModule* module, DiagnosticSink* sink)
// Go through every instruction in the module and legalize them as needed.
context.processInst(module->getModuleInst());

GlobalInstInliningContext().inlineGlobalValues(module);
// Some global insts are illegal, e.g. function calls.
// We need to inline and remove those.
GlobalInstInliningContext().inlineGlobalValuesAndRemoveIfUnused(module);
}

} // namespace Slang

0 comments on commit f89d28a

Please sign in to comment.