18#include <mlir/CAPI/IR.h>
19#include <mlir/CAPI/Registration.h>
20#include <mlir/CAPI/Support.h>
21#include <mlir/CAPI/Wrap.h>
22#include <mlir/IR/Attributes.h>
23#include <mlir/IR/Diagnostics.h>
24#include <mlir/Support/LLVM.h>
28#include <llvm/ADT/STLExtras.h>
29#include <llvm/ADT/SmallVectorExtras.h>
37MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(POD, llzk__pod,
PODDialect)
41static SmallVector<RecordValue>
43 return llvm::map_to_vector(ArrayRef(values, nValues), [](
const auto &record) {
44 return RecordValue {.name = unwrap(record.name), .value = unwrap(record.value)};
55 auto t = unwrap(type);
56 return wrap(RecordAttr::get(t.getContext(), StringAttr::get(t.getContext(), unwrap(name)), t));
77MlirType
llzkPodTypeGet(MlirContext context, intptr_t nRecords, MlirAttribute
const *records) {
78 SmallVector<Attribute> recordsSto;
79 auto recordAttrs = llvm::map_to_vector(unwrapList(nRecords, records, recordsSto), [](
auto attr) {
80 return mlir::cast<RecordAttr>(attr);
88 auto initialValues = fromRawRecordValues(nRecords, records);
100 MutableArrayRef<MlirAttribute> dstRef(dst, records.size());
101 llvm::transform(records, dstRef.begin(), [](
auto record) { return wrap(record); });
110lookupRecordImpl(
PodType type, StringRef name, llvm::function_ref<InFlightDiagnostic()> emitError) {
111 auto attr = type.
getRecord(name, emitError);
113 return MlirType {.ptr =
nullptr};
121 return lookupRecordImpl(
pod, unwrap(name), [
pod] {
122 auto *ctx =
pod.getContext();
123 return ctx->getDiagEngine().emit(Builder(ctx).
getUnknownLoc(), DiagnosticSeverity::Error);
130 return lookupRecordImpl(
pod, unwrap(name), [
pod, loc] {
131 return pod.getContext()->getDiagEngine().emit(unwrap(loc), DiagnosticSeverity::Error);
138 return unwrap(op)->emitError();
149 auto recordValues = fromRawRecordValues(nValues, values);
156 auto recordValues = fromRawRecordValues(nValues, values);
164 auto recordValues = fromRawRecordValues(nValues, values);
bool llzkOperationIsANewPodOp(MlirOperation op)
bool llzkTypeIsAPodType(MlirType type)
MlirAttribute llzkRecordAttrGetNameSym(MlirAttribute attr)
Returns the name of the record as a flat symbol attribute.
MlirType llzkPodTypeLookupRecordWithinLocation(MlirType type, MlirStringRef name, MlirLocation loc)
Lookups a record type by name.
MlirAttribute llzkPodTypeGetNthRecord(MlirType type, intptr_t n)
Returns the n-th record in the struct.
MlirStringRef llzkRecordAttrGetName(MlirAttribute attr)
Returns the name of the record.
MlirType llzkPodTypeGetFromInitialValues(MlirContext context, intptr_t nRecords, LlzkRecordValue const *records)
Creates an llzk::pod::PodType using a list of values for inferring the records.
MlirType llzkPodTypeGet(MlirContext context, intptr_t nRecords, MlirAttribute const *records)
Creates an llzk::pod::PodType using a list of attributes as records.
void llzkPodTypeGetRecords(MlirType type, MlirAttribute *dst)
Writes the records into the given array that must have been previously allocated with enough space.
MlirType llzkPodTypeLookupRecordWithinOperation(MlirType type, MlirStringRef name, MlirOperation op)
Lookups a record type by name.
MlirAttribute llzkRecordAttrGet(MlirStringRef name, MlirType type)
Creates a new llzk::pod::RecordAttr.
bool llzkOperationIsAReadPodOp(MlirOperation op)
MlirType llzkRecordAttrGetType(MlirAttribute attr)
Returns the type of the record.
bool llzkOperationIsAWritePodOp(MlirOperation op)
intptr_t llzkPodTypeGetNumRecords(MlirType type)
Returns the number of records in the struct.
MlirType llzkPodTypeLookupRecord(MlirType type, MlirStringRef name)
Lookups a record type by name.
bool llzkAttributeIsARecordAttr(MlirAttribute attr)
MlirAttribute llzkAffineMapOperandsBuilderGetDimsPerMapAttr(LlzkAffineMapOperandsBuilder builder, MlirContext context)
Returns the number of dimensions per map represented as an attribute.
Helper for unwrapping the C arguments for the map operands.
::llvm::FailureOr<::mlir::Type > getRecord(::llvm::StringRef name, ::llvm::function_ref<::mlir::InFlightDiagnostic()>) const
Searches a record by name.
static PodType fromInitialValues(::mlir::MLIRContext *ctx, InitializedRecords init)
Creates a new type from a set of initialized records.
static PodType get(::mlir::MLIRContext *context, ::llvm::ArrayRef<::llzk::pod::RecordAttr > records)
#define LLZK_DEFINE_OP_BUILD_METHOD(op,...)
#define LLZK_DEFINE_SUFFIX_OP_BUILD_METHOD(op, suffix,...)
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
mlir::Location getUnknownLoc(mlir::MLIRContext *context)
auto unwrap_cast(auto &from)
Encapsulates the arguments related to affine maps that are common in operation constructors that supp...
MlirValueRange * mapOperands