Annotations exist only as comments in an LLVM IR printout. They are not actually a part of the IR, and consequently they are not something you "add" in a pass.
The way to use an AssemblyAnnotationWriter
is to pass it as the 2nd argument to a print
function (e.g. Module::print
). You can do that inside a pass, but it's not something that is done automatically for you. Specifically, making the pass inherit from AssemblyAnnotationWriter
is nonsensical - the first approach you've shown in your question, inheriting from it in a separate class, is the correct approach.
In short, if you want a printout of the module in your pass, call print
on it. If you want to add comments to the printout, call the same print
but pass an instance of your CommentWriter
as the 2nd argument.