// Build the MLIR operation from the name and the two operands. The return
// type is always a generic array for binary operators.
- mlir::OperationState result(&context, location, op_name);
+ mlir::OperationState result(location, op_name);
result.types.push_back(getType(VarType{}));
result.operands.push_back(L);
result.operands.push_back(R);
bool mlirGen(ReturnExprAST &ret) {
auto location = loc(ret.loc());
// `return` takes an optional expression, we need to account for it here.
- mlir::OperationState result(&context, location, "toy.return");
+ mlir::OperationState result(location, "toy.return");
if (ret.getExpr().hasValue()) {
auto *expr = mlirGen(*ret.getExpr().getValue());
if (!expr)
.cast<mlir::DenseElementsAttr>());
// Build the MLIR op `toy.constant`, only boilerplate below.
- mlir::OperationState result(&context, location, "toy.constant");
+ mlir::OperationState result(location, "toy.constant");
result.types.push_back(type);
result.attributes.push_back(dataAttribute);
return builder->createOperation(result)->getResult(0);
}
// builtin have their custom operation, this is a straightforward emission.
if (callee == "transpose") {
- mlir::OperationState result(&context, location, "toy.transpose");
+ mlir::OperationState result(location, "toy.transpose");
result.types.push_back(getType(VarType{}));
result.operands = std::move(operands);
return builder->createOperation(result)->getResult(0);
// Calls to user-defined functions are mapped to a custom call that takes
// the callee name as an attribute.
- mlir::OperationState result(&context, location, "toy.generic_call");
+ mlir::OperationState result(location, "toy.generic_call");
result.types.push_back(getType(VarType{}));
result.operands = std::move(operands);
auto calleeAttr = builder->getStringAttr(call.getCallee());
if (!arg)
return false;
auto location = loc(call.loc());
- mlir::OperationState result(&context, location, "toy.print");
+ mlir::OperationState result(location, "toy.print");
result.operands.push_back(arg);
builder->createOperation(result);
return true;
// Emit a constant for a single number (FIXME: semantic? broadcast?)
mlir::Value *mlirGen(NumberExprAST &num) {
auto location = loc(num.loc());
- mlir::OperationState result(&context, location, "toy.constant");
+ mlir::OperationState result(location, "toy.constant");
mlir::Type elementType = mlir::FloatType::getF64(&context);
result.types.push_back(builder->getMemRefType({1}, elementType));
auto attr = mlir::FloatAttr::getChecked(elementType, num.getValue(),
// with specific shape, we emit a "reshape" operation. It will get
// optimized out later as needed.
if (!vardecl.getType().shape.empty()) {
- mlir::OperationState result(&context, location, "toy.reshape");
+ mlir::OperationState result(location, "toy.reshape");
result.types.push_back(getType(vardecl.getType()));
result.operands.push_back(value);
value = builder->createOperation(result)->getResult(0);
/// Create an operation of specific op type at the current insertion point.
template <typename OpTy, typename... Args>
OpTy create(Location location, Args... args) {
- OperationState state(getContext(), location, OpTy::getOperationName());
+ OperationState state(location, OpTy::getOperationName());
OpTy::build(this, &state, args...);
auto *op = createOperation(state);
auto result = dyn_cast<OpTy>(op);
template <typename OpTy>
void ensureRegionTerminator(Region ®ion, Builder &builder, Location loc) {
ensureRegionTerminator(region, loc, [&] {
- OperationState state(loc->getContext(), loc, OpTy::getOperationName());
+ OperationState state(loc, OpTy::getOperationName());
OpTy::build(&builder, &state);
return Operation::create(state);
});
bool resizableOperandList = false;
public:
- OperationState(MLIRContext *context, Location location, StringRef name);
+ OperationState(Location location, StringRef name);
- OperationState(MLIRContext *context, Location location, OperationName name);
+ OperationState(Location location, OperationName name);
- OperationState(MLIRContext *context, Location location, StringRef name,
- ArrayRef<Value *> operands, ArrayRef<Type> types,
- ArrayRef<NamedAttribute> attributes,
+ OperationState(Location location, StringRef name, ArrayRef<Value *> operands,
+ ArrayRef<Type> types, ArrayRef<NamedAttribute> attributes,
ArrayRef<Block *> successors = {},
MutableArrayRef<std::unique_ptr<Region>> regions = {},
bool resizableOperandList = false);
/// Add an attribute with the specified name.
void addAttribute(StringRef name, Attribute attr) {
- addAttribute(Identifier::get(name, context), attr);
+ addAttribute(Identifier::get(name, getContext()), attr);
}
/// Add an attribute with the specified name.
void setOperandListToResizable(bool isResizable = true) {
resizableOperandList = isResizable;
}
+
+ /// Get the context held by this operation state.
+ MLIRContext *getContext() { return location->getContext(); }
};
namespace detail {
/// without verifying to see if it is valid.
template <typename OpTy, typename... Args>
OpTy create(Location location, Args... args) {
- OperationState state(getContext(), location, OpTy::getOperationName());
+ OperationState state(location, OpTy::getOperationName());
OpTy::build(this, &state, args...);
auto *op = createOperation(state);
auto result = dyn_cast<OpTy>(op);
/// and return null.
template <typename OpTy, typename... Args>
OpTy createChecked(Location location, Args... args) {
- OperationState state(getContext(), location, OpTy::getOperationName());
+ OperationState state(location, OpTy::getOperationName());
OpTy::build(this, &state, args...);
auto *op = createOperation(state);
ArrayRef<ValueHandle> operands,
ArrayRef<Type> resultTypes,
ArrayRef<NamedAttribute> attributes) {
- OperationState state(ScopedContext::getContext(),
- ScopedContext::getLocation(), name);
+ OperationState state(ScopedContext::getLocation(), name);
SmallVector<Value *, 4> ops(operands.begin(), operands.end());
state.addOperands(ops);
state.addTypes(resultTypes);
// OperationState
//===----------------------------------------------------------------------===//
-OperationState::OperationState(MLIRContext *context, Location location,
- StringRef name)
- : context(context), location(location), name(name, context) {}
+OperationState::OperationState(Location location, StringRef name)
+ : context(location->getContext()), location(location),
+ name(name, location->getContext()) {}
-OperationState::OperationState(MLIRContext *context, Location location,
- OperationName name)
- : context(context), location(location), name(name) {}
+OperationState::OperationState(Location location, OperationName name)
+ : context(location->getContext()), location(location), name(name) {}
-OperationState::OperationState(MLIRContext *context, Location location,
- StringRef name, ArrayRef<Value *> operands,
- ArrayRef<Type> types,
+OperationState::OperationState(Location location, StringRef name,
+ ArrayRef<Value *> operands, ArrayRef<Type> types,
ArrayRef<NamedAttribute> attributes,
ArrayRef<Block *> successors,
MutableArrayRef<std::unique_ptr<Region>> regions,
bool resizableOperandList)
- : context(context), location(location), name(name, context),
+ : context(location->getContext()), location(location),
+ name(name, location->getContext()),
operands(operands.begin(), operands.end()),
types(types.begin(), types.end()),
attributes(attributes.begin(), attributes.end()),
consumeToken(Token::string);
- OperationState result(builder.getContext(), srcLocation, name);
+ OperationState result(srcLocation, name);
// Generic operations have a resizable operation list.
result.setOperandListToResizable();
auto srcLocation = getEncodedSourceLocation(opLoc);
// Have the op implementation take a crack and parsing this.
- OperationState opState(builder.getContext(), srcLocation, opDefinition->name);
+ OperationState opState(srcLocation, opDefinition->name);
CleanupOpStateRegions guard{opState};
if (opAsmParser.parseOperation(opDefinition, &opState))
return nullptr;
auto *block = new Block();
fn->push_back(block);
- OperationState state(builder.getContext(), builder.getUnknownLoc(),
- ReturnOp::getOperationName());
+ OperationState state(builder.getUnknownLoc(), ReturnOp::getOperationName());
ReturnOp::build(&builder, &state);
block->push_back(Operation::create(state));
spirv::ModuleOp Deserializer::createModuleOp() {
Builder builder(context);
- OperationState state(context, unknownLoc,
- spirv::ModuleOp::getOperationName());
+ OperationState state(unknownLoc, spirv::ModuleOp::getOperationName());
// TODO(antiagainst): use target environment to select the version
state.addAttribute("major_version", builder.getI32IntegerAttr(1));
state.addAttribute("minor_version", builder.getI32IntegerAttr(0));
auto attrs = materializeAttributes(opInst, hwVectorType);
- OperationState state(b.getContext(), opInst->getLoc(),
- opInst->getName().getStringRef(), operands,
- {hwVectorType}, attrs);
+ OperationState state(opInst->getLoc(), opInst->getName().getStringRef(),
+ operands, {hwVectorType}, attrs);
return b.createOperation(state);
}
unsigned memRefOperandPos = getMemRefOperandPos();
// Construct the new operation using this memref.
- OperationState state(opInst->getContext(), opInst->getLoc(),
- opInst->getName());
+ OperationState state(opInst->getLoc(), opInst->getName());
state.setOperandListToResizable(opInst->hasResizableOperandsList());
state.operands.reserve(opInst->getNumOperands() + extraIndices.size());
// Insert the non-memref operands.
auto attr = DenseElementsAttr::get(vectorType, constant.getValue());
auto *constantOpInst = constant.getOperation();
- OperationState state(b.getContext(), loc,
- constantOpInst->getName().getStringRef(), {},
+ OperationState state(loc, constantOpInst->getName().getStringRef(), {},
{vectorType}, {b.getNamedAttr("value", attr)});
return b.createOperation(state)->getResult(0);
// TODO(ntv): Is it worth considering an Operation.clone operation which
// changes the type so we can promote an Operation with less boilerplate?
OpBuilder b(opInst);
- OperationState newOp(b.getContext(), opInst->getLoc(),
- opInst->getName().getStringRef(), vectorOperands,
- vectorTypes, opInst->getAttrs(), /*successors=*/{},
+ OperationState newOp(opInst->getLoc(), opInst->getName().getStringRef(),
+ vectorOperands, vectorTypes, opInst->getAttrs(),
+ /*successors=*/{},
/*regions=*/{}, opInst->hasResizableOperandsList());
return b.createOperation(newOp);
}