LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Polymorphic.cpp
Go to the documentation of this file.
1//===-- Polymorphic.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
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 mlir::isa<TypeVarType>(unwrap(type)); }
53
54MlirType llzkTypeVarTypeGetFromAttr(MlirContext ctx, MlirAttribute attrWrapper) {
55 auto attr = unwrap(attrWrapper);
56 if (auto sym = mlir::dyn_cast<FlatSymbolRefAttr>(attr)) {
57 return wrap(TypeVarType::get(sym));
58 }
59 return wrap(TypeVarType::get(FlatSymbolRefAttr::get(mlir::cast<StringAttr>(attr))));
60}
61
62MlirStringRef llzkTypeVarTypeGetNameRef(MlirType type) {
63 return wrap(mlir::cast<TypeVarType>(unwrap(type)).getRefName());
64}
65
66MlirAttribute llzkTypeVarTypeGetName(MlirType type) {
67 return wrap(mlir::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(create<ApplyMapOp>(
77 builder, location, mlir::cast<AffineMapAttr>(unwrap(map)),
78 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
79 ));
80}
81
83 ApplyMapOp, WithAffineMap, MlirAffineMap map, MlirValueRange mapOperands
84) {
85 SmallVector<Value> mapOperandsSto;
86 return wrap(create<ApplyMapOp>(
87 builder, location, unwrap(map),
88 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
89 ));
90}
91
93 ApplyMapOp, WithAffineExpr, MlirAffineExpr expr, MlirValueRange mapOperands
94) {
95 SmallVector<Value> mapOperandsSto;
96 return wrap(create<ApplyMapOp>(
97 builder, location, unwrap(expr),
98 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
99 ));
100}
101
102bool llzkOperationIsAApplyMapOp(MlirOperation op) { return mlir::isa<ApplyMapOp>(unwrap(op)); }
103
105MlirAffineMap llzkApplyMapOpGetAffineMap(MlirOperation op) {
106 return wrap(mlir::unwrap_cast<ApplyMapOp>(op).getAffineMap());
107}
108
109static ValueRange dimOperands(MlirOperation op) {
110 return mlir::unwrap_cast<ApplyMapOp>(op).getDimOperands();
111}
112
113static ValueRange symbolOperands(MlirOperation op) {
114 return mlir::unwrap_cast<ApplyMapOp>(op).getSymbolOperands();
115}
116
117static void copyValues(ValueRange in, MlirValue *out) {
118 for (auto [n, value] : llvm::enumerate(in)) {
119 out[n] = wrap(value);
120 }
121}
122
124intptr_t llzkApplyMapOpGetNumDimOperands(MlirOperation op) {
125 return static_cast<intptr_t>(dimOperands(op).size());
126}
127
131void llzkApplyMapOpGetDimOperands(MlirOperation op, MlirValue *dst) {
132 copyValues(dimOperands(op), dst);
133}
134
136intptr_t llzkApplyMapOpGetNumSymbolOperands(MlirOperation op) {
137 return static_cast<intptr_t>(symbolOperands(op).size());
138}
139
143void llzkApplyMapOpGetSymbolOperands(MlirOperation op, MlirValue *dst) {
144 copyValues(symbolOperands(op), dst);
145}
MlirStringRef llzkTypeVarTypeGetNameRef(MlirType type)
Returns the var name of the TypeVarType as a StringRef.
MlirType llzkTypeVarTypeGetFromAttr(MlirContext ctx, MlirAttribute attrWrapper)
Creates a llzk::polymorphic::TypeVarType from either a StringAttr or a FlatSymbolRefAttr.
MlirAffineMap llzkApplyMapOpGetAffineMap(MlirOperation op)
Returns the affine map associated with the op.
bool llzkTypeIsATypeVarType(MlirType type)
intptr_t llzkApplyMapOpGetNumDimOperands(MlirOperation op)
Returns the number of operands that correspond to dimensions in the affine map.
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Polymorphic, llzk__polymorphic, llzk::polymorphic::PolymorphicDialect) MlirType llzkTypeVarTypeGet(MlirContext ctx
MlirAttribute llzkTypeVarTypeGetName(MlirType type)
Returns the var name of the TypeVarType as a FlatSymbolRefAttr.
void llzkApplyMapOpGetDimOperands(MlirOperation op, MlirValue *dst)
Writes into the destination buffer the operands that correspond to dimensions in the affine map.
MlirStringRef name
intptr_t llzkApplyMapOpGetNumSymbolOperands(MlirOperation op)
Returns the number of operands that correspond to symbols in the affine map.
bool llzkOperationIsAApplyMapOp(MlirOperation op)
void llzkApplyMapOpGetSymbolOperands(MlirOperation op, MlirValue *dst)
Writes into the destination buffer the operands that correspond to symbols in the affine map.
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