1 //===- standalone-cap-demo.c - Simple demo of C-API -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 // RUN: standalone-capi-test 2>&1 | FileCheck %s
14 #include "Standalone-c/Dialects.h"
15 #include "mlir-c/IR.h"
16 #include "mlir-c/RegisterEverything.h"
18 static void registerAllUpstreamDialects(MlirContext ctx
) {
19 MlirDialectRegistry registry
= mlirDialectRegistryCreate();
20 mlirRegisterAllDialects(registry
);
21 mlirContextAppendDialectRegistry(ctx
, registry
);
22 mlirDialectRegistryDestroy(registry
);
25 int 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
);
32 MlirModule module
= mlirModuleCreateParse(
33 ctx
, mlirStringRefCreateFromCString("%0 = arith.constant 2 : i32\n"
34 "%1 = standalone.foo %0 : i32\n"));
35 if (mlirModuleIsNull(module
)) {
36 printf("ERROR: Could not parse.\n");
37 mlirContextDestroy(ctx
);
40 MlirOperation op
= mlirModuleGetOperation(module
);
42 // CHECK: %[[C:.*]] = arith.constant 2 : i32
43 // CHECK: standalone.foo %[[C]] : i32
44 mlirOperationDump(op
);
46 mlirModuleDestroy(module
);
47 mlirContextDestroy(ctx
);