<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64166>64166</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Many target MCTargetDesc libraries still include headers from the target CodeGen library
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm,
bazel
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
rnk
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rnk
</td>
</tr>
</table>
<pre>
Every LLVM target is composed of several libraries. The primary library, the one called LLVM${TGT}CodeGen, is responsible for translating IR to MachineInstrs, and ultimately "printing" (or producing) MCInstrs, which is our representation of assembly. Below that, we have the MCTargetDesc library, which is intended to contain data about the ISA. This should not depend on CodeGen. The intention is that we can link `llvm-mc` for all targets without linking in either the target-independent library (LLVMCodeGen) or the target-specific codegen library (LLVMX86CodeGen).
This is how things are linked together in CMake. The problem is that our headers are not well organized, and we still reference headers from llvm/CodeGen/ from files in MCTargetDesc. This problem is detected by Bazel, which sandboxes each action and stages the necessary inputs to every action. If you [apply this patch](https://github.com/llvm/llvm-project/commit/fb8ce92a404a088b25aee49272ba4cd46be00d75) to remove the dep on CodeGen from all the MCTargetDesc libraries, you can observe compilation failures.
I have a stack of patches in a github branch to fix this issue. I'm filing this issue to describe the problem well once so I can link to it from the commit messages.
The error message from Bazel typically looks like a file-not-found error on an include, something like this:
```
In file included from external/llvm-project/llvm/lib/Target/AMDGPU/MCA/AMDGPUCustomBehaviour.cpp:16:
In file included from bazel-out/k8-fastbuild/bin/external/llvm-project/llvm/_virtual_includes/AMDGPUUtilsAndDesc/SIInstrInfo.h:17:
bazel-out/k8-fastbuild/bin/external/llvm-project/llvm/_virtual_includes/AMDGPUUtilsAndDesc/AMDGPUMIRFormatter.h:19:10: fatal error: 'llvm/CodeGen/MIRFormatter.h' file not found
#include "llvm/CodeGen/MIRFormatter.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
```
Aside from Bazel, we should fix these issues because it makes our code fragile. We reference these CodeGen headers but do not link against the corresponding cpp file, so the link will fail if someone decides to move an inline function out of line or to call a non-inline method.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vl1v2zoS_TX0yyCGTH_IfvCD41wXBq6BxW26u2_FiBxJXFOkQFJJ3Yf97QuSsp2kRYt9uUZgxBKHPHPmnBmi96oxRFu2fGScO3NmnLPl0wSH0Fq3deY8qay8bP94IXeBP__85wkCuoYCKA_Cdr31JMHW4OmFHGrQqnLoFPkpPLcEvVMdusv4-ML4HkJLYA2BQK1Jpj0ZX7Dy8fnTMyuf9lbSJzJxpfLgyPfWeFVpgto6CA6N1xiUaeD4FwQLJxStMnQ0Pjgfo9BIGHRQHQbSF2Cc906ZGME4B8bX1kHvrBxEerSB0_4e_Noq0caD7eDAUe_IkwkYlDUxS_SeukpfpvBI2r5CaDGkMIIWXyjldto_J4aeyIu3ed-2ViaQkSQjemFNQGVAYkDAyg4h7XH8vIv8KQ--tYOWYGwAST0ZCdbAyFGmOG2XACqfAEU0Ag1oZc7AVoXWL91DJ9iqSBSi1mMNPbyq0MYz49JIqTJAKrTkEoq86kGZfDKZcM0n0hgLdyvWBuy7GN-TULUSIKykhszHwH-vV_fYKSueWLHL3ylr5aFN9CrTeEBHCWGirKGETxnYn_BMV5nZSlN3YyBWryWU5HJ0pO-VtAbrGjTqO8mrUl4JfFBag6OaHBlBt8Da2Q4ieYwfbmAP-XGtNMVKvqv2WLI3YCQFEoEkVBd4xO-k70LwaGRlv5EHQtECilTCCMkHbMgnNg0J8j7ypkw_BB8lQ8mKef0UjjVc7ABs-Yh9ry-RMg89BtGy5RPj6zaE3rP5jvED44dGhXaopsLGnMbUkjx6Z_9DIjB-ELbrVPynrtaCNhwXxQKL9briSyRabHjJK1wIuVhVVBSyXMbiBwuOOjs6QFL_RqWZsKS6n7tDUbJeTCPK1lae3Aul7qJ0tl6NSg-O_DulHLPnMBImztGeKe9cF4ScK1QOjWgjwlp9y_Qo7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