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

Create a minimal C API #196

Merged
merged 12 commits into from
Nov 4, 2020
Merged

Create a minimal C API #196

merged 12 commits into from
Nov 4, 2020

Conversation

GeorgeLyon
Copy link
Contributor

No description provided.

@GeorgeLyon GeorgeLyon marked this pull request as ready for review November 2, 2020 21:50
Copy link
Contributor

@darthscsi darthscsi left a comment

Choose a reason for hiding this comment

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

Please add a test case which uses the library. See llvm/test/CAPI

PATTERN "*.def"
PATTERN "*.h"
PATTERN "*.inc"
PATTERN "*.td"
Copy link
Contributor

Choose a reason for hiding this comment

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

There are no inc, td, or def files in the c-api.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I copied this verbatim from LLVM. I don't see a problem leaving these in unless we explicitly want to take the stance that we are trying to build this API without these file types.

Copy link
Collaborator

@lattner lattner left a comment

Choose a reason for hiding this comment

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

Thank you for tackling this, LGTM with the @darthscsi 's comment addressed. Thanks!

@@ -0,0 +1,30 @@
/*===-- circt-c/RTLDialect.h - C API for RTL dialect --------------*- C -*-===*\
|* *|
*|
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: please fill in the file header

extern "C" {
#endif

/** Registers the RTL dialect with the given context. This allows the
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I see the MLIR C bindings are doing the same thing, but this still seems a bit weird to me: The use of /**/ comments is very inclusive to old C compilers, but I think that BCPL style // comments were part of ansi c 89/90, so I think they are ok to use. Doing so gets us better alignment with the c++ side of things. Could you ask the MLIR forum what the rationale here is, and whether using // would be ok with them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://llvm.discourse.group/t/a-quick-question-about-comment-styles/2144

Seems like there is a policy, and I'm not planning on rocking the boat too much yet :)

@@ -0,0 +1,21 @@
//===- RTLDialect.cpp - C Interface for RTL Dialect -----------------------===//
//
Copy link
Collaborator

Choose a reason for hiding this comment

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

same nit as the header.

@fabianschuiki
Copy link
Contributor

This is great! I think I will do a similar thing for LLHD, in order to be able to switch Moore from the Rust LLHD library to CIRCT as a foreign library at some point.

@GeorgeLyon
Copy link
Contributor Author

I'm a little confused at how the tests work.
In MLIR, CMakeLists explicitly includes the test subdirectory: https://github.com/llvm/llvm-project/blob/00eff96e1d02969ff1c1416a266b4d29506cd1c0/mlir/test/CMakeLists.txt#L1
In Circt, on the other hand, doesn't explicitly add subdirectories and seems like it is discovering tests automatically... https://github.com/llvm/circt/blob/master/test/CMakeLists.txt

Can someone please clarify what the best path forward is?

#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Support.h"

void mlirContextRegisterRTLDialect(MlirContext context) {
Copy link
Member

Choose a reason for hiding this comment

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

Am I missing something or should these symbols be extern "C"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mirrored the structure of StandardDialect.cpp: https://github.com/llvm/llvm-project/blob/64c0c9f01511dc300b29e7a20a13958c5932e314/mlir/lib/CAPI/Standard/StandardDialect.cpp
They are declared as extern "C" in the header:

@lattner
Copy link
Collaborator

lattner commented Nov 4, 2020

Cool, I think this looks good with a few nits about the file header. I agree that you should keep the comment style consistent with the MLIR style. If/when they change, we can change to match.

@darthscsi
Copy link
Contributor

I'm a little confused at how the tests work.
In MLIR, CMakeLists explicitly includes the test subdirectory: https://github.com/llvm/llvm-project/blob/00eff96e1d02969ff1c1416a266b4d29506cd1c0/mlir/test/CMakeLists.txt#L1
In Circt, on the other hand, doesn't explicitly add subdirectories and seems like it is discovering tests automatically... https://github.com/llvm/circt/blob/master/test/CMakeLists.txt

Can someone please clarify what the best path forward is?

add_subdirectory is a directive to cmake. The reason it is doing it is to pickup the cmake-based build of the executable in the test directory. add_subdirectory isn't directing the testing framework to do anything, it's just building the c-based test executable and registering it so the test target can depend on it.

@GeorgeLyon
Copy link
Contributor Author

I'm a little confused at how the tests work.
In MLIR, CMakeLists explicitly includes the test subdirectory: https://github.com/llvm/llvm-project/blob/00eff96e1d02969ff1c1416a266b4d29506cd1c0/mlir/test/CMakeLists.txt#L1
In Circt, on the other hand, doesn't explicitly add subdirectories and seems like it is discovering tests automatically... https://github.com/llvm/circt/blob/master/test/CMakeLists.txt

Can someone please clarify what the best path forward is?

It looks like ".mlir" style tests are handled differently from C tests which need to be compiled as part of the cmake project in order to be run. As such, C tests need to use "add_subdirectory" to provide their own CMakeLists.txt, which "mlir" and "fir" tests do not. The reason we don't have this yet in circt is because there are no C tests yet.

@GeorgeLyon
Copy link
Contributor Author

So in my latest attempt, I'm getting the following error: https://github.com/llvm/circt/runs/1354327993#step:8:363

I've tried to copy what is happening in the MLIR C test, but for some reason it isn't finding the executable (which is present after the build and produces the correct output).

╭ ~/Developer/Projects/circt on master                                                              
╰─○ ./build/bin/circt-capi-ir-test    
@registration
0

Does anyone have any thoughts as to what I may be missing? @darthscsi

@GeorgeLyon
Copy link
Contributor Author

This was the spot I missed 181011e

@GeorgeLyon GeorgeLyon merged commit 2a9eb35 into llvm:master Nov 4, 2020
@lattner
Copy link
Collaborator

lattner commented Nov 4, 2020

Cool, thank you George. It looks like MLIR just switched their comment style, could you adapt this code to follow the new approach in a follow-on? Thanks,

-Chris

@GeorgeLyon
Copy link
Contributor Author

Race conditions always get you :(

@lattner
Copy link
Collaborator

lattner commented Nov 5, 2020

If only there were some way to define them away!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants