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