forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FXML-4440] Introduce a pass that annotates the type of the argument …
…as an attribute (#167) Create a pass that annotates all func op inputs with their type as attributes.
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===- AnnotateInputTypes.cpp - Type attribute annotation for func ops ----===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements a pass that creates type attributes for func parameters, | ||
// that mirror the actual type. This is useful when the func op input types | ||
// might change. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Func/Transforms/Passes.h" | ||
|
||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/Pass/Pass.h" | ||
|
||
using namespace mlir; | ||
|
||
namespace mlir::func { | ||
#define GEN_PASS_DEF_ANNOTATEFUNCTIONTYPE | ||
#include "mlir/Dialect/Func/Transforms/Passes.h.inc" | ||
} // namespace mlir::func | ||
|
||
namespace { | ||
struct AnnotateFunctionTypePass | ||
: public mlir::func::impl::AnnotateFunctionTypeBase< | ||
AnnotateFunctionTypePass> { | ||
|
||
void runOnOperation() override { | ||
func::FuncOp func = getOperation(); | ||
auto inputs = func.getArgumentTypes(); | ||
auto results = func.getResultTypes(); | ||
|
||
for (const auto [argNum, type] : llvm::enumerate(inputs)) { | ||
func.setArgAttr(argNum, "func.orig_type", TypeAttr::get(type)); | ||
} | ||
|
||
for (const auto [resultNum, type] : llvm::enumerate(results)) { | ||
func.setResultAttr(resultNum, "func.orig_type", TypeAttr::get(type)); | ||
} | ||
} | ||
}; | ||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// RUN: mlir-opt %s --split-input-file --annotate-function-type | FileCheck %s | ||
|
||
// CHECK-LABEL: func.func @one_arg(%arg0: tensor<f32> {func.orig_type = tensor<f32>}) -> (tensor<f32> {func.orig_type = tensor<f32>}) { | ||
func.func @one_arg(%arg0: tensor<f32>) -> tensor<f32> { | ||
return %arg0 : tensor<f32> | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: func.func @one_arg_int(%arg0: tensor<ui8> {func.orig_type = tensor<ui8>}) -> (tensor<ui8> {func.orig_type = tensor<ui8>}) { | ||
func.func @one_arg_int(%arg0: tensor<ui8>) -> tensor<ui8> { | ||
return %arg0 : tensor<ui8> | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: func.func @n_rank_tensor(%arg0: tensor<3x4x5xf32> {func.orig_type = tensor<3x4x5xf32>}) -> (tensor<3x4x5xf32> {func.orig_type = tensor<3x4x5xf32>}) { | ||
func.func @n_rank_tensor(%arg0: tensor<3x4x5xf32>) -> tensor<3x4x5xf32> { | ||
return %arg0 : tensor<3x4x5xf32> | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: func.func @two_args(%arg0: f32 {func.orig_type = f32}, %arg1: f32 {func.orig_type = f32}) -> (f32 {func.orig_type = f32}) { | ||
func.func @two_args(%arg0: f32, %arg1: f32) -> f32 { | ||
return %arg0 : f32 | ||
} |