LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
OpTraits.cpp
Go to the documentation of this file.
1//===-- OpTraits.cpp --------------------------------------------*- 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
11
16
17#include <mlir/IR/Operation.h>
18#include <mlir/Support/LLVM.h>
19
20#include <llvm/ADT/StringRef.h>
21
22using namespace mlir;
23
24namespace llzk::function {
25
26namespace {
27
28auto parentFuncDefOpHasAttr = [](Operation *op, auto attrFn) -> bool {
29 if (FuncDefOp f = op->getParentOfType<FuncDefOp>()) {
30 return (f.*attrFn)();
31 }
32 return false;
33};
34
35template <typename Attr, typename F>
36LogicalResult verifyTraitIsPresentInFuncDefOp(Operation *op, F attrFn) {
37 // These are allowed anywhere outside of FuncDefOp but only allowed inside a FuncDefOp
38 // that is marked with the associated attribute.
39 if (FuncDefOp f = op->getParentOfType<FuncDefOp>()) {
40 if (!(f.*attrFn)()) {
41 return op->emitOpError() << "cannot be used within a '" << FuncDefOp::getOperationName()
42 << "' without the '" << Attr::name << "' attribute";
43 }
44 }
45 return success();
46}
47
48} // namespace
49
50LogicalResult verifyConstraintGenTraitImpl(Operation *op) {
51 if (parentFuncDefOpHasAttr(op, &FuncDefOp::hasAllowConstraintAttr)) {
52 return success();
53 }
54 return op->emitOpError() << "only valid within a '" << FuncDefOp::getOperationName() << "' with '"
55 << AllowConstraintAttr::name << "' attribute";
56}
57
58LogicalResult verifyWitnessGenTraitImpl(Operation *op) {
59 if (parentFuncDefOpHasAttr(op, &FuncDefOp::hasAllowWitnessAttr)) {
60 return success();
61 }
62 return op->emitOpError() << "only valid within a '" << FuncDefOp::getOperationName() << "' with '"
63 << AllowWitnessAttr::name << "' attribute";
64}
65
66LogicalResult verifyNotFieldNativeTraitImpl(Operation *op) {
67 return verifyTraitIsPresentInFuncDefOp<AllowNonNativeFieldOpsAttr>(
69 );
70}
71
72LogicalResult
73verifyVerificationTraitImpl(Operation *op, llvm::function_ref<LogicalResult()> check) {
74 if (failed(
75 verifyTraitIsPresentInFuncDefOp<AllowVerifOpsAttr>(op, &FuncDefOp::hasAllowVerifOpsAttr)
76 )) {
77 return failure();
78 }
79 if (FuncDefOp f = op->getParentOfType<FuncDefOp>()) {
80 if (check) {
81 return check();
82 }
83 }
84
85 return success();
86}
87
88} // namespace llzk::function
#define check(x)
Definition Ops.cpp:286
bool hasAllowNonNativeFieldOpsAttr()
Return true iff the function def has the allow_non_native_field_ops attribute.
Definition Ops.h.inc:833
bool hasAllowWitnessAttr()
Return true iff the function def has the allow_witness attribute.
Definition Ops.h.inc:825
bool hasAllowVerifOpsAttr()
Return true iff the function def has the allow_verif_ops attribute.
Definition Ops.h.inc:841
static constexpr ::llvm::StringLiteral getOperationName()
Definition Ops.h.inc:679
bool hasAllowConstraintAttr()
Return true iff the function def has the allow_constraint attribute.
Definition Ops.h.inc:817
LogicalResult verifyNotFieldNativeTraitImpl(Operation *op)
Definition OpTraits.cpp:66
LogicalResult verifyVerificationTraitImpl(Operation *op, llvm::function_ref< LogicalResult()> check)
Definition OpTraits.cpp:73
LogicalResult verifyConstraintGenTraitImpl(Operation *op)
Definition OpTraits.cpp:50
LogicalResult verifyWitnessGenTraitImpl(Operation *op)
Definition OpTraits.cpp:58