Skip to content

[mlir][llvmir] expose Type(To/From)LLVMIRTranslator C API #124864

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

Merged

Conversation

makslevental
Copy link
Contributor

@makslevental makslevental commented Jan 29, 2025

I'd like to enable the ability to convert between LLVM Types and MLIR Types using libMLIR-C only.

In addition I'd like to enable call_intrinsic return type inference, i.e., I'd like to retrieve intrinsic signatures without having to require/provide a return type. Thus, this PR adds TypeToLLVMIRTranslator::uncheckedGetIntrinsicSignature (and auxiliary code) which only requires an intrinsic ID and the parameter types (return type inference is handled by llvm::Intrinsic::getType).

@makslevental makslevental force-pushed the users/makslevental/llvm-translator branch from e36c294 to d2eab6e Compare January 29, 2025 01:25
@makslevental makslevental marked this pull request as ready for review January 29, 2025 22:34
@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-llvm

Author: Maksim Levental (makslevental)

Changes

I'd like to expose the ability to convert between LLVM Types and MLIR Types using libMLIR-C only.


Full diff: https://github.com/llvm/llvm-project/pull/124864.diff

3 Files Affected:

  • (modified) mlir/include/mlir-c/Target/LLVMIR.h (+15)
  • (modified) mlir/lib/CAPI/Target/LLVMIR.cpp (+19-3)
  • (modified) mlir/test/CAPI/translation.c (+17)
diff --git a/mlir/include/mlir-c/Target/LLVMIR.h b/mlir/include/mlir-c/Target/LLVMIR.h
index effa74b905ce66..90204626013952 100644
--- a/mlir/include/mlir-c/Target/LLVMIR.h
+++ b/mlir/include/mlir-c/Target/LLVMIR.h
@@ -32,6 +32,21 @@ extern "C" {
 MLIR_CAPI_EXPORTED LLVMModuleRef
 mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context);
 
+struct MlirTypeFromLLVMIRTranslator {
+  void *ptr;
+};
+
+typedef struct MlirTypeFromLLVMIRTranslator MlirTypeFromLLVMIRTranslator;
+
+MLIR_CAPI_EXPORTED MlirTypeFromLLVMIRTranslator
+mlirTypeFromLLVMIRTranslatorCreate(MlirContext ctx);
+
+MLIR_CAPI_EXPORTED MlirTypeFromLLVMIRTranslator
+mlirTypeFromLLVMIRTranslatorCreate(MlirContext ctx);
+
+MLIR_CAPI_EXPORTED MlirType mlirTypeFromLLVMIRTranslatorTranslateType(
+    MlirTypeFromLLVMIRTranslator translator, LLVMTypeRef llvmType);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mlir/lib/CAPI/Target/LLVMIR.cpp b/mlir/lib/CAPI/Target/LLVMIR.cpp
index dc798372be7467..7d81b490ed0c7b 100644
--- a/mlir/lib/CAPI/Target/LLVMIR.cpp
+++ b/mlir/lib/CAPI/Target/LLVMIR.cpp
@@ -8,16 +8,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir-c/Target/LLVMIR.h"
-#include "llvm-c/Support.h"
 
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
-#include <memory>
+#include "llvm/IR/Type.h"
 
 #include "mlir/CAPI/IR.h"
-#include "mlir/CAPI/Support.h"
 #include "mlir/CAPI/Wrap.h"
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
+#include "mlir/Target/LLVMIR/TypeFromLLVM.h"
 
 using namespace mlir;
 
@@ -34,3 +33,20 @@ LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module,
 
   return moduleRef;
 }
+
+DEFINE_C_API_PTR_METHODS(MlirTypeFromLLVMIRTranslator,
+                         mlir::LLVM::TypeFromLLVMIRTranslator);
+
+MlirTypeFromLLVMIRTranslator
+mlirTypeFromLLVMIRTranslatorCreate(MlirContext ctx) {
+  MLIRContext *context = unwrap(ctx);
+  auto *translator = new LLVM::TypeFromLLVMIRTranslator(*context);
+  return wrap(translator);
+}
+
+MlirType mlirTypeFromLLVMIRTranslatorTranslateType(
+    MlirTypeFromLLVMIRTranslator translator, LLVMTypeRef llvmType) {
+  LLVM::TypeFromLLVMIRTranslator *translator_ = unwrap(translator);
+  mlir::Type type = translator_->translateType(llvm::unwrap(llvmType));
+  return wrap(type);
+}
\ No newline at end of file
diff --git a/mlir/test/CAPI/translation.c b/mlir/test/CAPI/translation.c
index c9233d95fd5120..ddbf217c1d9862 100644
--- a/mlir/test/CAPI/translation.c
+++ b/mlir/test/CAPI/translation.c
@@ -58,11 +58,28 @@ static void testToLLVMIR(MlirContext ctx) {
   LLVMContextDispose(llvmCtx);
 }
 
+// CHECK-LABEL: testTypeFromLLVMIRTranslator
+static void testTypeFromLLVMIRTranslator(MlirContext ctx) {
+  fprintf(stderr, "testTypeFromLLVMIRTranslator\n");
+  LLVMContextRef llvmCtx = LLVMContextCreate();
+
+  LLVMTypeRef llvmTy = LLVMInt32TypeInContext(llvmCtx);
+  MlirTypeFromLLVMIRTranslator translator =
+      mlirTypeFromLLVMIRTranslatorCreate(ctx);
+  MlirType mlirTy =
+      mlirTypeFromLLVMIRTranslatorTranslateType(translator, llvmTy);
+  // CHECK: i32
+  mlirTypeDump(mlirTy);
+
+  LLVMContextDispose(llvmCtx);
+}
+
 int main(void) {
   MlirContext ctx = mlirContextCreate();
   mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx);
   mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm"));
   testToLLVMIR(ctx);
+  testTypeFromLLVMIRTranslator(ctx);
   mlirContextDestroy(ctx);
   return 0;
 }

@makslevental makslevental force-pushed the users/makslevental/llvm-translator branch from da50caa to 4a32519 Compare January 30, 2025 01:29
@makslevental makslevental changed the title [mlir][llvmir] expose TypeFromLLVMIRTranslator [mlir][llvmir] expose TypeFromLLVMIRTranslator C API Jan 30, 2025
@makslevental makslevental force-pushed the users/makslevental/llvm-translator branch from 2a26397 to 24f81e4 Compare January 30, 2025 02:53
@makslevental makslevental requested a review from zero9178 January 30, 2025 02:54
@makslevental makslevental changed the title [mlir][llvmir] expose TypeFromLLVMIRTranslator C API [mlir][llvmir] expose Type(To/From)LLVMIRTranslator C API Jan 30, 2025
@makslevental
Copy link
Contributor Author

As discussed offline, I've removed uncheckedGetIntrinsicSignature and will replace it (elsewhere) with direct uses of LLVM C APIs.

@makslevental makslevental force-pushed the users/makslevental/llvm-translator branch from 5c9faea to e275e15 Compare January 30, 2025 14:25
Copy link

github-actions bot commented Jan 30, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@makslevental makslevental force-pushed the users/makslevental/llvm-translator branch from e275e15 to cdf3d98 Compare January 30, 2025 14:35
Copy link
Member

@zero9178 zero9178 left a comment

Choose a reason for hiding this comment

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

LGTM!

@makslevental makslevental force-pushed the users/makslevental/llvm-translator branch from dc066c3 to 7d22af1 Compare January 30, 2025 17:10
@makslevental makslevental merged commit 7ae964c into llvm:main Jan 30, 2025
6 of 7 checks passed
@makslevental makslevental deleted the users/makslevental/llvm-translator branch January 30, 2025 17:43
@kazutakahirata
Copy link
Contributor

I've merged 0e43b95 to fix warnings from this PR.

@makslevental
Copy link
Contributor Author

I've merged 0e43b95 to fix warnings from this PR.

Thank you for that (and sorry for both missing them and stepping) but does this resolve the other buildbot failures? I got this in an email

/usr/bin/ld: tools/mlir/lib/CAPI/Target/CMakeFiles/obj.MLIRCAPITarget.dir/LLVMIR.cpp.o: in function `mlirTypeFromLLVMIRTranslatorCreate':
LLVMIR.cpp:(.text.mlirTypeFromLLVMIRTranslatorCreate+0x20): undefined reference to `mlir::LLVM::TypeFromLLVMIRTranslator::TypeFromLLVMIRTranslator(mlir::MLIRContext&)'
/usr/bin/ld: tools/mlir/lib/CAPI/Target/CMakeFiles/obj.MLIRCAPITarget.dir/LLVMIR.cpp.o: in function `mlirTypeFromLLVMIRTranslatorDestroy':
LLVMIR.cpp:(.text.mlirTypeFromLLVMIRTranslatorDestroy+0x14): undefined reference to `mlir::LLVM::TypeFromLLVMIRTranslator::~TypeFromLLVMIRTranslator()'
/usr/bin/ld: tools/mlir/lib/CAPI/Target/CMakeFiles/obj.MLIRCAPITarget.dir/LLVMIR.cpp.o: in function `mlirTypeFromLLVMIRTranslatorTranslateType':
LLVMIR.cpp:(.text.mlirTypeFromLLVMIRTranslatorTranslateType+0x0): undefined reference to `mlir::LLVM::TypeFromLLVMIRTranslator::translateType(llvm::Type*)'

@makslevental makslevental restored the users/makslevental/llvm-translator branch January 30, 2025 19:06
makslevental added a commit that referenced this pull request Jan 30, 2025
…24864)"

This reverts commit 7ae964c.

Revert "[mlir] Fix warnings"

This reverts commit 0e43b95.
@makslevental
Copy link
Contributor Author

makslevental commented Jan 30, 2025

I reverted both 345512c

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

Successfully merging this pull request may close these issues.

4 participants