LLZK 2.1.1
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
BuilderHelper.h
Go to the documentation of this file.
1//===-- BuilderHelper.h -----------------------------------------*- 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
14
15#include <mlir/IR/Builders.h>
16#include <mlir/IR/Location.h>
17
18namespace llzk {
19
20template <typename OpClass, typename... Args>
21inline OpClass delegate_to_build(mlir::Location location, Args &&...args) {
22 mlir::OpBuilder builder(location->getContext());
23 return builder.create<OpClass>(location, std::forward<Args>(args)...);
24}
25
26template <typename OpClass>
28 mlir::OpBuilder &odsBuilder, typename OpClass::Properties &props,
29 llvm::ArrayRef<mlir::Attribute> templateParams
30) {
31 if (!templateParams.empty()) {
32 // Must attempt to convert attribute types but `build()` functions do not have a failure path or
33 // error reporting. That comes during validation of the constructed op so ignore errors here.
34 llvm::FailureOr<llvm::SmallVector<mlir::Attribute>> r =
35 llzk::forceIntAttrTypes(templateParams, [&odsBuilder]() {
36 return llzk::InFlightDiagnosticWrapper::createSilent(odsBuilder.getContext());
37 });
38 llvm::ArrayRef<mlir::Attribute> converted = succeeded(r) ? r.value() : templateParams;
39 props.setTemplateParams(odsBuilder.getArrayAttr(converted));
40 }
41}
42
43} // namespace llzk
static InFlightDiagnosticWrapper createSilent(mlir::MLIRContext *ctx)
Construct a silent diagnostic that does nothing when appended to or reported.
Definition ErrorHelper.h:74
FailureOr< SmallVector< Attribute > > forceIntAttrTypes(ArrayRef< Attribute > attrList, EmitErrorFn emitError)
OpClass delegate_to_build(mlir::Location location, Args &&...args)
void addTemplateParams(mlir::OpBuilder &odsBuilder, typename OpClass::Properties &props, llvm::ArrayRef< mlir::Attribute > templateParams)