| 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, format: "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(context: ctx); |
| 40 | |
| 41 | MlirModule module = |
| 42 | mlirModuleCreateParse(context: ctx, module: mlirStringRefCreateFromCString(str: moduleString)); |
| 43 | |
| 44 | MlirOperation operation = mlirModuleGetOperation(module); |
| 45 | |
| 46 | LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(module: operation, context: 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(M: llvmModule); |
| 55 | |
| 56 | LLVMDisposeModule(M: llvmModule); |
| 57 | mlirModuleDestroy(module); |
| 58 | LLVMContextDispose(C: llvmCtx); |
| 59 | } |
| 60 | |
| 61 | // CHECK-LABEL: testTypeToFromLLVMIRTranslator |
| 62 | static void testTypeToFromLLVMIRTranslator(MlirContext ctx) { |
| 63 | fprintf(stderr, format: "testTypeToFromLLVMIRTranslator\n" ); |
| 64 | LLVMContextRef llvmCtx = LLVMContextCreate(); |
| 65 | |
| 66 | LLVMTypeRef llvmTy = LLVMInt32TypeInContext(C: llvmCtx); |
| 67 | MlirTypeFromLLVMIRTranslator fromLLVMTranslator = |
| 68 | mlirTypeFromLLVMIRTranslatorCreate(ctx); |
| 69 | MlirType mlirTy = |
| 70 | mlirTypeFromLLVMIRTranslatorTranslateType(translator: fromLLVMTranslator, llvmType: llvmTy); |
| 71 | // CHECK: i32 |
| 72 | mlirTypeDump(type: mlirTy); |
| 73 | |
| 74 | MlirTypeToLLVMIRTranslator toLLVMTranslator = |
| 75 | mlirTypeToLLVMIRTranslatorCreate(ctx: llvmCtx); |
| 76 | LLVMTypeRef llvmTy2 = |
| 77 | mlirTypeToLLVMIRTranslatorTranslateType(translator: toLLVMTranslator, mlirType: mlirTy); |
| 78 | // CHECK: i32 |
| 79 | LLVMDumpType(Val: llvmTy2); |
| 80 | fprintf(stderr, format: "\n" ); |
| 81 | |
| 82 | mlirTypeFromLLVMIRTranslatorDestroy(translator: fromLLVMTranslator); |
| 83 | mlirTypeToLLVMIRTranslatorDestroy(translator: toLLVMTranslator); |
| 84 | LLVMContextDispose(C: llvmCtx); |
| 85 | } |
| 86 | |
| 87 | int main(void) { |
| 88 | MlirContext ctx = mlirContextCreate(); |
| 89 | mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx); |
| 90 | mlirContextGetOrLoadDialect(context: ctx, name: mlirStringRefCreateFromCString(str: "llvm" )); |
| 91 | testToLLVMIR(ctx); |
| 92 | testTypeToFromLLVMIRTranslator(ctx); |
| 93 | mlirContextDestroy(context: ctx); |
| 94 | return 0; |
| 95 | } |
| 96 | |