2021-03-02 09:24:15 +08:00
|
|
|
# -*- 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(
|
2021-06-17 06:53:15 +08:00
|
|
|
# CHECK-SAME: %[[ARG:.*]]: !torch.int) -> !torch.optional<!torch.int> {
|
|
|
|
# CHECK: %[[RET:.*]] = torch.derefine %[[ARG]] : !torch.int to !torch.optional<!torch.int>
|
|
|
|
# CHECK: return %[[RET]] : !torch.optional<!torch.int>
|
2021-03-02 09:24:15 +08:00
|
|
|
@mb.import_function
|
|
|
|
@torch.jit.script
|
|
|
|
def optional_return(i: int) -> typing.Optional[int]:
|
|
|
|
return i
|
|
|
|
|
|
|
|
# CHECK-LABEL: func @__torch__.optional_arg(
|
2021-06-17 06:53:15 +08:00
|
|
|
# CHECK-SAME: %[[ARG:.*]]: !torch.optional<!torch.int>) -> !torch.none {
|
2021-03-02 09:24:15 +08:00
|
|
|
@mb.import_function
|
|
|
|
@torch.jit.script
|
|
|
|
def optional_arg(i: typing.Optional[int]) -> None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# CHECK-LABEL: func @__torch__.calls_optional_arg(
|
2021-06-17 06:53:15 +08:00
|
|
|
# CHECK-SAME: %[[ARG:.*]]: !torch.int) -> !torch.none {
|
|
|
|
# CHECK: %[[CALLEE:.*]] = constant @__torch__.optional_arg : (!torch.optional<!torch.int>) -> !torch.none
|
|
|
|
# CHECK: %[[DEREFINED:.*]] = torch.derefine %[[ARG]] : !torch.int to !torch.optional<!torch.int>
|
|
|
|
# CHECK: %{{.*}} = call_indirect %[[CALLEE]](%[[DEREFINED]]) : (!torch.optional<!torch.int>) -> !torch.none
|
2021-03-02 09:24:15 +08:00
|
|
|
@mb.import_function
|
|
|
|
@torch.jit.script
|
|
|
|
def calls_optional_arg(i: int):
|
|
|
|
optional_arg(i)
|
|
|
|
|
|
|
|
|
|
|
|
mb.module.operation.print()
|
|
|
|
print()
|