|
| 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: declare ptr @malloc(i64 %{{.*}}) |
| 50 | + // CHECK: declare void @free(ptr %{{.*}}) |
| 51 | + // CHECK: define i64 @add(i64 %[[arg1:.*]], i64 %[[arg2:.*]]) { |
| 52 | + // CHECK-NEXT: %[[arg3:.*]] = add i64 %[[arg1]], %[[arg2]] |
| 53 | + // CHECK-NEXT: ret i64 %[[arg3]] |
| 54 | + // CHECK-NEXT: } |
| 55 | + // clang-format on |
| 56 | + LLVMDumpModule(llvmModule); |
| 57 | + |
| 58 | + LLVMDisposeModule(llvmModule); |
| 59 | + mlirModuleDestroy(module); |
| 60 | +} |
| 61 | + |
| 62 | +int main(void) { |
| 63 | + MlirContext ctx = mlirContextCreate(); |
| 64 | + mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx); |
| 65 | + mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm")); |
| 66 | + testToLLVMIR(ctx); |
| 67 | + mlirContextDestroy(ctx); |
| 68 | + return 0; |
| 69 | +} |
0 commit comments