From 9a2daa91684a695159039ba5c61779f4d962e3e6 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 22 Nov 2023 14:30:37 +0100 Subject: [PATCH 1/6] [mlir] Add mlirTranslateModuleToLLVMIR to MLIC-C --- mlir/include/mlir-c/Target/LLVMIR.h | 35 +++++++++++++++++++++++ mlir/lib/CAPI/CMakeLists.txt | 2 +- mlir/lib/CAPI/Target/CMakeLists.txt | 9 ++++++ mlir/lib/CAPI/Target/LLVMIR.cpp | 34 ++++++++++++++++++++++ mlir/test/CAPI/CMakeLists.txt | 1 + mlir/test/CAPI/llvm.c | 44 ++++++++++++++++++++++++++++- 6 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 mlir/include/mlir-c/Target/LLVMIR.h create mode 100644 mlir/lib/CAPI/Target/CMakeLists.txt create mode 100644 mlir/lib/CAPI/Target/LLVMIR.cpp diff --git a/mlir/include/mlir-c/Target/LLVMIR.h b/mlir/include/mlir-c/Target/LLVMIR.h new file mode 100644 index 0000000000000..96070e5c8c7b0 --- /dev/null +++ b/mlir/include/mlir-c/Target/LLVMIR.h @@ -0,0 +1,35 @@ +//===- 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 +// +//===----------------------------------------------------------------------===// +// +// 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. +MLIR_CAPI_EXPORTED LLVMModuleRef +mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context); + +#ifdef __cplusplus +} +#endif + +#endif // MLIR_C_TARGET_LLVMIR_H diff --git a/mlir/lib/CAPI/CMakeLists.txt b/mlir/lib/CAPI/CMakeLists.txt index 707e78ac3d1ea..6c438508425b7 100644 --- a/mlir/lib/CAPI/CMakeLists.txt +++ b/mlir/lib/CAPI/CMakeLists.txt @@ -14,6 +14,7 @@ add_subdirectory(Interfaces) add_subdirectory(IR) add_subdirectory(RegisterEverything) add_subdirectory(Transforms) +add_subdirectory(Target) if(MLIR_ENABLE_EXECUTION_ENGINE) add_subdirectory(ExecutionEngine) @@ -36,4 +37,3 @@ if(MLIR_BUILD_MLIR_C_DYLIB) endif() endif() endif() - diff --git a/mlir/lib/CAPI/Target/CMakeLists.txt b/mlir/lib/CAPI/Target/CMakeLists.txt new file mode 100644 index 0000000000000..17e8c067f1090 --- /dev/null +++ b/mlir/lib/CAPI/Target/CMakeLists.txt @@ -0,0 +1,9 @@ +add_mlir_upstream_c_api_library(MLIRCAPITarget + LLVMIR.cpp + + LINK_LIBS PUBLIC + MLIRToLLVMIRTranslationRegistration + MLIRCAPIIR + MLIRLLVMToLLVMIRTranslation + MLIRSupport +) diff --git a/mlir/lib/CAPI/Target/LLVMIR.cpp b/mlir/lib/CAPI/Target/LLVMIR.cpp new file mode 100644 index 0000000000000..bd86192ba36d5 --- /dev/null +++ b/mlir/lib/CAPI/Target/LLVMIR.cpp @@ -0,0 +1,34 @@ +//===- LLVMIR.cpp - 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 + +#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 = reinterpret_cast(context); + + auto llvmModule = mlir::translateModuleToLLVMIR(moduleOp, *ctx); + + LLVMModuleRef moduleRef = reinterpret_cast( + const_cast(llvmModule.release())); + + return moduleRef; +} diff --git a/mlir/test/CAPI/CMakeLists.txt b/mlir/test/CAPI/CMakeLists.txt index 16a3d0ed9c62f..b13fc613660c7 100644 --- a/mlir/test/CAPI/CMakeLists.txt +++ b/mlir/test/CAPI/CMakeLists.txt @@ -43,6 +43,7 @@ _add_capi_test_executable(mlir-capi-llvm-test MLIRCAPIIR MLIRCAPILLVM MLIRCAPIRegisterEverything + MLIRCAPITarget ) _add_capi_test_executable(mlir-capi-pass-test diff --git a/mlir/test/CAPI/llvm.c b/mlir/test/CAPI/llvm.c index aaec7b113f0a9..d3b859619ab4a 100644 --- a/mlir/test/CAPI/llvm.c +++ b/mlir/test/CAPI/llvm.c @@ -9,9 +9,15 @@ // RUN: mlir-capi-llvm-test 2>&1 | FileCheck %s -#include "mlir-c/Dialect/LLVM.h" +#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/Target/LLVMIR.h" #include #include @@ -73,11 +79,47 @@ static void testTypeCreation(MlirContext ctx) { mlirTypeEqual(i32_i64_s, i32_i64_s_ref)); } +// 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: ; ModuleID = 'LLVMDialectModule' + // CHECK-NEXT: source_filename = "LLVMDialectModule" + // CHECK: declare ptr @malloc(i64 %0) + // CHECK: declare void @free(ptr %0) + // CHECK: define i64 @add(i64 %0, i64 %1) { + // CHECK-NEXT: %3 = add i64 %0, %1 + // CHECK-NEXT: ret i64 %3 + // 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")); testTypeCreation(ctx); + testToLLVMIR(ctx); mlirContextDestroy(ctx); return 0; } From 28f3c137412bdff93b7afc47aea0d66c29237d6a Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 22 Nov 2023 16:41:07 +0100 Subject: [PATCH 2/6] fix suggestions --- mlir/include/mlir-c/Target/LLVMIR.h | 20 ++++---- mlir/lib/CAPI/Target/CMakeLists.txt | 3 ++ mlir/lib/CAPI/Target/LLVMIR.cpp | 12 +++-- mlir/test/CAPI/CMakeLists.txt | 10 +++- mlir/test/CAPI/llvm.c | 44 +----------------- mlir/test/CAPI/translation.c | 72 +++++++++++++++++++++++++++++ mlir/test/lit.cfg.py | 1 + 7 files changed, 106 insertions(+), 56 deletions(-) create mode 100644 mlir/test/CAPI/translation.c diff --git a/mlir/include/mlir-c/Target/LLVMIR.h b/mlir/include/mlir-c/Target/LLVMIR.h index 96070e5c8c7b0..978986c11d8e4 100644 --- a/mlir/include/mlir-c/Target/LLVMIR.h +++ b/mlir/include/mlir-c/Target/LLVMIR.h @@ -1,6 +1,7 @@ -//===- LLVMIR.h - C Interface for MLIR LLVMIR Target -------------------===// +//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target -------------*- C -*-===// // -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// 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 // @@ -21,12 +22,15 @@ 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. -MLIR_CAPI_EXPORTED LLVMModuleRef -mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context); +/// 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, MlirStringRef llvmModuleName); #ifdef __cplusplus } diff --git a/mlir/lib/CAPI/Target/CMakeLists.txt b/mlir/lib/CAPI/Target/CMakeLists.txt index 17e8c067f1090..ce86fd3def964 100644 --- a/mlir/lib/CAPI/Target/CMakeLists.txt +++ b/mlir/lib/CAPI/Target/CMakeLists.txt @@ -1,6 +1,9 @@ add_mlir_upstream_c_api_library(MLIRCAPITarget LLVMIR.cpp + LINK_COMPONENTS + Core + LINK_LIBS PUBLIC MLIRToLLVMIRTranslationRegistration MLIRCAPIIR diff --git a/mlir/lib/CAPI/Target/LLVMIR.cpp b/mlir/lib/CAPI/Target/LLVMIR.cpp index bd86192ba36d5..a192fa14dac07 100644 --- a/mlir/lib/CAPI/Target/LLVMIR.cpp +++ b/mlir/lib/CAPI/Target/LLVMIR.cpp @@ -1,6 +1,7 @@ -//===- LLVMIR.cpp - C Interface for MLIR LLVMIR Target -------------------===// +//===-- LLVMIR.h - C Interface for MLIR LLVMIR Target ---------------------===// // -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// 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 // @@ -20,12 +21,15 @@ using namespace mlir; LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, - LLVMContextRef context) { + LLVMContextRef context, + MlirStringRef llvmModuleName) { Operation *moduleOp = unwrap(module); llvm::LLVMContext *ctx = reinterpret_cast(context); - auto llvmModule = mlir::translateModuleToLLVMIR(moduleOp, *ctx); + std::unique_ptr llvmModule = mlir::translateModuleToLLVMIR( + moduleOp, *ctx, + llvm::StringRef(llvmModuleName.data, llvmModuleName.length)); LLVMModuleRef moduleRef = reinterpret_cast( const_cast(llvmModule.release())); diff --git a/mlir/test/CAPI/CMakeLists.txt b/mlir/test/CAPI/CMakeLists.txt index b13fc613660c7..1096a3b080664 100644 --- a/mlir/test/CAPI/CMakeLists.txt +++ b/mlir/test/CAPI/CMakeLists.txt @@ -43,7 +43,6 @@ _add_capi_test_executable(mlir-capi-llvm-test MLIRCAPIIR MLIRCAPILLVM MLIRCAPIRegisterEverything - MLIRCAPITarget ) _add_capi_test_executable(mlir-capi-pass-test @@ -86,3 +85,12 @@ _add_capi_test_executable(mlir-capi-transform-test MLIRCAPIRegisterEverything MLIRCAPITransformDialect ) + +_add_capi_test_executable(mlir-capi-translation-test + translation.c + LINK_LIBS PRIVATE + MLIRCAPIIR + MLIRCAPILLVM + MLIRCAPIRegisterEverything + MLIRCAPITarget +) diff --git a/mlir/test/CAPI/llvm.c b/mlir/test/CAPI/llvm.c index d3b859619ab4a..aaec7b113f0a9 100644 --- a/mlir/test/CAPI/llvm.c +++ b/mlir/test/CAPI/llvm.c @@ -9,15 +9,9 @@ // RUN: mlir-capi-llvm-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/BuiltinTypes.h" #include "mlir-c/IR.h" -#include "mlir-c/RegisterEverything.h" -#include "mlir-c/Target/LLVMIR.h" #include #include @@ -79,47 +73,11 @@ static void testTypeCreation(MlirContext ctx) { mlirTypeEqual(i32_i64_s, i32_i64_s_ref)); } -// 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: ; ModuleID = 'LLVMDialectModule' - // CHECK-NEXT: source_filename = "LLVMDialectModule" - // CHECK: declare ptr @malloc(i64 %0) - // CHECK: declare void @free(ptr %0) - // CHECK: define i64 @add(i64 %0, i64 %1) { - // CHECK-NEXT: %3 = add i64 %0, %1 - // CHECK-NEXT: ret i64 %3 - // 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")); testTypeCreation(ctx); - testToLLVMIR(ctx); mlirContextDestroy(ctx); return 0; } diff --git a/mlir/test/CAPI/translation.c b/mlir/test/CAPI/translation.c new file mode 100644 index 0000000000000..be3ab5582a86f --- /dev/null +++ b/mlir/test/CAPI/translation.c @@ -0,0 +1,72 @@ +//===- 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 +#include +#include +#include +#include + +// 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); + + MlirStringRef name = mlirStringRefCreateFromCString("LLVMDialectModule"); + + LLVMModuleRef llvmModule = + mlirTranslateModuleToLLVMIR(operation, llvmCtx, name); + + // 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; +} diff --git a/mlir/test/lit.cfg.py b/mlir/test/lit.cfg.py index 87bbe51e95d4c..17c29445ba82b 100644 --- a/mlir/test/lit.cfg.py +++ b/mlir/test/lit.cfg.py @@ -106,6 +106,7 @@ def add_runtime(name): "mlir-capi-quant-test", "mlir-capi-sparse-tensor-test", "mlir-capi-transform-test", + "mlir-capi-translation-test", "mlir-cpu-runner", add_runtime("mlir_runner_utils"), add_runtime("mlir_c_runner_utils"), From f084f95f67339ca1dee46c0780f2fba8d91d8ac2 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 22 Nov 2023 17:20:36 +0100 Subject: [PATCH 3/6] add to cmakelist --- mlir/test/CMakeLists.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt index e4343095578c1..ccb4a98254cd5 100644 --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -45,10 +45,10 @@ if (MLIR_INCLUDE_INTEGRATION_TESTS) message(FATAL_ERROR "MLIR_INCLUDE_INTEGRATION_TESTS requires a native target") endif() - # When the Integration tests are requested via the MLIR_INCLUDE_INTEGRATION_TESTS - # configuration flag, we automatically include sm80 tests when build for - # cuSparse when the configuration flag MLIR_ENABLE_CUDA_CUSPARSE is set and - # include sm80 lt tests when the MLIR_ENABLE_CUDA_CUSPARSELT is set in + # When the Integration tests are requested via the MLIR_INCLUDE_INTEGRATION_TESTS + # configuration flag, we automatically include sm80 tests when build for + # cuSparse when the configuration flag MLIR_ENABLE_CUDA_CUSPARSE is set and + # include sm80 lt tests when the MLIR_ENABLE_CUDA_CUSPARSELT is set in # addition to those. if (MLIR_ENABLE_CUDA_CUSPARSE) set(MLIR_RUN_CUDA_SM80_TESTS ON) @@ -101,6 +101,7 @@ set(MLIR_TEST_DEPENDS mlir-capi-quant-test mlir-capi-sparse-tensor-test mlir-capi-transform-test + mlir-capi-translation-test mlir-linalg-ods-yaml-gen mlir-lsp-server mlir-pdll-lsp-server From ccb220539a5a32148916d901f065486ffa60611a Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 22 Nov 2023 17:48:01 +0100 Subject: [PATCH 4/6] use llvm::unwrap and wrap --- mlir/lib/CAPI/Target/LLVMIR.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mlir/lib/CAPI/Target/LLVMIR.cpp b/mlir/lib/CAPI/Target/LLVMIR.cpp index a192fa14dac07..0e5528f1e629f 100644 --- a/mlir/lib/CAPI/Target/LLVMIR.cpp +++ b/mlir/lib/CAPI/Target/LLVMIR.cpp @@ -11,6 +11,7 @@ #include "llvm-c/Support.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" #include #include "mlir/CAPI/IR.h" @@ -25,14 +26,13 @@ LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, MlirStringRef llvmModuleName) { Operation *moduleOp = unwrap(module); - llvm::LLVMContext *ctx = reinterpret_cast(context); + llvm::LLVMContext *ctx = llvm::unwrap(context); std::unique_ptr llvmModule = mlir::translateModuleToLLVMIR( moduleOp, *ctx, llvm::StringRef(llvmModuleName.data, llvmModuleName.length)); - LLVMModuleRef moduleRef = reinterpret_cast( - const_cast(llvmModule.release())); + LLVMModuleRef moduleRef = llvm::wrap(llvmModule.release()); return moduleRef; } From 31b58c508a90bfac16c09b14ba994d86efb4452b Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 23 Nov 2023 11:20:00 +0100 Subject: [PATCH 5/6] don't pass module name argument --- mlir/include/mlir-c/Target/LLVMIR.h | 4 ++-- mlir/lib/CAPI/Target/LLVMIR.cpp | 8 +++----- mlir/test/CAPI/translation.c | 5 +---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/mlir/include/mlir-c/Target/LLVMIR.h b/mlir/include/mlir-c/Target/LLVMIR.h index 978986c11d8e4..effa74b905ce6 100644 --- a/mlir/include/mlir-c/Target/LLVMIR.h +++ b/mlir/include/mlir-c/Target/LLVMIR.h @@ -29,8 +29,8 @@ extern "C" { /// /// \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, MlirStringRef llvmModuleName); +MLIR_CAPI_EXPORTED LLVMModuleRef +mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context); #ifdef __cplusplus } diff --git a/mlir/lib/CAPI/Target/LLVMIR.cpp b/mlir/lib/CAPI/Target/LLVMIR.cpp index 0e5528f1e629f..dc798372be746 100644 --- a/mlir/lib/CAPI/Target/LLVMIR.cpp +++ b/mlir/lib/CAPI/Target/LLVMIR.cpp @@ -22,15 +22,13 @@ using namespace mlir; LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, - LLVMContextRef context, - MlirStringRef llvmModuleName) { + LLVMContextRef context) { Operation *moduleOp = unwrap(module); llvm::LLVMContext *ctx = llvm::unwrap(context); - std::unique_ptr llvmModule = mlir::translateModuleToLLVMIR( - moduleOp, *ctx, - llvm::StringRef(llvmModuleName.data, llvmModuleName.length)); + std::unique_ptr llvmModule = + mlir::translateModuleToLLVMIR(moduleOp, *ctx); LLVMModuleRef moduleRef = llvm::wrap(llvmModule.release()); diff --git a/mlir/test/CAPI/translation.c b/mlir/test/CAPI/translation.c index be3ab5582a86f..5a555e492cf09 100644 --- a/mlir/test/CAPI/translation.c +++ b/mlir/test/CAPI/translation.c @@ -43,10 +43,7 @@ static void testToLLVMIR(MlirContext ctx) { MlirOperation operation = mlirModuleGetOperation(module); - MlirStringRef name = mlirStringRefCreateFromCString("LLVMDialectModule"); - - LLVMModuleRef llvmModule = - mlirTranslateModuleToLLVMIR(operation, llvmCtx, name); + LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(operation, llvmCtx); // clang-format off // CHECK: declare ptr @malloc(i64 %{{.*}}) From 72f84bf52a8d18fa68f8cf1cb55f3e0ee95907d5 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Tue, 28 Nov 2023 10:26:08 +0100 Subject: [PATCH 6/6] dont test unrelated code in mlirTranslateModuleToLLVMIR --- mlir/test/CAPI/translation.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/mlir/test/CAPI/translation.c b/mlir/test/CAPI/translation.c index 5a555e492cf09..e58a01d8c171d 100644 --- a/mlir/test/CAPI/translation.c +++ b/mlir/test/CAPI/translation.c @@ -46,8 +46,6 @@ static void testToLLVMIR(MlirContext ctx) { 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]]