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"
79 if (isa<UnknownLoc>(
loc))
108 value = std::move(newValue);
117 llvm::SmallPtrSet<ReferenceValue *, 4> visited;
118 auto currentValue =
value;
119 while (
auto *v = dyn_cast<ReferenceValue>(currentValue.get())) {
121 if (!visited.insert(v).second)
123 currentValue = v->getValue();
125 return success(currentValue);
137 template <
typename AttrTy>
139 return dyn_cast<AttrTy>(
attr);
154 static std::shared_ptr<EvaluatorValue>
get(Attribute
attr,
155 LocationAttr
loc = {});
156 static std::shared_ptr<EvaluatorValue>
get(Type
type, LocationAttr
loc = {});
177 friend std::shared_ptr<EvaluatorValue>
get(Attribute
attr, LocationAttr
loc);
178 friend std::shared_ptr<EvaluatorValue>
get(Type
type, LocationAttr
loc);
183 if (failed(value->finalize()))
185 if (
auto *ref = llvm::dyn_cast<ReferenceValue>(value.get())) {
186 auto v = ref->getStrippedValue();
248 fields = std::move(newFields);
254 auto clsConst =
const_cast<ClassOp &
>(
cls);
255 return ClassType::get(clsConst.getContext(),
256 FlatSymbolRefAttr::get(clsConst.getNameAttr()));
267 FailureOr<EvaluatorValuePtr>
getField(StringAttr field);
268 FailureOr<EvaluatorValuePtr>
getField(StringRef field) {
373SmallVector<EvaluatorValuePtr>
375 ArrayRef<Attribute> attributes);
385 FailureOr<evaluator::EvaluatorValuePtr>
386 instantiate(StringAttr className, ArrayRef<EvaluatorValuePtr> actualParams);
391 FailureOr<evaluator::EvaluatorValuePtr>
395 SmallVectorImpl<std::shared_ptr<evaluator::EvaluatorValue>> *;
405 auto val =
objects.lookup(key);
406 return val && val->isFullyEvaluated();
409 FailureOr<EvaluatorValuePtr>
411 FailureOr<EvaluatorValuePtr>
417 FailureOr<EvaluatorValuePtr>
425 FailureOr<EvaluatorValuePtr>
428 FailureOr<EvaluatorValuePtr>
433 FailureOr<EvaluatorValuePtr>
435 Location loc,
ObjectKey instanceKey = {});
436 FailureOr<EvaluatorValuePtr>
438 FailureOr<EvaluatorValuePtr>
447 FailureOr<evaluator::EvaluatorValuePtr>
450 FailureOr<evaluator::EvaluatorValuePtr>
453 FailureOr<evaluator::EvaluatorValuePtr>
456 FailureOr<evaluator::EvaluatorValuePtr>
459 FailureOr<ActualParameters>
469 std::unique_ptr<SmallVector<std::shared_ptr<evaluator::EvaluatorValue>>>>
477 DenseMap<ObjectKey, std::shared_ptr<evaluator::EvaluatorValue>>
objects;
481static inline mlir::Diagnostic &
484 if (
auto *attr = llvm::dyn_cast<evaluator::AttributeValue>(&evaluatorValue))
485 diag << attr->getAttr();
486 else if (
auto *
object =
487 llvm::dyn_cast<evaluator::ObjectValue>(&evaluatorValue))
488 diag <<
"Object(" <<
object->getType() <<
")";
489 else if (
auto *list = llvm::dyn_cast<evaluator::ListValue>(&evaluatorValue))
490 diag <<
"List(" << list->getType() <<
")";
491 else if (llvm::isa<evaluator::BasePathValue>(&evaluatorValue))
492 diag <<
"BasePath()";
493 else if (llvm::isa<evaluator::PathValue>(&evaluatorValue))
495 else if (llvm::isa<evaluator::UnknownValue>(&evaluatorValue))
498 assert(
false &&
"unhandled evaluator value");
503static inline mlir::Diagnostic &
505 return diag << *evaluatorValue.get();
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
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)
FailureOr< EvaluatorValuePtr > allocateObjectInstance(StringAttr clasName, ActualParameters actualParams)
std::queue< ObjectKey > worklist
A worklist that tracks values which needs to be fully evaluated.
FailureOr< evaluator::EvaluatorValuePtr > evaluateUnknownValue(UnknownValueOp op, Location loc)
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.
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< 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)
LogicalResult finalizeImpl()
friend std::shared_ptr< EvaluatorValue > get(Attribute attr, LocationAttr loc)
static bool classof(const EvaluatorValue *e)
Attribute getAttr() const
AttributeValue(PrivateTag, Attribute attr, Location loc)
friend std::shared_ptr< EvaluatorValue > get(Type type, LocationAttr loc)
AttributeValue(PrivateTag, Type type, Location loc)
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()
Return the associated MLIR context.
void setLocIfUnknown(Location l)
MLIRContext * getContext() const
void markFullyEvaluated()
A List which contains variadic length of elements with the same type.
static bool classof(const EvaluatorValue *e)
Implement LLVM RTTI.
const auto & getElements() const
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)
om::ListType getListType() const
Return the type of the value, which is a ListType.
A composite Object, which has a type and fields.
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.
const auto & getFields() const
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 StringAttr ref
StringAttr getAsString() const
LogicalResult finalizeImpl()
om::TargetKindAttr getTargetKind() const
StringAttr getField() const
om::TargetKindAttr targetKind
void setBasepath(const BasePathValue &basepath)
om::PathAttr getPath() const
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()
static bool classof(const EvaluatorValue *e)
ReferenceValue(Type type, Location loc)
FailureOr< EvaluatorValuePtr > getStrippedValue() const
Type getValueType() const
void setValue(EvaluatorValuePtr newValue)
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.
LogicalResult finalizeImpl()
static bool classof(const EvaluatorValue *e)
UnknownValue(MLIRContext *ctx, Location loc)
Create a value representing unknown information.