Skip to content
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

[Dialect] Trace dialect for automatically profiling models #388

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
47 changes: 47 additions & 0 deletions examples/TraceDialect/makefile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use better toolchain configuration: see here

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
BUDDY_OPT := ../../build/bin/buddy-opt
MLIR_OPT := ../../llvm/build/bin/mlir-opt
MLIR_TRANSLATE := ../../llvm/build/bin/mlir-translate
MLIR_CPU_RUNNER := ../../llvm/build/bin/mlir-cpu-runner
LLC := ../../llvm/build/bin/llc
OPT_FLAG := -O0

ifeq ($(shell uname),Linux)
MLIR_RUNNER_UTILS := ../../llvm/build/lib/libmlir_runner_utils.so
MLIR_C_RUNNER_UTILS := ../../llvm/build/lib/libmlir_c_runner_utils.so
MLIR_ASYNC_RUNTIME := ../../llvm/build/lib/libmlir_async_runtime.so
MTRIPLE := x86_64-unknown-linux-gnu
else ifeq ($(shell uname),Darwin)
MLIR_RUNNER_UTILS := ../../llvm/build/lib/libmlir_runner_utils.dylib
MLIR_C_RUNNER_UTILS := ../../llvm/build/lib/libmlir_c_runner_utils.dylib
MLIR_ASYNC_RUNTIME := ./../llvm/build/lib/libmlir_async_runtime.dylib
MTRIPLE := x86_64-apple-darwin
endif

trace-conv2d-lower:
@${BUDDY_OPT} ./trace-conv2d.mlir \
--lower-trace | \
${MLIR_OPT} \
-convert-linalg-to-loops -lower-affine -convert-scf-to-cf \
-convert-vector-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm \
-convert-func-to-llvm -reconcile-unrealized-casts \
-o ./log.mlir

trace-conv2d-translate:
@${BUDDY_OPT} ./trace-conv2d.mlir \
--lower-trace | \
${MLIR_OPT} \
-convert-linalg-to-loops -lower-affine -convert-scf-to-cf \
-convert-vector-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm \
-convert-func-to-llvm -reconcile-unrealized-casts | \
${MLIR_TRANSLATE} --mlir-to-llvmir -o log.ll

trace-conv2d-run:
@${BUDDY_OPT} ./trace-conv2d.mlir \
--lower-trace | \
${MLIR_OPT} ${MLIR_OPT_OPTIONS} \
-convert-linalg-to-loops -lower-affine -convert-scf-to-cf \
-convert-vector-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm \
-convert-func-to-llvm -reconcile-unrealized-casts | \
${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void -shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}

44 changes: 44 additions & 0 deletions examples/TraceDialect/trace-conv2d.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: buddy-opt %s --lower-trace | FileCheck %s

#map = affine_map<(d0, d1) -> (d0 + d1 - 1)>

module {
func.func private @rtclock() -> f64
func.func private @printF64(f64)
func.func private @printNewline()

func.func @alloc_2d_filled_f32(%rows: index, %cols: index, %value: f32) -> memref<?x?xf32> {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%alloc = memref.alloc(%rows, %cols) : memref<?x?xf32>
scf.for %i = %c0 to %rows step %c1 {
scf.for %j = %c0 to %cols step %c1 {
memref.store %value, %alloc[%i, %j] : memref<?x?xf32>
}
}
return %alloc : memref<?x?xf32>
}

func.func @main() {
%cst_one = arith.constant 1.0 : f32
%cst_zero = arith.constant 0.0 : f32
%c3 = arith.constant 3 : index
%c8 = arith.constant 8 : index
%c10 = arith.constant 10 : index

%filter = call @alloc_2d_filled_f32(%c3, %c3, %cst_one) : (index, index, f32) -> memref<?x?xf32>
%input = call @alloc_2d_filled_f32(%c10, %c10, %cst_one) : (index, index, f32) -> memref<?x?xf32>
%output = call @alloc_2d_filled_f32(%c8, %c8, %cst_zero) : (index, index, f32) -> memref<?x?xf32>
// start timing
// CHECK: call @rtclock() : () -> f64
%start_time = trace.time_start : -> f64
linalg.conv_2d ins(%input, %filter : memref<?x?xf32>, memref<?x?xf32>) outs(%output : memref<?x?xf32>)
// end timing
// CHECK: call @rtclock() : () -> f64
%end_time = trace.time_end : -> f64
%elapsed_time = arith.subf %end_time, %start_time : f64
call @printF64(%elapsed_time) : (f64) -> ()
call @printNewline() : () -> ()
return
}
}
1 change: 1 addition & 0 deletions midend/include/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ add_subdirectory(RVV)
add_subdirectory(VectorExp)
add_subdirectory(Gemmini)
add_subdirectory(Sche)
add_subdirectory(Trace)
13 changes: 13 additions & 0 deletions midend/include/Dialect/Trace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(LLVM_TARGET_DEFINITIONS TraceOps.td)
mlir_tablegen(TraceOps.h.inc -gen-op-decls)
mlir_tablegen(TraceOps.cpp.inc -gen-op-defs)

set(LLVM_TARGET_DEFINITIONS TraceOps.td)
mlir_tablegen(TraceOpsEnums.h.inc -gen-enum-decls)
mlir_tablegen(TraceOpsEnums.cpp.inc -gen-enum-defs)

set(LLVM_TARGET_DEFINITIONS TraceOps.td)
mlir_tablegen(TraceOpsAttributes.h.inc -gen-attrdef-decls)
mlir_tablegen(TraceOpsAttributes.cpp.inc -gen-attrdef-defs)

add_mlir_dialect(TraceOps trace)
28 changes: 28 additions & 0 deletions midend/include/Dialect/Trace/TraceDialect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===- TraceDialect.h - Trace Dialect Definition --------------------*- C++ -*-===//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong format: 80-col limitation.

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This is the header file for the bud dialect.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bud -> trace

//
//===----------------------------------------------------------------------===//

#ifndef TRACE_TRACEDIALECT_H
#define TRACE_TRACEDIALECT_H

#include "mlir/IR/Dialect.h"

#include "Trace/TraceOpsDialect.h.inc"

#endif // TRACE_TRACEDIALECT_H
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an empty line here.

47 changes: 47 additions & 0 deletions midend/include/Dialect/Trace/TraceDialect.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===- TraceDialect.td - Trace Dialect Definition --------------*- tablegen -*-===//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong format: 80-col limitation.

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This is the top level file for the Trace dialect.
//
//===----------------------------------------------------------------------===//

#ifndef TRACE_TRACEDIALECT_TD
#define TRACE_TRACEDIALECT_TD

include "mlir/IR/OpBase.td"

//===----------------------------------------------------------------------===//
// Trace Dialect Definition.
//===----------------------------------------------------------------------===//

def Trace_Dialect : Dialect {
let name = "trace";
let summary = "The Trace Dialect.";
let description = [{
The `trace` dialect is for trace.
}];
let cppNamespace = "::buddy::trace";
// let useDefaultAttributePrinterParser = 1;
}

//===----------------------------------------------------------------------===//
// Base Trace Operation Definition.
//===----------------------------------------------------------------------===//

class Trace_Op<string mnemonic, list<Trait> traits = []> :
Op<Trace_Dialect, mnemonic, traits>;

#endif // TRACE_TRACEDIALECT_TD
40 changes: 40 additions & 0 deletions midend/include/Dialect/Trace/TraceOps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===- TraceOps.h - Trace Dialect Ops -------------------------------*- C++
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong format.

//-*-===//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This is the header file for operations in Trace dialect.
//
//===----------------------------------------------------------------------===//

#ifndef TRACE_TRACEOPS_H
#define TRACE_TRACEOPS_H

#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"

#include "Trace/TraceOpsEnums.h.inc"

#define GET_ATTRDEF_CLASSES
#include "Trace/TraceOpsAttributes.h.inc"

#define GET_OP_CLASSES
#include "Trace/TraceOps.h.inc"

#endif // TRACE_TRACEOPS_H
57 changes: 57 additions & 0 deletions midend/include/Dialect/Trace/TraceOps.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===- TraceOps.td - Trace Dialect Ops -------------------------*- tablegen -*-===//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong format: 80-col limitation.

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This is the top level file for operations in the trace dialect.
//
//===----------------------------------------------------------------------===//

#ifndef TRACE_TRACEOPS_TD
#define TRACE_TRACEOPS_TD

include "TraceDialect.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/EnumAttr.td"
include "mlir/IR/OpBase.td"
include "mlir/IR/BuiltinTypes.td"

//===----------------------------------------------------------------------===//
// Start timing operation.
//===----------------------------------------------------------------------===//
def Trace_TimeStartOp : Trace_Op<"time_start", []> {
let summary = "Start trace an operation";
let description = [{
This operation marks the beginning of a trace analysis.
}];
let results = (outs F64:$result); // Returns a f64 type representing the start time.
let assemblyFormat = "attr-dict `:` `->` type($result) "; // Simplified format, showing only result type.
}

//===----------------------------------------------------------------------===//
// End timing operation.
//===----------------------------------------------------------------------===//
def Trace_TimeEndOp : Trace_Op<"time_end", []> {
let summary = "End trace an operation";
let description = [{
This operation marks the end of a trace analysis.
}];
let results = (outs F64:$result); // Returns a f64 type representing the start time.
let assemblyFormat = "attr-dict `:` `->` type($result) "; // Simplified format, showing only result type.
}



#endif // TRACE_TRACEOPS_TD
1 change: 1 addition & 0 deletions midend/lib/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ add_subdirectory(LowerLinalgToGemmini)
add_subdirectory(SchedulingOnDevices)
add_subdirectory(LowerSche)
add_subdirectory(FuncBufferize)
add_subdirectory(LowerTrace)
3 changes: 3 additions & 0 deletions midend/lib/Conversion/LowerTrace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_mlir_library(LowerTracePass
LowerTracePass.cpp
)
Loading