Skip to content

Commit

Permalink
DNI: Do the same kind of simplification pass that SPIR-V legalization…
Browse files Browse the repository at this point in the history
… does, for WGSL

DNI: This doesn't seem to help at all!
  • Loading branch information
aleino-nv committed Dec 5, 2024
1 parent df2f492 commit 8210b91
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion source/slang/slang-emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ Result linkAndOptimizeIR(
case CodeGenTarget::WGSLSPIRV:
case CodeGenTarget::WGSLSPIRVAssembly:
{
legalizeIRForWGSL(irModule, sink);
legalizeIRForWGSL(targetProgram, irModule, sink);
}
break;

Expand Down
48 changes: 47 additions & 1 deletion source/slang/slang-ir-wgsl-legalize.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "slang-ir-wgsl-legalize.h"

#include "slang-ir-dce.h"
#include "slang-ir-insts.h"
#include "slang-ir-legalize-varying-params.h"
#include "slang-ir-legalize-global-values.h"
#include "slang-ir-peephole.h"
#include "slang-ir-redundancy-removal.h"
#include "slang-ir-sccp.h"
#include "slang-ir-simplify-cfg.h"
#include "slang-ir-util.h"
#include "slang-ir.h"
#include "slang-parameter-binding.h"
Expand Down Expand Up @@ -1617,7 +1622,47 @@ struct GlobalInstInliningContext: public GlobalInstInliningContextGeneric
}
};

void legalizeIRForWGSL(IRModule* module, DiagnosticSink* sink)
static void simplifyIRForWGSLLegalization(TargetProgram* target, DiagnosticSink* sink, IRModule* module)
{
bool changed = true;
const int kMaxIterations = 8;
const int kMaxFuncIterations = 16;
int iterationCounter = 0;

while (changed && iterationCounter < kMaxIterations)
{
if (sink && sink->getErrorCount())
break;

changed = false;

changed |= applySparseConditionalConstantPropagationForGlobalScope(module, sink);
changed |= peepholeOptimizeGlobalScope(target, module);

for (auto inst : module->getGlobalInsts())
{
auto func = as<IRGlobalValueWithCode>(inst);
if (!func)
continue;
bool funcChanged = true;
int funcIterationCount = 0;
while (funcChanged && funcIterationCount < kMaxFuncIterations)
{
funcChanged = false;
funcChanged |= applySparseConditionalConstantPropagation(func, sink);
funcChanged |= peepholeOptimize(target, func);
funcChanged |= removeRedundancyInFunc(func);
CFGSimplificationOptions options;
options.removeTrivialSingleIterationLoops = true;
options.removeSideEffectFreeLoops = false;
funcChanged |= simplifyCFG(func, options);
eliminateDeadCode(func);
}
}
}
}

void legalizeIRForWGSL(TargetProgram * targetProgram, IRModule* module, DiagnosticSink* sink)
{
List<EntryPointInfo> entryPoints;
for (auto inst : module->getGlobalInsts())
Expand All @@ -1644,6 +1689,7 @@ void legalizeIRForWGSL(IRModule* module, DiagnosticSink* sink)
context.processInst(module->getModuleInst());

GlobalInstInliningContext().inlineGlobalValues(module);
simplifyIRForWGSLLegalization(targetProgram, sink, module);
}

} // namespace Slang
3 changes: 2 additions & 1 deletion source/slang/slang-ir-wgsl-legalize.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Slang
{
class DiagnosticSink;
class TargetProgram;

void legalizeIRForWGSL(IRModule* module, DiagnosticSink* sink);
void legalizeIRForWGSL(TargetProgram* targetProgram, IRModule* module, DiagnosticSink* sink);
} // namespace Slang

0 comments on commit 8210b91

Please sign in to comment.