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);.

what should I do if i waht to insert a globa op into head of builtin module which results:

module {
  @.str = private unnamed_addr constant [7 x i8] c"%61=%d\00", align 1
  func.func @simple_mul_dispatch_0_generic_1024_f32() {
    %c0 = arith.constant 0 : index
    %0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : memref<1024xf32, #dlc.address_space<hbm>>
    %alloca = memref.alloca() : memref<1024xf32, #dlc.address_space<vmem>>
    memref.copy %0, %alloca : memref<1024xf32, #dlc.address_space<hbm>> to memref<1024xf32, #dlc.address_space<vmem>>
    memref.assume_alignment %0, 64 : memref<1024xf32, #dlc.address_space<hbm>>
    %1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : memref<1024xf32, #dlc.address_space<hbm>>
    %alloca_0 = memref.alloca() : memref<1024xf32, #dlc.address_space<vmem>>
    memref.copy %1, %alloca_0 : memref<1024xf32, #dlc.address_space<hbm>> to memref<1024xf32, #dlc.address_space<vmem>>
    memref.assume_alignment %1, 64 : memref<1024xf32, #dlc.address_space<hbm>>
    %2 = hal.interface.binding.subspan set(0) binding(2) type(storage_buffer) alignment(64) offset(%c0) : memref<1024xf32, #dlc.address_space<hbm>>
    memref.assume_alignment %2, 64 : memref<1024xf32, #dlc.address_space<hbm>>
    %3 = vector.load %alloca[%c0] : memref<1024xf32, #dlc.address_space<vmem>>, vector<1024xf32>
    %4 = vector.load %alloca_0[%c0] : memref<1024xf32, #dlc.address_space<vmem>>, vector<1024xf32>
    %5 = arith.mulf %3, %4 : vector<1024xf32>
    %alloca_1 = memref.alloca() : memref<1024xf32, #dlc.address_space<vmem>>
    vector.store %5, %alloca_1[%c0] : memref<1024xf32, #dlc.address_space<vmem>>, vector<1024xf32>
    memref.copy %alloca_1, %2 : memref<1024xf32, #dlc.address_space<vmem>> to memref<1024xf32, #dlc.address_space<hbm>>
    return
  }
}
what should i do? 

You should do exactly what the solution above says.