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

Changes for Project MLIR-TensorRT: #487

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions mlir-tensorrt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,30 @@ if(PROJECT_IS_TOP_LEVEL)
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
elseif(MLIR_TRT_LLVM_COMMIT)
set(patch_dir "${CMAKE_CURRENT_LIST_DIR}/build_tools/patches/mlir")
mtrt_llvm_project(
NAME llvm_project
VERSION 0.0.20241126
URL "https://github.com/llvm/llvm-project/archive/${MLIR_TRT_LLVM_COMMIT}.zip"
EXCLUDE_FROM_ALL TRUE
SOURCE_SUBDIR "llvm"
PATCHES
"${CMAKE_CURRENT_LIST_DIR}/build_tools/patches/mlir/000_fix_bufferization_tensor_encoding_memory_spaces.patch"
"${CMAKE_CURRENT_LIST_DIR}/build_tools/patches/mlir/001-mlir-Add-a-null-pointer-check-in-symbol-lookup-11516.patch"
"${patch_dir}/000_fix_bufferization_tensor_encoding_memory_spaces.patch"
"${patch_dir}/001-mlir-Add-a-null-pointer-check-in-symbol-lookup-11516.patch"
"${patch_dir}/0003-mlir-EmitC-memref-to-emitc-insert-conversion_casts-1.patch"
"${patch_dir}/0004-NFC-mlir-emitc-fix-misspelling-in-description-of-emi.patch"
"${patch_dir}/0005-emitc-func-Set-default-dialect-to-emitc-116297.patch"
"${patch_dir}/0006-MLIR-EmitC-arith-to-emitc-Fix-lowering-of-fptoui-118.patch"
"${patch_dir}/0007-mlir-emitc-Add-support-for-C-API-python-binding-to-E.patch"
"${patch_dir}/0008-mlir-emitc-DCE-unimplemented-decls-121253.patch"
"${patch_dir}/0009-Re-introduce-Type-Conversion-on-EmitC-121476.patch"
"${patch_dir}/0010-mlir-emitc-Fix-invalid-syntax-in-example-of-emitc.re.patch"
"${patch_dir}/0011-mlir-emitc-Don-t-emit-extra-semicolon-after-bracket-.patch"
"${patch_dir}/0012-mlir-emitc-Expose-emitc-dialect-types-119645.patch"
"${patch_dir}/0013-mlir-emitc-Support-convert-arith.extf-and-arith.trun.patch"
"${patch_dir}/0014-EmitC-Allow-arrays-of-size-zero-123292.patch"
"${patch_dir}/0015-mlir-EmitC-Add-MathToEmitC-pass-for-math-function-lo.patch"
"${patch_dir}/0016-mlir-emitc-Set-default-dialect-to-emitc-in-ops-with-.patch"
OPTIONS
"LLVM_ENABLE_PROJECTS mlir"
"MLIR_ENABLE_BINDINGS_PYTHON ${MLIR_TRT_ENABLE_PYTHON}"
Expand Down
3 changes: 2 additions & 1 deletion mlir-tensorrt/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"LLVM_ENABLE_ASSERTIONS": "ON",
"CPM_SOURCE_CACHE": "${sourceDir}/.cache.cpm",
"CPM_USE_NAMED_CACHE_DIRECTORIES": "ON"
"CPM_USE_NAMED_CACHE_DIRECTORIES": "ON",
"LLVM_INSTALL_TOOLCHAIN_ONLY": "ON"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
From 32a1faae7c6bd8b3df1aefcce707c9aef1557eea Mon Sep 17 00:00:00 2001
From: Simon Camphausen <[email protected]>
Date: Wed, 30 Oct 2024 15:27:23 +0100
Subject: [PATCH 03/16] [mlir][EmitC] memref-to-emitc: insert conversion_casts
(#114204)

Add materializations to the conversion pass, such that types of
non-converted operands are legalized.
---
.../MemRefToEmitC/MemRefToEmitCPass.cpp | 13 +++++++
.../MemRefToEmitC/memref-to-emitc.mlir | 35 +++++++++++--------
2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitCPass.cpp b/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitCPass.cpp
index 11bfde890bce..7f433254e95a 100644
--- a/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitCPass.cpp
+++ b/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitCPass.cpp
@@ -40,6 +40,19 @@ struct ConvertMemRefToEmitCPass

populateMemRefToEmitCTypeConversion(converter);

+ auto materializeAsUnrealizedCast = [](OpBuilder &builder, Type resultType,
+ ValueRange inputs,
+ Location loc) -> Value {
+ if (inputs.size() != 1)
+ return Value();
+
+ return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
+ .getResult(0);
+ };
+
+ converter.addSourceMaterialization(materializeAsUnrealizedCast);
+ converter.addTargetMaterialization(materializeAsUnrealizedCast);
+
RewritePatternSet patterns(&getContext());
populateMemRefToEmitCConversionPatterns(patterns, converter);

diff --git a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
index f4722da08cc4..f5ef821cc9c0 100644
--- a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
+++ b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
@@ -1,28 +1,35 @@
// RUN: mlir-opt -convert-memref-to-emitc %s -split-input-file | FileCheck %s

-// CHECK-LABEL: memref_store
-// CHECK-SAME: %[[v:.*]]: f32, %[[i:.*]]: index, %[[j:.*]]: index
-func.func @memref_store(%v : f32, %i: index, %j: index) {
- // CHECK-NEXT: %[[ALLOCA:.*]] = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.array<4x8xf32>
- %0 = memref.alloca() : memref<4x8xf32>
+// CHECK-LABEL: alloca()
+func.func @alloca() {
+ // CHECK-NEXT: %[[ALLOCA:.*]] = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.array<2xf32>
+ %0 = memref.alloca() : memref<2xf32>
+ return
+}

- // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[ALLOCA]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
+// -----
+
+// CHECK-LABEL: memref_store
+// CHECK-SAME: %[[buff:.*]]: memref<4x8xf32>, %[[v:.*]]: f32, %[[i:.*]]: index, %[[j:.*]]: index
+func.func @memref_store(%buff : memref<4x8xf32>, %v : f32, %i: index, %j: index) {
+ // CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.array<4x8xf32>
+
+ // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[BUFFER]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
// CHECK-NEXT: emitc.assign %[[v]] : f32 to %[[SUBSCRIPT]] : <f32>
- memref.store %v, %0[%i, %j] : memref<4x8xf32>
+ memref.store %v, %buff[%i, %j] : memref<4x8xf32>
return
}

// -----

// CHECK-LABEL: memref_load
-// CHECK-SAME: %[[i:.*]]: index, %[[j:.*]]: index
-func.func @memref_load(%i: index, %j: index) -> f32 {
- // CHECK-NEXT: %[[ALLOCA:.*]] = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.array<4x8xf32>
- %0 = memref.alloca() : memref<4x8xf32>
-
- // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[ALLOCA]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
+// CHECK-SAME: %[[buff:.*]]: memref<4x8xf32>, %[[i:.*]]: index, %[[j:.*]]: index
+func.func @memref_load(%buff : memref<4x8xf32>, %i: index, %j: index) -> f32 {
+ // CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.array<4x8xf32>
+
+ // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[BUFFER]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
// CHECK-NEXT: %[[LOAD:.*]] = emitc.load %[[SUBSCRIPT]] : <f32>
- %1 = memref.load %0[%i, %j] : memref<4x8xf32>
+ %1 = memref.load %buff[%i, %j] : memref<4x8xf32>
// CHECK-NEXT: return %[[LOAD]] : f32
return %1 : f32
}
--
2.46.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
From 8ffa6d49dabf4eaef97cfaa1f8438c1cc4396b0b Mon Sep 17 00:00:00 2001
From: Andrey Timonin <[email protected]>
Date: Wed, 13 Nov 2024 14:17:00 +0300
Subject: [PATCH 04/16] [NFC][mlir][emitc] fix misspelling in description of
emitc.global (#115548)

Missing `!` before `emitc.global` was added in the `EmitC.td`.
---
mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 7c84ab4dd39e..071541fa9895 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -1110,9 +1110,11 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {

```mlir
// Global variable with an initial value.
- emitc.global @x : emitc.array<2xf32> = dense<0.0, 2.0>
+ emitc.global @x : !emitc.array<2xf32> = dense<0.0>
+ // Global variable with an initial values.
+ emitc.global @x : !emitc.array<3xi32> = dense<[0, 1, 2]>
// External global variable
- emitc.global extern @x : emitc.array<2xf32>
+ emitc.global extern @x : !emitc.array<2xf32>
// Constant global variable with internal linkage
emitc.global static const @x : i32 = 0
```
--
2.46.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
From 45846991cdda3c87d12fb8ad9578b7d73f086ca2 Mon Sep 17 00:00:00 2001
From: Matthias Gehre <[email protected]>
Date: Mon, 18 Nov 2024 17:26:21 +0100
Subject: [PATCH 05/16] emitc: func: Set default dialect to 'emitc' (#116297)

Makes `emitc.func` implement the `OpAsmOpInterface` and overwrite the
`getDefaultDialect`. This allows ops inside `emitc.func`'s body to omit
the 'emitc.' prefix in the assembly.
---
mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | 12 +++++++++++-
.../Conversion/FuncToEmitC/func-to-emitc.mlir | 18 +++++++++---------
2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 071541fa9895..fc5a33541533 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -21,6 +21,7 @@ include "mlir/Interfaces/CastInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/FunctionInterfaces.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
+include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/RegionKindInterface.td"

//===----------------------------------------------------------------------===//
@@ -632,7 +633,7 @@ def EmitC_DeclareFuncOp : EmitC_Op<"declare_func", [

def EmitC_FuncOp : EmitC_Op<"func", [
AutomaticAllocationScope,
- FunctionOpInterface, IsolatedFromAbove
+ FunctionOpInterface, IsolatedFromAbove, OpAsmOpInterface
]> {
let summary = "An operation with a name containing a single `SSACFG` region";
let description = [{
@@ -700,6 +701,15 @@ def EmitC_FuncOp : EmitC_Op<"func", [

/// Returns the result types of this function.
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }
+
+ //===------------------------------------------------------------------===//
+ // OpAsmOpInterface Methods
+ //===------------------------------------------------------------------===//
+
+ /// EmitC ops in the body can omit their 'emitc.' prefix in the assembly.
+ static ::llvm::StringRef getDefaultDialect() {
+ return "emitc";
+ }
}];
let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;
diff --git a/mlir/test/Conversion/FuncToEmitC/func-to-emitc.mlir b/mlir/test/Conversion/FuncToEmitC/func-to-emitc.mlir
index 5730f7a4814f..bd48886ed739 100644
--- a/mlir/test/Conversion/FuncToEmitC/func-to-emitc.mlir
+++ b/mlir/test/Conversion/FuncToEmitC/func-to-emitc.mlir
@@ -1,7 +1,7 @@
// RUN: mlir-opt -split-input-file -convert-func-to-emitc %s | FileCheck %s

// CHECK-LABEL: emitc.func @foo()
-// CHECK-NEXT: emitc.return
+// CHECK-NEXT: return
func.func @foo() {
return
}
@@ -9,7 +9,7 @@ func.func @foo() {
// -----

// CHECK-LABEL: emitc.func private @foo() attributes {specifiers = ["static"]}
-// CHECK-NEXT: emitc.return
+// CHECK-NEXT: return
func.func private @foo() {
return
}
@@ -25,7 +25,7 @@ func.func @foo(%arg0: i32) {
// -----

// CHECK-LABEL: emitc.func @foo(%arg0: i32) -> i32
-// CHECK-NEXT: emitc.return %arg0 : i32
+// CHECK-NEXT: return %arg0 : i32
func.func @foo(%arg0: i32) -> i32 {
return %arg0 : i32
}
@@ -41,14 +41,14 @@ func.func @foo(%arg0: i32, %arg1: i32) -> i32 {
// -----

// CHECK-LABEL: emitc.func private @return_i32(%arg0: i32) -> i32 attributes {specifiers = ["static"]}
-// CHECK-NEXT: emitc.return %arg0 : i32
+// CHECK-NEXT: return %arg0 : i32
func.func private @return_i32(%arg0: i32) -> i32 {
return %arg0 : i32
}

// CHECK-LABEL: emitc.func @call(%arg0: i32) -> i32
-// CHECK-NEXT: %0 = emitc.call @return_i32(%arg0) : (i32) -> i32
-// CHECK-NEXT: emitc.return %0 : i32
+// CHECK-NEXT: %0 = call @return_i32(%arg0) : (i32) -> i32
+// CHECK-NEXT: return %0 : i32
func.func @call(%arg0: i32) -> i32 {
%0 = call @return_i32(%arg0) : (i32) -> (i32)
return %0 : i32
@@ -62,14 +62,14 @@ func.func private @return_i32(%arg0: i32) -> i32
// -----

// CHECK-LABEL: emitc.func private @return_void() attributes {specifiers = ["static"]}
-// CHECK-NEXT: emitc.return
+// CHECK-NEXT: return
func.func private @return_void() {
return
}

// CHECK-LABEL: emitc.func @call()
-// CHECK-NEXT: emitc.call @return_void() : () -> ()
-// CHECK-NEXT: emitc.return
+// CHECK-NEXT: call @return_void() : () -> ()
+// CHECK-NEXT: return
func.func @call() {
call @return_void() : () -> ()
return
--
2.46.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
From ae59bead48221d52aec82b8059885f02451949df Mon Sep 17 00:00:00 2001
From: Matthias Gehre <[email protected]>
Date: Thu, 5 Dec 2024 14:50:35 +0100
Subject: [PATCH 06/16] [MLIR][EmitC] arith-to-emitc: Fix lowering of fptoui
(#118504)

`arith.fptoui %arg0 : f32 to i16` was lowered to
```
%0 = emitc.cast %arg0 : f32 to ui32
emitc.cast %0 : ui32 to i16
```
and is now lowered to
```
%0 = emitc.cast %arg0 : f32 to ui16
emitc.cast %0 : ui16 to i16
```
---
mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp | 2 +-
mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
index 50384d9a08e5..ccbc1669b7a9 100644
--- a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
+++ b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
@@ -674,7 +674,7 @@ public:
Type actualResultType = dstType;
if (isa<arith::FPToUIOp>(castOp)) {
actualResultType =
- rewriter.getIntegerType(operandType.getIntOrFloatBitWidth(),
+ rewriter.getIntegerType(dstType.getIntOrFloatBitWidth(),
/*isSigned=*/false);
}

diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
index afd1198ede0f..1728c3a2557e 100644
--- a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
@@ -587,6 +587,10 @@ func.func @arith_float_to_int_cast_ops(%arg0: f32, %arg1: f64) {
// CHECK: emitc.cast %[[CAST0]] : ui32 to i32
%4 = arith.fptoui %arg0 : f32 to i32

+ // CHECK: %[[CAST0:.*]] = emitc.cast %arg0 : f32 to ui16
+ // CHECK: emitc.cast %[[CAST0]] : ui16 to i16
+ %5 = arith.fptoui %arg0 : f32 to i16
+
return
}

--
2.46.0

Loading