LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Poly.cpp
Go to the documentation of this file.
1//===-- Poly.cpp - Polymorphic dialect C API impl ---------------*- 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/CAPI/Builder.h"
11#include "llzk/CAPI/Support.h"
16
17#include "llzk-c/Dialect/Poly.h"
18
19#include <mlir/CAPI/AffineExpr.h>
20#include <mlir/CAPI/AffineMap.h>
21#include <mlir/CAPI/Pass.h>
22#include <mlir/CAPI/Registration.h>
23#include <mlir/CAPI/Wrap.h>
24#include <mlir/IR/BuiltinAttributes.h>
25#include <mlir/Support/LLVM.h>
26
27#include <mlir-c/Pass.h>
28
29static void registerLLZKPolymorphicTransformationPasses() {
31}
32
33using namespace llzk;
34using namespace llzk::polymorphic;
35using namespace mlir;
36
37// Include impl for transformation passes
39
41 Polymorphic, llzk__polymorphic, llzk::polymorphic::PolymorphicDialect
42)
43
44//===----------------------------------------------------------------------===//
45// TypeVarType
46//===----------------------------------------------------------------------===//
47
48MlirType llzkTypeVarTypeGet(MlirContext ctx, MlirStringRef name) {
49 return wrap(TypeVarType::get(FlatSymbolRefAttr::get(StringAttr::get(unwrap(ctx), unwrap(name)))));
50}
51
52bool llzkTypeIsATypeVarType(MlirType type) { return llvm::isa<TypeVarType>(unwrap(type)); }
53
54MlirType llzkTypeVarTypeGetFromAttr(MlirContext /*ctx*/, MlirAttribute attrWrapper) {
55 auto attr = unwrap(attrWrapper);
56 if (auto sym = llvm::dyn_cast<FlatSymbolRefAttr>(attr)) {
57 return wrap(TypeVarType::get(sym));
58 }
59 return wrap(TypeVarType::get(FlatSymbolRefAttr::get(llvm::cast<StringAttr>(attr))));
60}
61
62MlirStringRef llzkTypeVarTypeGetNameRef(MlirType type) {
63 return wrap(llvm::cast<TypeVarType>(unwrap(type)).getRefName());
64}
65
66MlirAttribute llzkTypeVarTypeGetName(MlirType type) {
67 return wrap(llvm::cast<TypeVarType>(unwrap(type)).getNameRef());
68}
69
70//===----------------------------------------------------------------------===//
71// ApplyMapOp
72//===----------------------------------------------------------------------===//
73
74LLZK_DEFINE_OP_BUILD_METHOD(ApplyMapOp, MlirAttribute map, MlirValueRange mapOperands) {
75 SmallVector<Value> mapOperandsSto;
76 return wrap(
78 builder, location, llvm::cast<AffineMapAttr>(unwrap(map)),
79 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
80 )
81 );
82}
83
85 ApplyMapOp, WithAffineMap, MlirAffineMap map, MlirValueRange mapOperands
86) {
87 SmallVector<Value> mapOperandsSto;
88 return wrap(
90 builder, location, unwrap(map),
91 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
92 )
93 );
94}
95
97 ApplyMapOp, WithAffineExpr, MlirAffineExpr expr, MlirValueRange mapOperands
98) {
99 SmallVector<Value> mapOperandsSto;
100 return wrap(
102 builder, location, unwrap(expr),
103 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
104 )
105 );
106}
107
108bool llzkOperationIsAApplyMapOp(MlirOperation op) { return llvm::isa<ApplyMapOp>(unwrap(op)); }
109
111MlirAffineMap llzkApplyMapOpGetAffineMap(MlirOperation op) {
112 return wrap(mlir::unwrap_cast<ApplyMapOp>(op).getAffineMap());
113}
114
115static ValueRange dimOperands(MlirOperation op) {
116 return mlir::unwrap_cast<ApplyMapOp>(op).getDimOperands();
117}
118
119static ValueRange symbolOperands(MlirOperation op) {
120 return mlir::unwrap_cast<ApplyMapOp>(op).getSymbolOperands();
121}
122
123static void copyValues(ValueRange in, MlirValue *out) {
124 for (auto [n, value] : llvm::enumerate(in)) {
125 out[n] = wrap(value);
126 }
127}
128
130intptr_t llzkApplyMapOpGetNumDimOperands(MlirOperation op) {
131 return static_cast<intptr_t>(dimOperands(op).size());
132}
133
137void llzkApplyMapOpGetDimOperands(MlirOperation op, MlirValue *dst) {
138 copyValues(dimOperands(op), dst);
139}
140
142intptr_t llzkApplyMapOpGetNumSymbolOperands(MlirOperation op) {
143 return static_cast<intptr_t>(symbolOperands(op).size());
144}
145
149void llzkApplyMapOpGetSymbolOperands(MlirOperation op, MlirValue *dst) {
150 copyValues(symbolOperands(op), dst);
151}
MlirStringRef llzkTypeVarTypeGetNameRef(MlirType type)
Returns the var name of the TypeVarType as a StringRef.
Definition Poly.cpp:62
MlirAffineMap llzkApplyMapOpGetAffineMap(MlirOperation op)
Returns the affine map associated with the op.
Definition Poly.cpp:111
bool llzkTypeIsATypeVarType(MlirType type)
Definition Poly.cpp:52
intptr_t llzkApplyMapOpGetNumDimOperands(MlirOperation op)
Returns the number of operands that correspond to dimensions in the affine map.
Definition Poly.cpp:130
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Polymorphic, llzk__polymorphic, llzk::polymorphic::PolymorphicDialect) MlirType llzkTypeVarTypeGet(MlirContext ctx
MlirType llzkTypeVarTypeGetFromAttr(MlirContext, MlirAttribute attrWrapper)
Creates a llzk::polymorphic::TypeVarType from either a StringAttr or a FlatSymbolRefAttr.
Definition Poly.cpp:54
MlirAttribute llzkTypeVarTypeGetName(MlirType type)
Returns the var name of the TypeVarType as a FlatSymbolRefAttr.
Definition Poly.cpp:66
void llzkApplyMapOpGetDimOperands(MlirOperation op, MlirValue *dst)
Writes into the destination buffer the operands that correspond to dimensions in the affine map.
Definition Poly.cpp:137
MlirStringRef name
Definition Poly.cpp:48
intptr_t llzkApplyMapOpGetNumSymbolOperands(MlirOperation op)
Returns the number of operands that correspond to symbols in the affine map.
Definition Poly.cpp:142
bool llzkOperationIsAApplyMapOp(MlirOperation op)
Definition Poly.cpp:108
void llzkApplyMapOpGetSymbolOperands(MlirOperation op, MlirValue *dst)
Writes into the destination buffer the operands that correspond to symbols in the affine map.
Definition Poly.cpp:149
MLIR_CAPI_EXPORTED MlirType llzkTypeVarTypeGet(MlirContext context, MlirStringRef value)
Creates a llzk::polymorphic::TypeVarType.
static TypeVarType get(::mlir::MLIRContext *context, ::mlir::FlatSymbolRefAttr nameRef)
Definition Types.cpp.inc:67
#define LLZK_DEFINE_OP_BUILD_METHOD(op,...)
Definition Support.h:27
#define LLZK_DEFINE_SUFFIX_OP_BUILD_METHOD(op, suffix,...)
Definition Support.h:25
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
Definition Builder.h:41
auto unwrap_cast(auto &from)
Definition Support.h:30
MlirValue const * values
Definition Support.h:56
intptr_t size
Definition Support.h:57