LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
CommonAttrOrTypeCAPITestGen.h
Go to the documentation of this file.
1//===- CommonAttrOrTypeCAPITestGen.h - Common test generation utilities ---===//
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//===----------------------------------------------------------------------===//
40//===----------------------------------------------------------------------===//
41
42#pragma once
43
44#include "CommonCAPIGen.h"
45
46#include <mlir/TableGen/AttrOrTypeDef.h>
47#include <mlir/TableGen/Dialect.h>
48
49#include <llvm/ADT/StringExtras.h>
50#include <llvm/Support/FormatVariadic.h>
51#include <llvm/Support/raw_ostream.h>
52
62std::string
63generateDummyParamsForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def, bool isType);
64
72std::string generateParamListForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def);
73
79 using TestGenerator::TestGenerator;
80
81 ~AttrOrTypeTestGenerator() override = default;
82
85 void setParamName(mlir::StringRef name) {
86 this->paramName = name;
88 }
89
93 virtual void
94 genGetBuilderTest(const std::string &dummyParams, const std::string &paramList) const {
95 static constexpr char fmt[] = R"(
97TEST_F({2}{1}LinkTests, Get_{3}) {{
98 auto test{1} = createIndex{1}();
99
100 // We only verify the function compiles and links, wrapped in an unreachable condition
101 if ({0}{1}IsA_{2}_{3}(test{1})) {{
102{4}
103 (void){0}{2}_{3}Get(context{5});
104 }
105}
106)";
107 assert(!className.empty() && "className must be set");
108 os << llvm::formatv(
109 fmt,
110 getFunctionPrefix(), // {0}
111 kind, // {1}
113 className, // {3}
114 dummyParams, // {4}
115 paramList // {5}
116 );
117 }
118
120 virtual void genParamGetterTest(bool isInteger = false) const {
121 static constexpr char fmt[] = R"(
123TEST_F({2}{1}LinkTests, Get_{3}_{4}) {{
124 auto test{1} = createIndex{1}();
125
126 if ({0}{1}IsA_{2}_{3}(test{1})) {{
127 (void){0}{2}_{3}Get{5}(test{1}{6});
128 }
129}
130)";
131 assert(!className.empty() && "className must be set");
132 assert(!paramName.empty() && "paramName must be set");
133 os << llvm::formatv(
135 paramNameCapitalized, isInteger ? ", [](MlirStringRef, void *) {}, nullptr" : ""
136 );
137 }
138
140 virtual void genArrayRefParamCountTest() const {
141 static constexpr char fmt[] = R"(
143TEST_F({2}{1}LinkTests, Get_{3}_{4}Count) {{
144 auto test{1} = createIndex{1}();
145
146 if ({0}{1}IsA_{2}_{3}(test{1})) {{
147 (void){0}{2}_{3}Get{5}Count(test{1});
148 }
149}
150)";
151 assert(!className.empty() && "className must be set");
152 assert(!paramName.empty() && "paramName must be set");
153 os << llvm::formatv(
156 );
157 }
158
160 virtual void genArrayRefParamAtTest() const {
161 static constexpr char fmt[] = R"(
163TEST_F({2}{1}LinkTests, Get_{3}_{4}At) {{
164 auto test{1} = createIndex{1}();
165
166 if ({0}{1}IsA_{2}_{3}(test{1})) {{
167 (void){0}{2}_{3}Get{5}At(test{1}, 0);
168 }
169}
170)";
171 assert(!className.empty() && "className must be set");
172 assert(!paramName.empty() && "paramName must be set");
173 os << llvm::formatv(
176 );
177 }
178
179 void genCompleteRecord(const mlir::tblgen::AttrOrTypeDef &def, bool isType) {
180 mlir::tblgen::Dialect defDialect = def.getDialect();
181
182 // Generate for the selected dialect only
183 if (defDialect.getName() != getDialectName()) {
184 return;
185 }
186
187 this->setNamespaceAndClassName(defDialect, def.getCppClassName());
189 // Generate IsA test
190 if (getGenIsA()) {
191 this->genIsATest();
192 }
193
194 // Generate Get builder test
195 if (getGenTypeOrAttrGet() && !def.skipDefaultBuilders()) {
196 std::string dummyParams = generateDummyParamsForAttrOrTypeGet(def, isType);
197 std::string paramList = generateParamListForAttrOrTypeGet(def);
198 this->genGetBuilderTest(dummyParams, paramList);
199 }
200
201 // Generate parameter getter tests
203 for (const auto &param : def.getParameters()) {
204 this->setParamName(param.getName());
205 mlir::StringRef cppType = param.getCppType();
206 if (isArrayRefType(cppType)) {
209 } else {
211 }
212 }
213 }
214
215 // Generate extra class method tests
217 std::optional<mlir::StringRef> extraDecls = def.getExtraDecls();
218 if (extraDecls.has_value()) {
219 this->genExtraMethods(extraDecls.value());
220 }
221 }
222 }
223
224protected:
225 mlir::StringRef paramName;
226 std::string paramNameCapitalized;
227};
std::string generateDummyParamsForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def, bool isType)
Generate dummy parameters for Get builder (used by both Attr and Type).
std::string generateParamListForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def)
Generate parameter list for Get builder call (used by both Attr and Type).
llvm::cl::opt< std::string > & getFunctionPrefix()
Return the FunctionPrefix option, registering it on first access.
llvm::cl::opt< std::string > & getDialectName()
Return the DialectName option, registering it on first access.
llvm::cl::opt< bool > & getGenExtraClassMethods()
Return the GenExtraClassMethods option, registering it on first access.
llvm::cl::opt< bool > & getGenTypeOrAttrParamGetters()
Return the GenTypeOrAttrParamGetters option, registering it on first access.
llvm::cl::opt< bool > & getGenIsA()
Return the GenIsA option, registering it on first access.
llvm::cl::opt< bool > & getGenTypeOrAttrGet()
Return the GenTypeOrAttrGet option, registering it on first access.
bool isDynamicAPIntType(mlir::StringRef cppType)
Whether a parameter represents an arbitrary-precision mathematical integer.
bool isArrayRefType(mlir::StringRef cppType)
Check if a C++ type is an ArrayRef type.
std::string toPascalCase(mlir::StringRef str)
Convert names separated by underscore or colon to PascalCase.
Base class for attribute and type test generators.
virtual void genArrayRefParamCountTest() const
Generate ArrayRef parameter count getter test.
void setParamName(mlir::StringRef name)
Set the parameter name for code generation.
virtual void genGetBuilderTest(const std::string &dummyParams, const std::string &paramList) const
Generate Get builder test for a definition.
virtual void genParamGetterTest(bool isInteger=false) const
Generate parameter getter test.
~AttrOrTypeTestGenerator() override=default
virtual void genArrayRefParamAtTest() const
Generate ArrayRef parameter element getter test.
void genCompleteRecord(const mlir::tblgen::AttrOrTypeDef &def, bool isType)
mlir::StringRef className
virtual void genExtraMethods(mlir::StringRef extraDecl) const
Generate code for extra methods from an extraClassDeclaration.
virtual void setNamespaceAndClassName(const mlir::tblgen::Dialect &d, mlir::StringRef cppClassName)
Set the dialect and class name for code generation.
std::string dialectNameCapitalized
llvm::raw_ostream & os
std::string kind
Generator for common test implementation file elements.
virtual void genIsATest() const
Generate IsA test for a class.