Skip to content

Commit 6cf9e80

Browse files
committed
Tentative fix for not removing newly internal functions
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 in the work queue. Not sure how to test this. To test all the interactions between alwaysinliner, DXILfinalizelinkage and any other optimization passes, it kinda needs to be end-to-end. Fixes #106139
1 parent 85561dd commit 6cf9e80

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_dxc -T cs_6_0 %s | Filecheck %s
2+
3+
// Verify that internal linkage unused functions are removed
4+
5+
RWBuffer<unsigned> buf;
6+
7+
// CHECK-NOT: define{{.*}}donothing
8+
void donothing() {
9+
buf[1] = 1; // never called, does nothing!
10+
}
11+
12+
13+
[numthreads(1,1,1)]
14+
[shader("compute")]
15+
void main() {
16+
buf[0] = 0;// I'm doing something!!!
17+
}

llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
using namespace llvm;
1919

2020
static bool finalizeLinkage(Module &M) {
21-
SmallPtrSet<Function *, 8> EntriesAndExports;
21+
SmallPtrSet<Function *, 8> Funcs;
2222

2323
// Find all entry points and export functions
2424
for (Function &EF : M.functions()) {
25-
if (!EF.hasFnAttribute("hlsl.shader") && !EF.hasFnAttribute("hlsl.export"))
25+
if (EF.hasFnAttribute("hlsl.shader") || EF.hasFnAttribute("hlsl.export"))
2626
continue;
27-
EntriesAndExports.insert(&EF);
27+
Funcs.insert(&EF);
2828
}
2929

30-
for (Function &F : M.functions()) {
31-
if (F.getLinkage() == GlobalValue::ExternalLinkage &&
32-
!EntriesAndExports.contains(&F)) {
33-
F.setLinkage(GlobalValue::InternalLinkage);
30+
for (Function *F : Funcs) {
31+
if (F->getLinkage() == GlobalValue::ExternalLinkage) {
32+
F->setLinkage(GlobalValue::InternalLinkage);
33+
if (F->isDefTriviallyDead())
34+
M.getFunctionList().erase(F);
3435
}
3536
}
3637

0 commit comments

Comments
 (0)