Skip to content

Commit 396fa2f

Browse files
committed
fix suggestions
1 parent dc6bcab commit 396fa2f

File tree

7 files changed

+106
-56
lines changed

7 files changed

+106
-56
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//===- LLVMIR.h - C Interface for MLIR LLVMIR Target -------------------===//
1+
//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target -------------*- C -*-===//
22
//
3-
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
45
// See https://llvm.org/LICENSE.txt for license information.
56
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67
//
@@ -21,12 +22,15 @@
2122
extern "C" {
2223
#endif
2324

24-
// Translate operation that satisfies LLVM dialect module requirements into an
25-
// LLVM IR module living in the given context. This translates operations from
26-
// any dilalect that has a registered implementation of
27-
// LLVMTranslationDialectInterface.
28-
MLIR_CAPI_EXPORTED LLVMModuleRef
29-
mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context);
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 mlirTranslateModuleToLLVMIR(
33+
MlirOperation module, LLVMContextRef context, MlirStringRef llvmModuleName);
3034

3135
#ifdef __cplusplus
3236
}

mlir/lib/CAPI/Target/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
add_mlir_upstream_c_api_library(MLIRCAPITarget
22
LLVMIR.cpp
33

4+
LINK_COMPONENTS
5+
Core
6+
47
LINK_LIBS PUBLIC
58
MLIRToLLVMIRTranslationRegistration
69
MLIRCAPIIR

mlir/lib/CAPI/Target/LLVMIR.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//===- LLVMIR.cpp - C Interface for MLIR LLVMIR Target -------------------===//
1+
//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target ---------------------===//
22
//
3-
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
45
// See https://llvm.org/LICENSE.txt for license information.
56
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67
//
@@ -20,12 +21,15 @@
2021
using namespace mlir;
2122

2223
LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module,
23-
LLVMContextRef context) {
24+
LLVMContextRef context,
25+
MlirStringRef llvmModuleName) {
2426
Operation *moduleOp = unwrap(module);
2527

2628
llvm::LLVMContext *ctx = reinterpret_cast<llvm::LLVMContext *>(context);
2729

28-
auto llvmModule = mlir::translateModuleToLLVMIR(moduleOp, *ctx);
30+
std::unique_ptr<llvm::Module> llvmModule = mlir::translateModuleToLLVMIR(
31+
moduleOp, *ctx,
32+
llvm::StringRef(llvmModuleName.data, llvmModuleName.length));
2933

3034
LLVMModuleRef moduleRef = reinterpret_cast<LLVMModuleRef>(
3135
const_cast<llvm::Module *>(llvmModule.release()));

mlir/test/CAPI/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ _add_capi_test_executable(mlir-capi-llvm-test
4343
MLIRCAPIIR
4444
MLIRCAPILLVM
4545
MLIRCAPIRegisterEverything
46-
MLIRCAPITarget
4746
)
4847

4948
_add_capi_test_executable(mlir-capi-pass-test
@@ -86,3 +85,12 @@ _add_capi_test_executable(mlir-capi-transform-test
8685
MLIRCAPIRegisterEverything
8786
MLIRCAPITransformDialect
8887
)
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/llvm.c

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@
99

1010
// RUN: mlir-capi-llvm-test 2>&1 | FileCheck %s
1111

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"
1712
#include "mlir-c/Dialect/LLVM.h"
13+
#include "mlir-c/BuiltinTypes.h"
1814
#include "mlir-c/IR.h"
19-
#include "mlir-c/RegisterEverything.h"
20-
#include "mlir-c/Target/LLVMIR.h"
2115

2216
#include <assert.h>
2317
#include <math.h>
@@ -79,47 +73,11 @@ static void testTypeCreation(MlirContext ctx) {
7973
mlirTypeEqual(i32_i64_s, i32_i64_s_ref));
8074
}
8175

82-
// CHECK-LABEL: testToLLVMIR()
83-
static void testToLLVMIR(MlirContext ctx) {
84-
fprintf(stderr, "testToLLVMIR()\n");
85-
LLVMContextRef llvmCtx = LLVMContextCreate();
86-
87-
const char *moduleString = "llvm.func @add(%arg0: i64, %arg1: i64) -> i64 { \
88-
%0 = llvm.add %arg0, %arg1 : i64 \
89-
llvm.return %0 : i64 \
90-
}";
91-
92-
mlirRegisterAllLLVMTranslations(ctx);
93-
94-
MlirModule module =
95-
mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
96-
97-
MlirOperation operation = mlirModuleGetOperation(module);
98-
99-
LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(operation, llvmCtx);
100-
101-
// clang-format off
102-
// CHECK: ; ModuleID = 'LLVMDialectModule'
103-
// CHECK-NEXT: source_filename = "LLVMDialectModule"
104-
// CHECK: declare ptr @malloc(i64 %0)
105-
// CHECK: declare void @free(ptr %0)
106-
// CHECK: define i64 @add(i64 %0, i64 %1) {
107-
// CHECK-NEXT: %3 = add i64 %0, %1
108-
// CHECK-NEXT: ret i64 %3
109-
// CHECK-NEXT: }
110-
// clang-format on
111-
LLVMDumpModule(llvmModule);
112-
113-
LLVMDisposeModule(llvmModule);
114-
mlirModuleDestroy(module);
115-
}
116-
11776
int main(void) {
11877
MlirContext ctx = mlirContextCreate();
11978
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx);
12079
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm"));
12180
testTypeCreation(ctx);
122-
testToLLVMIR(ctx);
12381
mlirContextDestroy(ctx);
12482
return 0;
12583
}

mlir/test/CAPI/translation.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
MlirStringRef name = mlirStringRefCreateFromCString("LLVMDialectModule");
47+
48+
LLVMModuleRef llvmModule =
49+
mlirTranslateModuleToLLVMIR(operation, llvmCtx, name);
50+
51+
// clang-format off
52+
// CHECK: declare ptr @malloc(i64 %{{.*}})
53+
// CHECK: declare void @free(ptr %{{.*}})
54+
// CHECK: define i64 @add(i64 %[[arg1:.*]], i64 %[[arg2:.*]]) {
55+
// CHECK-NEXT: %[[arg3:.*]] = add i64 %[[arg1]], %[[arg2]]
56+
// CHECK-NEXT: ret i64 %[[arg3]]
57+
// CHECK-NEXT: }
58+
// clang-format on
59+
LLVMDumpModule(llvmModule);
60+
61+
LLVMDisposeModule(llvmModule);
62+
mlirModuleDestroy(module);
63+
}
64+
65+
int main(void) {
66+
MlirContext ctx = mlirContextCreate();
67+
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx);
68+
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm"));
69+
testToLLVMIR(ctx);
70+
mlirContextDestroy(ctx);
71+
return 0;
72+
}

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)