llvm/torch-mlir

torch.constant.int with large negative values causes error

Opened this issue · 2 comments

Reproduction instructions:

Save this reproducer to repro.mlir:

module {
    func.func @intfail() -> !torch.int {
        %0 = torch.vtensor.literal(dense<-9223372036854775807> : tensor<1xsi64>) : !torch.vtensor<[1],si64>
        %1 = torch.aten.item %0 : !torch.vtensor<[1],si64> -> !torch.int
        return %1 : !torch.int
    }
}

run

torch-mlir-opt repro.mlir --canonicalize -o repro2.mlir

repro2.mlir should contain

module {
  func.func @intfail() -> !torch.int {
    %int-9223372036854775807 = torch.constant.int -9223372036854775807
    return %int-9223372036854775807 : !torch.int
  }
}

If you try

torch-mlir-opt repro2.mlir --convert-torch-to-arith

It returns

repro2.mlir:3:51: error: custom op 'torch.constant.int' integer value too large
    %int-9223372036854775807 = torch.constant.int -9223372036854775807

However, running from the original repro.mlir:

torch-mlir-opt repro.mlir --canonicalize --convert-torch-to-arith

generates valid IR:

module {
  func.func @intfail() -> !torch.int {
    %c-9223372036854775807_i64 = arith.constant -9223372036854775807 : i64
    %0 = torch_c.from_i64 %c-9223372036854775807_i64
    return %0 : !torch.int
  }
}

It seems like an error with parsing the constant int op? I'm not sure what is going on here.

if you --print-ir-after-all do you get repro2 as an intermediate?

if you --print-ir-after-all do you get repro2 as an intermediate?

Yeah:

// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
module {
  func.func @intfail() -> !torch.int {
    %0 = torch.vtensor.literal(dense<-9223372036854775807> : tensor<1xsi64>) : !torch.vtensor<[1],si64>
    %1 = torch.aten.item %0 : !torch.vtensor<[1],si64> -> !torch.int
    return %1 : !torch.int
  }
}


// -----// IR Dump Before ConvertTorchToArith (convert-torch-to-arith) //----- //
func.func @intfail() -> !torch.int {
  %int-9223372036854775807 = torch.constant.int -9223372036854775807
  return %int-9223372036854775807 : !torch.int
}

module {
  func.func @intfail() -> !torch.int {
    %c-9223372036854775807_i64 = arith.constant -9223372036854775807 : i64
    %0 = torch_c.from_i64 %c-9223372036854775807_i64
    return %0 : !torch.int
  }
}