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"
26#include "llvm/Support/Debug.h"
66 ++(*fullyEvaluatedCounter);
104 if (isa<UnknownLoc>(
loc))
135 value = std::move(newValue);
144 llvm::SmallPtrSet<ReferenceValue *, 4> visited;
145 auto currentValue =
value;
146 while (
auto *v = dyn_cast<ReferenceValue>(currentValue.get())) {
148 if (!visited.insert(v).second)
150 currentValue = v->getValue();
152 return success(currentValue);
164 template <
typename AttrTy>
166 return dyn_cast<AttrTy>(
attr);
181 static std::shared_ptr<EvaluatorValue>
get(Attribute
attr,
182 LocationAttr
loc = {});
183 static std::shared_ptr<EvaluatorValue>
get(Type
type, LocationAttr
loc = {});
204 friend std::shared_ptr<EvaluatorValue>
get(Attribute
attr, LocationAttr
loc);
205 friend std::shared_ptr<EvaluatorValue>
get(Type
type, LocationAttr
loc);
210 if (failed(value->finalize()))
212 if (
auto *ref = llvm::dyn_cast<ReferenceValue>(value.get())) {
213 auto v = ref->getStrippedValue();
275 fields = std::move(newFields);
281 auto clsNonConst =
const_cast<om::ClassLike &
>(
cls);
282 return ClassType::get(clsNonConst.getContext(),
283 FlatSymbolRefAttr::get(clsNonConst.getSymNameAttr()));
294 FailureOr<EvaluatorValuePtr>
getField(StringAttr field);
295 FailureOr<EvaluatorValuePtr>
getField(StringRef field) {
379SmallVector<EvaluatorValuePtr>
381 ArrayRef<Attribute> attributes);
391 FailureOr<evaluator::EvaluatorValuePtr>
392 instantiate(StringAttr className, ArrayRef<EvaluatorValuePtr> actualParams);
397 FailureOr<evaluator::EvaluatorValuePtr>
401 SmallVectorImpl<std::shared_ptr<evaluator::EvaluatorValue>> *;
414 auto val =
objects.lookup(key);
415 return val && val->isFullyEvaluated();
420 if (value && !value->isFullyEvaluated())
424 FailureOr<evaluator::EvaluatorValuePtr>
426 ArrayRef<EvaluatorValuePtr> actualParams);
428 FailureOr<EvaluatorValuePtr>
430 FailureOr<EvaluatorValuePtr>
436 FailureOr<EvaluatorValuePtr>
444 FailureOr<EvaluatorValuePtr>
448 FailureOr<EvaluatorValuePtr>
450 Location loc,
ObjectKey instanceKey = {});
451 FailureOr<EvaluatorValuePtr>
453 FailureOr<EvaluatorValuePtr>
456 FailureOr<EvaluatorValuePtr>
465 FailureOr<EvaluatorValuePtr>
468 FailureOr<EvaluatorValuePtr>
471 FailureOr<EvaluatorValuePtr>
474 FailureOr<evaluator::EvaluatorValuePtr>
477 FailureOr<evaluator::EvaluatorValuePtr>
480 FailureOr<evaluator::EvaluatorValuePtr>
483 FailureOr<evaluator::EvaluatorValuePtr>
492 FailureOr<ActualParameters>
502 std::unique_ptr<SmallVector<std::shared_ptr<evaluator::EvaluatorValue>>>>
520 DenseMap<ObjectKey, std::shared_ptr<evaluator::EvaluatorValue>>
objects;
536 raw_ostream &
dbgs(
unsigned extra = 0) {
537 return llvm::dbgs().indent(
debugNesting * 2 + extra * 2);
540 llvm::indent
indent(
unsigned extra = 0) {
547static inline mlir::Diagnostic &
550 if (
auto *attr = llvm::dyn_cast<evaluator::AttributeValue>(&evaluatorValue))
551 diag << attr->getAttr();
552 else if (
auto *
object =
553 llvm::dyn_cast<evaluator::ObjectValue>(&evaluatorValue))
554 diag <<
"Object(" <<
object->getType() <<
")";
555 else if (
auto *list = llvm::dyn_cast<evaluator::ListValue>(&evaluatorValue))
556 diag <<
"List(" << list->getType() <<
")";
558 llvm::dyn_cast<evaluator::ReferenceValue>(&evaluatorValue))
559 diag <<
"Reference(" << ref->getValueType() <<
")";
560 else if (llvm::isa<evaluator::BasePathValue>(&evaluatorValue))
561 diag <<
"BasePath()";
562 else if (llvm::isa<evaluator::PathValue>(&evaluatorValue))
565 assert(
false &&
"unhandled evaluator value");
569 diag <<
" [unknown]";
574static inline mlir::Diagnostic &
576 return diag << *evaluatorValue.get();
582static inline llvm::raw_ostream &
586 llvm::raw_string_ostream ss(buf);
587 mlir::Diagnostic diag(UnknownLoc::get(evaluatorValue.
getContext()),
588 mlir::DiagnosticSeverity::Note);
589 diag << evaluatorValue;
591 return os << ss.str();
594static inline llvm::raw_ostream &
597 return os << *evaluatorValue.get();
598 return os <<
"<null>";
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< EvaluatorValuePtr > evaluateElaboratedObject(ElaboratedObjectOp 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 > evaluateStringConcat(StringConcatOp op, ActualParameters actualParams, Location loc)
Evaluator dispatch function for String concatenation.
void attachCounter(evaluator::EvaluatorValuePtr &value)
Attach the evaluation counter to a newly created value.
LogicalResult evaluatePropertyAssert(PropertyAssertOp op, ActualParameters actualParams)
Evaluator dispatch function for property assertions.
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.
FailureOr< evaluator::EvaluatorValuePtr > createUnknownValue(Type type, Location loc)
Create an unknown value of the specified type.
SmallVector< std::unique_ptr< SmallVector< std::shared_ptr< evaluator::EvaluatorValue > > > > actualParametersBuffers
This uniquely stores vectors that represent parameters.
std::vector< ObjectKey > nextWorklist
bool isFullyEvaluated(Value value, ActualParameters key)
FailureOr< EvaluatorValuePtr > allocateObjectInstance(StringAttr clasName, ActualParameters actualParams)
FailureOr< EvaluatorValuePtr > evaluateIntegerBinary(IntegerBinaryOp op, ActualParameters actualParams, Location loc)
llvm::indent indent(unsigned extra=0)
unsigned debugNesting
Current nesting depth for debug output indentation.
FailureOr< evaluator::EvaluatorValuePtr > evaluateUnknownValue(UnknownValueOp op, Location loc)
Evaluate an unknown value.
raw_ostream & dbgs(unsigned extra=0)
uint64_t fullyEvaluatedCount
Counter for fully evaluated nodes.
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< evaluator::EvaluatorValuePtr > instantiateImpl(StringAttr className, ArrayRef< EvaluatorValuePtr > actualParams)
FailureOr< EvaluatorValuePtr > evaluateBinaryEquality(BinaryEqualityOp op, ActualParameters actualParams, Location loc)
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
std::vector< ObjectKey > worklist
Worklists that track values which need to be fully evaluated.
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.
uint64_t getFullyEvaluatedCount() const
Get the number of fully evaluated nodes tracked by this evaluator.
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)
std::queue< std::pair< PropertyAssertOp, ActualParameters > > pendingAsserts
A queue of pending property assertions to be evaluated after the worklist is fully drained.
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)
void markUnknown()
Mark this value as unknown.
MLIRContext * getContext()
Return the associated MLIR context.
void setFullyEvaluatedCounter(uint64_t *counter)
Set a counter to increment when this value becomes fully evaluated.
bool isUnknown() const
Return true if the value is unknown (has unknown in its fan-in).
void setLocIfUnknown(Location l)
MLIRContext * getContext() const
void markFullyEvaluated()
uint64_t * fullyEvaluatedCounter
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
FailureOr< EvaluatorValuePtr > getField(StringRef field)
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)
ObjectValue(om::ClassLike cls, Location loc)
om::ClassLike getClassOp() const
llvm::SmallDenseMap< StringAttr, EvaluatorValuePtr > fields
ObjectValue(om::ClassLike cls, ObjectFields fields, Location loc)
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.
RAII helper to increment/decrement debugNesting.
DebugNesting(unsigned &depth)