Skip to content

Commit 01700c4

Browse files
committed
Store an Identifier instead of a StringRef for the OperationName inside an AbstractOperation (NFC)
Instead of storing a StringRef, we keep an Identifier which otherwise requires a lock on the context to retrieve. This will allow to get an Identifier for any registered Operation for "free". Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D86994
1 parent 2d11ae0 commit 01700c4

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

mlir/include/mlir/IR/Identifier.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class Identifier {
6767
return Identifier(static_cast<const EntryType *>(entry));
6868
}
6969

70+
/// Compare the underlying StringRef.
71+
int compare(Identifier rhs) const { return strref().compare(rhs.strref()); }
72+
7073
private:
7174
/// This contains the bytes of the string, which is guaranteed to be nul
7275
/// terminated.

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class AbstractOperation {
8282
using OperationProperties = uint32_t;
8383

8484
/// This is the name of the operation.
85-
const StringRef name;
85+
const Identifier name;
8686

8787
/// This is the dialect that this operation belongs to.
8888
Dialect &dialect;
@@ -171,13 +171,7 @@ class AbstractOperation {
171171
SmallVectorImpl<OpFoldResult> &results),
172172
void (&getCanonicalizationPatterns)(OwningRewritePatternList &results,
173173
MLIRContext *context),
174-
detail::InterfaceMap &&interfaceMap, bool (&hasTrait)(TypeID traitID))
175-
: name(name), dialect(dialect), typeID(typeID),
176-
parseAssembly(parseAssembly), printAssembly(printAssembly),
177-
verifyInvariants(verifyInvariants), foldHook(foldHook),
178-
getCanonicalizationPatterns(getCanonicalizationPatterns),
179-
opProperties(opProperties), interfaceMap(std::move(interfaceMap)),
180-
hasRawTrait(hasTrait) {}
174+
detail::InterfaceMap &&interfaceMap, bool (&hasTrait)(TypeID traitID));
181175

182176
/// The properties of the operation.
183177
const OperationProperties opProperties;
@@ -302,9 +296,12 @@ class OperationName {
302296
/// Return the operation name with dialect name stripped, if it has one.
303297
StringRef stripDialect() const;
304298

305-
/// Return the name of this operation. This always succeeds.
299+
/// Return the name of this operation. This always succeeds.
306300
StringRef getStringRef() const;
307301

302+
/// Return the name of this operation as an identifier. This always succeeds.
303+
Identifier getIdentifier() const;
304+
308305
/// If this operation has a registered operation description, return it.
309306
/// Otherwise return null.
310307
const AbstractOperation *getAbstractOperation() const;

mlir/lib/IR/MLIRContext.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,25 @@ const AbstractOperation *AbstractOperation::lookup(StringRef opName,
668668
return nullptr;
669669
}
670670

671+
AbstractOperation::AbstractOperation(
672+
StringRef name, Dialect &dialect, OperationProperties opProperties,
673+
TypeID typeID,
674+
ParseResult (&parseAssembly)(OpAsmParser &parser, OperationState &result),
675+
void (&printAssembly)(Operation *op, OpAsmPrinter &p),
676+
LogicalResult (&verifyInvariants)(Operation *op),
677+
LogicalResult (&foldHook)(Operation *op, ArrayRef<Attribute> operands,
678+
SmallVectorImpl<OpFoldResult> &results),
679+
void (&getCanonicalizationPatterns)(OwningRewritePatternList &results,
680+
MLIRContext *context),
681+
detail::InterfaceMap &&interfaceMap, bool (&hasTrait)(TypeID traitID))
682+
: name(Identifier::get(name, dialect.getContext())), dialect(dialect),
683+
typeID(typeID), parseAssembly(parseAssembly),
684+
printAssembly(printAssembly), verifyInvariants(verifyInvariants),
685+
foldHook(foldHook),
686+
getCanonicalizationPatterns(getCanonicalizationPatterns),
687+
opProperties(opProperties), interfaceMap(std::move(interfaceMap)),
688+
hasRawTrait(hasTrait) {}
689+
671690
/// Get the dialect that registered the type with the provided typeid.
672691
const AbstractType &AbstractType::lookup(TypeID typeID, MLIRContext *context) {
673692
auto &impl = context->getImpl();

mlir/lib/IR/Operation.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ StringRef OperationName::stripDialect() const {
4545
return splitName.second.empty() ? splitName.first : splitName.second;
4646
}
4747

48-
/// Return the name of this operation. This always succeeds.
48+
/// Return the name of this operation. This always succeeds.
4949
StringRef OperationName::getStringRef() const {
50+
return getIdentifier().strref();
51+
}
52+
53+
/// Return the name of this operation as an identifier. This always succeeds.
54+
Identifier OperationName::getIdentifier() const {
5055
if (auto *op = representation.dyn_cast<const AbstractOperation *>())
5156
return op->name;
52-
return representation.get<Identifier>().strref();
57+
return representation.get<Identifier>();
5358
}
5459

5560
const AbstractOperation *OperationName::getAbstractOperation() const {

mlir/lib/Parser/Parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,8 @@ class CustomOpAsmParser : public OpAsmParser {
863863
/// Emit a diagnostic at the specified location and return failure.
864864
InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) override {
865865
emittedError = true;
866-
return parser.emitError(loc, "custom op '" + opDefinition->name + "' " +
867-
message);
866+
return parser.emitError(loc, "custom op '" + opDefinition->name.strref() +
867+
"' " + message);
868868
}
869869

870870
llvm::SMLoc getCurrentLocation() override {

0 commit comments

Comments
 (0)