Skip to content

[mlir] Start rewrite tool #77668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions mlir/docs/Tools/mlir-rewrite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# mlir-rewrite

Tool to simplify rewriting .mlir files. There are a couple of build in rewrites
discussed below along with usage.

Note: This is still in very early stage. Its so early its less a tool than a
growing collection of useful functions: to use its best to do what's needed on
a brance by just hacking it (dialects registered, rewrites etc) to say help
ease a rename, upstream useful utility functions, point to ease others
migrating, and then bin eventually. Once there are actually useful parts it
should be refactored same as mlir-opt.

[TOC]

## simple-rename

Rename per op given a substring to a target. The match and replace uses LLVM's
regex sub for the match and replace while the op-name is matched via regular
string comparison. E.g.,

```
mlir-rewrite input.mlir -o output.mlir --simple-rename \
--simple-rename-op-name="test.concat" --simple-rename-match="axis" \
--simple-rename-replace="bxis"
```

to replace `axis` substring in the text of the range corresponding to
`test.concat` ops with `bxis`.

1 change: 1 addition & 0 deletions mlir/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ set(MLIR_TEST_DEPENDS
mlir-opt
mlir-query
mlir-reduce
mlir-rewrite
mlir-tblgen
mlir-translate
tblgen-lsp-server
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/mlir-rewrite/simple.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: mlir-opt %s | mlir-rewrite --simple-rename --simple-rename-op-name="test.concat" --simple-rename-match="axis" --simple-rename-replace="bxis" | FileCheck %s -check-prefix=RENAME
// RUN: mlir-opt %s | mlir-rewrite --mark-ranges | FileCheck %s -check-prefix=RANGE
// Note: running through mlir-opt to just strip out comments & avoid self matches.

func.func @two_dynamic_one_direct_shape(%arg0: tensor<?x4x?xf32>, %arg1: tensor<2x4x?xf32>) -> tensor<?x4x?xf32> {
// RENAME: "test.concat"({{.*}}) {bxis = 0 : i64}
// RANGE: 《%{{.*}} = 〖"test.concat"〗({{.*}}) {axis = 0 : i64} : (tensor<?x4x?xf32>, tensor<2x4x?xf32>) -> tensor<?x4x?xf32>》
%5 = "test.concat"(%arg0, %arg1) {axis = 0 : i64} : (tensor<?x4x?xf32>, tensor<2x4x?xf32>) -> tensor<?x4x?xf32>
return %5 : tensor<?x4x?xf32>
}

1 change: 1 addition & 0 deletions mlir/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_subdirectory(mlir-parser-fuzzer)
add_subdirectory(mlir-pdll-lsp-server)
add_subdirectory(mlir-query)
add_subdirectory(mlir-reduce)
add_subdirectory(mlir-rewrite)
add_subdirectory(mlir-shlib)
add_subdirectory(mlir-spirv-cpu-runner)
add_subdirectory(mlir-translate)
Expand Down
35 changes: 35 additions & 0 deletions mlir/tools/mlir-rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
set(LLVM_LINK_COMPONENTS
Support
)

set(LIBS
${dialect_libs}
${test_libs}

MLIRAffineAnalysis
MLIRAnalysis
MLIRCastInterfaces
MLIRDialect
MLIRParser
MLIRPass
MLIRTransforms
MLIRTransformUtils
MLIRSupport
MLIRIR
)

include_directories(../../../clang/include)

add_mlir_tool(mlir-rewrite
mlir-rewrite.cpp

DEPENDS
${LIBS}
SUPPORT_PLUGINS
)
target_link_libraries(mlir-rewrite PRIVATE ${LIBS})
llvm_update_compile_flags(mlir-rewrite)

mlir_check_all_link_libraries(mlir-rewrite)
export_executable_symbols_for_plugins(mlir-rewrite)
Loading
Loading