opt, non-machine specific optimizations and passBuilder resulting in malformed block

Hi,

I operate on IR, which should later-on be lowered on different target
machines. Due to the generation of the IR via partly random processes, I
have lots of ineffective instruction in there.

Every now and then i'd like to cut back all the fuzz.
I can do this e.g. via opt -o3 -S input.ll -o output.ll
which however applies machine dependent optimizations.

What would be the 'simplify as much as possible without machine
dependent stuff' approach using opt?

Another option instead of the commandline opt would be to use the (new)
passbuilder. As a starter, I tried to apply the PerModuleDefaultPipeline.
My Idea was that I could later reduce this to only applying
buildModuleSimplificationPipeline etc.
However, the following results in a transformed .bc file, for which
llvm-dis reports 'malformed block'.

I can not figure out whats the problem here.
Also, the verifierPass does not report any problems (might be because I
add it too late)?

What am I missing here?

Thanks!
Alex

llvm::PassBuilder passBuilder;

LoopAnalysisManager LAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;

// Register all the basic analyses with the managers.
passBuilder.registerModuleAnalyses(MAM);
passBuilder.registerCGSCCAnalyses(CGAM);
passBuilder.registerFunctionAnalyses(FAM);
passBuilder.registerLoopAnalyses(LAM);
passBuilder.crossRegisterProxies(LAM, FAM, CGAM, MAM);

ModulePassManager
MPM=passBuilder.buildPerModuleDefaultPipeline(llvm::PassBuilder::OptimizationLevel::O1,
false); //debug logging=false
MPM.addPass(VerifierPass());

MPM.run(rM, MAM);
dses::write_BCFromP(&rM,"trafo.bc");