Add a utility Instruction::getDialect method to return the dialect an operation is associated with, or nullptr if the associated dialect has not been registered.

PiperOrigin-RevId: 240402300
This commit is contained in:
River Riddle 2019-03-26 12:22:52 -07:00 committed by jpienaar
parent bee7b53031
commit 97db10d413
2 changed files with 17 additions and 0 deletions

View File

@ -92,6 +92,10 @@ public:
/// Return the context this operation is associated with.
MLIRContext *getContext();
/// Return the dialact this operation is associated with, or nullptr if the
/// associated dialect is not registered.
Dialect *getDialect();
/// The source location the operation was defined or derived from.
Location getLoc() { return location; }

View File

@ -300,6 +300,19 @@ MLIRContext *Instruction::getContext() {
return getFunction()->getContext();
}
/// Return the dialact this operation is associated with, or nullptr if the
/// associated dialect is not registered.
Dialect *Instruction::getDialect() {
if (auto *abstractOp = getAbstractOperation())
return &abstractOp->dialect;
// If this operation hasn't been registered or doesn't have abstract
// operation, fall back to a dialect which matches the prefix.
auto opName = getName().getStringRef();
auto dialectPrefix = opName.split('.').first;
return getContext()->getRegisteredDialect(dialectPrefix);
}
Instruction *Instruction::getParentInst() {
return block ? block->getContainingInst() : nullptr;
}