LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
CommonAttrOrTypeCAPITestGen.cpp
Go to the documentation of this file.
1//===- CommonAttrOrTypeCAPITestGen.cpp - 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//===----------------------------------------------------------------------===//
9//
10// Implementation of CAPI test generation utilities for Attribute and Type.
11//
12//===----------------------------------------------------------------------===//
13
15
16#include <llvm/Support/FormatVariadic.h>
17
18using namespace mlir;
19using namespace mlir::tblgen;
20
22std::string generateDummyParamsForAttrOrTypeGet(const AttrOrTypeDef &def, bool isType) {
23 // Use raw_string_ostream for efficient string building
24 std::string paramsBuffer;
25 // Reserve approximate space: ~80 chars per parameter
26 paramsBuffer.reserve(80 * def.getParameters().size());
27 llvm::raw_string_ostream paramsStream(paramsBuffer);
28
29 for (const auto &param : def.getParameters()) {
30 // Cache the string conversions to avoid repeated calls
31 const StringRef cppType = param.getCppType();
32 const StringRef pName = param.getName();
33
34 if (isArrayRefType(cppType)) {
35 paramsStream << llvm::formatv(" intptr_t {0}Count = 0;\n", pName);
36 const StringRef cppElemType = extractArrayRefElementType(cppType);
37 const std::string elemType = mapCppTypeToCapiType(cppElemType);
38 if (isPrimitiveType(cppElemType)) {
39 paramsStream << llvm::formatv(" {0} {1}Array = 0;\n", elemType, pName);
40 paramsStream << llvm::formatv(" {0} *{1} = &{1}Array;\n", elemType, pName);
41 } else if (isType && elemType == "MlirType") {
42 paramsStream << llvm::formatv(" auto {0}Elem = createIndexType();\n", pName);
43 paramsStream << llvm::formatv(" {0} *{1} = &{0}Elem;\n", elemType, pName);
44 } else if (!isType && elemType == "MlirAttribute") {
45 paramsStream << llvm::formatv(" auto {0}Elem = createIndexAttribute();\n", pName);
46 paramsStream << llvm::formatv(" {0} *{1} = &{0}Elem;\n", elemType, pName);
47 } else {
48 paramsStream << llvm::formatv(" {0} {1}Elem = {{}};\n", elemType, pName);
49 paramsStream << llvm::formatv(" {0} *{1} = &{1}Elem;\n", elemType, pName);
50 }
51 } else {
52 const std::string capiType = mapCppTypeToCapiType(cppType);
53 if (isPrimitiveType(cppType)) {
54 paramsStream << llvm::formatv(" {0} {1} = 0;\n", capiType, pName);
55 } else if (isType && capiType == "MlirType") {
56 paramsStream << llvm::formatv(" auto {0} = createIndexType();\n", pName);
57 } else if (!isType && capiType == "MlirAttribute") {
58 paramsStream << llvm::formatv(" auto {0} = createIndexAttribute();\n", pName);
59 } else {
60 // For enum or other types, use static_cast to initialize with 0
61 paramsStream << llvm::formatv(" {0} {1} = static_cast<{0}>(0);\n", capiType, pName);
62 }
63 }
64 }
65
66 return paramsBuffer;
67}
68
70std::string generateParamListForAttrOrTypeGet(const AttrOrTypeDef &def) {
71 // Use raw_string_ostream for efficient string building
72 std::string paramsBuffer;
73 // Reserve approximate space: ~30 chars per parameter
74 paramsBuffer.reserve(30 * def.getParameters().size());
75 llvm::raw_string_ostream paramsStream(paramsBuffer);
76
77 for (const auto &param : def.getParameters()) {
78 // Cache the string conversion to avoid repeated calls
79 const StringRef pName = param.getName();
80 if (isArrayRefType(param.getCppType())) {
81 paramsStream << llvm::formatv(", {0}Count, {0}", pName);
82 } else {
83 paramsStream << llvm::formatv(", {0}", pName);
84 }
85 }
86
87 return paramsBuffer;
88}
std::string generateParamListForAttrOrTypeGet(const AttrOrTypeDef &def)
Generate parameter list for Get builder call.
std::string generateDummyParamsForAttrOrTypeGet(const AttrOrTypeDef &def, bool isType)
Generate dummy parameters for Get builder.
This file provides common utilities for generating C API link tests for attributes and types.
std::string mapCppTypeToCapiType(StringRef cppType)
mlir::StringRef extractArrayRefElementType(mlir::StringRef cppType)
Extract element type from ArrayRef<...>
bool isPrimitiveType(mlir::StringRef cppType)
Check if a C++ type is a known primitive type.
bool isArrayRefType(mlir::StringRef cppType)
Check if a C++ type is an ArrayRef type.