LLZK
3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Toggle main menu visibility
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
10
#include "
llzk/Dialect/Function/IR/OpTraits.h
"
11
12
#include "
llzk/Dialect/Function/IR/Ops.h
"
13
#include "
llzk/Dialect/Polymorphic/IR/Ops.h
"
14
#include "
llzk/Dialect/Shared/OpHelpers.h
"
15
#include "
llzk/Dialect/Verif/IR/Ops.h
"
16
17
#include <mlir/IR/Operation.h>
18
#include <mlir/Support/LLVM.h>
19
20
#include <llvm/ADT/StringRef.h>
21
22
using namespace
mlir
;
23
24
namespace
llzk::function
{
25
26
namespace
{
27
28
auto
parentFuncDefOpHasAttr = [](Operation *op,
auto
attrFn) ->
bool
{
29
if
(
FuncDefOp
f = op->getParentOfType<
FuncDefOp
>()) {
30
return
(f.*attrFn)();
31
}
32
return
false
;
33
};
34
35
template
<
typename
Attr,
typename
F>
36
LogicalResult 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
50
LogicalResult
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
58
LogicalResult
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
66
LogicalResult
verifyNotFieldNativeTraitImpl
(Operation *op) {
67
return
verifyTraitIsPresentInFuncDefOp<AllowNonNativeFieldOpsAttr>(
68
op, &
FuncDefOp::hasAllowNonNativeFieldOpsAttr
69
);
70
}
71
72
LogicalResult
73
verifyVerificationTraitImpl
(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
Ops.h
OpHelpers.h
OpTraits.h
check
#define check(x)
Definition
Ops.cpp:286
Ops.h
Ops.h
llzk::function::FuncDefOp
Definition
Ops.h.inc:633
llzk::function::FuncDefOp::hasAllowNonNativeFieldOpsAttr
bool hasAllowNonNativeFieldOpsAttr()
Return true iff the function def has the allow_non_native_field_ops attribute.
Definition
Ops.h.inc:833
llzk::function::FuncDefOp::hasAllowWitnessAttr
bool hasAllowWitnessAttr()
Return true iff the function def has the allow_witness attribute.
Definition
Ops.h.inc:825
llzk::function::FuncDefOp::hasAllowVerifOpsAttr
bool hasAllowVerifOpsAttr()
Return true iff the function def has the allow_verif_ops attribute.
Definition
Ops.h.inc:841
llzk::function::FuncDefOp::getOperationName
static constexpr ::llvm::StringLiteral getOperationName()
Definition
Ops.h.inc:679
llzk::function::FuncDefOp::hasAllowConstraintAttr
bool hasAllowConstraintAttr()
Return true iff the function def has the allow_constraint attribute.
Definition
Ops.h.inc:817
llzk::function
Definition
Ops.cpp:50
llzk::function::verifyNotFieldNativeTraitImpl
LogicalResult verifyNotFieldNativeTraitImpl(Operation *op)
Definition
OpTraits.cpp:66
llzk::function::verifyVerificationTraitImpl
LogicalResult verifyVerificationTraitImpl(Operation *op, llvm::function_ref< LogicalResult()> check)
Definition
OpTraits.cpp:73
llzk::function::verifyConstraintGenTraitImpl
LogicalResult verifyConstraintGenTraitImpl(Operation *op)
Definition
OpTraits.cpp:50
llzk::function::verifyWitnessGenTraitImpl
LogicalResult verifyWitnessGenTraitImpl(Operation *op)
Definition
OpTraits.cpp:58
mlir
Definition
ValueModel.h:30
lib
Dialect
Function
IR
OpTraits.cpp
Generated by
1.17.0
Copyright 2025 Veridise Inc. under the Apache License v2.0. Copyright 2026 Project LLZK under the Apache License v2.0.