Files
clang-p2996/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
Justin Bogner ae34440834 [DirectX] Make DXILOpLowering responsible for cleaning up dead intrinsics (#138199)
This moves the responsibility for cleaning up dead intrinsics from
DXILFinalizeLinkage to DXILOpLowering, and moves DXILFinalizeLinkage
back to it's pre-#136244 place in the pipeline. Doing this avoids issues
with DXIL passes running on obviously dead code, and makes it more clear
what DXILFinalizeLinkage is really doing.

This also helps with the story for #134260, as cleaning up dead
intrinsics doesn't make sense if this becomes a more generic pass.

Note that test/CodeGen/DirectX/remove-dead-intriniscs.ll already covers
most of the testing here. It'd be nice to have something that catches
the regression from changing the pass ordering but I couldn't come up
with anything that wouldn't be incredibly fragile.

Fixes #138180.
2025-05-01 22:07:07 -07:00

63 lines
1.9 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/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) {
SmallVector<Function *> Funcs;
// Collect non-entry and non-exported functions to set to internal linkage.
for (Function &EF : M.functions()) {
if (EF.isIntrinsic())
continue;
if (EF.hasFnAttribute("hlsl.shader") || EF.hasFnAttribute("hlsl.export"))
continue;
Funcs.push_back(&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);
}
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();
}