13 #ifndef CIRCT_DIALECT_OM_EVALUATOR_EVALUATOR_H
14 #define CIRCT_DIALECT_OM_EVALUATOR_EVALUATOR_H
18 #include "mlir/IR/BuiltinOps.h"
19 #include "mlir/IR/Diagnostics.h"
20 #include "mlir/IR/Location.h"
21 #include "mlir/IR/MLIRContext.h"
22 #include "mlir/IR/SymbolTable.h"
23 #include "mlir/Support/LogicalResult.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallString.h"
78 if (isa<UnknownLoc>(
loc))
106 value = std::move(newValue);
115 llvm::SmallPtrSet<ReferenceValue *, 4> visited;
116 auto currentValue =
value;
117 while (
auto *v = dyn_cast<ReferenceValue>(currentValue.get())) {
119 if (!visited.insert(v).second)
121 currentValue = v->getValue();
123 return success(currentValue);
148 template <
typename AttrTy>
150 return dyn_cast<AttrTy>(
attr);
171 if (failed(value->finalize()))
173 if (
auto *ref = llvm::dyn_cast<ReferenceValue>(value.get())) {
174 auto v = ref->getStrippedValue();
232 void setElements(DenseMap<Attribute, EvaluatorValuePtr> newElements) {
272 fields = std::move(newFields);
278 auto clsConst =
const_cast<ClassOp &
>(
cls);
291 FailureOr<EvaluatorValuePtr>
getField(StringAttr field);
292 FailureOr<EvaluatorValuePtr>
getField(StringRef field) {
312 elements(std::move(tupleElements)) {
414 SmallVector<EvaluatorValuePtr>
416 ArrayRef<Attribute> attributes);
425 FailureOr<evaluator::EvaluatorValuePtr>
426 instantiate(StringAttr className, ArrayRef<EvaluatorValuePtr> actualParams);
431 FailureOr<evaluator::EvaluatorValuePtr>
435 SmallVectorImpl<std::shared_ptr<evaluator::EvaluatorValue>> *;
445 auto val =
objects.lookup(key);
446 return val && val->isFullyEvaluated();
449 FailureOr<EvaluatorValuePtr>
451 FailureOr<EvaluatorValuePtr>
457 FailureOr<EvaluatorValuePtr>
465 FailureOr<EvaluatorValuePtr>
468 FailureOr<EvaluatorValuePtr>
473 FailureOr<EvaluatorValuePtr>
475 Location loc,
ObjectKey instanceKey = {});
476 FailureOr<EvaluatorValuePtr>
478 FailureOr<EvaluatorValuePtr>
487 FailureOr<EvaluatorValuePtr>
490 FailureOr<EvaluatorValuePtr>
492 FailureOr<evaluator::EvaluatorValuePtr>
495 FailureOr<evaluator::EvaluatorValuePtr>
498 FailureOr<evaluator::EvaluatorValuePtr>
501 FailureOr<evaluator::EvaluatorValuePtr>
505 FailureOr<ActualParameters>
515 std::unique_ptr<SmallVector<std::shared_ptr<evaluator::EvaluatorValue>>>>
523 DenseMap<ObjectKey, std::shared_ptr<evaluator::EvaluatorValue>>
objects;
527 static inline mlir::Diagnostic &
530 if (
auto *attr = llvm::dyn_cast<evaluator::AttributeValue>(&evaluatorValue))
531 diag << attr->getAttr();
532 else if (
auto *
object =
533 llvm::dyn_cast<evaluator::ObjectValue>(&evaluatorValue))
534 diag <<
"Object(" <<
object->getType() <<
")";
535 else if (
auto *list = llvm::dyn_cast<evaluator::ListValue>(&evaluatorValue))
536 diag <<
"List(" << list->getType() <<
")";
537 else if (
auto *map = llvm::dyn_cast<evaluator::MapValue>(&evaluatorValue))
538 diag <<
"Map(" << map->getType() <<
")";
539 else if (llvm::isa<evaluator::BasePathValue>(&evaluatorValue))
540 diag <<
"BasePath()";
541 else if (llvm::isa<evaluator::PathValue>(&evaluatorValue))
544 assert(
false &&
"unhandled evaluator value");
549 static inline mlir::Diagnostic &
551 return diag << *evaluatorValue.get();
assert(baseType &&"element must be base type")
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
static LogicalResult finalizeEvaluatorValue(EvaluatorValuePtr &value)
std::shared_ptr< EvaluatorValue > EvaluatorValuePtr
A value of an object in memory.
evaluator::EvaluatorValuePtr EvaluatorValuePtr
static mlir::Diagnostic & operator<<(mlir::Diagnostic &diag, const evaluator::EvaluatorValue &evaluatorValue)
Helper to enable printing objects in Diagnostics.
SmallVector< EvaluatorValuePtr > getEvaluatorValuesFromAttributes(MLIRContext *context, ArrayRef< Attribute > attributes)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
An Evaluator, which is constructed with an IR module and can instantiate Objects.
FailureOr< evaluator::EvaluatorValuePtr > evaluateBasePathCreate(FrozenBasePathCreateOp op, ActualParameters actualParams, Location loc)
FailureOr< evaluator::EvaluatorValuePtr > evaluateEmptyPath(FrozenEmptyPathOp op, ActualParameters actualParams, Location loc)
SymbolTable symbolTable
The symbol table for the IR module the Evaluator was constructed with.
FailureOr< evaluator::EvaluatorValuePtr > getPartiallyEvaluatedValue(Type type, Location loc)
FailureOr< EvaluatorValuePtr > evaluateValue(Value value, ActualParameters actualParams, Location loc)
Evaluate a Value in a Class body according to the small expression grammar described in the rationale...
FailureOr< EvaluatorValuePtr > evaluateConstant(ConstantOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for constants.
FailureOr< EvaluatorValuePtr > evaluateIntegerBinaryArithmetic(IntegerBinaryArithmeticOp op, ActualParameters actualParams, Location loc)
mlir::ModuleOp getModule()
Get the Module this Evaluator is built from.
FailureOr< EvaluatorValuePtr > evaluateObjectField(ObjectFieldOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for Object fields.
SmallVector< std::unique_ptr< SmallVector< std::shared_ptr< evaluator::EvaluatorValue > > > > actualParametersBuffers
This uniquely stores vectors that represent parameters.
bool isFullyEvaluated(Value value, ActualParameters key)
std::queue< ObjectKey > worklist
A worklist that tracks values which needs to be fully evaluated.
Evaluator(ModuleOp mod)
Construct an Evaluator with an IR module.
FailureOr< evaluator::EvaluatorValuePtr > evaluateMapCreate(MapCreateOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for Map creation.
FailureOr< evaluator::EvaluatorValuePtr > instantiate(StringAttr className, ArrayRef< EvaluatorValuePtr > actualParams)
Instantiate an Object with its class name and actual parameters.
FailureOr< EvaluatorValuePtr > getOrCreateValue(Value value, ActualParameters actualParams, Location loc)
SmallVectorImpl< std::shared_ptr< evaluator::EvaluatorValue > > * ActualParameters
DenseMap< ObjectKey, std::shared_ptr< evaluator::EvaluatorValue > > objects
Evaluator value storage.
FailureOr< EvaluatorValuePtr > evaluateListCreate(ListCreateOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for List creation.
FailureOr< EvaluatorValuePtr > evaluateListConcat(ListConcatOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for List concatenation.
FailureOr< EvaluatorValuePtr > allocateObjectInstance(StringAttr clasName, ActualParameters actualParams)
std::pair< Value, ActualParameters > ObjectKey
FailureOr< EvaluatorValuePtr > evaluateParameter(BlockArgument formalParam, ActualParameters actualParams, Location loc)
Evaluator dispatch functions for the small expression grammar.
FailureOr< EvaluatorValuePtr > evaluateObjectInstance(StringAttr className, ActualParameters actualParams, Location loc, ObjectKey instanceKey={})
Instantiate an Object with its class name and actual parameters.
FailureOr< EvaluatorValuePtr > evaluateTupleGet(TupleGetOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for List creation.
FailureOr< EvaluatorValuePtr > evaluateTupleCreate(TupleCreateOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for Tuple creation.
FailureOr< evaluator::EvaluatorValuePtr > evaluatePathCreate(FrozenPathCreateOp op, ActualParameters actualParams, Location loc)
FailureOr< ActualParameters > createParametersFromOperands(ValueRange range, ActualParameters actualParams, Location loc)
Evaluator dispatch function for Object instances.
bool isFullyEvaluated(ObjectKey key)
Values which can be directly representable by MLIR attributes.
LogicalResult setAttr(Attribute attr)
AttributeValue(Attribute attr)
LogicalResult finalizeImpl()
static bool classof(const EvaluatorValue *e)
AttributeValue(Type type)
Attribute getAttr() const
AttributeValue(Attribute attr, Location loc)
AttributeValue(Type type, Location loc)
BasePathValue(MLIRContext *context)
static bool classof(const EvaluatorValue *e)
Implement LLVM RTTI.
LogicalResult finalizeImpl()
Finalize the evaluator value.
void setBasepath(const BasePathValue &basepath)
Set the basepath which this path is relative to.
om::PathAttr getPath() const
BasePathValue(om::PathAttr path, Location loc)
Create a path value representing a basepath.
Base class for evaluator runtime values.
bool isFullyEvaluated() const
EvaluatorValue(MLIRContext *ctx, Kind kind, Location loc)
MLIRContext * getContext() const
void setLocIfUnknown(Location l)
MLIRContext * getContext()
Return the associated MLIR context.
void markFullyEvaluated()
A List which contains variadic length of elements with the same type.
static bool classof(const EvaluatorValue *e)
Implement LLVM RTTI.
ListValue(om::ListType type, Location loc)
void setElements(SmallVector< EvaluatorValuePtr > newElements)
SmallVector< EvaluatorValuePtr > elements
LogicalResult finalizeImpl()
ListValue(om::ListType type, SmallVector< EvaluatorValuePtr > elements, Location loc)
const auto & getElements() const
om::ListType getListType() const
Return the type of the value, which is a ListType.
const auto & getElements() const
MapValue(om::MapType type, DenseMap< Attribute, EvaluatorValuePtr > elements, Location loc)
ArrayAttr getKeys()
Return an array of keys in the ascending order.
void setElements(DenseMap< Attribute, EvaluatorValuePtr > newElements)
MapValue(om::MapType type, Location loc)
LogicalResult finalizeImpl()
static bool classof(const EvaluatorValue *e)
Implement LLVM RTTI.
om::MapType getMapType() const
Return the type of the value, which is a MapType.
DenseMap< Attribute, EvaluatorValuePtr > elements
A composite Object, which has a type and fields.
const auto & getFields() const
LogicalResult finalizeImpl()
om::ClassType getObjectType() const
Return the type of the value, which is a ClassType.
FailureOr< EvaluatorValuePtr > getField(StringAttr field)
Get a field of the Object by name.
ObjectValue(om::ClassOp cls, ObjectFields fields, Location loc)
FailureOr< EvaluatorValuePtr > getField(StringRef field)
ObjectValue(om::ClassOp cls, Location loc)
ArrayAttr getFieldNames()
Get all the field names of the Object.
static bool classof(const EvaluatorValue *e)
Implement LLVM RTTI.
void setFields(llvm::SmallDenseMap< StringAttr, EvaluatorValuePtr > newFields)
om::ClassOp getClassOp() const
llvm::SmallDenseMap< StringAttr, EvaluatorValuePtr > fields
StringAttr getModule() const
StringAttr getAsString() const
LogicalResult finalizeImpl()
om::TargetKindAttr getTargetKind() const
StringAttr getField() const
om::TargetKindAttr targetKind
void setBasepath(const BasePathValue &basepath)
om::PathAttr getPath() const
PathValue(om::TargetKindAttr targetKind, om::PathAttr path, StringAttr module, StringAttr ref, StringAttr field, Location loc)
Create a path value representing a regular path.
static bool classof(const EvaluatorValue *e)
Implement LLVM RTTI.
static PathValue getEmptyPath(Location loc)
StringAttr getRef() const
Values which can be used as pointers to different values.
EvaluatorValuePtr getValue() const
LogicalResult finalizeImpl()
FailureOr< EvaluatorValuePtr > getStrippedValue() const
static bool classof(const EvaluatorValue *e)
ReferenceValue(Type type, Location loc)
Type getValueType() const
void setValue(EvaluatorValuePtr newValue)
static bool classof(const EvaluatorValue *e)
Implement LLVM RTTI.
const TupleElements & getElements() const
TupleValue(TupleType type, Location loc)
TupleType getTupleType() const
Return the type of the value, which is a TupleType.
llvm::SmallVector< EvaluatorValuePtr > TupleElements
LogicalResult finalizeImpl()
TupleValue(TupleType type, TupleElements tupleElements, Location loc)
void setElements(TupleElements newElements)