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/SmallString.h"
25#include "llvm/Support/Debug.h"
91 if (isa<UnknownLoc>(
loc))
107 template <
typename AttrTy>
109 return dyn_cast<AttrTy>(
attr);
121 static std::shared_ptr<EvaluatorValue>
get(Attribute
attr,
122 LocationAttr
loc = {});
123 static std::shared_ptr<EvaluatorValue>
get(Type
type, LocationAttr
loc = {});
144 friend std::shared_ptr<EvaluatorValue>
get(Attribute
attr, LocationAttr
loc);
145 friend std::shared_ptr<EvaluatorValue>
get(Type
type, LocationAttr
loc);
199 fields = std::move(newFields);
205 auto clsNonConst =
const_cast<om::ClassLike &
>(
cls);
206 return ClassType::get(clsNonConst.getContext(),
207 FlatSymbolRefAttr::get(clsNonConst.getSymNameAttr()));
218 FailureOr<EvaluatorValuePtr>
getField(StringAttr field);
219 FailureOr<EvaluatorValuePtr>
getField(StringRef field) {
294SmallVector<EvaluatorValuePtr>
296 ArrayRef<Attribute> attributes);
306 FailureOr<evaluator::EvaluatorValuePtr>
307 instantiate(StringAttr className, ArrayRef<EvaluatorValuePtr> actualParams);
312 FailureOr<evaluator::EvaluatorValuePtr>
318 FailureOr<evaluator::EvaluatorValuePtr>
320 ArrayRef<EvaluatorValuePtr> actualParams);
322 FailureOr<EvaluatorValuePtr>
327 FailureOr<EvaluatorValuePtr>
335 FailureOr<EvaluatorValuePtr>
339 FailureOr<EvaluatorValuePtr>
342 FailureOr<EvaluatorValuePtr>
351 FailureOr<evaluator::EvaluatorValuePtr>
354 FailureOr<evaluator::EvaluatorValuePtr>
357 FailureOr<evaluator::EvaluatorValuePtr>
360 FailureOr<evaluator::EvaluatorValuePtr>
371 DenseMap<Value, std::shared_ptr<evaluator::EvaluatorValue>>
objects;
384 raw_ostream &
dbgs(
unsigned extra = 0) {
385 return llvm::dbgs().indent(
debugNesting * 2 + extra * 2);
388 llvm::indent
indent(
unsigned extra = 0) {
395static inline mlir::Diagnostic &
398 if (
auto *attr = llvm::dyn_cast<evaluator::AttributeValue>(&evaluatorValue))
399 diag << attr->getAttr();
400 else if (
auto *
object =
401 llvm::dyn_cast<evaluator::ObjectValue>(&evaluatorValue))
402 diag <<
"Object(" <<
object->getType() <<
")";
403 else if (
auto *list = llvm::dyn_cast<evaluator::ListValue>(&evaluatorValue))
404 diag <<
"List(" << list->getType() <<
")";
405 else if (llvm::isa<evaluator::BasePathValue>(&evaluatorValue))
406 diag <<
"BasePath()";
407 else if (llvm::isa<evaluator::PathValue>(&evaluatorValue))
410 assert(
false &&
"unhandled evaluator value");
414 diag <<
" [unknown]";
419static inline mlir::Diagnostic &
421 return diag << *evaluatorValue.get();
427static inline llvm::raw_ostream &
431 llvm::raw_string_ostream ss(buf);
432 mlir::Diagnostic diag(UnknownLoc::get(evaluatorValue.
getContext()),
433 mlir::DiagnosticSeverity::Note);
434 diag << evaluatorValue;
436 return os << ss.str();
439static inline llvm::raw_ostream &
442 return os << *evaluatorValue.get();
443 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.
DenseMap< Value, std::shared_ptr< evaluator::EvaluatorValue > > objects
Evaluator value storage for the current instantiation.
mlir::ModuleOp getModule()
Get the Module this Evaluator is built from.
ArrayRef< EvaluatorValuePtr > ActualParameters
FailureOr< evaluator::EvaluatorValuePtr > createUnknownValue(Type type, Location loc)
Create an unknown value of the specified type.
FailureOr< EvaluatorValuePtr > evaluateObjectInstance(StringAttr className, ActualParameters actualParams, Location loc)
Instantiate an Object with its class name and actual parameters.
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)
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)
FailureOr< evaluator::EvaluatorValuePtr > instantiateImpl(StringAttr className, ArrayRef< EvaluatorValuePtr > actualParams)
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 > evaluateParameter(BlockArgument formalParam, ActualParameters actualParams, Location loc)
Evaluator dispatch functions for the small expression grammar.
FailureOr< evaluator::EvaluatorValuePtr > evaluatePathCreate(FrozenPathCreateOp op, ActualParameters actualParams, Location loc)
Values which can be directly representable by MLIR attributes.
LogicalResult setAttr(Attribute attr)
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.
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.
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()
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
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.
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
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
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)