LLZK 2.1.1
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
19using namespace mlir;
20
21namespace llzk::function {
22
23namespace {
24
25auto parentFuncDefOpHasAttr = [](Operation *op, auto attrFn) -> bool {
26 if (FuncDefOp f = op->getParentOfType<FuncDefOp>()) {
27 return (f.*attrFn)();
28 }
29 return false;
30};
31
32} // namespace
33
34LogicalResult verifyConstraintGenTraitImpl(Operation *op) {
35 if (parentFuncDefOpHasAttr(op, &FuncDefOp::hasAllowConstraintAttr)) {
36 return success();
37 }
38 return op->emitOpError() << "only valid within a '" << FuncDefOp::getOperationName() << "' with '"
39 << AllowConstraintAttr::name << "' attribute";
40}
41
42LogicalResult verifyWitnessGenTraitImpl(Operation *op) {
43 if (parentFuncDefOpHasAttr(op, &FuncDefOp::hasAllowWitnessAttr)) {
44 return success();
45 }
46 return op->emitOpError() << "only valid within a '" << FuncDefOp::getOperationName() << "' with '"
47 << AllowWitnessAttr::name << "' attribute";
48}
49
50LogicalResult verifyNotFieldNativeTraitImpl(Operation *op) {
51 // These are allowed anywhere outside of FuncDefOp but only allowed inside a FuncDefOp
52 // that is marked with the associated attribute.
53 if (FuncDefOp f = op->getParentOfType<FuncDefOp>()) {
54 if (!f.hasAllowNonNativeFieldOpsAttr()) {
55 return op->emitOpError() << "cannot be used within a '" << FuncDefOp::getOperationName()
56 << "' without the '" << AllowNonNativeFieldOpsAttr::name
57 << "' attribute";
58 }
59 }
60 return success();
61}
62
63} // namespace llzk::function
bool hasAllowWitnessAttr()
Return true iff the function def has the allow_witness attribute.
Definition Ops.h.inc:812
static constexpr ::llvm::StringLiteral getOperationName()
Definition Ops.h.inc:666
bool hasAllowConstraintAttr()
Return true iff the function def has the allow_constraint attribute.
Definition Ops.h.inc:804
LogicalResult verifyNotFieldNativeTraitImpl(Operation *op)
Definition OpTraits.cpp:50
LogicalResult verifyConstraintGenTraitImpl(Operation *op)
Definition OpTraits.cpp:34
LogicalResult verifyWitnessGenTraitImpl(Operation *op)
Definition OpTraits.cpp:42