Skip to content
22 changes: 21 additions & 1 deletion lib/NeuraDialect/NeuraPasses.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"

#include "Conversion/ConversionPasses.h"
#include "NeuraDialect/NeuraDialect.h"
#include "NeuraDialect/NeuraOps.h"
#include "NeuraDialect/NeuraPasses.h"
#include "NeuraDialect/NeuraTypes.h"

std::string filename = "opgraph.dot";
std::error_code EC;
llvm::raw_fd_ostream os(filename, EC, llvm::sys::fs::OF_Text);
if (EC) {
llvm::errs() << "Cannot create dot file: " << EC.message() << "\n";
}

// This pass pipeline can convert all the other dialects into the Neura dialect
void mlir::neura::registerNeuraConversionPassPipeline() {
PassPipelineRegistration<>(
Expand All @@ -17,6 +26,17 @@ void mlir::neura::registerNeuraConversionPassPipeline() {
// Convert all the other dialects into the Neura dialect
pm.addPass(mlir::createLowerArithToNeuraPass());
pm.addPass(mlir::createLowerLlvmToNeuraPass());
pm.addPass(mlir::createPrintOpGraphPass());
pm.addPass(mlir::createPrintOpGraphPass(os));

pm.addPass(mlir::neura::createCanonicalizeCastPass());
pm.addPass(mlir::neura::createCanonicalizeLiveInPass());
pm.addPass(mlir::neura::createLeveragePredicatedValuePass());
pm.addPass(mlir::createPrintOpGraphPass(os));

pm.addPass(mlir::neura::createTransformCtrlToDataFlowPass());
pm.addPass(mlir::neura::createFoldConstantPass());
pm.addPass(mlir::neura::createFusePatternPass());
pm.addPass(mlir::neura::createInsertDataMovPass());
pm.addPass(mlir::createPrintOpGraphPass(os));
});
}
12 changes: 11 additions & 1 deletion test/visualize/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ dot -Tpng test.dot -o test.png

It will generate a `test.png` file in the current directory, as shown below:

![test](test.png)
![test](test.png)

If there are multiple graphs in one dot file, you can use the option `-O` to generate multiple DFGs from one dot file.

Example:

```bash
dot -Tpng test.dot -O
```

It will generate `test.dot.png`, `test.dot.1.png`, `test.dot.2.png`, etc.
12 changes: 11 additions & 1 deletion test/visualize/test2.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Test PrintOpGraphPass in neura-compiler
// RUN: neura-compiler --neura-conversion %s 2>&1 | FileCheck %s --check-prefix=CHECK-GRAPH
// RUN: neura-compiler --neura-conversion %s
// RUN: FileCheck %s --input-file=%S/opgraph.dot -check-prefix=CHECK-GRAPH

func.func @test_print_op_graph(%a: f32, %b: f32) -> f32 {
%c = arith.constant 1.0 : f32
Expand All @@ -13,3 +14,12 @@ func.func @test_print_op_graph(%a: f32, %b: f32) -> f32 {
// CHECK-GRAPH: label = "func.func : ()\n\naccelerator: \"neura\"\nfunction_type: (f32, f32) -> f32\nsym_name: \"test_print_op_graph...";
// CHECK-GRAPH: label = "neura.fadd : (f32)\n"
// CHECK-GRAPH: label = "neura.return : ()\n"
// CHECK-GRAPH: digraph G
// CHECK-GRAPH: label = "func.func : ()\n\naccelerator: \"neura\"\nfunction_type: (f32, f32) -> f32\nsym_name: \"test_print_op_graph...";
// CHECK-GRAPH: label = "neura.constant : (!neura.data<f32, i1>)
// CHECK-GRAPH: label = "neura.fadd : (!neura.data<f32, i1>)\n"
// CHECK-GRAPH: digraph G
// CHECK-GRAPH: label = "func.func : ()\n\naccelerator: \"neura\"\nfunction_type: (f32, f32) -> f32\nsym_name: \"test_print_op_graph...";
// CHECK-GRAPH: label = "neura.constant : (!neura.data<f32, i1>)
// CHECK-GRAPH: label = "neura.data_mov : (!neura.data<f32, i1>)
// CHECK-GRAPH: label = "neura.fadd : (!neura.data<f32, i1>)\n"
Loading