Skip to content

Commit 85f4411

Browse files
committed
more cleanup and fixes.
1 parent 5672ad0 commit 85f4411

6 files changed

+64
-20
lines changed

source/core/slang-common.h

+20
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ namespace Slang
8484
}
8585
}
8686

87+
// SLANG_DEFER
88+
template<typename F>
89+
class SlangDeferImpl
90+
{
91+
const F& f;
92+
public:
93+
SlangDeferImpl(const F& f)
94+
: f(f)
95+
{}
96+
~SlangDeferImpl()
97+
{
98+
f();
99+
}
100+
};
101+
102+
#ifndef SLANG_DEFER_LAMBDA
103+
#define SLANG_DEFER_LAMBDA(x) auto SLANG_CONCAT(slang_defer_, __LINE__) = SlangDeferImpl(x)
104+
#define SLANG_DEFER(x) auto SLANG_CONCAT(slang_defer_##,__LINE__) = SlangDeferImpl([&](){x;})
105+
#endif
106+
87107
//
88108
// Some macros for avoiding boilerplate
89109
// TODO: could probably deduce the size with templates, and move the whole

source/slang/slang-ir-inline.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct InliningPassBase
2929
/// The module that we are optimizing/transforming
3030
IRModule* m_module = nullptr;
3131

32+
HashSet<IRInst*>* m_modifiedFuncs = nullptr;
33+
3234
/// Initialize an inlining pass to operate on the given `module`
3335
InliningPassBase(IRModule* module)
3436
: m_module(module)
@@ -157,6 +159,11 @@ struct InliningPassBase
157159
// given call site, we hand off the a worker routine
158160
// that does the meat of the work.
159161
//
162+
if (m_modifiedFuncs)
163+
{
164+
if (auto parentFunc = getParentFunc(call))
165+
m_modifiedFuncs->add(parentFunc);
166+
}
160167
inlineCallSite(callSite);
161168
return true;
162169
}
@@ -698,11 +705,12 @@ struct MandatoryEarlyInliningPass : InliningPassBase
698705
};
699706

700707

701-
bool performMandatoryEarlyInlining(IRModule* module)
708+
bool performMandatoryEarlyInlining(IRModule* module, HashSet<IRInst*>* modifiedFuncs)
702709
{
703710
SLANG_PROFILE;
704711

705712
MandatoryEarlyInliningPass pass(module);
713+
pass.m_modifiedFuncs = modifiedFuncs;
706714
return pass.considerAllCallSites();
707715
}
708716

source/slang/slang-ir-inline.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#pragma once
33

44
#include "../../slang-com-helper.h"
5+
#include "core/slang-basic.h"
56

67
namespace Slang
78
{
@@ -10,12 +11,13 @@ namespace Slang
1011
struct IRGlobalValueWithCode;
1112
class DiagnosticSink;
1213
class TargetProgram;
14+
struct IRInst;
1315

1416
/// Any call to a function that takes or returns a string/RefType parameter is inlined
1517
Result performTypeInlining(IRModule* module, DiagnosticSink* sink);
1618

1719
/// Inline any call sites to functions marked `[unsafeForceInlineEarly]`
18-
bool performMandatoryEarlyInlining(IRModule* module);
20+
bool performMandatoryEarlyInlining(IRModule* module, HashSet<IRInst*>* modifiedFuncs = nullptr);
1921

2022
/// Inline any call sites to functions marked `[ForceInline]`
2123
void performForceInlining(IRModule* module);

source/slang/slang-ir-specialize.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ struct SpecializationContext
6161
, targetProgram(target)
6262
{
6363
}
64+
~SpecializationContext()
65+
{
66+
module->getContainerPool().free(&workList);
67+
module->getContainerPool().free(&workListSet);
68+
module->getContainerPool().free(&cleanInsts);
69+
}
6470

6571
// An instruction is then fully specialized if and only
6672
// if it is in our set.
@@ -1246,6 +1252,9 @@ struct SpecializationContext
12461252

12471253
List<IRInst*>& localWorkList = *module->getContainerPool().getList<IRInst>();
12481254
HashSet<IRInst*>& processedInsts = *module->getContainerPool().getHashSet<IRInst>();
1255+
SLANG_DEFER(module->getContainerPool().free(&localWorkList));
1256+
SLANG_DEFER(module->getContainerPool().free(&processedInsts));
1257+
12491258
localWorkList.add(inst);
12501259
processedInsts.add(inst);
12511260

source/slang/slang-ir-uniformity.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ namespace Slang
193193
{
194194
List<IRInst*>& nextWorkList = *module->getContainerPool().getList<IRInst>();
195195
HashSet<IRInst*>& workListSet = *module->getContainerPool().getHashSet<IRInst>();
196+
SLANG_DEFER([&]() { module->getContainerPool().free(&nextWorkList); });
197+
SLANG_DEFER([&]() { module->getContainerPool().free(&workListSet); });
198+
196199
auto addToWorkList = [&](IRInst* inst)
197200
{
198201
if (workListSet.add(inst))
@@ -412,6 +415,8 @@ namespace Slang
412415
void analyzeModule()
413416
{
414417
List<IRInst*>& workList = *module->getContainerPool().getList<IRInst>();
418+
SLANG_DEFER([&]() { module->getContainerPool().free(&workList); });
419+
415420
for (auto globalInst : module->getGlobalInsts())
416421
{
417422
if (auto code = as<IRGlobalValueWithCode>(globalInst))
@@ -449,6 +454,8 @@ namespace Slang
449454
void eliminateAsDynamicUniformInst()
450455
{
451456
List<IRInst*>& workList = *module->getContainerPool().getList<IRInst>();
457+
SLANG_DEFER([&]() { module->getContainerPool().free(&workList); });
458+
452459
workList.add(module->getModuleInst());
453460
for (Index i = 0; i < workList.getCount(); i++)
454461
{

source/slang/slang-lower-to-ir.cpp

+16-18
Original file line numberDiff line numberDiff line change
@@ -10881,32 +10881,30 @@ RefPtr<IRModule> generateIRForTranslationUnit(
1088110881
// are eliminated from the callee, and not copied into
1088210882
// call sites.
1088310883
//
10884+
HashSet<IRInst*>* modifiedFuncs = module->getContainerPool().getHashSet<IRInst>();
10885+
SLANG_DEFER(module->getContainerPool().free(modifiedFuncs));
10886+
bool minimumOptimizations = sharedContextStorage.m_linkage->m_optionSet.getBoolOption(CompilerOptionName::MinimumSlangOptimization);
1088410887
for (;;)
1088510888
{
1088610889
bool changed = false;
10887-
changed |= performMandatoryEarlyInlining(module);
10888-
if (!changed)
10889-
break;
10890-
}
10891-
10892-
// Optionally, run optimization after inlining.
10893-
if (!sharedContextStorage.m_linkage->m_optionSet.getBoolOption(CompilerOptionName::MinimumSlangOptimization))
10894-
{
10895-
for (;;)
10890+
modifiedFuncs->clear();
10891+
changed = performMandatoryEarlyInlining(module, modifiedFuncs);
10892+
if (changed)
1089610893
{
10897-
bool changed = false;
10898-
changed |= constructSSA(module);
10899-
simplifyCFG(module, CFGSimplificationOptions::getDefault());
10900-
changed |= applySparseConditionalConstantPropagation(module, compileRequest->getSink());
10901-
changed |= peepholeOptimize(nullptr, module, PeepholeOptimizationOptions::getPrelinking());
10902-
for (auto inst : module->getGlobalInsts())
10894+
changed = peepholeOptimizeGlobalScope(nullptr, module);
10895+
if (!minimumOptimizations)
1090310896
{
10904-
if (auto func = as<IRGlobalValueWithCode>(inst))
10897+
for (auto func : *modifiedFuncs)
10898+
{
10899+
changed |= constructSSA(func);
10900+
changed |= applySparseConditionalConstantPropagation(func, compileRequest->getSink());
10901+
changed |= peepholeOptimize(nullptr, func);
1090510902
eliminateDeadCode(func);
10903+
}
1090610904
}
10907-
if (!changed)
10908-
break;
1090910905
}
10906+
if (!changed)
10907+
break;
1091010908
}
1091110909

1091210910
// Check for using uninitialized out parameters.

0 commit comments

Comments
 (0)