llvm-project/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
Greg Roth ab75180501
[DirectX] Remove trivially dead functions at linkage finalize (#106146)
Functions are not removed even when made internal by
DXILFinalizeLinkage. The removal code is called from alwaysinliner and
globalopt, which are invoked too early to remove functions made internal
by this pass.

This adds a check similar to that in alwaysinliner that removes
trivially dead functions after being marked internal. It refactors that
code a bit to make it simpler including reversing what is stored int he
work queue.

Tests both the pass in isolation and the full i0nlining, linkage
finalization and function removal steps.

Fixes #106139
2024-10-17 12:54:52 -06:00

66 lines
2.0 KiB
C++

//===- DXILFinalizeLinkage.cpp - Finalize linkage of functions ------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DXILFinalizeLinkage.h"
#include "DirectX.h"
#include "llvm/Analysis/DXILResource.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#define DEBUG_TYPE "dxil-finalize-linkage"
using namespace llvm;
static bool finalizeLinkage(Module &M) {
SmallPtrSet<Function *, 8> Funcs;
// Collect non-entry and non-exported functions to set to internal linkage.
for (Function &EF : M.functions()) {
if (EF.hasFnAttribute("hlsl.shader") || EF.hasFnAttribute("hlsl.export"))
continue;
Funcs.insert(&EF);
}
for (Function *F : Funcs) {
if (F->getLinkage() == GlobalValue::ExternalLinkage)
F->setLinkage(GlobalValue::InternalLinkage);
if (F->isDefTriviallyDead())
M.getFunctionList().erase(F);
}
return false;
}
PreservedAnalyses DXILFinalizeLinkage::run(Module &M,
ModuleAnalysisManager &AM) {
if (finalizeLinkage(M))
return PreservedAnalyses::none();
return PreservedAnalyses::all();
}
bool DXILFinalizeLinkageLegacy::runOnModule(Module &M) {
return finalizeLinkage(M);
}
void DXILFinalizeLinkageLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<DXILResourceWrapperPass>();
}
char DXILFinalizeLinkageLegacy::ID = 0;
INITIALIZE_PASS_BEGIN(DXILFinalizeLinkageLegacy, DEBUG_TYPE,
"DXIL Finalize Linkage", false, false)
INITIALIZE_PASS_END(DXILFinalizeLinkageLegacy, DEBUG_TYPE,
"DXIL Finalize Linkage", false, false)
ModulePass *llvm::createDXILFinalizeLinkageLegacyPass() {
return new DXILFinalizeLinkageLegacy();
}