Skip to content

Commit eeaa830

Browse files
committed
[mlir] Start rewrite tool
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 5c9b713 commit eeaa830

File tree

9 files changed

+494
-0
lines changed

9 files changed

+494
-0
lines changed

mlir/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,8 @@ endif()
285285
if(MLIR_STANDALONE_BUILD)
286286
llvm_distribution_add_targets()
287287
endif()
288+
289+
# FIXME: Currently depends on utility functions inside clang.
290+
if ("clang" IN_LIST LLVM_ENABLE_PROJECTS)
291+
set(MLIR_ENABLE_REWRITE ON CACHE BOOL "mlir-rewrite enabled")
292+
endif()

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
197197
)
198198
endif()
199199

200+
# FIXME: Currently depends on utility functions inside clang.
201+
if ("clang" IN_LIST LLVM_ENABLE_PROJECTS)
202+
list(APPEND MLIR_TEST_DEPENDS
203+
mlir-rewrite
204+
)
205+
endif()
206+
200207
# This target can be used to just build the dependencies
201208
# for the check-mlir target without executing the tests.
202209
# This is useful for bots when splitting the build step

mlir/test/lit.cfg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def add_runtime(name):
144144
)
145145
)
146146

147+
if config.enable_mlir_rewrite:
148+
tools.extend(["mlir-rewrite"])
149+
config.available_features.add('mlir-rewrite')
150+
147151
# The following tools are optional
148152
tools.extend(
149153
[

mlir/test/lit.site.cfg.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ config.mlir_obj_root = "@MLIR_BINARY_DIR@"
2323
config.mlir_tools_dir = "@MLIR_TOOLS_DIR@"
2424
config.mlir_cmake_dir = "@MLIR_CMAKE_DIR@"
2525
config.mlir_lib_dir = "@MLIR_LIB_DIR@"
26+
config.enable_mlir_rewrite = "@MLIR_ENABLE_REWRITE@"
2627

2728
config.build_examples = @LLVM_BUILD_EXAMPLES@
2829
config.run_cuda_tests = @MLIR_ENABLE_CUDA_CONVERSIONS@

mlir/test/mlir-rewrite/simple.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
// REQUIRES: mlir-rewrite
5+
6+
func.func @two_dynamic_one_direct_shape(%arg0: tensor<?x4x?xf32>, %arg1: tensor<2x4x?xf32>) -> tensor<?x4x?xf32> {
7+
// RENAME: "test.concat"({{.*}}) {bxis = 0 : i64}
8+
// RANGE: 《%{{.*}} = 〖"test.concat"〗({{.*}}) {axis = 0 : i64} : (tensor<?x4x?xf32>, tensor<2x4x?xf32>) -> tensor<?x4x?xf32>》
9+
%5 = "test.concat"(%arg0, %arg1) {axis = 0 : i64} : (tensor<?x4x?xf32>, tensor<2x4x?xf32>) -> tensor<?x4x?xf32>
10+
return %5 : tensor<?x4x?xf32>
11+
}
12+

mlir/tools/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ add_subdirectory(tblgen-to-irdl)
1515
if(MLIR_ENABLE_EXECUTION_ENGINE)
1616
add_subdirectory(mlir-cpu-runner)
1717
endif()
18+
19+
# FIXME: Currently depends on utility functions inside clang.
20+
if ("clang" IN_LIST LLVM_ENABLE_PROJECTS)
21+
add_subdirectory(mlir-rewrite)
22+
endif()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
clangRewrite
11+
MLIRAffineAnalysis
12+
MLIRAnalysis
13+
MLIRCastInterfaces
14+
MLIRDialect
15+
MLIROptLib
16+
MLIRParser
17+
MLIRPass
18+
MLIRTransforms
19+
MLIRTransformUtils
20+
MLIRSupport
21+
MLIRIR
22+
)
23+
24+
include_directories(../../../clang/include)
25+
26+
add_mlir_tool(mlir-rewrite
27+
mlir-rewrite.cpp
28+
29+
DEPENDS
30+
${LIBS}
31+
SUPPORT_PLUGINS
32+
)
33+
target_link_libraries(mlir-rewrite PRIVATE ${LIBS})
34+
llvm_update_compile_flags(mlir-rewrite)
35+
36+
mlir_check_all_link_libraries(mlir-rewrite)
37+
export_executable_symbols_for_plugins(mlir-rewrite)

0 commit comments

Comments
 (0)