diff --git a/mlir/include/mlir/IR/Builders.h b/mlir/include/mlir/IR/Builders.h --- a/mlir/include/mlir/IR/Builders.h +++ b/mlir/include/mlir/IR/Builders.h @@ -53,6 +53,7 @@ MLIRContext *getContext() const { return context; } Identifier getIdentifier(StringRef str); + Identifier getIdentifierTwine(const Twine &str); // Locations. Location getUnknownLoc(); @@ -95,6 +96,7 @@ FloatAttr getFloatAttr(Type type, double value); FloatAttr getFloatAttr(Type type, const APFloat &value); StringAttr getStringAttr(StringRef bytes); + StringAttr getStringAttrTwine(const Twine &bytes); ArrayAttr getArrayAttr(ArrayRef value); FlatSymbolRefAttr getSymbolRefAttr(Operation *value); FlatSymbolRefAttr getSymbolRefAttr(StringRef value); @@ -393,7 +395,7 @@ /// Create an operation of specific op type at the current insertion point. template - OpTy create(Location location, Args &&... args) { + OpTy create(Location location, Args &&...args) { OperationState state(location, OpTy::getOperationName()); if (!state.name.getAbstractOperation()) llvm::report_fatal_error("Building op `" + @@ -411,7 +413,7 @@ /// the results after folding the operation. template void createOrFold(SmallVectorImpl &results, Location location, - Args &&... args) { + Args &&...args) { // Create the operation without using 'createOperation' as we don't want to // insert it yet. OperationState state(location, OpTy::getOperationName()); @@ -433,7 +435,7 @@ template typename std::enable_if(), Value>::type - createOrFold(Location location, Args &&... args) { + createOrFold(Location location, Args &&...args) { SmallVector results; createOrFold(results, location, std::forward(args)...); return results.front(); @@ -443,7 +445,7 @@ template typename std::enable_if(), OpTy>::type - createOrFold(Location location, Args &&... args) { + createOrFold(Location location, Args &&...args) { auto op = create(location, std::forward(args)...); SmallVector unused; tryFold(op.getOperation(), unused); diff --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td --- a/mlir/include/mlir/IR/BuiltinAttributes.td +++ b/mlir/include/mlir/IR/BuiltinAttributes.td @@ -828,6 +828,10 @@ let extraClassDeclaration = [{ using ValueType = StringRef; + // Twine support. + static StringAttr getTwine(const Twine &bytes, Type type); + static StringAttr getTwine(MLIRContext *context, const Twine &bytes); + private: /// Return an empty StringAttr with NoneType type. This is a special variant /// of the `get` method that is used by the MLIRContext to cache the diff --git a/mlir/include/mlir/IR/Identifier.h b/mlir/include/mlir/IR/Identifier.h --- a/mlir/include/mlir/IR/Identifier.h +++ b/mlir/include/mlir/IR/Identifier.h @@ -39,6 +39,8 @@ public: /// Return an identifier for the specified string. static Identifier get(StringRef str, MLIRContext *context); + static Identifier getTwine(const Twine &str, MLIRContext *context); + Identifier(const Identifier &) = default; Identifier &operator=(const Identifier &other) = default; diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp --- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp +++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp @@ -291,12 +291,9 @@ if (parser.parseLParen() || parser.parseKeyword(&defval) || parser.parseRParen()) return failure(); - SmallString<16> attrval; // The def prefix is required for the attribute as "private" is a keyword // in C++. - attrval += "def"; - attrval += defval; - auto attr = parser.getBuilder().getStringAttr(attrval); + auto attr = parser.getBuilder().getStringAttrTwine("def" + defval); result.addAttribute("default_val", attr); } else if (keyword == "proc_bind") { // Fail if there was already another proc_bind clause. diff --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp --- a/mlir/lib/IR/Builders.cpp +++ b/mlir/lib/IR/Builders.cpp @@ -22,6 +22,9 @@ Identifier Builder::getIdentifier(StringRef str) { return Identifier::get(str, context); } +Identifier Builder::getIdentifierTwine(const Twine &str) { + return Identifier::getTwine(str, context); +} //===----------------------------------------------------------------------===// // Locations. @@ -203,6 +206,9 @@ StringAttr Builder::getStringAttr(StringRef bytes) { return StringAttr::get(context, bytes); } +StringAttr Builder::getStringAttrTwine(const Twine &bytes) { + return StringAttr::getTwine(context, bytes); +} ArrayAttr Builder::getArrayAttr(ArrayRef value) { return ArrayAttr::get(context, value); diff --git a/mlir/lib/IR/BuiltinAttributes.cpp b/mlir/lib/IR/BuiltinAttributes.cpp --- a/mlir/lib/IR/BuiltinAttributes.cpp +++ b/mlir/lib/IR/BuiltinAttributes.cpp @@ -197,10 +197,29 @@ return Base::get(context, ArrayRef()); } +//===----------------------------------------------------------------------===// +// StringAttr +//===----------------------------------------------------------------------===// + StringAttr StringAttr::getEmptyStringAttrUnchecked(MLIRContext *context) { return Base::get(context, "", NoneType::get(context)); } +/// Twine support for StringAttr. +StringAttr StringAttr::getTwine(MLIRContext *context, const Twine &twine) { + // Fast-path empty twine. + if (twine.isTriviallyEmpty()) + return get(context); + SmallVector tempStr; + return get(context, twine.toStringRef(tempStr)); +} + +/// Twine support for StringAttr. +StringAttr StringAttr::getTwine(const Twine &twine, Type type) { + SmallVector tempStr; + return get(twine.toStringRef(tempStr), type); +} + //===----------------------------------------------------------------------===// // FloatAttr //===----------------------------------------------------------------------===// diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp --- a/mlir/lib/IR/MLIRContext.cpp +++ b/mlir/lib/IR/MLIRContext.cpp @@ -800,6 +800,11 @@ return Identifier(localEntry); } +Identifier Identifier::getTwine(const Twine &str, MLIRContext *context) { + SmallString<32> tempStr; + return get(str.toStringRef(tempStr), context); +} + Dialect *Identifier::getDialect() { return entry->second.dyn_cast(); }