Canonicalization of 'x + (+0.0)' in tosa

I found that mlir/test/Dialect/Tosa/canonicalize.mlir (link) is folding ‘x + (+0.0)’ into ‘x’.

// CHECK-LABEL: @add_zero_float
func @add_zero_float(%arg0: tensor<2x3xf32>) -> tensor<2x3xf32> {
  // CHECK: return %arg0
  // CHECK-NOT: tosa.add
  %zeros = "tosa.const"() {value = dense<0.0> : tensor<2x3xf32>} : () -> tensor<2x3xf32>
  %1 = "tosa.add"(%arg0, %zeros) : (tensor<2x3xf32>, tensor<2x3xf32>) -> tensor<2x3xf32>
  return %1 : tensor<2x3xf32>

If %arg0 is -0.0, this isn’t correct because -0.0 + (+0.0) is +0.0.
My question is - does tosa’s canonicalization assume unsafe fp math by default?


It appears that this is an area where the TOSA specification isn’t clear on behavior. In the specification, we require support for signed zero, and define multiply and divide by +/-0, but we don’t define addition with -0.0. So the current canonicalization is taking advantage of a grey area.

I think the right thing to do is disable this specific canonicalization for FP, leaving it in place for integer operation. In addition, I’ll try to clarify this specific level of behavior in the specification. (Section 1.9 of the specification here: :zap: TOSA ( has the details of what we’ve defined for FP behavior.)


Thank you for a quick reply. The specification document in the link looks great well.