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

[ARC][CAPI] Add basic C API for initializing ARC #6997

Merged
merged 1 commit into from
May 9, 2024
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
25 changes: 25 additions & 0 deletions include/circt-c/Dialect/Arc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===- Arc.h - C interface for the Arc dialect ------------------*- C -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_C_DIALECT_ARC_H
#define CIRCT_C_DIALECT_ARC_H

#include "mlir-c/IR.h"

#ifdef __cplusplus
extern "C" {
#endif

MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Arc, arc);
MLIR_CAPI_EXPORTED void registerArcPasses(void);

#ifdef __cplusplus
}
#endif

#endif // CIRCT_C_DIALECT_ARC_H
18 changes: 18 additions & 0 deletions lib/CAPI/Dialect/Arc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===- Arc.cpp - C interface for the Arc dialect ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "circt-c/Dialect/Arc.h"
#include "circt/Dialect/Arc/ArcDialect.h"
#include "circt/Dialect/Arc/ArcPasses.h"
#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Registration.h"
#include "mlir/CAPI/Support.h"

MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Arc, arc, circt::arc::ArcDialect)

void registerArcPasses() { circt::arc::registerPasses(); }
10 changes: 10 additions & 0 deletions lib/CAPI/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO: Make the check source feature optional as an argument on *_add_library.
set(LLVM_OPTIONAL_SOURCES
Arc.cpp
CHIRRTL.cpp
Comb.cpp
Debug.cpp
Expand All @@ -20,6 +21,15 @@ set(LLVM_OPTIONAL_SOURCES
Verif.cpp
)

add_circt_public_c_api_library(CIRCTCAPIArc
Arc.cpp

LINK_LIBS PUBLIC
MLIRCAPIIR
CIRCTArc
CIRCTArcTransforms
)

add_circt_public_c_api_library(CIRCTCAPIComb
Comb.cpp

Expand Down
13 changes: 13 additions & 0 deletions test/CAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ target_link_libraries(circt-capi-firtool-test
CIRCTCAPIFIRRTL
CIRCTCAPIFirtool
)

add_llvm_executable(circt-capi-arc-test
PARTIAL_SOURCES_INTENDED
arc.c
)
llvm_update_compile_flags(circt-capi-arc-test)

target_link_libraries(circt-capi-arc-test
PRIVATE

MLIRCAPIIR
CIRCTCAPIArc
)
67 changes: 67 additions & 0 deletions test/CAPI/arc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*===- arc.c - Simple test of Arc Dialect C APIs -------------------------===*\
|* *|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|* Exceptions. *|
|* See https://llvm.org/LICENSE.txt for license information. *|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|* *|
\*===----------------------------------------------------------------------===*/

/* RUN: circt-capi-ir-test 2>&1 | FileCheck %s
*/

#include "circt-c/Dialect/Arc.h"

#include <stdio.h>
#include <string.h>

int registerOnlyArc(void) {
MlirContext ctx = mlirContextCreate();
// The built-in dialect is always loaded.
if (mlirContextGetNumLoadedDialects(ctx) != 1)
return 1;

// Arc dialect tests.
MlirDialectHandle arcHandle = mlirGetDialectHandle__arc__();

MlirDialect arc = mlirContextGetOrLoadDialect(
ctx, mlirDialectHandleGetNamespace(arcHandle));
if (!mlirDialectIsNull(arc))
return 2;

mlirDialectHandleRegisterDialect(arcHandle, ctx);
if (mlirContextGetNumRegisteredDialects(ctx) != 2)
return 3;
if (mlirContextGetNumLoadedDialects(ctx) != 1)
return 4;

arc = mlirContextGetOrLoadDialect(ctx,
mlirDialectHandleGetNamespace(arcHandle));
if (mlirDialectIsNull(arc))
return 5;
if (mlirContextGetNumLoadedDialects(ctx) != 2)
return 6;

MlirDialect alsoArc = mlirDialectHandleLoadDialect(arcHandle, ctx);
if (!mlirDialectEqual(arc, alsoArc))
return 7;

registerArcPasses();

mlirContextDestroy(ctx);

return 0;
}

int main(void) {
fprintf(stderr, "@registration\n");
int errcode = registerOnlyArc();
fprintf(stderr, "%d\n", errcode);

// clang-format off
// CHECK-LABEL: @registration
// CHECK: 0
// clang-format on

return 0;
}
Loading