LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
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 /* raw pointer to C++ object */ \
28 storage *ptr; \
29 }; \
30 typedef struct name name
31
33DEFINE_C_API_STRUCT(MlirOpBuilder, void);
35DEFINE_C_API_STRUCT(MlirOpBuilderListener, void);
36
37#undef DEFINE_C_API_STRUCT
38
43 MlirBlock block;
45 MlirOperation point;
46};
48
50typedef void (*MlirNotifyOperationInserted)(MlirOperation, MlirOpBuilderInsertPoint, void *);
52typedef void (*MlirNotifyBlockInserted)(MlirBlock, MlirRegion, MlirBlock, void *);
53
54//===----------------------------------------------------------------------===//
55// MlirOpBuilder
56//===----------------------------------------------------------------------===//
57
58// The API for OpBuilder is left barebones for now since we only need a reference that we can pass
59// to op build methods that we expose. More methods can be added as the need for them arises.
60
61#define DECLARE_SUFFIX_OP_BUILDER_CREATE_FN(suffix, ...) \
62 /* Create a new builder with the given context */ \
63 MLIR_CAPI_EXPORTED MlirOpBuilder mlirOpBuilderCreate##suffix(__VA_ARGS__); \
64 /* Create a new builder with the given context and listener */ \
65 MLIR_CAPI_EXPORTED MlirOpBuilder mlirOpBuilderCreate##suffix##WithListener( \
66 __VA_ARGS__, MlirOpBuilderListener \
67 );
68#define DECLARE_OP_BUILDER_CREATE_FN(...) DECLARE_SUFFIX_OP_BUILDER_CREATE_FN(, __VA_ARGS__)
69
70DECLARE_OP_BUILDER_CREATE_FN(MlirContext context)
71
72#undef DECLARE_OP_BUILDER_CREATE_FN
73
75MLIR_CAPI_EXPORTED void mlirOpBuilderDestroy(MlirOpBuilder builder);
76
78MLIR_CAPI_EXPORTED MlirContext mlirOpBuilderGetContext(MlirOpBuilder builder);
79
81MLIR_CAPI_EXPORTED void
82mlirOpBuilderSetInsertionPointToStart(MlirOpBuilder builder, MlirBlock block);
83
85MLIR_CAPI_EXPORTED MlirOperation mlirOpBuilderGetInsertionPoint(MlirOpBuilder builder);
86
88MLIR_CAPI_EXPORTED MlirBlock mlirOpBuilderGetInsertionBlock(MlirOpBuilder builder);
89
91MLIR_CAPI_EXPORTED MlirOperation mlirOpBuilderInsert(MlirOpBuilder builder, MlirOperation op);
92
93//===----------------------------------------------------------------------===//
94// MlirOpBuilderListener
95//===----------------------------------------------------------------------===//
96
99MLIR_CAPI_EXPORTED MlirOpBuilderListener mlirOpBuilderListenerCreate(
100 MlirNotifyOperationInserted operationCb, MlirNotifyBlockInserted blockCb, void *userData
101);
102
104MLIR_CAPI_EXPORTED void mlirOpBuilderListenerDestroy(MlirOpBuilderListener listener);
105
106#ifdef __cplusplus
107}
108#endif
109
110#endif // LLZK_C_BUILDER_H
MLIR_CAPI_EXPORTED MlirOpBuilderListener mlirOpBuilderListenerCreate(MlirNotifyOperationInserted operationCb, MlirNotifyBlockInserted blockCb, void *userData)
Creates a new mlir::OpBuilder::Listener.
Definition Builder.cpp:119
#define DECLARE_OP_BUILDER_CREATE_FN(...)
Definition Builder.h:68
void(* MlirNotifyOperationInserted)(MlirOperation, MlirOpBuilderInsertPoint, void *)
Callback type for listening to operation insertions in an mlir::OpBuilder.
Definition Builder.h:50
MLIR_CAPI_EXPORTED MlirOperation mlirOpBuilderGetInsertionPoint(MlirOpBuilder builder)
Returns the current insertion point in the builder.
Definition Builder.cpp:91
#define DEFINE_C_API_STRUCT(name, storage)
Definition Builder.h:25
MLIR_CAPI_EXPORTED void mlirOpBuilderDestroy(MlirOpBuilder builder)
Destroys the given builder.
Definition Builder.cpp:74
MLIR_CAPI_EXPORTED MlirContext mlirOpBuilderGetContext(MlirOpBuilder builder)
Returns the context.
Definition Builder.cpp:79
MLIR_CAPI_EXPORTED MlirBlock mlirOpBuilderGetInsertionBlock(MlirOpBuilder builder)
Returns the current insertion block in the builder.
Definition Builder.cpp:103
MLIR_CAPI_EXPORTED void mlirOpBuilderListenerDestroy(MlirOpBuilderListener listener)
Destroys the given listener.
Definition Builder.cpp:126
MLIR_CAPI_EXPORTED MlirOperation mlirOpBuilderInsert(MlirOpBuilder builder, MlirOperation op)
Insert the given operation at the current insertion point and return it.
Definition Builder.cpp:108
MLIR_CAPI_EXPORTED void mlirOpBuilderSetInsertionPointToStart(MlirOpBuilder builder, MlirBlock block)
Sets the insertion point to the beginning of the given block.
Definition Builder.cpp:85
void(* MlirNotifyBlockInserted)(MlirBlock, MlirRegion, MlirBlock, void *)
Callback type for listening to block insertions in an mlir::OpBuilder.
Definition Builder.h:52
Current insertion point of an mlir::OpBuilder instance represented as a block and an operation within...
Definition Builder.h:41
MlirOperation point
The operation that the builder is inserting before.
Definition Builder.h:45
MlirBlock block
The block that the builder is inserting into.
Definition Builder.h:43