auto viewType = view->getType().dyn_cast<ViewType>();
(void)viewType;
assert(viewType.isa<ViewType>() && "expected a ViewType");
- while (auto slice = view->getDefiningOp()->dyn_cast<SliceOp>()) {
+ while (auto slice = dyn_cast<SliceOp>(view->getDefiningOp())) {
view = slice.getParentView();
assert(viewType.isa<ViewType>() && "expected a ViewType");
}
(void)viewType;
assert(viewType.isa<ViewType>() && "expected a ViewType");
assert(dim < viewType.getRank() && "dim exceeds rank");
- if (auto viewOp = view->getDefiningOp()->dyn_cast<ViewOp>())
+ if (auto viewOp = dyn_cast<ViewOp>(view->getDefiningOp()))
return std::make_pair(viewOp.getIndexing(dim), dim);
auto sliceOp = view->getDefiningOp()->cast<SliceOp>();
assert(ivs.size() == indexings.size());
for (unsigned i = 0, e = indexings.size(); i < e; ++i) {
auto rangeOp =
- indexings[i].getValue()->getDefiningOp()->dyn_cast<RangeOp>();
+ llvm::dyn_cast<RangeOp>(indexings[i].getValue()->getDefiningOp());
if (!rangeOp) {
continue;
}
unsigned linalg::getViewRank(Value *view) {
assert(view->getType().isa<ViewType>() && "expected a ViewType");
- if (auto viewOp = view->getDefiningOp()->dyn_cast<ViewOp>())
+ if (auto viewOp = dyn_cast<ViewOp>(view->getDefiningOp()))
return viewOp.getRank();
return view->getDefiningOp()->cast<SliceOp>().getRank();
}
// analyses. This builds the chain.
static SmallVector<Value *, 8> getViewChain(mlir::Value *v) {
assert(v->getType().isa<ViewType>() && "ViewType expected");
- if (v->getDefiningOp()->dyn_cast<ViewOp>()) {
+ if (v->getDefiningOp()->isa<ViewOp>()) {
return SmallVector<mlir::Value *, 8>{v};
}
tmp.push_back(v);
v = sliceOp.getParentView();
} while (!v->getType().isa<ViewType>());
- assert(v->getDefiningOp()->cast<ViewOp>() && "must be a ViewOp");
+ assert(v->getDefiningOp()->isa<ViewOp>() && "must be a ViewOp");
tmp.push_back(v);
return SmallVector<mlir::Value *, 8>(tmp.rbegin(), tmp.rend());
}
extractRangesFromViewOrSliceOp(mlir::Value *view) {
// This expects a viewType which must come from either ViewOp or SliceOp.
assert(view->getType().isa<linalg::ViewType>() && "expected ViewType");
- if (auto viewOp = view->getDefiningOp()->dyn_cast<linalg::ViewOp>())
+ if (auto viewOp = llvm::dyn_cast<linalg::ViewOp>(view->getDefiningOp()))
return viewOp.getRanges();
auto sliceOp = view->getDefiningOp()->cast<linalg::SliceOp>();
void linalg::lowerToFinerGrainedTensorContraction(mlir::Function *f) {
f->walk([](Operation *op) {
- if (auto matmulOp = op->dyn_cast<linalg::MatmulOp>()) {
+ if (auto matmulOp = dyn_cast<linalg::MatmulOp>(op)) {
matmulOp.writeAsFinerGrainTensorContraction();
- } else if (auto matvecOp = op->dyn_cast<linalg::MatvecOp>()) {
+ } else if (auto matvecOp = dyn_cast<linalg::MatvecOp>(op)) {
matvecOp.writeAsFinerGrainTensorContraction();
} else {
return;
llvm::Optional<SmallVector<mlir::AffineForOp, 4>>
linalg::writeAsLoops(Operation *op) {
- if (auto matmulOp = op->dyn_cast<linalg::MatmulOp>()) {
+ if (auto matmulOp = dyn_cast<linalg::MatmulOp>(op)) {
return writeContractionAsLoops(matmulOp);
- } else if (auto matvecOp = op->dyn_cast<linalg::MatvecOp>()) {
+ } else if (auto matvecOp = dyn_cast<linalg::MatvecOp>(op)) {
return writeContractionAsLoops(matvecOp);
- } else if (auto dotOp = op->dyn_cast<linalg::DotOp>()) {
+ } else if (auto dotOp = dyn_cast<linalg::DotOp>(op)) {
return writeContractionAsLoops(dotOp);
}
return llvm::None;
Rewriter<linalg::LoadOp>::matchAndRewrite(Operation *op,
PatternRewriter &rewriter) const {
auto load = op->cast<linalg::LoadOp>();
- SliceOp slice = load.getView()->getDefiningOp()->dyn_cast<SliceOp>();
+ SliceOp slice = dyn_cast<SliceOp>(load.getView()->getDefiningOp());
ViewOp view = slice ? emitAndReturnFullyComposedView(slice.getResult())
: load.getView()->getDefiningOp()->cast<ViewOp>();
ScopedContext scope(FuncBuilder(load), load.getLoc());
Rewriter<linalg::StoreOp>::matchAndRewrite(Operation *op,
PatternRewriter &rewriter) const {
auto store = op->cast<linalg::StoreOp>();
- SliceOp slice = store.getView()->getDefiningOp()->dyn_cast<SliceOp>();
+ SliceOp slice = dyn_cast<SliceOp>(store.getView()->getDefiningOp());
ViewOp view = slice ? emitAndReturnFullyComposedView(slice.getResult())
: store.getView()->getDefiningOp()->cast<ViewOp>();
ScopedContext scope(FuncBuilder(store), store.getLoc());
}
static bool isZeroIndex(Value *v) {
- return v->getDefiningOp() && v->getDefiningOp()->isa<ConstantIndexOp>() &&
- v->getDefiningOp()->dyn_cast<ConstantIndexOp>().getValue() == 0;
+ return isa_and_nonnull<ConstantIndexOp>(v->getDefiningOp()) &&
+ cast<ConstantIndexOp>(v->getDefiningOp()).getValue() == 0;
}
template <typename ConcreteOp>
llvm::Optional<SmallVector<mlir::AffineForOp, 8>>
linalg::writeAsTiledViews(Operation *op, ArrayRef<Value *> tileSizes) {
- if (auto matmulOp = op->dyn_cast<linalg::MatmulOp>()) {
+ if (auto matmulOp = dyn_cast<linalg::MatmulOp>(op)) {
return writeContractionAsTiledViews(matmulOp, tileSizes);
- } else if (auto matvecOp = op->dyn_cast<linalg::MatvecOp>()) {
+ } else if (auto matvecOp = dyn_cast<linalg::MatvecOp>(op)) {
return writeContractionAsTiledViews(matvecOp, tileSizes);
- } else if (auto dotOp = op->dyn_cast<linalg::DotOp>()) {
+ } else if (auto dotOp = dyn_cast<linalg::DotOp>(op)) {
return writeContractionAsTiledViews(dotOp, tileSizes);
}
return llvm::None;
void linalg::lowerToTiledViews(mlir::Function *f, ArrayRef<Value *> tileSizes) {
f->walk([tileSizes](Operation *op) {
- if (auto matmulOp = op->dyn_cast<linalg::MatmulOp>()) {
+ if (auto matmulOp = dyn_cast<linalg::MatmulOp>(op)) {
writeAsTiledViews(matmulOp, tileSizes);
- } else if (auto matvecOp = op->dyn_cast<linalg::MatvecOp>()) {
+ } else if (auto matvecOp = dyn_cast<linalg::MatvecOp>(op)) {
writeAsTiledViews(matvecOp, tileSizes);
- } else if (auto dotOp = op->dyn_cast<linalg::DotOp>()) {
+ } else if (auto dotOp = dyn_cast<linalg::DotOp>(op)) {
writeAsTiledViews(dotOp, tileSizes);
} else {
return;
LLVM_DEBUG(llvm::dbgs() << "Inferring shape for: " << *op << "\n");
// The add operation is trivial: propagate the input type as is.
- if (auto addOp = op->dyn_cast<AddOp>()) {
+ if (auto addOp = llvm::dyn_cast<AddOp>(op)) {
op->getResult(0)->setType(op->getOperand(0)->getType());
continue;
}
// Transpose is easy: just invert the dimensions.
- if (auto transpose = op->dyn_cast<TransposeOp>()) {
+ if (auto transpose = llvm::dyn_cast<TransposeOp>(op)) {
SmallVector<int64_t, 2> dims;
auto arrayTy = transpose.getOperand()->getType().cast<ToyArrayType>();
dims.insert(dims.end(), arrayTy.getShape().begin(),
// catch it but shape inference earlier in the pass could generate an
// invalid IR (from an invalid Toy input of course) and we wouldn't want
// to crash here.
- if (auto mulOp = op->dyn_cast<MulOp>()) {
+ if (auto mulOp = llvm::dyn_cast<MulOp>(op)) {
auto lhs = mulOp.getLHS()->getType().cast<ToyArrayType>();
auto rhs = mulOp.getRHS()->getType().cast<ToyArrayType>();
auto lhsRank = lhs.getShape().size();
// for this function, queue the callee in the inter-procedural work list,
// and return. The current function stays in the work list and will
// restart after the callee is processed.
- if (auto callOp = op->dyn_cast<GenericCallOp>()) {
+ if (auto callOp = llvm::dyn_cast<GenericCallOp>(op)) {
auto calleeName = callOp.getCalleeName();
auto *callee = getModule().getNamedFunction(calleeName);
if (!callee) {
// Look through the input of the current transpose.
mlir::Value *transposeInput = transpose.getOperand();
TransposeOp transposeInputOp =
- mlir::dyn_cast_or_null<TransposeOp>(transposeInput->getDefiningOp());
+ llvm::dyn_cast_or_null<TransposeOp>(transposeInput->getDefiningOp());
// If the input is defined by another Transpose, bingo!
if (!transposeInputOp)
return matchFailure();
mlir::PatternRewriter &rewriter) const override {
ReshapeOp reshape = op->cast<ReshapeOp>();
// Look through the input of the current reshape.
- ConstantOp constantOp = mlir::dyn_cast_or_null<ConstantOp>(
+ ConstantOp constantOp = llvm::dyn_cast_or_null<ConstantOp>(
reshape.getOperand()->getDefiningOp());
// If the input is defined by another constant, bingo!
if (!constantOp)
// First patch calls type to return memref instead of ToyArray
for (auto &function : getModule()) {
function.walk([&](Operation *op) {
- auto callOp = op->dyn_cast<CallOp>();
+ auto callOp = dyn_cast<CallOp>(op);
if (!callOp)
return;
if (!callOp.getNumResults())
for (auto &function : getModule()) {
function.walk([&](Operation *op) {
// Turns toy.alloc into sequence of alloc/dealloc (later malloc/free).
- if (auto allocOp = op->dyn_cast<toy::AllocOp>()) {
+ if (auto allocOp = dyn_cast<toy::AllocOp>(op)) {
auto result = allocTensor(allocOp);
allocOp.replaceAllUsesWith(result);
allocOp.erase();
return;
}
// Eliminate all type.cast before lowering to LLVM.
- if (auto typeCastOp = op->dyn_cast<toy::TypeCastOp>()) {
+ if (auto typeCastOp = dyn_cast<toy::TypeCastOp>(op)) {
typeCastOp.replaceAllUsesWith(typeCastOp.getOperand());
typeCastOp.erase();
return;
// Insert a `dealloc` operation right before the `return` operations, unless
// it is returned itself in which case the caller is responsible for it.
builder.getFunction()->walk([&](Operation *op) {
- auto returnOp = op->dyn_cast<ReturnOp>();
+ auto returnOp = dyn_cast<ReturnOp>(op);
if (!returnOp)
return;
if (returnOp.getNumOperands() && returnOp.getOperand(0) == alloc)
LLVM_DEBUG(llvm::dbgs() << "Inferring shape for: " << *op << "\n");
// The add operation is trivial: propagate the input type as is.
- if (auto addOp = op->dyn_cast<AddOp>()) {
+ if (auto addOp = llvm::dyn_cast<AddOp>(op)) {
op->getResult(0)->setType(op->getOperand(0)->getType());
continue;
}
// catch it but shape inference earlier in the pass could generate an
// invalid IR (from an invalid Toy input of course) and we wouldn't want
// to crash here.
- if (auto mulOp = op->dyn_cast<MulOp>()) {
+ if (auto mulOp = llvm::dyn_cast<MulOp>(op)) {
auto lhs = mulOp.getLHS()->getType().cast<ToyArrayType>();
auto rhs = mulOp.getRHS()->getType().cast<ToyArrayType>();
auto lhsRank = lhs.getShape().size();
// for this function, queue the callee in the inter-procedural work list,
// and return. The current function stays in the work list and will
// restart after the callee is processed.
- if (auto callOp = op->dyn_cast<GenericCallOp>()) {
+ if (auto callOp = llvm::dyn_cast<GenericCallOp>(op)) {
auto calleeName = callOp.getCalleeName();
auto *callee = getModule().getNamedFunction(calleeName);
if (!callee) {
if (op->getNumResults() == 1) {
return ValueHandle(op->getResult(0));
} else if (op->getNumResults() == 0) {
- if (auto f = op->dyn_cast<AffineForOp>()) {
+ if (auto f = dyn_cast<AffineForOp>(op)) {
return ValueHandle(f.getInductionVar());
}
}
OperationState state(getContext(), location, OpTy::getOperationName());
OpTy::build(this, &state, args...);
auto *op = createOperation(state);
- auto result = op->dyn_cast<OpTy>();
+ auto result = dyn_cast<OpTy>(op);
assert(result && "Builder didn't return the right type");
return result;
}
/// Specialization of walk to only visit operations of 'OpTy'.
template <typename OpTy> void walk(std::function<void(OpTy)> callback) {
walk([&](Operation *opInst) {
- if (auto op = opInst->dyn_cast<OpTy>())
+ if (auto op = dyn_cast<OpTy>(opInst))
callback(op);
});
}
/// This is the hook used by the AsmPrinter to emit this to the .mlir file.
/// Op implementations should provide a print method.
static void printAssembly(Operation *op, OpAsmPrinter *p) {
- auto opPointer = op->dyn_cast<ConcreteType>();
+ auto opPointer = dyn_cast<ConcreteType>(op);
assert(opPointer &&
"op's name does not match name of concrete type instantiated with");
opPointer.print(p);
/// This is a public constructor. Any op can be initialized to null.
explicit Op() : OpState(nullptr) {}
+ Op(std::nullptr_t) : OpState(nullptr) {}
-protected:
- /// This is a private constructor only accessible through the
- /// Operation::cast family of methods.
- explicit Op(Operation *state) : OpState(state) {}
+ /// This is a public constructor to enable access via the llvm::cast family of
+ /// methods. This should not be used directly.
+ explicit Op(Operation *state) : OpState(state) {
+ assert(!state || isa<ConcreteOpType>(state));
+ }
friend class Operation;
private:
// Conversions to declared operations like DimOp
//===--------------------------------------------------------------------===//
- /// The dyn_cast methods perform a dynamic cast from an Operation to a typed
- /// Op like DimOp. This returns a null Op on failure.
- template <typename OpClass> OpClass dyn_cast() {
- if (isa<OpClass>())
- return cast<OpClass>();
- return OpClass();
- }
-
/// The cast methods perform a cast from an Operation to a typed Op like
/// DimOp. This aborts if the parameter to the template isn't an instance of
/// the template type argument.
/// including this one.
void walk(const std::function<void(Operation *)> &callback);
- /// Specialization of walk to only visit operations of 'OpTy'.
- template <typename OpTy> void walk(std::function<void(OpTy)> callback) {
+ /// Specialization of walk to only visit operations of 'T'.
+ template <typename T> void walk(std::function<void(T)> callback) {
walk([&](Operation *op) {
- if (auto derivedOp = op->dyn_cast<OpTy>())
+ if (auto derivedOp = dyn_cast<T>(op))
callback(derivedOp);
});
}
return {operand_begin(), operand_end()};
}
-/// Provide dyn_cast_or_null functionality for Operation casts.
-template <typename T> T dyn_cast_or_null(Operation *op) {
- return op ? op->dyn_cast<T>() : T();
-}
-
-/// Provide isa_and_nonnull functionality for Operation casts, i.e. if the
-/// operation is non-null and a class of 'T'.
-template <typename T> bool isa_and_nonnull(Operation *op) {
- return op && op->isa<T>();
-}
-
/// This class implements the result iterators for the Operation class
/// in terms of getResult(idx).
class ResultIterator final
} // end namespace mlir
+namespace llvm {
+/// Provide isa functionality for operation casts.
+template <typename T> struct isa_impl<T, ::mlir::Operation> {
+ static inline bool doit(const ::mlir::Operation &op) {
+ return T::classof(const_cast<::mlir::Operation *>(&op));
+ }
+};
+
+/// Provide specializations for operation casts as the resulting T is value
+/// typed.
+template <typename T> struct cast_retty_impl<T, ::mlir::Operation *> {
+ using ret_type = T;
+};
+template <typename T> struct cast_retty_impl<T, ::mlir::Operation> {
+ using ret_type = T;
+};
+template <class T>
+struct cast_convert_val<T, ::mlir::Operation, ::mlir::Operation> {
+ static T doit(::mlir::Operation &val) { return T(&val); }
+};
+template <class T>
+struct cast_convert_val<T, ::mlir::Operation *, ::mlir::Operation *> {
+ static T doit(::mlir::Operation *val) { return T(val); }
+};
+} // end namespace llvm
+
#endif // MLIR_IR_OPERATION_H
OperationState state(getContext(), location, OpTy::getOperationName());
OpTy::build(this, &state, args...);
auto *op = createOperation(state);
- auto result = op->dyn_cast<OpTy>();
+ auto result = dyn_cast<OpTy>(op);
assert(result && "Builder didn't return the right type");
return result;
}
// If the Operation we produce is valid, return it.
if (!OpTy::verifyInvariants(op)) {
- auto result = op->dyn_cast<OpTy>();
+ auto result = dyn_cast<OpTy>(op);
assert(result && "Builder didn't return the right type");
return result;
}
using llvm::dyn_cast;
using llvm::dyn_cast_or_null;
using llvm::isa;
+using llvm::isa_and_nonnull;
// Containers.
using llvm::ArrayRef;
if (op->getParentOp() == nullptr || op->isa<ConstantOp>())
return true;
// Affine apply operation is ok if all of its operands are ok.
- if (auto applyOp = op->dyn_cast<AffineApplyOp>())
+ if (auto applyOp = dyn_cast<AffineApplyOp>(op))
return applyOp.isValidDim();
// The dim op is okay if its operand memref/tensor is defined at the top
// level.
- if (auto dimOp = op->dyn_cast<DimOp>())
+ if (auto dimOp = dyn_cast<DimOp>(op))
return isTopLevelSymbol(dimOp.getOperand());
return false;
}
if (op->getParentOp() == nullptr || op->isa<ConstantOp>())
return true;
// Affine apply operation is ok if all of its operands are ok.
- if (auto applyOp = op->dyn_cast<AffineApplyOp>())
+ if (auto applyOp = dyn_cast<AffineApplyOp>(op))
return applyOp.isValidSymbol();
// The dim op is okay if its operand memref/tensor is defined at the top
// level.
- if (auto dimOp = op->dyn_cast<DimOp>())
+ if (auto dimOp = dyn_cast<DimOp>(op))
return isTopLevelSymbol(dimOp.getOperand());
return false;
}
loadAndStores.match(forOp, &loadAndStoresMatched);
for (auto ls : loadAndStoresMatched) {
auto *op = ls.getMatchedOperation();
- auto load = op->dyn_cast<LoadOp>();
- auto store = op->dyn_cast<StoreOp>();
+ auto load = dyn_cast<LoadOp>(op);
+ auto store = dyn_cast<StoreOp>(op);
// Only scalar types are considered vectorizable, all load/store must be
// vectorizable for a loop to qualify as vectorizable.
// TODO(ntv): ponder whether we want to be more general here.
bool mlir::isVectorizableLoopBody(AffineForOp loop, int *memRefDim) {
VectorizableOpFun fun([memRefDim](AffineForOp loop, Operation &op) {
- auto load = op.dyn_cast<LoadOp>();
- auto store = op.dyn_cast<StoreOp>();
+ auto load = dyn_cast<LoadOp>(op);
+ auto store = dyn_cast<StoreOp>(op);
return load ? isContiguousAccess(loop.getInductionVar(), load, memRefDim)
: isContiguousAccess(loop.getInductionVar(), store, memRefDim);
});
void MemRefBoundCheck::runOnFunction() {
getFunction().walk([](Operation *opInst) {
- if (auto loadOp = opInst->dyn_cast<LoadOp>()) {
+ if (auto loadOp = dyn_cast<LoadOp>(opInst)) {
boundCheckLoadOrStoreOp(loadOp);
- } else if (auto storeOp = opInst->dyn_cast<StoreOp>()) {
+ } else if (auto storeOp = dyn_cast<StoreOp>(opInst)) {
boundCheckLoadOrStoreOp(storeOp);
}
// TODO(bondhugula): do this for DMA ops as well.
return;
}
- if (auto forOp = op->dyn_cast<AffineForOp>()) {
+ if (auto forOp = dyn_cast<AffineForOp>(op)) {
for (auto &u : forOp.getInductionVar()->getUses()) {
auto *ownerInst = u.getOwner();
if (forwardSlice->count(ownerInst) == 0) {
AffineForOp currAffineForOp;
// Traverse up the hierarchy collecing all 'affine.for' operation while
// skipping over 'affine.if' operations.
- while (currOp && ((currAffineForOp = currOp->dyn_cast<AffineForOp>()) ||
+ while (currOp && ((currAffineForOp = dyn_cast<AffineForOp>(currOp)) ||
currOp->isa<AffineIfOp>())) {
if (currAffineForOp)
loops->push_back(currAffineForOp);
assert(isValidSymbol(symbol));
// Check if the symbol is a constant.
if (auto *op = symbol->getDefiningOp()) {
- if (auto constOp = op->dyn_cast<ConstantIndexOp>()) {
+ if (auto constOp = dyn_cast<ConstantIndexOp>(op)) {
cst.setIdToConstant(*symbol, constOp.getValue());
}
}
}
if (level == positions.size() - 1)
return &op;
- if (auto childAffineForOp = op.dyn_cast<AffineForOp>())
+ if (auto childAffineForOp = dyn_cast<AffineForOp>(op))
return getInstAtPosition(positions, level + 1,
childAffineForOp.getBody());
// Constructs MemRefAccess populating it with the memref, its indices and
// opinst from 'loadOrStoreOpInst'.
MemRefAccess::MemRefAccess(Operation *loadOrStoreOpInst) {
- if (auto loadOp = loadOrStoreOpInst->dyn_cast<LoadOp>()) {
+ if (auto loadOp = dyn_cast<LoadOp>(loadOrStoreOpInst)) {
memref = loadOp.getMemRef();
opInst = loadOrStoreOpInst;
auto loadMemrefType = loadOp.getMemRefType();
}
} else {
assert(loadOrStoreOpInst->isa<StoreOp>() && "load/store op expected");
- auto storeOp = loadOrStoreOpInst->dyn_cast<StoreOp>();
+ auto storeOp = dyn_cast<StoreOp>(loadOrStoreOpInst);
opInst = loadOrStoreOpInst;
memref = storeOp.getMemRef();
auto storeMemrefType = storeOp.getMemRefType();
void mlir::getSequentialLoops(
AffineForOp forOp, llvm::SmallDenseSet<Value *, 8> *sequentialLoops) {
forOp.getOperation()->walk([&](Operation *op) {
- if (auto innerFor = op->dyn_cast<AffineForOp>())
+ if (auto innerFor = dyn_cast<AffineForOp>(op))
if (!isLoopParallel(innerFor))
sequentialLoops->insert(innerFor.getInductionVar());
});
SetVector<Operation *> res;
auto *current = op;
while (auto *parent = current->getParentOp()) {
- if (auto typedParent = parent->template dyn_cast<T>()) {
+ if (auto typedParent = dyn_cast<T>(parent)) {
assert(res.count(parent) == 0 && "Already inserted");
res.insert(parent);
}
}
}
- if (auto load = op->dyn_cast<LoadOp>()) {
+ if (auto load = dyn_cast<LoadOp>(op)) {
return ::makePermutationMap(load.getIndices(), enclosingLoopToVectorDim);
}
/// do not have to special case. Maybe a trait, or just a method, unclear atm.
bool mustDivide = false;
VectorType superVectorType;
- if (auto read = op.dyn_cast<VectorTransferReadOp>()) {
+ if (auto read = dyn_cast<VectorTransferReadOp>(op)) {
superVectorType = read.getResultType();
mustDivide = true;
- } else if (auto write = op.dyn_cast<VectorTransferWriteOp>()) {
+ } else if (auto write = dyn_cast<VectorTransferWriteOp>(op)) {
superVectorType = write.getVectorType();
mustDivide = true;
} else if (op.getNumResults() == 0) {
if (op->getNumResults() == 1) {
return ValueHandle(op->getResult(0));
}
- if (auto f = op->dyn_cast<AffineForOp>()) {
+ if (auto f = dyn_cast<AffineForOp>(op)) {
return ValueHandle(f.getInductionVar());
}
llvm_unreachable("unsupported operation, use an OperationHandle instead");
if (!lbDef || !ubDef)
return llvm::Optional<ValueHandle>();
- auto lbConst = lbDef->dyn_cast<ConstantIndexOp>();
- auto ubConst = ubDef->dyn_cast<ConstantIndexOp>();
+ auto lbConst = dyn_cast<ConstantIndexOp>(lbDef);
+ auto ubConst = dyn_cast<ConstantIndexOp>(ubDef);
if (!lbConst || !ubConst)
return llvm::Optional<ValueHandle>();
// TODO(ntv) expose as a primitive for other passes.
static LogicalResult tileLinalgOp(Operation *op, ArrayRef<int64_t> tileSizes,
PerFunctionState &state) {
- if (auto matmulOp = op->dyn_cast<MatmulOp>()) {
+ if (auto matmulOp = dyn_cast<MatmulOp>(op)) {
return tileLinalgOp(matmulOp, tileSizes, state);
- } else if (auto matvecOp = op->dyn_cast<MatvecOp>()) {
+ } else if (auto matvecOp = dyn_cast<MatvecOp>(op)) {
return tileLinalgOp(matvecOp, tileSizes, state);
- } else if (auto dotOp = op->dyn_cast<DotOp>()) {
+ } else if (auto dotOp = dyn_cast<DotOp>(op)) {
return tileLinalgOp(dotOp, tileSizes, state);
}
return failure();
SmallVector<Value *, 8> mlir::getRanges(Operation *op) {
SmallVector<Value *, 8> res;
- if (auto view = op->dyn_cast<ViewOp>()) {
+ if (auto view = dyn_cast<ViewOp>(op)) {
res.append(view.getIndexings().begin(), view.getIndexings().end());
- } else if (auto slice = op->dyn_cast<SliceOp>()) {
+ } else if (auto slice = dyn_cast<SliceOp>(op)) {
for (auto *i : slice.getIndexings())
if (i->getType().isa<RangeType>())
res.push_back(i);
Value *mlir::createOrReturnView(FuncBuilder *b, Location loc,
Operation *viewDefiningOp,
ArrayRef<Value *> ranges) {
- if (auto view = viewDefiningOp->dyn_cast<ViewOp>()) {
+ if (auto view = dyn_cast<ViewOp>(viewDefiningOp)) {
auto indexings = view.getIndexings();
if (std::equal(indexings.begin(), indexings.end(), ranges.begin()))
return view.getResult();
void rewrite(Operation *op, PatternRewriter &rewriter) const override {
for (unsigned i = 0, e = op->getNumOperands(); i != e; ++i)
if (auto *memref = op->getOperand(i)->getDefiningOp())
- if (auto cast = memref->dyn_cast<MemRefCastOp>())
+ if (auto cast = dyn_cast<MemRefCastOp>(memref))
op->setOperand(i, cast.getOperand());
rewriter.updatedRootInPlace(op);
}
// Emit branches. We need to look up the remapped blocks and ignore the block
// arguments that were transformed into PHI nodes.
- if (auto brOp = opInst.dyn_cast<LLVM::BrOp>()) {
+ if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
builder.CreateBr(blockMapping[brOp.getSuccessor(0)]);
return false;
}
- if (auto condbrOp = opInst.dyn_cast<LLVM::CondBrOp>()) {
+ if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
builder.CreateCondBr(valueMapping.lookup(condbrOp.getOperand(0)),
blockMapping[condbrOp.getSuccessor(0)],
blockMapping[condbrOp.getSuccessor(1)]);
// For conditional branches, we need to check if the current block is reached
// through the "true" or the "false" branch and take the relevant operands.
- auto condBranchOp = terminator.dyn_cast<LLVM::CondBrOp>();
+ auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator);
assert(condBranchOp &&
"only branch operations can be terminators of a block that "
"has successors");
static bool getFullMemRefAsRegion(Operation *opInst, unsigned numParamLoopIVs,
MemRefRegion *region) {
unsigned rank;
- if (auto loadOp = opInst->dyn_cast<LoadOp>()) {
+ if (auto loadOp = dyn_cast<LoadOp>(opInst)) {
rank = loadOp.getMemRefType().getRank();
region->memref = loadOp.getMemRef();
region->setWrite(false);
- } else if (auto storeOp = opInst->dyn_cast<StoreOp>()) {
+ } else if (auto storeOp = dyn_cast<StoreOp>(opInst)) {
rank = storeOp.getMemRefType().getRank();
region->memref = storeOp.getMemRef();
region->setWrite(true);
});
for (auto it = curBegin; it != block->end(); ++it) {
- if (auto forOp = it->dyn_cast<AffineForOp>()) {
+ if (auto forOp = dyn_cast<AffineForOp>(&*it)) {
// Returns true if the footprint is known to exceed capacity.
auto exceedsCapacity = [&](AffineForOp forOp) {
Optional<int64_t> footprint =
// Walk this range of operations to gather all memory regions.
block->walk(begin, end, [&](Operation *opInst) {
// Gather regions to allocate to buffers in faster memory space.
- if (auto loadOp = opInst->dyn_cast<LoadOp>()) {
+ if (auto loadOp = dyn_cast<LoadOp>(opInst)) {
if (loadOp.getMemRefType().getMemorySpace() != slowMemorySpace)
return;
- } else if (auto storeOp = opInst->dyn_cast<StoreOp>()) {
+ } else if (auto storeOp = dyn_cast<StoreOp>(opInst)) {
if (storeOp.getMemRefType().getMemorySpace() != slowMemorySpace)
return;
} else {
// For a range of operations, a note will be emitted at the caller.
AffineForOp forOp;
uint64_t sizeInKib = llvm::divideCeil(totalDmaBuffersSizeInBytes, 1024);
- if (llvm::DebugFlag && (forOp = begin->dyn_cast<AffineForOp>())) {
+ if (llvm::DebugFlag && (forOp = dyn_cast<AffineForOp>(&*begin))) {
forOp.emitRemark()
<< sizeInKib
<< " KiB of DMA buffers in fast memory space for this block\n";
DenseMap<Operation *, unsigned> forToNodeMap;
for (auto &op : f.front()) {
- if (auto forOp = op.dyn_cast<AffineForOp>()) {
+ if (auto forOp = dyn_cast<AffineForOp>(op)) {
// Create graph node 'id' to represent top-level 'forOp' and record
// all loads and store accesses it contains.
LoopNestStateCollector collector;
}
forToNodeMap[&op] = node.id;
nodes.insert({node.id, node});
- } else if (auto loadOp = op.dyn_cast<LoadOp>()) {
+ } else if (auto loadOp = dyn_cast<LoadOp>(op)) {
// Create graph node for top-level load op.
Node node(nextNodeId++, &op);
node.loads.push_back(&op);
auto *memref = op.cast<LoadOp>().getMemRef();
memrefAccesses[memref].insert(node.id);
nodes.insert({node.id, node});
- } else if (auto storeOp = op.dyn_cast<StoreOp>()) {
+ } else if (auto storeOp = dyn_cast<StoreOp>(op)) {
// Create graph node for top-level store op.
Node node(nextNodeId++, &op);
node.stores.push_back(&op);
auto *fn = dstNode->op->getFunction();
for (unsigned i = 0, e = fn->getNumArguments(); i != e; ++i) {
for (auto &use : fn->getArgument(i)->getUses()) {
- if (auto loadOp = use.getOwner()->dyn_cast<LoadOp>()) {
+ if (auto loadOp = dyn_cast<LoadOp>(use.getOwner())) {
// Gather loops surrounding 'use'.
SmallVector<AffineForOp, 4> loops;
getLoopIVs(*use.getOwner(), &loops);
for (auto &block : f)
for (auto &op : block)
- if (auto forOp = op.dyn_cast<AffineForOp>())
+ if (auto forOp = dyn_cast<AffineForOp>(op))
getMaximalPerfectLoopNest(forOp);
}
// unroll-and-jammed by this pass. However, runOnAffineForOp can be called on
// any for operation.
auto &entryBlock = getFunction().front();
- if (auto forOp = entryBlock.front().dyn_cast<AffineForOp>())
+ if (auto forOp = dyn_cast<AffineForOp>(entryBlock.front()))
runOnAffineForOp(forOp);
}
// Rewrite all of the ifs and fors. We walked the operations in postorders,
// so we know that we will rewrite them in the reverse order.
for (auto *op : llvm::reverse(instsToRewrite)) {
- if (auto ifOp = op->dyn_cast<AffineIfOp>()) {
+ if (auto ifOp = dyn_cast<AffineIfOp>(op)) {
if (lowerAffineIf(ifOp))
return signalPassFailure();
- } else if (auto forOp = op->dyn_cast<AffineForOp>()) {
+ } else if (auto forOp = dyn_cast<AffineForOp>(op)) {
if (lowerAffineFor(forOp))
return signalPassFailure();
} else if (lowerAffineApply(op->cast<AffineApplyOp>())) {
if (op->getNumRegions() != 0)
return op->emitError("NYI path Op with region"), true;
- if (auto write = op->dyn_cast<VectorTransferWriteOp>()) {
+ if (auto write = dyn_cast<VectorTransferWriteOp>(op)) {
auto *clone = instantiate(&b, write, state->hwVectorType,
state->hwVectorInstance, state->substitutionsMap);
return clone == nullptr;
}
- if (auto read = op->dyn_cast<VectorTransferReadOp>()) {
+ if (auto read = dyn_cast<VectorTransferReadOp>(op)) {
auto *clone = instantiate(&b, read, state->hwVectorType,
state->hwVectorInstance, state->substitutionsMap);
if (!clone) {
SmallVector<Operation *, 8> storeOps;
unsigned minSurroundingLoops = getNestingDepth(*loadOpInst);
for (auto &use : loadOp.getMemRef()->getUses()) {
- auto storeOp = use.getOwner()->dyn_cast<StoreOp>();
+ auto storeOp = dyn_cast<StoreOp>(use.getOwner());
if (!storeOp)
continue;
auto *storeOpInst = storeOp.getOperation();
// Collect outgoing DMA operations - needed to check for dependences below.
SmallVector<DmaStartOp, 4> outgoingDmaOps;
for (auto &op : *forOp.getBody()) {
- auto dmaStartOp = op.dyn_cast<DmaStartOp>();
+ auto dmaStartOp = dyn_cast<DmaStartOp>(op);
if (dmaStartOp && dmaStartOp.isSrcMemorySpaceFaster())
outgoingDmaOps.push_back(dmaStartOp);
}
dmaFinishInsts.push_back(&op);
continue;
}
- auto dmaStartOp = op.dyn_cast<DmaStartOp>();
+ auto dmaStartOp = dyn_cast<DmaStartOp>(op);
if (!dmaStartOp)
continue;
}
// If this op is a constant that are used and cannot be de-duplicated,
// remember it for cleanup later.
- else if (auto constant = op->dyn_cast<ConstantOp>()) {
+ else if (auto constant = dyn_cast<ConstantOp>(op)) {
existingConstants.push_back(op);
}
}
// into the value it contains. We need to consider constants before the
// constant folding logic to avoid re-creating the same constant later.
// TODO: Extend to support dialect-specific constant ops.
- if (auto constant = op->dyn_cast<ConstantOp>()) {
+ if (auto constant = dyn_cast<ConstantOp>(op)) {
// If this constant is dead, update bookkeeping and signal the caller.
if (constant.use_empty()) {
notifyRemoval(op);
nestedLoops.push_back(curr);
auto *currBody = curr.getBody();
while (currBody->begin() == std::prev(currBody->end(), 2) &&
- (curr = curr.getBody()->front().dyn_cast<AffineForOp>())) {
+ (curr = dyn_cast<AffineForOp>(curr.getBody()->front()))) {
nestedLoops.push_back(curr);
currBody = curr.getBody();
}
static bool affineApplyOp(Operation &op) { return op.isa<AffineApplyOp>(); }
static bool singleResultAffineApplyOpWithoutUses(Operation &op) {
- auto app = op.dyn_cast<AffineApplyOp>();
+ auto app = dyn_cast<AffineApplyOp>(op);
return app && app.use_empty();
}
loadAndStores.match(loop.getOperation(), &loadAndStoresMatches);
for (auto ls : loadAndStoresMatches) {
auto *opInst = ls.getMatchedOperation();
- auto load = opInst->dyn_cast<LoadOp>();
- auto store = opInst->dyn_cast<StoreOp>();
+ auto load = dyn_cast<LoadOp>(opInst);
+ auto store = dyn_cast<StoreOp>(opInst);
LLVM_DEBUG(opInst->print(dbgs()));
LogicalResult result =
load ? vectorizeRootOrTerminal(loop.getInductionVar(), load, state)
return nullptr;
}
// 3. vectorize constant.
- if (auto constant = operand->getDefiningOp()->dyn_cast<ConstantOp>()) {
+ if (auto constant = dyn_cast<ConstantOp>(operand->getDefiningOp())) {
return vectorizeConstant(
op, constant,
VectorType::get(state->strategy->vectorSizes, operand->getType()));
assert(!opInst->isa<VectorTransferWriteOp>() &&
"vector.transfer_write cannot be further vectorized");
- if (auto store = opInst->dyn_cast<StoreOp>()) {
+ if (auto store = dyn_cast<StoreOp>(opInst)) {
auto *memRef = store.getMemRef();
auto *value = store.getValueToStore();
auto *vectorValue = vectorizeOperand(value, opInst, state);
}
// Output the check and the rewritten builder string.
- os << "if (auto op = opInst.dyn_cast<" << op.getQualCppClassName()
- << ">()) {\n";
+ os << "if (auto op = dyn_cast<" << op.getQualCppClassName()
+ << ">(opInst)) {\n";
os << bs.str() << builderStrRef << "\n";
os << " return false;\n";
os << "}\n";