nl: Sum a 1-D f32 tensor of 16 elements using stablehlo.reduce with an add body.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<f32> {
    %init = stablehlo.constant dense<0.0> : tensor<f32>
    %0 = stablehlo.reduce(%a init: %init) across dimensions = [0] : (tensor<16xf32>, tensor<f32>) -> tensor<f32>
      reducer(%arg0: tensor<f32>, %arg1: tensor<f32>) {
        %sum = stablehlo.add %arg0, %arg1 : tensor<f32>
        stablehlo.return %sum : tensor<f32>
      }
    return %0 : tensor<f32>
  }
}

op: reduce

dialect: stablehlo+func


nl: Apply a 2-D convolution of an input tensor 1x4x4x3 (NHWC) with a filter 3x3x3x8.

mlir:
module {
  func.func @f(%a: tensor<1x4x4x3xf32>, %w: tensor<3x3x3x8xf32>) -> tensor<1x2x2x8xf32> {
    %0 = stablehlo.convolution(%a, %w)
      dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f],
      window = {stride = [1, 1], pad = [[0, 0], [0, 0]]}
      {batch_group_count = 1 : i64, feature_group_count = 1 : i64}
      : (tensor<1x4x4x3xf32>, tensor<3x3x3x8xf32>) -> tensor<1x2x2x8xf32>
    return %0 : tensor<1x2x2x8xf32>
  }
}

op: convolution


nl: Pad a 4x4 f32 tensor with 1-pixel borders of zero on each side, producing 6x6.

mlir:
module {
  func.func @f(%a: tensor<4x4xf32>) -> tensor<6x6xf32> {
    %pad = stablehlo.constant dense<0.0> : tensor<f32>
    %0 = stablehlo.pad %a, %pad, low = [1, 1], high = [1, 1], interior = [0, 0]
      : (tensor<4x4xf32>, tensor<f32>) -> tensor<6x6xf32>
    return %0 : tensor<6x6xf32>
  }
}

op: pad


nl: Slice 8 elements from a 1-D f32 tensor of 32 starting at dynamic index.

mlir:
module {
  func.func @f(%a: tensor<32xf32>, %i: tensor<i32>) -> tensor<8xf32> {
    %0 = stablehlo.dynamic_slice %a, %i, sizes = [8] : (tensor<32xf32>, tensor<i32>) -> tensor<8xf32>
    return %0 : tensor<8xf32>
  }
}


nl: Produce a 1-D i32 iota tensor of 16 elements (values 0..15).

mlir:
module {
  func.func @f() -> tensor<16xi32> {
    %0 = stablehlo.iota dim = 0 : tensor<16xi32>
    return %0 : tensor<16xi32>
  }
}

op: iota


nl: Clamp a 1-D f32 tensor of 16 elements to the [0, 1] range.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xf32> {
    %lo = stablehlo.constant dense<0.0> : tensor<16xf32>
    %hi = stablehlo.constant dense<1.0> : tensor<16xf32>
    %0 = stablehlo.clamp %lo, %a, %hi : tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: clamp


nl: Elementwise select between two f32 tensors of shape 16 based on a bool mask.

mlir:
module {
  func.func @f(%p: tensor<16xi1>, %x: tensor<16xf32>, %y: tensor<16xf32>) -> tensor<16xf32> {
    %0 = stablehlo.select %p, %x, %y : (tensor<16xi1>, tensor<16xf32>, tensor<16xf32>) -> tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: select


nl: Elementwise less-than comparison of two f32 tensors of shape 16.

mlir:
module {
  func.func @f(%a: tensor<16xf32>, %b: tensor<16xf32>) -> tensor<16xi1> {
    %0 = stablehlo.compare LT, %a, %b, FLOAT : (tensor<16xf32>, tensor<16xf32>) -> tensor<16xi1>
    return %0 : tensor<16xi1>
  }
}

op: compare


nl: Concatenate two 4x4 f32 tensors along axis 0 to produce an 8x4 tensor.

mlir:
module {
  func.func @f(%a: tensor<4x4xf32>, %b: tensor<4x4xf32>) -> tensor<8x4xf32> {
    %0 = stablehlo.concatenate %a, %b, dim = 0 : (tensor<4x4xf32>, tensor<4x4xf32>) -> tensor<8x4xf32>
    return %0 : tensor<8x4xf32>
  }
}

op: concatenate


nl: Reduce max across rows of a 4x8 f32 tensor, producing a 4-element vector.

mlir:
module {
  func.func @f(%a: tensor<4x8xf32>) -> tensor<4xf32> {
    %init = stablehlo.constant dense<0xFF800000> : tensor<f32>
    %0 = stablehlo.reduce(%a init: %init) across dimensions = [1] : (tensor<4x8xf32>, tensor<f32>) -> tensor<4xf32>
      reducer(%arg0: tensor<f32>, %arg1: tensor<f32>) {
        %m = stablehlo.maximum %arg0, %arg1 : tensor<f32>
        stablehlo.return %m : tensor<f32>
      }
    return %0 : tensor<4xf32>
  }
}


nl: Apply a 1-D convolution to an input tensor of shape 1x16x4 (NWC) with filter 3x4x8.

mlir:
module {
  func.func @f(%a: tensor<1x16x4xf32>, %w: tensor<3x4x8xf32>) -> tensor<1x14x8xf32> {
    %0 = stablehlo.convolution(%a, %w)
      dim_numbers = [b, 0, f]x[0, i, o]->[b, 0, f],
      window = {stride = [1], pad = [[0, 0]]}
      {batch_group_count = 1 : i64, feature_group_count = 1 : i64}
      : (tensor<1x16x4xf32>, tensor<3x4x8xf32>) -> tensor<1x14x8xf32>
    return %0 : tensor<1x14x8xf32>
  }
}


nl: Gather 4 rows from an 8x16 f32 tensor using a 4-element i32 index vector.

mlir:
module {
  func.func @f(%a: tensor<8x16xf32>, %idx: tensor<4x1xi32>) -> tensor<4x16xf32> {
    %0 = "stablehlo.gather"(%a, %idx) {
      dimension_numbers = #stablehlo.gather<
        offset_dims = [1],
        collapsed_slice_dims = [0],
        start_index_map = [0],
        index_vector_dim = 1
      >,
      slice_sizes = array<i64: 1, 16>,
      indices_are_sorted = false
    } : (tensor<8x16xf32>, tensor<4x1xi32>) -> tensor<4x16xf32>
    return %0 : tensor<4x16xf32>
  }
}

op: gather


nl: Take the first 4x4 block of an 8x8 f32 tensor using stablehlo.slice.

mlir:
module {
  func.func @f(%a: tensor<8x8xf32>) -> tensor<4x4xf32> {
    %0 = stablehlo.slice %a [0:4, 0:4] : (tensor<8x8xf32>) -> tensor<4x4xf32>
    return %0 : tensor<4x4xf32>
  }
}

op: slice


nl: Compute elementwise reciprocal square root of a 16-element f32 tensor.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xf32> {
    %0 = stablehlo.rsqrt %a : tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: rsqrt


nl: Compute elementwise sign of a 16-element f32 tensor.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xf32> {
    %0 = stablehlo.sign %a : tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: sign


nl: Compute elementwise hyperbolic tangent of a 16-element f32 tensor.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xf32> {
    %0 = stablehlo.tanh %a : tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: tanh


nl: Sort a 1-D f32 tensor of 16 elements in ascending order using stablehlo.sort.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xf32> {
    %0 = "stablehlo.sort"(%a) ({
      ^bb0(%x: tensor<f32>, %y: tensor<f32>):
        %cmp = stablehlo.compare LT, %x, %y, FLOAT : (tensor<f32>, tensor<f32>) -> tensor<i1>
        stablehlo.return %cmp : tensor<i1>
    }) {dimension = 0 : i64, is_stable = true} : (tensor<16xf32>) -> tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: sort


nl: Compute elementwise power a^b of two 16-element f32 tensors.

mlir:
module {
  func.func @f(%a: tensor<16xf32>, %b: tensor<16xf32>) -> tensor<16xf32> {
    %0 = stablehlo.power %a, %b : tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: power


nl: Update a 4-element slice of a 32-element f32 tensor at a dynamic index.

mlir:
module {
  func.func @f(%a: tensor<32xf32>, %u: tensor<4xf32>, %i: tensor<i32>) -> tensor<32xf32> {
    %0 = stablehlo.dynamic_update_slice %a, %u, %i : (tensor<32xf32>, tensor<4xf32>, tensor<i32>) -> tensor<32xf32>
    return %0 : tensor<32xf32>
  }
}


nl: Reverse the order of elements in a 1-D f32 tensor of 16 elements.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xf32> {
    %0 = stablehlo.reverse %a, dims = [0] : tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: reverse


nl: Round elementwise to nearest even integer on a 16-element f32 tensor.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xf32> {
    %0 = stablehlo.round_nearest_even %a : tensor<16xf32>
    return %0 : tensor<16xf32>
  }
}

op: round


nl: Elementwise is_finite on a 16-element f32 tensor, returning a bool tensor.

mlir:
module {
  func.func @f(%a: tensor<16xf32>) -> tensor<16xi1> {
    %0 = stablehlo.is_finite %a : (tensor<16xf32>) -> tensor<16xi1>
    return %0 : tensor<16xi1>
  }
}


nl: Elementwise left-shift of two 16-element i32 tensors.

mlir:
module {
  func.func @f(%a: tensor<16xi32>, %b: tensor<16xi32>) -> tensor<16xi32> {
    %0 = stablehlo.shift_left %a, %b : tensor<16xi32>
    return %0 : tensor<16xi32>
  }
}


nl: Elementwise bitwise xor of two 16-element i32 tensors.

mlir:
module {
  func.func @f(%a: tensor<16xi32>, %b: tensor<16xi32>) -> tensor<16xi32> {
    %0 = stablehlo.xor %a, %b : tensor<16xi32>
    return %0 : tensor<16xi32>
  }
}

op: xor


nl: Elementwise population-count of a 16-element i32 tensor.

mlir:
module {
  func.func @f(%a: tensor<16xi32>) -> tensor<16xi32> {
    %0 = stablehlo.popcnt %a : tensor<16xi32>
    return %0 : tensor<16xi32>
  }
}

op: popcnt