I’m directly editing .mlir
files, and I’d like to have some automatic formatting, ideally using some sort of canonical format.
For example, let’s say I was writing a matmul file, I’d like if the below (poorly formatted) code could be automatically indented, perhaps have new lines inserted at appropriate places to increase clarity, etc.
// C += A * B.
func.func @matmul(%A
: memref<2048x2048xf64>, %B
: memref<2048x2048xf64>, %C
: memref<2048x2048xf64>) {
affine.for %arg3 = 0 to 2048 {
affine.for %arg4 = 0 to 2048 {
affine.for %arg5 = 0 to 2048 {
%a = affine.load %A[%arg3, %arg5] : memref<2048x2048xf64> %b =
affine.load %B[%arg5, %arg4] : memref<2048x2048xf64> %ci =
affine.load %C[%arg3, %arg4] : memref<2048x2048xf64> %p =
mulf %a,
%b : f64 %co = addf %ci, %p : f64 affine.store %co,
%C[% arg3, % arg4] : memref<2048x2048xf64>
}
}
}
return
}
clang-format seems like the right tool for this job, and is integrated into most IDEs. However I’ve not been able to find a .clang-format
configuration for MLIR that gives me what I want. There is the .clang-format
in the root of mlir
, but I think this is just for the C++ code of MLIR. Running clang-format using this config does not improve the file, in fact it just breaks syntax because it inserts spaces between % and the SSA variable name, e.g., %A -> % A
.
We have some MLIR files in the test directory, e.g., here, which look nicely formatted. However I am unsure if this has been done automatically, or through the toil of contributors.
Is there a .clang-format
for MLIR files, or some other canonical way to automatically format them?
If I run just mlir-opt [my_mlir_file]
with no optimizations, it provides the type of formatting I’m looking for. However it also applies some small yet significant changes (e.g., changing my SSA names). Nevertheless, it shows that auto-formatting is available somewhere. Is there a way I can get this automatically in my editor?