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#pragma once
11
12#include "llzk-c/Builder.h"
13
14#include <mlir/CAPI/IR.h>
15#include <mlir/CAPI/Wrap.h>
16#include <mlir/IR/Builders.h>
17
18DEFINE_C_API_PTR_METHODS(MlirOpBuilder, mlir::OpBuilder)
19
20namespace llzk {
21
22// Taken from mlir/IR/Builders.h
23template <typename OpT>
24mlir::RegisteredOperationName getCheckRegisteredInfo(mlir::MLIRContext *ctx) {
25 std::optional<mlir::RegisteredOperationName> opName =
26 mlir::RegisteredOperationName::lookup(OpT::getOperationName(), ctx);
27 if (LLVM_UNLIKELY(!opName)) {
28 llvm::report_fatal_error(
29 "Building op `" + OpT::getOperationName() +
30 "` but it isn't known in this MLIRContext: the dialect may not "
31 "be loaded or this operation hasn't been added by the dialect. See "
32 "also https://mlir.llvm.org/getting_started/Faq/"
33 "#registered-loaded-dependent-whats-up-with-dialects-management"
34 );
35 }
36 return *opName;
37}
38
40template <typename OpTy, typename... Args>
41mlir::Operation *create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args) {
42 auto location = unwrap(cLocation);
43 auto *builder = unwrap(cBuilder);
44 assert(builder);
45 mlir::OperationState state(location, getCheckRegisteredInfo<OpTy>(location.getContext()));
46 OpTy::build(*builder, state, std::forward<Args>(args)...);
47 auto *op = mlir::Operation::create(state);
48 assert(mlir::isa<OpTy>(op) && "builder didn't return the right type");
49 return op;
50}
51
52} // namespace llzk
mlir::RegisteredOperationName getCheckRegisteredInfo(mlir::MLIRContext *ctx)
Definition Builder.h:24
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
Definition Builder.h:41