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
29using namespace mlir;
30using namespace llzk;
31using namespace llzk::polymorphic;
32
33static void registerLLZKPolymorphicTransformationPasses() { registerTransformationPasses(); }
34
35// Include impl for transformation passes
37
38MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Polymorphic, llzk__polymorphic, PolymorphicDialect)
39
40//===----------------------------------------------------------------------===//
41// TypeVarType
42//===----------------------------------------------------------------------===//
43
44MlirType llzkTypeVarTypeGet(MlirContext ctx, MlirStringRef name) {
45 return wrap(TypeVarType::get(FlatSymbolRefAttr::get(StringAttr::get(unwrap(ctx), unwrap(name)))));
46}
47
48bool llzkTypeIsATypeVarType(MlirType type) { return llvm::isa<TypeVarType>(unwrap(type)); }
49
50MlirType llzkTypeVarTypeGetFromAttr(MlirContext /*ctx*/, MlirAttribute attrWrapper) {
51 auto attr = unwrap(attrWrapper);
52 if (auto sym = llvm::dyn_cast<FlatSymbolRefAttr>(attr)) {
53 return wrap(TypeVarType::get(sym));
54 }
55 return wrap(TypeVarType::get(FlatSymbolRefAttr::get(llvm::cast<StringAttr>(attr))));
56}
57
58MlirStringRef llzkTypeVarTypeGetNameRef(MlirType type) {
59 return wrap(llvm::cast<TypeVarType>(unwrap(type)).getRefName());
60}
61
62MlirAttribute llzkTypeVarTypeGetName(MlirType type) {
63 return wrap(llvm::cast<TypeVarType>(unwrap(type)).getNameRef());
64}
65
66//===----------------------------------------------------------------------===//
67// ApplyMapOp
68//===----------------------------------------------------------------------===//
69
70LLZK_DEFINE_OP_BUILD_METHOD(ApplyMapOp, MlirAttribute map, MlirValueRange mapOperands) {
71 SmallVector<Value> mapOperandsSto;
72 return wrap(
74 builder, location, llvm::cast<AffineMapAttr>(unwrap(map)),
75 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
76 )
77 );
78}
79
81 ApplyMapOp, WithAffineMap, MlirAffineMap map, MlirValueRange mapOperands
82) {
83 SmallVector<Value> mapOperandsSto;
84 return wrap(
86 builder, location, unwrap(map),
87 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
88 )
89 );
90}
91
93 ApplyMapOp, WithAffineExpr, MlirAffineExpr expr, MlirValueRange mapOperands
94) {
95 SmallVector<Value> mapOperandsSto;
96 return wrap(
98 builder, location, unwrap(expr),
99 ValueRange(unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto))
100 )
101 );
102}
103
104bool llzkOperationIsAApplyMapOp(MlirOperation op) { return llvm::isa<ApplyMapOp>(unwrap(op)); }
105
107MlirAffineMap llzkApplyMapOpGetAffineMap(MlirOperation op) {
108 return wrap(unwrap_cast<ApplyMapOp>(op).getAffineMap());
109}
110
111static ValueRange dimOperands(MlirOperation op) {
112 return unwrap_cast<ApplyMapOp>(op).getDimOperands();
113}
114
115static ValueRange symbolOperands(MlirOperation op) {
116 return unwrap_cast<ApplyMapOp>(op).getSymbolOperands();
117}
118
119static void copyValues(ValueRange in, MlirValue *out) {
120 for (auto [n, value] : llvm::enumerate(in)) {
121 out[n] = wrap(value);
122 }
123}
124
126intptr_t llzkApplyMapOpGetNumDimOperands(MlirOperation op) {
127 return static_cast<intptr_t>(dimOperands(op).size());
128}
129
133void llzkApplyMapOpGetDimOperands(MlirOperation op, MlirValue *dst) {
134 copyValues(dimOperands(op), dst);
135}
136
138intptr_t llzkApplyMapOpGetNumSymbolOperands(MlirOperation op) {
139 return static_cast<intptr_t>(symbolOperands(op).size());
140}
141
145void llzkApplyMapOpGetSymbolOperands(MlirOperation op, MlirValue *dst) {
146 copyValues(symbolOperands(op), dst);
147}
MlirStringRef llzkTypeVarTypeGetNameRef(MlirType type)
Returns the var name of the TypeVarType as a StringRef.
Definition Poly.cpp:58
MlirAffineMap llzkApplyMapOpGetAffineMap(MlirOperation op)
Returns the affine map associated with the op.
Definition Poly.cpp:107
bool llzkTypeIsATypeVarType(MlirType type)
Definition Poly.cpp:48
intptr_t llzkApplyMapOpGetNumDimOperands(MlirOperation op)
Returns the number of operands that correspond to dimensions in the affine map.
Definition Poly.cpp:126
MlirType llzkTypeVarTypeGetFromAttr(MlirContext, MlirAttribute attrWrapper)
Creates a llzk::polymorphic::TypeVarType from either a StringAttr or a FlatSymbolRefAttr.
Definition Poly.cpp:50
MlirType llzkTypeVarTypeGet(MlirContext ctx, MlirStringRef name)
Creates a llzk::polymorphic::TypeVarType.
Definition Poly.cpp:44
MlirAttribute llzkTypeVarTypeGetName(MlirType type)
Returns the var name of the TypeVarType as a FlatSymbolRefAttr.
Definition Poly.cpp:62
void llzkApplyMapOpGetDimOperands(MlirOperation op, MlirValue *dst)
Writes into the destination buffer the operands that correspond to dimensions in the affine map.
Definition Poly.cpp:133
intptr_t llzkApplyMapOpGetNumSymbolOperands(MlirOperation op)
Returns the number of operands that correspond to symbols in the affine map.
Definition Poly.cpp:138
bool llzkOperationIsAApplyMapOp(MlirOperation op)
Definition Poly.cpp:104
void llzkApplyMapOpGetSymbolOperands(MlirOperation op, MlirValue *dst)
Writes into the destination buffer the operands that correspond to symbols in the affine map.
Definition Poly.cpp:145
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
void registerTransformationPasses()
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