Attaching ops to a module

Hi Folks:
I have something like this in my code (simplified here). I find that even though op1 and op2 are created successfully, I dont know how to attach them to the module.

mlir::OwningModuleReference theModule(mlir::parseSourceString(R"(module attributes  {...} {})"), context);
mlir::OpBuilder theBuilder(context);
auto op1 = theBuilder.create<...>();
auto op2 = theBuilder.create<mlir::MyDialect::TensorOp>(...., op1.getResult());

Do I attach op2.result(0) as operand to the module (tried unsuccessfully), or do I need to first create a function/region inside the module first? I tried looking around in mlir code base for some similar code but could not find how to go best about it. Please point me to some example code if possible.
Thanks a lot.

In general an OpBuilder has an “insertion point”, but you need to set it first and then created operation will be inserted at the current insertion point.

For example you can do theBuilder.setInsertionPointToStart(theModule->getBody()); before creating the ops. You can also create the builder directly with an insertion point: mlir::OpBuilder builder = mlir::OpBuilder::atBlockEnd(theModule->getBody());.

If you have operation that you need to anchor somewhere, then you need to insert them in a block (a block is just a doubly linked list of operations): theModule->getBody()->push_back(op1);.