Skip to content

Commit 5231605

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

File tree

8 files changed

+170
-5
lines changed

8 files changed

+170
-5
lines changed

mlir/include/mlir-c/Target/LLVMIR.h

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-

mlir/lib/CAPI/Target/CMakeLists.txt

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+
}

mlir/test/CAPI/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,12 @@ _add_capi_test_executable(mlir-capi-transform-test
8585
MLIRCAPIRegisterEverything
8686
MLIRCAPITransformDialect
8787
)
88+
89+
_add_capi_test_executable(mlir-capi-translation-test
90+
translation.c
91+
LINK_LIBS PRIVATE
92+
MLIRCAPIIR
93+
MLIRCAPILLVM
94+
MLIRCAPIRegisterEverything
95+
MLIRCAPITarget
96+
)

mlir/test/CAPI/translation.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===- translation.c - Test MLIR Target translations ----------------------===//
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+
// RUN: mlir-capi-translation-test 2>&1 | FileCheck %s
11+
12+
#include "llvm-c/Core.h"
13+
#include "llvm-c/Support.h"
14+
#include "llvm-c/Types.h"
15+
16+
#include "mlir-c/BuiltinTypes.h"
17+
#include "mlir-c/Dialect/LLVM.h"
18+
#include "mlir-c/IR.h"
19+
#include "mlir-c/RegisterEverything.h"
20+
#include "mlir-c/Support.h"
21+
#include "mlir-c/Target/LLVMIR.h"
22+
23+
#include <assert.h>
24+
#include <math.h>
25+
#include <stdio.h>
26+
#include <stdlib.h>
27+
#include <string.h>
28+
29+
// CHECK-LABEL: testToLLVMIR()
30+
static void testToLLVMIR(MlirContext ctx) {
31+
fprintf(stderr, "testToLLVMIR()\n");
32+
LLVMContextRef llvmCtx = LLVMContextCreate();
33+
34+
const char *moduleString = "llvm.func @add(%arg0: i64, %arg1: i64) -> i64 { \
35+
%0 = llvm.add %arg0, %arg1 : i64 \
36+
llvm.return %0 : i64 \
37+
}";
38+
39+
mlirRegisterAllLLVMTranslations(ctx);
40+
41+
MlirModule module =
42+
mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
43+
44+
MlirOperation operation = mlirModuleGetOperation(module);
45+
46+
LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(operation, llvmCtx);
47+
48+
// clang-format off
49+
// CHECK: define i64 @add(i64 %[[arg1:.*]], i64 %[[arg2:.*]]) {
50+
// CHECK-NEXT: %[[arg3:.*]] = add i64 %[[arg1]], %[[arg2]]
51+
// CHECK-NEXT: ret i64 %[[arg3]]
52+
// CHECK-NEXT: }
53+
// clang-format on
54+
LLVMDumpModule(llvmModule);
55+
56+
LLVMDisposeModule(llvmModule);
57+
mlirModuleDestroy(module);
58+
}
59+
60+
int main(void) {
61+
MlirContext ctx = mlirContextCreate();
62+
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx);
63+
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm"));
64+
testToLLVMIR(ctx);
65+
mlirContextDestroy(ctx);
66+
return 0;
67+
}

mlir/test/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ if (MLIR_INCLUDE_INTEGRATION_TESTS)
4545
message(FATAL_ERROR "MLIR_INCLUDE_INTEGRATION_TESTS requires a native target")
4646
endif()
4747

48-
# When the Integration tests are requested via the MLIR_INCLUDE_INTEGRATION_TESTS
49-
# configuration flag, we automatically include sm80 tests when build for
50-
# cuSparse when the configuration flag MLIR_ENABLE_CUDA_CUSPARSE is set and
51-
# include sm80 lt tests when the MLIR_ENABLE_CUDA_CUSPARSELT is set in
48+
# When the Integration tests are requested via the MLIR_INCLUDE_INTEGRATION_TESTS
49+
# configuration flag, we automatically include sm80 tests when build for
50+
# cuSparse when the configuration flag MLIR_ENABLE_CUDA_CUSPARSE is set and
51+
# include sm80 lt tests when the MLIR_ENABLE_CUDA_CUSPARSELT is set in
5252
# addition to those.
5353
if (MLIR_ENABLE_CUDA_CUSPARSE)
5454
set(MLIR_RUN_CUDA_SM80_TESTS ON)
@@ -101,6 +101,7 @@ set(MLIR_TEST_DEPENDS
101101
mlir-capi-quant-test
102102
mlir-capi-sparse-tensor-test
103103
mlir-capi-transform-test
104+
mlir-capi-translation-test
104105
mlir-linalg-ods-yaml-gen
105106
mlir-lsp-server
106107
mlir-pdll-lsp-server

mlir/test/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def add_runtime(name):
106106
"mlir-capi-quant-test",
107107
"mlir-capi-sparse-tensor-test",
108108
"mlir-capi-transform-test",
109+
"mlir-capi-translation-test",
109110
"mlir-cpu-runner",
110111
add_runtime("mlir_runner_utils"),
111112
add_runtime("mlir_c_runner_utils"),

0 commit comments

Comments
 (0)