WHLSLPruneUnreachableStandardLibraryFunctions.cpp [plain text]
#include "config.h"
#include "WHLSLPruneUnreachableStandardLibraryFunctions.h"
#if ENABLE(WHLSL_COMPILER)
#include "WHLSLAST.h"
#include "WHLSLProgram.h"
#include "WHLSLVisitor.h"
namespace WebCore {
namespace WHLSL {
class ReachableStdlibFunctions final : public Visitor {
public:
void visit(AST::FunctionDefinition& function) override
{
auto addResult = m_reachableFunctions.add(&function);
if (addResult.isNewEntry)
Visitor::visit(function);
}
void visit(AST::CallExpression& callExpression) override
{
Visitor::visit(callExpression);
if (is<AST::FunctionDefinition>(callExpression.function()))
visit(downcast<AST::FunctionDefinition>(callExpression.function()));
}
HashSet<AST::FunctionDefinition*> takeReachableFunctions() { return WTFMove(m_reachableFunctions); }
private:
HashSet<AST::FunctionDefinition*> m_reachableFunctions;
};
void pruneUnreachableStandardLibraryFunctions(Program& program)
{
ReachableStdlibFunctions reachableStdlibFunctions;
Vector<UniqueRef<AST::FunctionDefinition>> functionDefinitions = WTFMove(program.functionDefinitions());
for (auto& function : functionDefinitions) {
if (function->parsingMode() != ParsingMode::StandardLibrary)
reachableStdlibFunctions.visit(function.get());
else
RELEASE_ASSERT(!function->entryPointType());
}
auto reachableFunctions = reachableStdlibFunctions.takeReachableFunctions();
Vector<UniqueRef<AST::FunctionDefinition>> newFunctionDefinitions;
for (UniqueRef<AST::FunctionDefinition>& entry : functionDefinitions) {
if (reachableFunctions.contains(&entry.get()))
newFunctionDefinitions.append(WTFMove(entry));
}
program.functionDefinitions() = WTFMove(newFunctionDefinitions);
}
}
}
#endif // ENABLE(WHLSL_COMPILER)