<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/106139>106139</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DXIL] trivially dead functions marked internal by DXILFinalizeLinkage don't get removed
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:codegen,
backend:DirectX
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
pow2clk
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pow2clk
</td>
</tr>
</table>
<pre>
The GlobalOpt pass will remove any "trivially dead" functions. That is functions that have no calls and whose linkage doesn't make them externally available. #95331 made this more relevant for HLSL since it makes most functions default to internal linkage instead of external. However, the DXILFinalizeLinkage pass runs after the last invocation of GlobalOpt during backend codegen.
Invoking GlobalOpt isn't possible because it uses the new pass interface. Instead, I propose adding an addendum to the DXILFinalizeLinkage pass that removes the function if it is found to be trivially dead after being made internal. A similar check is done for the AlwaysInliner pass: https://github.com/llvm/llvm-project/blob/2e0583ef8b92fe9c0a5e7e903c9d78bbc4a9a5b7/llvm/lib/Transforms/IPO/AlwaysInliner.cpp#L89-L96
This is a repro. I'm not actually sure if godbolt is running the pass in question yet, but it demonstrates an example of what is expected to be removed and currently isn't anyway.
https://godbolt.org/z/c7vedoaTv
Testing involves passes at extreme ends of the run and might need to be fully end-to-end.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVM1u4zYQfhr6MoggU1ZkHXRwN3DXgIEt0Bz2OiRHEmuKVElKXu_TF5SVOFkUBXqxBYKc-eb7GQxBd5aoYeVvjPPRXbk0F8Y5K182OMXe-WY93Ainbs1rT_C7cQLNtzHCiCHAVRsDngY3E6C9AeM8ej1rNOYGilAxzqGdrIza2ZDBa48RdHgcQUwnPc4E1oFEYwKgVXDtXSAw2l6wI1COgmW8ijDghSD2NAD9iOTt0ghn1AaFoQzYF84OOeNFXRbFFgZU6boOMDhP4MnQjDZC6zx8Pf95hqCtJND3wulWiB_AKWpxM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