summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-dce.cpp
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-29 14:49:26 +0800
committerGitHub <noreply@github.com>2024-10-29 14:49:26 +0800
commitf65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch)
treeea1d61342cd29368e19135000ec2948813096205 /source/slang/slang-ir-dce.cpp
parenta729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff)
format
* format * Minor test fixes * enable checking cpp format in ci
Diffstat (limited to 'source/slang/slang-ir-dce.cpp')
-rw-r--r--source/slang/slang-ir-dce.cpp78
1 files changed, 31 insertions, 47 deletions
diff --git a/source/slang/slang-ir-dce.cpp b/source/slang/slang-ir-dce.cpp
index 12cc4ed93..9c82715d4 100644
--- a/source/slang/slang-ir-dce.cpp
+++ b/source/slang/slang-ir-dce.cpp
@@ -1,9 +1,9 @@
// slang-ir-dce.cpp
#include "slang-ir-dce.h"
-#include "slang-ir.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
+#include "slang-ir.h"
namespace Slang
{
@@ -16,9 +16,9 @@ struct DeadCodeEliminationContext
// the parameters that were passed to the top-level
// `eliminateDeadCode` function.
//
- IRModule* module;
+ IRModule* module;
- IRDeadCodeEliminationOptions options;
+ IRDeadCodeEliminationOptions options;
// If we removed an inst, there may be still "weak references" to the inst.
// These uses will be replaced with `undefInst`.
@@ -36,7 +36,8 @@ struct DeadCodeEliminationContext
//
bool isInstAlive(IRInst* inst)
{
- if (!inst) return false;
+ if (!inst)
+ return false;
return inst->scratchData != 0;
}
@@ -64,7 +65,8 @@ struct DeadCodeEliminationContext
// Again, we safeguard against null instructions
// just in case.
//
- if(!inst) return;
+ if (!inst)
+ return;
if (!inst->scratchData)
{
@@ -212,10 +214,7 @@ struct DeadCodeEliminationContext
// dive into the task of actually finding all
// the live code in a module.
//
- bool processModule()
- {
- return processInst(module->getModuleInst());
- }
+ bool processModule() { return processInst(module->getModuleInst()); }
bool eliminateDeadInstsRec(IRInst* inst)
{
@@ -225,7 +224,7 @@ struct DeadCodeEliminationContext
//
// The easy case is if `inst` is dead (that is, not live).
//
- if( !isInstAlive(inst) )
+ if (!isInstAlive(inst))
{
// We can simply remove and deallocate `inst` because it is
// dead, and not worry about any of its descendents,
@@ -261,7 +260,7 @@ struct DeadCodeEliminationContext
List<IRInst*> children;
for (auto child : inst->getDecorationsAndChildren())
children.add(child);
- for(IRInst* child : children)
+ for (IRInst* child : children)
{
changed |= eliminateDeadInstsRec(child);
}
@@ -302,8 +301,7 @@ bool isPtrUsed(IRInst* ptrInst)
return true;
else if (as<IRCall>(use->getUser())) // TODO: narrow this case to 'inout' parameters only.
return true;
- else if (as<IRPtrTypeBase>(use->getUser()->getDataType()) &&
- isPtrUsed(use->getUser()))
+ else if (as<IRPtrTypeBase>(use->getUser()->getDataType()) && isPtrUsed(use->getUser()))
return true;
}
@@ -315,8 +313,7 @@ bool isFieldUsed(IRStructField* fieldInst)
auto structKey = fieldInst->getKey();
for (auto use = structKey->firstUse; use; use = use->nextUse)
{
- if (as<IRPtrTypeBase>(use->getUser()->getDataType()) &&
- isPtrUsed(use->getUser()))
+ if (as<IRPtrTypeBase>(use->getUser()->getDataType()) && isPtrUsed(use->getUser()))
return true;
if (as<IRFieldExtract>(use->getUser()))
@@ -325,7 +322,7 @@ bool isFieldUsed(IRStructField* fieldInst)
// Check fields that have this field as a sub-field.
auto parentType = cast<IRStructType>(fieldInst->getParent());
-
+
if (as<IRModuleInst>(parentType->getParent()))
{
for (auto use = parentType->firstUse; use; use = use->nextUse)
@@ -381,7 +378,7 @@ bool removeStoresIntoInst(IRInst* ptrInst)
if (auto subAddr = as<IRFieldAddress>(use->getUser()))
changed |= removeStoresIntoInst(subAddr);
-
+
if (auto subIndex = as<IRGetElementPtr>(use->getUser()))
changed |= removeStoresIntoInst(subIndex);
}
@@ -463,7 +460,7 @@ bool isStructEmpty(IRType* type)
auto structType = as<IRStructType>(type);
if (!structType)
return false;
-
+
UCount nonEmptyFieldCount = 0;
for (auto field : structType->getFields())
{
@@ -486,11 +483,11 @@ bool trimOptimizableType(IRStructType* type)
// We'll ignore void-type fields, since they're handled differently.
if (as<IRVoidType>(field->getFieldType()))
continue;
-
+
// ... same for empty struct fields.
- if(as<IRStructType>(field->getFieldType()) && isStructEmpty(field->getFieldType()))
+ if (as<IRStructType>(field->getFieldType()) && isStructEmpty(field->getFieldType()))
continue;
-
+
if (!isFieldUsed(field))
fieldsToRemove.add(field);
}
@@ -512,7 +509,7 @@ bool trimOptimizableType(IRStructType* type)
}
bool trimOptimizableTypes(IRModule* module)
-{
+{
bool changed = false;
for (auto inst : module->getGlobalInsts())
{
@@ -539,8 +536,8 @@ bool shouldInstBeLiveIfParentIsLive(IRInst* inst, IRDeadCodeEliminationOptions o
// when it is executed, then we should keep it around.
//
SideEffectAnalysisOptions sideEffectOptions = options.useFastAnalysis
- ? SideEffectAnalysisOptions::None
- : SideEffectAnalysisOptions::UseDominanceTree;
+ ? SideEffectAnalysisOptions::None
+ : SideEffectAnalysisOptions::UseDominanceTree;
if (inst->mightHaveSideEffects(sideEffectOptions))
{
@@ -585,11 +582,8 @@ bool shouldInstBeLiveIfParentIsLive(IRInst* inst, IRDeadCodeEliminationOptions o
{
switch (decor->getOp())
{
- case kIROp_ExportDecoration:
- return true;
- case kIROp_ImportDecoration:
- isImported = true;
- break;
+ case kIROp_ExportDecoration: return true;
+ case kIROp_ImportDecoration: isImported = true; break;
}
}
for (auto decor : innerInst->getDecorations())
@@ -598,9 +592,7 @@ bool shouldInstBeLiveIfParentIsLive(IRInst* inst, IRDeadCodeEliminationOptions o
{
case kIROp_ForwardDerivativeDecoration:
case kIROp_UserDefinedBackwardDerivativeDecoration:
- case kIROp_PrimalSubstituteDecoration:
- shouldKeptAliveIfImported = true;
- break;
+ case kIROp_PrimalSubstituteDecoration: shouldKeptAliveIfImported = true; break;
}
}
if (isImported && shouldKeptAliveIfImported)
@@ -651,13 +643,10 @@ bool shouldInstBeLiveIfParentIsLive(IRInst* inst, IRDeadCodeEliminationOptions o
// this problem.
//
case kIROp_StructField:
- case kIROp_WitnessTableEntry:
- return true;
+ case kIROp_WitnessTableEntry: return true;
- case kIROp_GlobalParam:
- return options.keepGlobalParamsAlive;
- default:
- break;
+ case kIROp_GlobalParam: return options.keepGlobalParamsAlive;
+ default: break;
}
// If none of the explicit cases above matched, then we will consider
@@ -684,8 +673,7 @@ bool isWeakReferenceOperand(IRInst* inst, UInt operandIndex)
// Ignore all operands of SpecializationDictionaryItem.
// This inst is used as a cache and shouldn't hold anything alive.
return true;
- default:
- break;
+ default: break;
}
return false;
}
@@ -694,9 +682,7 @@ bool isWeakReferenceOperand(IRInst* inst, UInt operandIndex)
// is straighforward. We set up the context object
// and then defer to it for the real work.
//
-bool eliminateDeadCode(
- IRModule* module,
- IRDeadCodeEliminationOptions const& options)
+bool eliminateDeadCode(IRModule* module, IRDeadCodeEliminationOptions const& options)
{
DeadCodeEliminationContext context;
context.module = module;
@@ -704,9 +690,7 @@ bool eliminateDeadCode(
return context.processModule();
}
-bool eliminateDeadCode(
- IRInst* root,
- IRDeadCodeEliminationOptions const& options)
+bool eliminateDeadCode(IRInst* root, IRDeadCodeEliminationOptions const& options)
{
DeadCodeEliminationContext context;
context.module = root->getModule();
@@ -714,4 +698,4 @@ bool eliminateDeadCode(
return context.processInst(root);
}
-}
+} // namespace Slang