LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
OpCAPIParamHelper.cpp
Go to the documentation of this file.
1//===- OpCAPIParamHelper.cpp ----------------------------------------------===//
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 "OpCAPIParamHelper.h"
11
12#include "CommonCAPIGen.h"
13
14#include <mlir/TableGen/Interfaces.h>
15#include <mlir/TableGen/Operator.h>
16
17#include <llvm/ADT/StringSet.h>
18#include <llvm/Support/FormatVariadic.h>
19
20llvm::SmallVector<ExtraMethod> getCAPIExposedOpMethods(const mlir::tblgen::Operator &op) {
21 llvm::SmallVector<ExtraMethod> methods = parseExtraMethods(op.getExtraClassDeclaration());
22 llvm::StringSet<> methodNames;
23 for (const ExtraMethod &method : methods) {
24 methodNames.insert(method.methodName);
25 }
26
27 for (const mlir::tblgen::Trait &traitDef : op.getTraits()) {
28 const auto *trait = llvm::dyn_cast<mlir::tblgen::InterfaceTrait>(&traitDef);
29 if (!trait || !trait->shouldDeclareMethods()) {
30 continue;
31 }
32
33 llvm::StringSet<> requestedMethods;
34 for (llvm::StringRef methodName : trait->getAlwaysDeclaredMethods()) {
35 requestedMethods.insert(methodName);
36 }
37 mlir::tblgen::Interface interface = trait->getInterface();
38 for (const mlir::tblgen::InterfaceMethod &interfaceMethod : interface.getMethods()) {
39 // `alwaysOverriddenMethods` is the explicit C API opt-in. Keep the remaining declaration
40 // checks in sync with MLIR's OpEmitter::genOpInterfaceMethods().
41 if (!requestedMethods.contains(interfaceMethod.getName()) || interfaceMethod.isStatic() ||
42 interfaceMethod.getBody()) {
43 continue;
44 }
45
46 if (!methodNames.insert(interfaceMethod.getName()).second) {
47 warnSkipped(interfaceMethod.getName(), "C API does not support method overloading");
48 continue;
49 }
50
51 ExtraMethod method;
52 method.returnType = interfaceMethod.getReturnType().str();
53 method.methodName = interfaceMethod.getName().str();
54 for (const auto &argument : interfaceMethod.getArguments()) {
55 method.parameters.emplace_back(argument.type.str(), argument.name.str());
56 }
57 methods.push_back(std::move(method));
58 }
59 }
60
61 return methods;
62}
63
64std::string GenStringFromOpPieces::gen(const mlir::tblgen::Operator &op) {
65 std::string params;
66 llvm::raw_string_ostream oss(params);
67 genHeader(oss);
68 if (!op.allResultTypesKnown()) {
69 // If result types are not inferred, call handler for each result
70 for (auto [i, result] : llvm::enumerate(op.getResults())) {
71 llvm::StringRef name = result.name;
72 genResult(oss, result, name.empty() ? llvm::formatv("result{0}", i).str() : name.str());
73 }
74 } else {
75 // Otherewise, call inferred result handler
77 }
78 for (const mlir::tblgen::NamedTypeConstraint &operand : op.getOperands()) {
79 genOperand(oss, operand);
80 }
81 {
82 auto attrs = op.getAttributes();
83 if (!attrs.empty()) {
84 genAttributesPrefix(oss, op);
85 for (const mlir::tblgen::NamedAttribute &namedAttr : attrs) {
86 genAttribute(oss, namedAttr);
87 }
88 genAttributesSuffix(oss, op);
89 }
90 }
91 {
92 auto regions = op.getRegions();
93 if (!regions.empty()) {
94 genRegionsPrefix(oss, op);
95 for (const mlir::tblgen::NamedRegion &region : regions) {
96 genRegion(oss, region);
97 }
98 genRegionsSuffix(oss, op);
99 }
100 }
101 return params;
102}
SmallVector< ExtraMethod > parseExtraMethods(StringRef extraDecl)
Parse method declarations from extraClassDeclaration using Clang's Lexer.
llvm::SmallVector< ExtraMethod > getCAPIExposedOpMethods(const mlir::tblgen::Operator &op)
Return public operation methods explicitly requested by DeclareOpInterfaceMethods traits and declared...
Structure to represent a parsed method signature from an extraClassDeclaration.
std::string returnType
The C++ return type of the method.
virtual void genRegionsSuffix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate region section suffix code to os. Default does nothing.
virtual void genOperand(llvm::raw_ostream &os, const mlir::tblgen::NamedTypeConstraint &operand)=0
Generate code for operand to os.
std::string gen(const mlir::tblgen::Operator &op)
Generate string from the operation pieces.
virtual void genAttributesSuffix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate attribute section suffix code to os. Default does nothing.
virtual void genResultInferred(llvm::raw_ostream &os)
Generate code to os when result type is inferred. Default does nothing.
virtual void genHeader(llvm::raw_ostream &os)
Generate header code to os. Default does nothing.
virtual void genRegion(llvm::raw_ostream &os, const mlir::tblgen::NamedRegion &region)=0
Generate code for region to os.
virtual void genAttributesPrefix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate attribute section prefix code to os. Default does nothing.
virtual void genResult(llvm::raw_ostream &os, const mlir::tblgen::NamedTypeConstraint &result, const std::string &resultName)=0
Generate code for result to os.
virtual void genAttribute(llvm::raw_ostream &os, const mlir::tblgen::NamedAttribute &attr)=0
Generate code for attr to os.
virtual void genRegionsPrefix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate region section prefix code to os. Default does nothing.