Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit d39c951

Browse files
authored
[mlir] Re-Add mlirTranslateModuleToLLVMIR to MLIR-C (#73627)
The test was checking something unrelated to what it controlled so it failed after that part changed, i removed that. See llvm/llvm-project#73117
1 parent e0b30ce commit d39c951

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target -------------*- 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+
// This header declares the C interface to target LLVMIR with MLIR.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef MLIR_C_TARGET_LLVMIR_H
15+
#define MLIR_C_TARGET_LLVMIR_H
16+
17+
#include "mlir-c/IR.h"
18+
#include "mlir-c/Support.h"
19+
#include "llvm-c/Support.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
/// Translate operation that satisfies LLVM dialect module requirements into an
26+
/// LLVM IR module living in the given context. This translates operations from
27+
/// any dilalect that has a registered implementation of
28+
/// LLVMTranslationDialectInterface.
29+
///
30+
/// \returns the generated LLVM IR Module from the translated MLIR module, it is
31+
/// owned by the caller.
32+
MLIR_CAPI_EXPORTED LLVMModuleRef
33+
mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context);
34+
35+
#ifdef __cplusplus
36+
}
37+
#endif
38+
39+
#endif // MLIR_C_TARGET_LLVMIR_H

mlir/lib/CAPI/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_subdirectory(Interfaces)
1414
add_subdirectory(IR)
1515
add_subdirectory(RegisterEverything)
1616
add_subdirectory(Transforms)
17+
add_subdirectory(Target)
1718

1819
if(MLIR_ENABLE_EXECUTION_ENGINE)
1920
add_subdirectory(ExecutionEngine)
@@ -36,4 +37,3 @@ if(MLIR_BUILD_MLIR_C_DYLIB)
3637
endif()
3738
endif()
3839
endif()
39-
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_mlir_upstream_c_api_library(MLIRCAPITarget
2+
LLVMIR.cpp
3+
4+
LINK_COMPONENTS
5+
Core
6+
7+
LINK_LIBS PUBLIC
8+
MLIRToLLVMIRTranslationRegistration
9+
MLIRCAPIIR
10+
MLIRLLVMToLLVMIRTranslation
11+
MLIRSupport
12+
)

mlir/lib/CAPI/Target/LLVMIR.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target ---------------------===//
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+
#include "mlir-c/Target/LLVMIR.h"
11+
#include "llvm-c/Support.h"
12+
13+
#include "llvm/IR/LLVMContext.h"
14+
#include "llvm/IR/Module.h"
15+
#include <memory>
16+
17+
#include "mlir/CAPI/IR.h"
18+
#include "mlir/CAPI/Support.h"
19+
#include "mlir/CAPI/Wrap.h"
20+
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
21+
22+
using namespace mlir;
23+
24+
LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module,
25+
LLVMContextRef context) {
26+
Operation *moduleOp = unwrap(module);
27+
28+
llvm::LLVMContext *ctx = llvm::unwrap(context);
29+
30+
std::unique_ptr<llvm::Module> llvmModule =
31+
mlir::translateModuleToLLVMIR(moduleOp, *ctx);
32+
33+
LLVMModuleRef moduleRef = llvm::wrap(llvmModule.release());
34+
35+
return moduleRef;
36+
}

0 commit comments

Comments
 (0)