Rename Operation::getAs to Operation::dyn_cast

Also rename Operation::is to Operation::isa
Introduce Operation::cast

All of these are for consistency with global dyn_cast/cast/isa operators.

PiperOrigin-RevId: 217878786
This commit is contained in:
Feng Liu 2018-10-19 09:07:58 -07:00 committed by jpienaar
parent 8c7478d10c
commit 34927e2474
14 changed files with 96 additions and 76 deletions

View File

@ -217,7 +217,7 @@ public:
OperationState state(getContext(), location, OpTy::getOperationName());
OpTy::build(this, &state, args...);
auto *inst = createOperation(state);
auto result = inst->template getAs<OpTy>();
auto result = inst->dyn_cast<OpTy>();
assert(result && "Builder didn't return the right type");
return result;
}
@ -233,7 +233,7 @@ public:
// If the OperationInst we produce is valid, return it.
if (!OpTy::verifyInvariants(inst)) {
auto result = inst->template getAs<OpTy>();
auto result = inst->dyn_cast<OpTy>();
assert(result && "Builder didn't return the right type");
return result;
}
@ -364,7 +364,7 @@ public:
OperationState state(getContext(), location, OpTy::getOperationName());
OpTy::build(this, &state, args...);
auto *stmt = createOperation(state);
auto result = stmt->template getAs<OpTy>();
auto result = stmt->dyn_cast<OpTy>();
assert(result && "Builder didn't return the right type");
return result;
}
@ -380,7 +380,7 @@ public:
// If the OperationStmt we produce is valid, return it.
if (!OpTy::verifyInvariants(stmt)) {
auto result = stmt->template getAs<OpTy>();
auto result = stmt->dyn_cast<OpTy>();
assert(result && "Builder didn't return the right type");
return result;
}

View File

@ -207,8 +207,8 @@ public:
static bool constantFoldHook(const Operation *op,
ArrayRef<Attribute *> operands,
SmallVectorImpl<Attribute *> &results) {
return op->getAs<ConcreteType>()->constantFold(operands, results,
op->getContext());
return op->cast<ConcreteType>()->constantFold(operands, results,
op->getContext());
}
/// Op implementations can implement this hook. It should attempt to constant
@ -241,7 +241,7 @@ public:
ArrayRef<Attribute *> operands,
SmallVectorImpl<Attribute *> &results) {
auto *result =
op->getAs<ConcreteType>()->constantFold(operands, op->getContext());
op->cast<ConcreteType>()->constantFold(operands, op->getContext());
if (!result)
return true;
@ -679,7 +679,7 @@ public:
/// 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(const Operation *op, OpAsmPrinter *p) {
auto opPointer = op->getAs<ConcreteType>();
auto opPointer = op->dyn_cast<ConcreteType>();
assert(opPointer &&
"op's name does not match name of concrete type instantiated with");
opPointer->print(p);
@ -694,7 +694,7 @@ public:
/// diagnostic subsystem and returns true.
static bool verifyInvariants(const Operation *op) {
return BaseVerifier<Traits<ConcreteType>...>::verifyTrait(op) ||
op->getAs<ConcreteType>()->verify();
op->cast<ConcreteType>()->verify();
}
// Returns the properties of an operation by combining the properties of the

View File

@ -176,27 +176,49 @@ public:
return OpPointer<OpClass>(OpClass(nullptr));
}
/// The getAs methods perform a dynamic cast from an Operation (like
/// The dyn_cast methods perform a dynamic cast from an Operation (like
/// OperationInst and OperationStmt) to a typed Op like DimOp. This returns
/// a null OpPointer on failure.
template <typename OpClass>
OpPointer<OpClass> getAs() {
bool isMatch = OpClass::isClassFor(this);
return OpPointer<OpClass>(OpClass(isMatch ? this : nullptr));
template <typename OpClass> OpPointer<OpClass> dyn_cast() {
if (isa<OpClass>()) {
return cast<OpClass>();
} else {
return OpPointer<OpClass>(OpClass(nullptr));
}
}
/// The getAs methods perform a dynamic cast from an Operation (like
/// The dyn_cast methods perform a dynamic cast from an Operation (like
/// OperationInst and OperationStmt) to a typed Op like DimOp. This returns
/// a null ConstOpPointer on failure.
template <typename OpClass>
ConstOpPointer<OpClass> getAs() const {
bool isMatch = OpClass::isClassFor(this);
return ConstOpPointer<OpClass>(OpClass(isMatch ? this : nullptr));
template <typename OpClass> ConstOpPointer<OpClass> dyn_cast() const {
if (isa<OpClass>()) {
return cast<OpClass>();
} else {
return ConstOpPointer<OpClass>(OpClass(nullptr));
}
}
/// The cast methods perform a cast from an Operation (like
/// OperationInst and OperationStmt) to a typed Op like DimOp. This aborts
/// if the parameter to the template isn't an instance of the template type
/// argument.
template <typename OpClass> OpPointer<OpClass> cast() {
assert(isa<OpClass>() && "cast<Ty>() argument of incompatible type!");
return OpPointer<OpClass>(OpClass(this));
}
/// The cast methods perform a cast from an Operation (like
/// OperationInst and OperationStmt) to a typed Op like DimOp. This aborts
/// if the parameter to the template isn't an instance of the template type
/// argument.
template <typename OpClass> ConstOpPointer<OpClass> cast() const {
assert(isa<OpClass>() && "cast<Ty>() argument of incompatible type!");
return ConstOpPointer<OpClass>(OpClass(this));
}
/// The is methods return true if the operation is a typed op (like DimOp) of
/// of the given class.
template <typename OpClass> bool is() const {
template <typename OpClass> bool isa() const {
return OpClass::isClassFor(this);
}

View File

@ -322,11 +322,11 @@ void mlir::getReachableAffineApplyOps(
auto *opStmt = state.value->getDefiningStmt();
// Note: getDefiningStmt will return nullptr if the operand is not an
// OperationStmt (i.e. ForStmt), which is a terminator for the search.
if (opStmt == nullptr || !opStmt->is<AffineApplyOp>()) {
if (opStmt == nullptr || !opStmt->isa<AffineApplyOp>()) {
worklist.pop_back();
continue;
}
if (auto affineApplyOp = opStmt->getAs<AffineApplyOp>()) {
if (auto affineApplyOp = opStmt->dyn_cast<AffineApplyOp>()) {
if (state.operandIndex == 0) {
// Pre-Visit: Add 'opStmt' to reachable sequence.
affineApplyOps.push_back(opStmt);

View File

@ -145,7 +145,7 @@ static bool isAccessInvariant(MLValue *input, MemRefType *memRefType,
assert(affineApplyOps.size() == 1 &&
"CompositionAffineMapsPass must have "
"been run: there should be at most one AffineApplyOp");
auto composeOp = affineApplyOps[0]->getAs<AffineApplyOp>();
auto composeOp = affineApplyOps[0]->cast<AffineApplyOp>();
return !AffineValueMap(*composeOp).isFunctionOf(dim, input);
}
@ -186,8 +186,8 @@ bool mlir::isVectorizableLoop(const ForStmt &loop) {
auto &matches = loadAndStores.match(forStmt);
for (auto ls : matches) {
auto *op = cast<OperationStmt>(ls.first);
auto load = op->getAs<LoadOp>();
auto store = op->getAs<StoreOp>();
auto load = op->dyn_cast<LoadOp>();
auto store = op->dyn_cast<StoreOp>();
bool contiguous = load ? isContiguousAccess(forStmt, load)
: isContiguousAccess(forStmt, store);
if (!contiguous) {

View File

@ -250,7 +250,7 @@ MLFunctionMatcher Red(MutableArrayRef<MLFunctionMatcher> children) {
FilterFunctionType isLoadOrStore = [](Statement *stmt) {
auto *opStmt = dyn_cast<OperationStmt>(stmt);
return opStmt && (opStmt->is<LoadOp>() || opStmt->is<StoreOp>());
return opStmt && (opStmt->isa<LoadOp>() || opStmt->isa<StoreOp>());
};
MLFunctionMatcher LoadStores() {
return MLFunctionMatcher(Statement::Kind::Operation, {}, isLoadOrStore);

View File

@ -977,16 +977,16 @@ protected:
// Give constant integers special names.
if (auto *op = value->getDefiningOperation()) {
if (auto intOp = op->getAs<ConstantIntOp>()) {
if (auto intOp = op->dyn_cast<ConstantIntOp>()) {
// i1 constants get special names.
if (intOp->getType()->isInteger(1)) {
specialName << (intOp->getValue() ? "true" : "false");
} else {
specialName << 'c' << intOp->getValue() << '_' << *intOp->getType();
}
} else if (auto intOp = op->getAs<ConstantIndexOp>()) {
} else if (auto intOp = op->dyn_cast<ConstantIndexOp>()) {
specialName << 'c' << intOp->getValue();
} else if (auto constant = op->getAs<ConstantOp>()) {
} else if (auto constant = op->dyn_cast<ConstantOp>()) {
if (isa<FunctionAttr>(constant->getValue()))
specialName << 'f';
else

View File

@ -71,64 +71,64 @@ Operation::~Operation() {}
/// Return the context this operation is associated with.
MLIRContext *Operation::getContext() const {
if (auto *inst = dyn_cast<OperationInst>(this))
if (auto *inst = llvm::dyn_cast<OperationInst>(this))
return inst->getContext();
return cast<OperationStmt>(this)->getContext();
return llvm::cast<OperationStmt>(this)->getContext();
}
/// The source location the operation was defined or derived from. Note that
/// it is possible for this pointer to be null.
Location *Operation::getLoc() const {
if (auto *inst = dyn_cast<OperationInst>(this))
if (auto *inst = llvm::dyn_cast<OperationInst>(this))
return inst->getLoc();
return cast<OperationStmt>(this)->getLoc();
return llvm::cast<OperationStmt>(this)->getLoc();
}
/// Return the function this operation is defined in.
Function *Operation::getOperationFunction() {
if (auto *inst = dyn_cast<OperationInst>(this))
if (auto *inst = llvm::dyn_cast<OperationInst>(this))
return inst->getFunction();
return cast<OperationStmt>(this)->findFunction();
return llvm::cast<OperationStmt>(this)->findFunction();
}
/// Return the number of operands this operation has.
unsigned Operation::getNumOperands() const {
if (auto *inst = dyn_cast<OperationInst>(this))
if (auto *inst = llvm::dyn_cast<OperationInst>(this))
return inst->getNumOperands();
return cast<OperationStmt>(this)->getNumOperands();
return llvm::cast<OperationStmt>(this)->getNumOperands();
}
SSAValue *Operation::getOperand(unsigned idx) {
if (auto *inst = dyn_cast<OperationInst>(this))
if (auto *inst = llvm::dyn_cast<OperationInst>(this))
return inst->getOperand(idx);
return cast<OperationStmt>(this)->getOperand(idx);
return llvm::cast<OperationStmt>(this)->getOperand(idx);
}
void Operation::setOperand(unsigned idx, SSAValue *value) {
if (auto *inst = dyn_cast<OperationInst>(this)) {
inst->setOperand(idx, cast<CFGValue>(value));
if (auto *inst = llvm::dyn_cast<OperationInst>(this)) {
inst->setOperand(idx, llvm::cast<CFGValue>(value));
} else {
auto *stmt = cast<OperationStmt>(this);
stmt->setOperand(idx, cast<MLValue>(value));
auto *stmt = llvm::cast<OperationStmt>(this);
stmt->setOperand(idx, llvm::cast<MLValue>(value));
}
}
/// Return the number of results this operation has.
unsigned Operation::getNumResults() const {
if (auto *inst = dyn_cast<OperationInst>(this))
if (auto *inst = llvm::dyn_cast<OperationInst>(this))
return inst->getNumResults();
return cast<OperationStmt>(this)->getNumResults();
return llvm::cast<OperationStmt>(this)->getNumResults();
}
/// Return the indicated result.
SSAValue *Operation::getResult(unsigned idx) {
if (auto *inst = dyn_cast<OperationInst>(this))
if (auto *inst = llvm::dyn_cast<OperationInst>(this))
return inst->getResult(idx);
return cast<OperationStmt>(this)->getResult(idx);
return llvm::cast<OperationStmt>(this)->getResult(idx);
}
/// Return true if there are no users of any results of this operation.

View File

@ -98,10 +98,10 @@ const MLValue *Statement::getOperand(unsigned idx) const {
bool MLValue::isValidDim() const {
if (auto *stmt = getDefiningStmt()) {
// Top level statement or constant operation is ok.
if (stmt->getParentStmt() == nullptr || stmt->is<ConstantOp>())
if (stmt->getParentStmt() == nullptr || stmt->isa<ConstantOp>())
return true;
// Affine apply operation is ok if all of its operands are ok.
if (auto op = stmt->getAs<AffineApplyOp>())
if (auto op = stmt->dyn_cast<AffineApplyOp>())
return op->isValidDim();
return false;
}
@ -116,10 +116,10 @@ bool MLValue::isValidDim() const {
bool MLValue::isValidSymbol() const {
if (auto *stmt = getDefiningStmt()) {
// Top level statement or constant operation is ok.
if (stmt->getParentStmt() == nullptr || stmt->is<ConstantOp>())
if (stmt->getParentStmt() == nullptr || stmt->isa<ConstantOp>())
return true;
// Affine apply operation is ok if all of its operands are ok.
if (auto op = stmt->getAs<AffineApplyOp>())
if (auto op = stmt->dyn_cast<AffineApplyOp>())
return op->isValidSymbol();
return false;
}
@ -291,7 +291,7 @@ MLIRContext *OperationStmt::getContext() const {
return findFunction()->getContext();
}
bool OperationStmt::isReturn() const { return is<ReturnOp>(); }
bool OperationStmt::isReturn() const { return isa<ReturnOp>(); }
//===----------------------------------------------------------------------===//
// ForStmt
@ -444,7 +444,7 @@ bool ForStmt::constantFoldBound(bool lower) {
for (const auto *operand : boundOperands) {
Attribute *operandCst = nullptr;
if (auto *operandOp = operand->getDefiningOperation()) {
if (auto operandConstantOp = operandOp->getAs<ConstantOp>())
if (auto operandConstantOp = operandOp->dyn_cast<ConstantOp>())
operandCst = operandConstantOp->getValue();
}
operandConstants.push_back(operandCst);

View File

@ -45,8 +45,7 @@ struct SimplifyXMinusX : public Pattern {
std::pair<PatternBenefit, std::unique_ptr<PatternState>>
match(Operation *op) const override {
// TODO: Rename getAs -> dyn_cast, and add a cast<> method.
auto subi = op->getAs<SubIOp>();
auto subi = op->dyn_cast<SubIOp>();
assert(subi && "Matcher should have produced this");
if (subi->getOperand(0) == subi->getOperand(1))
@ -61,8 +60,7 @@ struct SimplifyXMinusX : public Pattern {
// compiler error), it is emitted through the normal MLIR diagnostic
// hooks and the IR is left in a valid state.
virtual void rewrite(Operation *op, FuncBuilder &builder) const override {
// TODO: Rename getAs -> dyn_cast, and add a cast<> method.
auto subi = op->getAs<SubIOp>();
auto subi = op->dyn_cast<SubIOp>();
assert(subi && "Matcher should have produced this");
auto result =
@ -172,7 +170,7 @@ void Canonicalizer::simplifyFunction(std::vector<Operation *> &worklist,
for (auto *operand : op->getOperands()) {
Attribute *operandCst = nullptr;
if (auto *operandOp = operand->getDefiningOperation()) {
if (auto operandConstantOp = operandOp->getAs<ConstantOp>())
if (auto operandConstantOp = operandOp->dyn_cast<ConstantOp>())
operandCst = operandConstantOp->getValue();
}
operandConstants.push_back(operandCst);

View File

@ -70,7 +70,7 @@ void ComposeAffineMaps::walk(StmtListType::iterator Start,
}
void ComposeAffineMaps::visitOperationStmt(OperationStmt *opStmt) {
if (auto affineApplyOp = opStmt->getAs<AffineApplyOp>()) {
if (auto affineApplyOp = opStmt->dyn_cast<AffineApplyOp>()) {
forwardSubstitute(affineApplyOp);
bool allUsesEmpty = true;
for (auto *result : affineApplyOp->getOperation()->getResults()) {

View File

@ -53,7 +53,7 @@ bool ConstantFold::foldOperation(Operation *op,
// If this operation is already a constant, just remember it for cleanup
// later, and don't try to fold it.
if (auto constant = op->getAs<ConstantOp>()) {
if (auto constant = op->dyn_cast<ConstantOp>()) {
existingConstants.push_back(constant);
return true;
}
@ -64,7 +64,7 @@ bool ConstantFold::foldOperation(Operation *op,
for (auto *operand : op->getOperands()) {
Attribute *operandCst = nullptr;
if (auto *operandOp = operand->getDefiningOperation()) {
if (auto operandConstantOp = operandOp->getAs<ConstantOp>())
if (auto operandConstantOp = operandOp->dyn_cast<ConstantOp>())
operandCst = operandConstantOp->getValue();
}
operandConstants.push_back(operandCst);

View File

@ -61,8 +61,8 @@ MLFunctionPass *mlir::createPipelineDataTransferPass() {
// Temporary utility: will be replaced when DmaStart/DmaFinish abstract op's are
// added. TODO(b/117228571)
static unsigned getTagMemRefPos(const OperationStmt &dmaStmt) {
assert(dmaStmt.is<DmaStartOp>() || dmaStmt.is<DmaWaitOp>());
if (dmaStmt.is<DmaStartOp>()) {
assert(dmaStmt.isa<DmaStartOp>() || dmaStmt.isa<DmaWaitOp>());
if (dmaStmt.isa<DmaStartOp>()) {
// Second to last operand.
return dmaStmt.getNumOperands() - 2;
}
@ -166,12 +166,12 @@ static void findMatchingStartFinishStmts(
if (!opStmt)
continue;
// Collect DMA finish statements.
if (opStmt->is<DmaWaitOp>()) {
if (opStmt->isa<DmaWaitOp>()) {
dmaFinishStmts.push_back(opStmt);
continue;
}
OpPointer<DmaStartOp> dmaStartOp;
if (!(dmaStartOp = opStmt->getAs<DmaStartOp>()))
if (!(dmaStartOp = opStmt->dyn_cast<DmaStartOp>()))
continue;
// Only DMAs incoming into higher memory spaces.
// TODO(bondhugula): outgoing DMAs.
@ -197,8 +197,8 @@ static void findMatchingStartFinishStmts(
// For each start statement, we look for a matching finish statement.
for (auto *dmaStartStmt : dmaStartStmts) {
for (auto *dmaFinishStmt : dmaFinishStmts) {
if (checkTagMatch(dmaStartStmt->getAs<DmaStartOp>(),
dmaFinishStmt->getAs<DmaWaitOp>())) {
if (checkTagMatch(dmaStartStmt->cast<DmaStartOp>(),
dmaFinishStmt->cast<DmaWaitOp>())) {
startWaitPairs.push_back({dmaStartStmt, dmaFinishStmt});
break;
}
@ -235,7 +235,7 @@ PassResult PipelineDataTransfer::runOnForStmt(ForStmt *forStmt) {
for (auto &pair : startWaitPairs) {
auto *dmaStartStmt = pair.first;
const MLValue *oldMemRef = cast<MLValue>(dmaStartStmt->getOperand(
dmaStartStmt->getAs<DmaStartOp>()->getFasterMemPos()));
dmaStartStmt->cast<DmaStartOp>()->getFasterMemPos()));
if (!doubleBuffer(oldMemRef, forStmt)) {
// Normally, double buffering should not fail because we already checked
// that there are no uses outside.
@ -264,7 +264,7 @@ PassResult PipelineDataTransfer::runOnForStmt(ForStmt *forStmt) {
DenseMap<const Statement *, unsigned> stmtDelayMap;
for (auto &pair : startWaitPairs) {
auto *dmaStartStmt = pair.first;
assert(dmaStartStmt->is<DmaStartOp>());
assert(dmaStartStmt->isa<DmaStartOp>());
stmtDelayMap[dmaStartStmt] = 0;
// Set shifts for DMA start stmt's affine operand computation slices to 0.
if (auto *slice = mlir::createAffineComputationSlice(dmaStartStmt)) {

View File

@ -35,8 +35,8 @@ using namespace mlir;
// Temporary utility: will be replaced when this is modeled through
// side-effects/op traits. TODO(b/117228571)
static bool isMemRefDereferencingOp(const Operation &op) {
if (op.is<LoadOp>() || op.is<StoreOp>() || op.is<DmaStartOp>() ||
op.is<DmaWaitOp>())
if (op.isa<LoadOp>() || op.isa<StoreOp>() || op.isa<DmaStartOp>() ||
op.isa<DmaWaitOp>())
return true;
return false;
}
@ -175,8 +175,8 @@ mlir::createComposedAffineApplyOp(MLFuncBuilder *builder, Location *loc,
AffineValueMap valueMap(map, operands);
for (auto *opStmt : affineApplyOps) {
assert(opStmt->is<AffineApplyOp>());
auto affineApplyOp = opStmt->getAs<AffineApplyOp>();
assert(opStmt->isa<AffineApplyOp>());
auto affineApplyOp = opStmt->cast<AffineApplyOp>();
// Forward substitute 'affineApplyOp' into 'valueMap'.
valueMap.forwardSubstitute(*affineApplyOp);
}
@ -231,7 +231,7 @@ OperationStmt *mlir::createAffineComputationSlice(OperationStmt *opStmt) {
subOperands.reserve(opStmt->getNumOperands());
for (auto *operand : opStmt->getOperands()) {
auto *defStmt = operand->getDefiningStmt();
if (defStmt && defStmt->is<AffineApplyOp>()) {
if (defStmt && defStmt->isa<AffineApplyOp>()) {
subOperands.push_back(operand);
}
}
@ -307,7 +307,7 @@ void mlir::forwardSubstitute(OpPointer<AffineApplyOp> affineApplyOp) {
auto *useStmt = use.getOwner();
auto *useOpStmt = dyn_cast<OperationStmt>(useStmt);
// Skip if use is not AffineApplyOp.
if (useOpStmt == nullptr || !useOpStmt->is<AffineApplyOp>())
if (useOpStmt == nullptr || !useOpStmt->isa<AffineApplyOp>())
continue;
// Advance iterator past 'opStmt' operands which also use 'result'.
while (it != result->use_end() && it->getOwner() == useStmt)
@ -315,7 +315,7 @@ void mlir::forwardSubstitute(OpPointer<AffineApplyOp> affineApplyOp) {
MLFuncBuilder builder(useOpStmt);
// Initialize AffineValueMap with 'affineApplyOp' which uses 'result'.
auto oldAffineApplyOp = useOpStmt->getAs<AffineApplyOp>();
auto oldAffineApplyOp = useOpStmt->cast<AffineApplyOp>();
AffineValueMap valueMap(*oldAffineApplyOp);
// Forward substitute 'result' at index 'i' into 'valueMap'.
valueMap.forwardSubstituteSingle(*affineApplyOp, resultIndex);