-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[mlir] Add mlirTranslateModuleToLLVMIR to MLIR-C #73117
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target -------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This header declares the C interface to target LLVMIR with MLIR. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_C_TARGET_LLVMIR_H | ||
#define MLIR_C_TARGET_LLVMIR_H | ||
|
||
#include "mlir-c/IR.h" | ||
#include "mlir-c/Support.h" | ||
#include "llvm-c/Support.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/// Translate operation that satisfies LLVM dialect module requirements into an | ||
/// LLVM IR module living in the given context. This translates operations from | ||
/// any dilalect that has a registered implementation of | ||
/// LLVMTranslationDialectInterface. | ||
/// | ||
/// \returns the generated LLVM IR Module from the translated MLIR module, it is | ||
/// owned by the caller. | ||
MLIR_CAPI_EXPORTED LLVMModuleRef | ||
mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // MLIR_C_TARGET_LLVMIR_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
add_mlir_upstream_c_api_library(MLIRCAPITarget | ||
LLVMIR.cpp | ||
|
||
LINK_COMPONENTS | ||
Core | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRToLLVMIRTranslationRegistration | ||
MLIRCAPIIR | ||
MLIRLLVMToLLVMIRTranslation | ||
MLIRSupport | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target ---------------------===// | ||
// | ||
// 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 "mlir-c/Target/LLVMIR.h" | ||
#include "llvm-c/Support.h" | ||
|
||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/Module.h" | ||
#include <memory> | ||
|
||
#include "mlir/CAPI/IR.h" | ||
#include "mlir/CAPI/Support.h" | ||
#include "mlir/CAPI/Wrap.h" | ||
#include "mlir/Target/LLVMIR/ModuleTranslation.h" | ||
|
||
using namespace mlir; | ||
|
||
LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, | ||
LLVMContextRef context) { | ||
Operation *moduleOp = unwrap(module); | ||
|
||
llvm::LLVMContext *ctx = llvm::unwrap(context); | ||
|
||
std::unique_ptr<llvm::Module> llvmModule = | ||
mlir::translateModuleToLLVMIR(moduleOp, *ctx); | ||
|
||
LLVMModuleRef moduleRef = llvm::wrap(llvmModule.release()); | ||
|
||
return moduleRef; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===- translation.c - Test MLIR Target translations ----------------------===// | ||
// | ||
// 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: mlir-capi-translation-test 2>&1 | FileCheck %s | ||
|
||
#include "llvm-c/Core.h" | ||
#include "llvm-c/Support.h" | ||
#include "llvm-c/Types.h" | ||
|
||
#include "mlir-c/BuiltinTypes.h" | ||
#include "mlir-c/Dialect/LLVM.h" | ||
#include "mlir-c/IR.h" | ||
#include "mlir-c/RegisterEverything.h" | ||
#include "mlir-c/Support.h" | ||
#include "mlir-c/Target/LLVMIR.h" | ||
|
||
#include <assert.h> | ||
#include <math.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// CHECK-LABEL: testToLLVMIR() | ||
static void testToLLVMIR(MlirContext ctx) { | ||
fprintf(stderr, "testToLLVMIR()\n"); | ||
LLVMContextRef llvmCtx = LLVMContextCreate(); | ||
|
||
const char *moduleString = "llvm.func @add(%arg0: i64, %arg1: i64) -> i64 { \ | ||
%0 = llvm.add %arg0, %arg1 : i64 \ | ||
llvm.return %0 : i64 \ | ||
}"; | ||
|
||
mlirRegisterAllLLVMTranslations(ctx); | ||
|
||
MlirModule module = | ||
mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString)); | ||
|
||
MlirOperation operation = mlirModuleGetOperation(module); | ||
|
||
LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(operation, llvmCtx); | ||
|
||
// clang-format off | ||
// CHECK: declare ptr @malloc(i64 %{{.*}}) | ||
// CHECK: declare void @free(ptr %{{.*}}) | ||
// CHECK: define i64 @add(i64 %[[arg1:.*]], i64 %[[arg2:.*]]) { | ||
// CHECK-NEXT: %[[arg3:.*]] = add i64 %[[arg1]], %[[arg2]] | ||
// CHECK-NEXT: ret i64 %[[arg3]] | ||
// CHECK-NEXT: } | ||
// clang-format on | ||
LLVMDumpModule(llvmModule); | ||
|
||
LLVMDisposeModule(llvmModule); | ||
mlirModuleDestroy(module); | ||
} | ||
|
||
int main(void) { | ||
MlirContext ctx = mlirContextCreate(); | ||
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx); | ||
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm")); | ||
testToLLVMIR(ctx); | ||
mlirContextDestroy(ctx); | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.