Skip to content

Commit 4c25a53

Browse files
authored
[mlir] Start rewrite tool (#77668)
Initial commit of a tool to help in textual rewrites of .mlir files. This tool builds of of AsmParserState and is rather simple. Took some inspiration from when I used clang's AST rewrites where I'd often treat it as a "localizing" regex applicator in fallback cases, and started with that as functionality. There though, one does have access to the lower level info than here, but still a step up over sed over entire file. This aims to be helpful (e.g., rewrite syntax including best effort inside comments) rather than bulletproof tool. It may even be better suited under utils than tools. And most of the rewrites would be rather short lived and might never make it upstream (while the helpers of those rewrites may for future rewrites). The layering at the moment is not ideal as it is reusing the RewriteBuffer class from clang's rewrite engine. So only optionally enabling where clang is also enable. There doesn't seem to be anything clang specific there (the dep does pull in more dependencies than ideal, but leaving both refactorings). Additionally started it as a single file to prototype more easily, planning to refactor later to include and libs for out of file usage.
1 parent c275080 commit 4c25a53

File tree

7 files changed

+491
-0
lines changed

7 files changed

+491
-0
lines changed

mlir/docs/Tools/mlir-rewrite.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# mlir-rewrite
2+
3+
Tool to simplify rewriting .mlir files. There are a couple of build in rewrites
4+
discussed below along with usage.
5+
6+
Note: This is still in very early stage. Its so early its less a tool than a
7+
growing collection of useful functions: to use its best to do what's needed on
8+
a brance by just hacking it (dialects registered, rewrites etc) to say help
9+
ease a rename, upstream useful utility functions, point to ease others
10+
migrating, and then bin eventually. Once there are actually useful parts it
11+
should be refactored same as mlir-opt.
12+
13+
[TOC]
14+
15+
## simple-rename
16+
17+
Rename per op given a substring to a target. The match and replace uses LLVM's
18+
regex sub for the match and replace while the op-name is matched via regular
19+
string comparison. E.g.,
20+
21+
```
22+
mlir-rewrite input.mlir -o output.mlir --simple-rename \
23+
--simple-rename-op-name="test.concat" --simple-rename-match="axis" \
24+
--simple-rename-replace="bxis"
25+
```
26+
27+
to replace `axis` substring in the text of the range corresponding to
28+
`test.concat` ops with `bxis`.
29+

mlir/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ set(MLIR_TEST_DEPENDS
115115
mlir-opt
116116
mlir-query
117117
mlir-reduce
118+
mlir-rewrite
118119
mlir-tblgen
119120
mlir-translate
120121
tblgen-lsp-server

mlir/test/mlir-rewrite/simple.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// 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
2+
// RUN: mlir-opt %s | mlir-rewrite --mark-ranges | FileCheck %s -check-prefix=RANGE
3+
// Note: running through mlir-opt to just strip out comments & avoid self matches.
4+
5+
func.func @two_dynamic_one_direct_shape(%arg0: tensor<?x4x?xf32>, %arg1: tensor<2x4x?xf32>) -> tensor<?x4x?xf32> {
6+
// RENAME: "test.concat"({{.*}}) {bxis = 0 : i64}
7+
// RANGE: 《%{{.*}} = 〖"test.concat"〗({{.*}}) {axis = 0 : i64} : (tensor<?x4x?xf32>, tensor<2x4x?xf32>) -> tensor<?x4x?xf32>》
8+
%5 = "test.concat"(%arg0, %arg1) {axis = 0 : i64} : (tensor<?x4x?xf32>, tensor<2x4x?xf32>) -> tensor<?x4x?xf32>
9+
return %5 : tensor<?x4x?xf32>
10+
}
11+

mlir/tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_subdirectory(mlir-parser-fuzzer)
44
add_subdirectory(mlir-pdll-lsp-server)
55
add_subdirectory(mlir-query)
66
add_subdirectory(mlir-reduce)
7+
add_subdirectory(mlir-rewrite)
78
add_subdirectory(mlir-shlib)
89
add_subdirectory(mlir-spirv-cpu-runner)
910
add_subdirectory(mlir-translate)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
set(LLVM_LINK_COMPONENTS
3+
Support
4+
)
5+
6+
set(LIBS
7+
${dialect_libs}
8+
${test_libs}
9+
10+
MLIRAffineAnalysis
11+
MLIRAnalysis
12+
MLIRCastInterfaces
13+
MLIRDialect
14+
MLIRParser
15+
MLIRPass
16+
MLIRTransforms
17+
MLIRTransformUtils
18+
MLIRSupport
19+
MLIRIR
20+
)
21+
22+
include_directories(../../../clang/include)
23+
24+
add_mlir_tool(mlir-rewrite
25+
mlir-rewrite.cpp
26+
27+
DEPENDS
28+
${LIBS}
29+
SUPPORT_PLUGINS
30+
)
31+
target_link_libraries(mlir-rewrite PRIVATE ${LIBS})
32+
llvm_update_compile_flags(mlir-rewrite)
33+
34+
mlir_check_all_link_libraries(mlir-rewrite)
35+
export_executable_symbols_for_plugins(mlir-rewrite)

0 commit comments

Comments
 (0)