1//===- standalone-cap-demo.c - Simple demo of C-API -----------------------===//
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: standalone-capi-test 2>&1 | FileCheck %s
11
12#include <stdio.h>
13
14#include "Standalone-c/Dialects.h"
15#include "mlir-c/IR.h"
16#include "mlir-c/RegisterEverything.h"
17
18static void registerAllUpstreamDialects(MlirContext ctx) {
19 MlirDialectRegistry registry = mlirDialectRegistryCreate();
20 mlirRegisterAllDialects(registry);
21 mlirContextAppendDialectRegistry(ctx, registry);
22 mlirDialectRegistryDestroy(registry);
23}
24
25int main(int argc, char **argv) {
26 MlirContext ctx = mlirContextCreate();
27 // TODO: Create the dialect handles for the builtin dialects and avoid this.
28 // This adds dozens of MB of binary size over just the standalone dialect.
29 registerAllUpstreamDialects(ctx);
30 mlirDialectHandleRegisterDialect(mlirGetDialectHandle__standalone__(), ctx);
31
32 MlirModule module = mlirModuleCreateParse(
33 context: ctx, module: mlirStringRefCreateFromCString(str: "%0 = arith.constant 2 : i32\n"
34 "%1 = standalone.foo %0 : i32\n"));
35 if (mlirModuleIsNull(module)) {
36 printf(format: "ERROR: Could not parse.\n");
37 mlirContextDestroy(context: ctx);
38 return 1;
39 }
40 MlirOperation op = mlirModuleGetOperation(module);
41
42 // CHECK: %[[C:.*]] = arith.constant 2 : i32
43 // CHECK: standalone.foo %[[C]] : i32
44 mlirOperationDump(op);
45
46 mlirModuleDestroy(module);
47 mlirContextDestroy(context: ctx);
48 return 0;
49}
50

source code of mlir/examples/standalone/test/CAPI/standalone-capi-test.c