Is there a way to randomize memref or assign values to memref one by one?

The memory allocated by memref on spike simulator is all zeros.I want to do some arithmetic and run some tests. Is there a way to initialize memref? Because I’m allocating a lot of memory.If anyone could help me, I would be grateful.Thanks!

Hi, mlir::linalg::FillRng2DOp from the linalg dialect should help you with that. For constant values use the mlir::linalg::FillOp.

Alternatively, you could initialize it manually.

%alloc = memref.alloc() : memref<4x4xf64>
%cst = arith.constant 0.000000e+00 : f64 // or some other value
affine.for %arg0 = 0 to 4 {
  affine.for %arg1 = 0 to 4 { %cst, %alloc[%arg0, %arg1] : memref<4x4xf64>


Bear in mind that FillRng2DOp lowers code in the IR to do the RNG.

@philipportner’s manual initialization is more or less what FillOp would do.

Another option is to use dense globals: "private" constant @__constant : memref<2x2x3x8xf32> = dense<1.000000e+00> {alignment = 128 : i64}
%1 = memref.get_global @__constant : memref<2x2x3x8xf32>

At tensor level, you can just declare an arith.constant (which will become a global+get after bufferization):

%cst = arith.constant dense<4.900000e+01> : tensor<2x2x3x8xf32>

The dense attribute doesn’t need to be a splat (all of the same value), but if not, you have to add every single value there, which can be messy. There’s also a hex representation that compacts things a bit if you have large tensors.