Skip to content

Commit 3c464d2

Browse files
authoredDec 11, 2024··
[mlir][emitc] Add support for C-API/python binding to EmitC dialect (#119476)
Added EmitC dialect bindings.
1 parent b0b546d commit 3c464d2

File tree

7 files changed

+106
-0
lines changed

7 files changed

+106
-0
lines changed
 

‎mlir/include/mlir-c/Dialect/EmitC.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- mlir-c/Dialect/EmitC.h - C API for EmitC dialect ----------*- C -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef MLIR_C_DIALECT_EmitC_H
11+
#define MLIR_C_DIALECT_EmitC_H
12+
13+
#include "mlir-c/IR.h"
14+
#include "mlir-c/Support.h"
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(EmitC, emitc);
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif
25+
26+
#endif // MLIR_C_DIALECT_EmitC_H

‎mlir/lib/CAPI/Dialect/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIControlFlow
4040
MLIRControlFlowDialect
4141
)
4242

43+
add_mlir_upstream_c_api_library(MLIRCAPIEmitC
44+
EmitC.cpp
45+
46+
PARTIAL_SOURCES_INTENDED
47+
LINK_LIBS PUBLIC
48+
MLIRCAPIIR
49+
MLIREmitCDialect
50+
)
51+
4352
add_mlir_upstream_c_api_library(MLIRCAPIMath
4453
Math.cpp
4554

‎mlir/lib/CAPI/Dialect/EmitC.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===- EmitC.cpp - C Interface for EmitC dialect --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir-c/Dialect/EmitC.h"
10+
#include "mlir/CAPI/Registration.h"
11+
#include "mlir/Dialect/EmitC/IR/EmitC.h"
12+
13+
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(EmitC, emitc, mlir::emitc::EmitCDialect)

‎mlir/python/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ declare_mlir_python_sources(
352352
dialects/quant.py
353353
_mlir_libs/_mlir/dialects/quant.pyi)
354354

355+
declare_mlir_dialect_python_bindings(
356+
ADD_TO_PARENT MLIRPythonSources.Dialects
357+
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
358+
TD_FILE dialects/EmitC.td
359+
SOURCES
360+
dialects/emitc.py
361+
DIALECT_NAME emitc)
362+
355363
declare_mlir_dialect_python_bindings(
356364
ADD_TO_PARENT MLIRPythonSources.Dialects
357365
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"

‎mlir/python/mlir/dialects/EmitC.td

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- EmitC.td - Entry point for EmitC bind --------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef PYTHON_BINDINGS_EMITC
10+
#define PYTHON_BINDINGS_EMITC
11+
12+
include "mlir/Dialect/EmitC/IR/EmitC.td"
13+
14+
#endif

‎mlir/python/mlir/dialects/emitc.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
from ._emitc_ops_gen import *
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# RUN: %PYTHON %s | FileCheck %s
2+
3+
from mlir.ir import *
4+
import mlir.dialects.emitc as emitc
5+
6+
7+
def run(f):
8+
print("\nTEST:", f.__name__)
9+
with Context() as ctx, Location.unknown():
10+
module = Module.create()
11+
with InsertionPoint(module.body):
12+
f(ctx)
13+
print(module)
14+
15+
16+
# CHECK-LABEL: TEST: testConstantOp
17+
@run
18+
def testConstantOp(ctx):
19+
i32 = IntegerType.get_signless(32)
20+
a = emitc.ConstantOp(result=i32, value=IntegerAttr.get(i32, 42))
21+
# CHECK: %{{.*}} = "emitc.constant"() <{value = 42 : i32}> : () -> i32
22+
23+
24+
# CHECK-LABEL: TEST: testAddOp
25+
@run
26+
def testAddOp(ctx):
27+
i32 = IntegerType.get_signless(32)
28+
lhs = emitc.ConstantOp(result=i32, value=IntegerAttr.get(i32, 0))
29+
rhs = emitc.ConstantOp(result=i32, value=IntegerAttr.get(i32, 0))
30+
a = emitc.AddOp(i32, lhs, rhs)
31+
# CHECK: %{{.*}} = emitc.add %{{.*}}, %{{.*}} : (i32, i32) -> i32

0 commit comments

Comments
 (0)
Please sign in to comment.