Add serialized remarks blocks in bitcode produced

Hi all,

I’m trying to add LLVM serialized remarks in the bitstream produced (not in an external remarks file).

I follow this great documentation page : Remarks — LLVM 17.0.0git documentation

To develop my own llvm plugin and use the following code to produce a simple remark when a module is parsed :

//-----------------------------------------------------------------------------
// InjectRemarks implementation
//-----------------------------------------------------------------------------
bool InjectRemarks::runOnModule(Module &M) {

auto &CTX = M.getContext();

// Emit the remarks section contents.
if (remarks::RemarkStreamer *RS = M.getContext().getMainRemarkStreamer()) {
// Build a remark
remarks::Remark R;
R.RemarkType = remarks::Type::Passed;
R.PassName = “PassTest”;
R.RemarkName = “RemarkTest”;
R.FunctionName = “main”;
R.Loc = remarks::RemarkLocation{“main.c”, 3, 4};
R.Hotness = 5;
RS->getSerializer().emit(R);
}

With this plugin, I’m able to generate remarks in yaml in a standalone file:
opt-14 -load-pass-plugin libInjectRemarks.so -passes=inject-remarks -pass-remarks-format=yaml -pass-remarks=. -pass-remarks-output=/tmp/test.remarks.yaml test.bc -o test.opt.bc

And also to generate remarks in bitstream in a standalone file:
opt-14 -load-pass-plugin libInjectRemarks.so -passes=inject-remarks -pass-remarks-format=bitstream -pass-remarks=. -remarks-section=1 -pass-remarks-output=test.remarks.bc test.bc -o test.opt.bc

$ llvm-bcanalyzer-14 -dump test.remarks.bc
<BLOCKINFO_BLOCK/>
<Meta NumWords=3 BlockCodeSize=3>
  <Container info abbrevid=4 op0=0 op1=1/>
  <Remark version abbrevid=5 op0=0/>
</Meta>
<Remark NumWords=8 BlockCodeSize=4>
  <Remark header abbrevid=4 op0=1 op1=0 op2=1 op3=2/>
  <Remark debug location abbrevid=5 op0=3 op1=3 op2=4/>
  <Remark hotness abbrevid=6 op0=5/>
  <Argument abbrevid=8 op0=4 op1=5/>
  <Argument with debug location abbrevid=7 op0=6 op1=7 op2=8 op3=6 op4=7/>
</Remark>

But unfortunately, I’m not able to add the serialized remarks blocks inside test.opt.bc output using the following command (the same as above without pass-remarks-output option):
opt-14 -load-pass-plugin libInjectRemarks.so -passes=inject-remarks -pass-remarks-format=bitstream -pass-remarks=. -remarks-section=1 test.bc -o test.opt.bc

Somebody see what I’m missing ?
I use the main remark streamer, maybe i have to create another one ?
Thanks for your help.