LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Builder.h
Go to the documentation of this file.
1//===-- Builder.h - C API for op builder --------------------------*- C -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9//
10// This header declares a type that supports the creation of operations and
11// handles their insertion in blocks.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLZK_C_BUILDER_H
16#define LLZK_C_BUILDER_H
17
18#include <mlir-c/IR.h>
19#include <mlir-c/Support.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25#define DEFINE_C_API_STRUCT(name, storage) \
26 struct name { \
27 storage *ptr; \
28 }; \
29 typedef struct name name
30
31DEFINE_C_API_STRUCT(MlirOpBuilder, void);
32DEFINE_C_API_STRUCT(MlirOpBuilderListener, void);
33
34#undef DEFINE_C_API_STRUCT
35
36// Commented out because they are not used for the callbacks in MLIR 18
37// but are used for MLIR 20+, which we plan to move on in the near future.
38#if 0
39struct MlirOpInsertionPoint {
40 MlirBlock block;
41 MlirOperation point;
42};
43typedef struct MlirOpInsertionPoint MlirOpInsertionPoint;
44
45struct MlirBlockInsertionPoint {
46 MlirRegion region;
47 MlirBlock point;
48};
49typedef struct MlirBlockInsertionPoint MlirBlockInsertionPoint;
50#endif
51
52typedef void (*MlirNotifyOperationInserted)(MlirOperation, void *);
53typedef void (*MlirNotifyBlockInserted)(MlirBlock, void *);
54
55//===----------------------------------------------------------------------===//
56// MlirOpBuilder
57//===----------------------------------------------------------------------===//
58
59// The API for OpBuilder is left barebones for now since we only need a reference that we can pass
60// to op build methods that we expose. More methods can be added as the need for them arises.
61
62#define DECLARE_SUFFIX_OP_BUILDER_CREATE_FN(suffix, ...) \
63 MLIR_CAPI_EXPORTED MlirOpBuilder mlirOpBuilderCreate##suffix(__VA_ARGS__); \
64 MLIR_CAPI_EXPORTED MlirOpBuilder mlirOpBuilderCreate##suffix##WithListener( \
65 __VA_ARGS__, MlirOpBuilderListener \
66 );
67#define DECLARE_OP_BUILDER_CREATE_FN(...) DECLARE_SUFFIX_OP_BUILDER_CREATE_FN(, __VA_ARGS__)
68
69DECLARE_OP_BUILDER_CREATE_FN(MlirContext context)
70
71#undef DECLARE_OP_BUILDER_CREATE_FN
72
74MLIR_CAPI_EXPORTED void mlirOpBuilderDestroy(MlirOpBuilder builder);
75
77MLIR_CAPI_EXPORTED MlirContext mlirOpBuilderGetContext(MlirOpBuilder builder);
78
80MLIR_CAPI_EXPORTED void
81mlirOpBuilderSetInsertionPointToStart(MlirOpBuilder builder, MlirBlock block);
82
84MLIR_CAPI_EXPORTED MlirOperation mlirOpBuilderGetInsertionPoint(MlirOpBuilder builder);
85
87MLIR_CAPI_EXPORTED MlirBlock mlirOpBuilderGetInsertionBlock(MlirOpBuilder builder);
88
89//===----------------------------------------------------------------------===//
90// MlirOpBuilderListener
91//===----------------------------------------------------------------------===//
92
95MLIR_CAPI_EXPORTED MlirOpBuilderListener mlirOpBuilderListenerCreate(
96 MlirNotifyOperationInserted operationCb, MlirNotifyBlockInserted blockCb, void *userData
97);
98
100MLIR_CAPI_EXPORTED void mlirOpBuilderListenerDestroy(MlirOpBuilderListener listener);
101
102#ifdef __cplusplus
103}
104#endif
105
106#endif
MLIR_CAPI_EXPORTED MlirOpBuilderListener mlirOpBuilderListenerCreate(MlirNotifyOperationInserted operationCb, MlirNotifyBlockInserted blockCb, void *userData)
Creates a new mlir::OpBuilder::Listener.
Definition Builder.cpp:87
#define DECLARE_OP_BUILDER_CREATE_FN(...)
Definition Builder.h:67
MLIR_CAPI_EXPORTED MlirOperation mlirOpBuilderGetInsertionPoint(MlirOpBuilder builder)
Returns the current insertion point in the builder.
Definition Builder.cpp:68
#define DEFINE_C_API_STRUCT(name, storage)
Definition Builder.h:25
void(* MlirNotifyOperationInserted)(MlirOperation, void *)
Definition Builder.h:52
MLIR_CAPI_EXPORTED void mlirOpBuilderDestroy(MlirOpBuilder builder)
Destroys the given builder.
Definition Builder.cpp:56
MLIR_CAPI_EXPORTED MlirContext mlirOpBuilderGetContext(MlirOpBuilder builder)
Returns the context.
Definition Builder.cpp:60
void(* MlirNotifyBlockInserted)(MlirBlock, void *)
Definition Builder.h:53
MLIR_CAPI_EXPORTED MlirBlock mlirOpBuilderGetInsertionBlock(MlirOpBuilder builder)
Returns the current insertion block in the builder.
Definition Builder.cpp:79
MLIR_CAPI_EXPORTED void mlirOpBuilderListenerDestroy(MlirOpBuilderListener listener)
Destroys the given listener.
Definition Builder.cpp:93
MLIR_CAPI_EXPORTED void mlirOpBuilderSetInsertionPointToStart(MlirOpBuilder builder, MlirBlock block)
Sets the insertion point to the beginning of the given block.
Definition Builder.cpp:64