# -*- Python -*- # This file is licensed under a pytorch-style license # See frontends/pytorch/LICENSE for license information. import torch import torch_mlir import typing # RUN: %PYTHON %s | npcomp-opt | FileCheck %s mb = torch_mlir.ModuleBuilder() # CHECK-LABEL: func @__torch__.optional_return( # CHECK-SAME: %[[ARG:.*]]: i64) -> !torch.optional { # CHECK: %[[RET:.*]] = torch.derefine %[[ARG]] : i64 to !torch.optional # CHECK: return %[[RET]] : !torch.optional @mb.import_function @torch.jit.script def optional_return(i: int) -> typing.Optional[int]: return i # CHECK-LABEL: func @__torch__.optional_arg( # CHECK-SAME: %[[ARG:.*]]: !torch.optional) -> !basicpy.NoneType { @mb.import_function @torch.jit.script def optional_arg(i: typing.Optional[int]) -> None: return # CHECK-LABEL: func @__torch__.calls_optional_arg( # CHECK-SAME: %[[ARG:.*]]: i64) -> !basicpy.NoneType { # CHECK: %[[CALLEE:.*]] = constant @__torch__.optional_arg : (!torch.optional) -> !basicpy.NoneType # CHECK: %[[DEREFINED:.*]] = torch.derefine %[[ARG]] : i64 to !torch.optional # CHECK: %{{.*}} = call_indirect %[[CALLEE]](%[[DEREFINED]]) : (!torch.optional) -> !basicpy.NoneType @mb.import_function @torch.jit.script def calls_optional_arg(i: int): optional_arg(i) mb.module.operation.print() print()