Skip to content

Commit 5d613e4

Browse files
author
Vladislav Vinogradov
committed
[mlir][ODS] Use StringLiteral instead of StringRef when applicable
Use `StringLiteral` for function return type if it is known to return constant string literals only. This will make it visible to API users, that such values can be safely stored, since they refers to constant data, which will never be deallocated. `StringRef` is general is not safe to store for a long term, since it might refer to temporal data allocated in heap. Add `inline` and `constexpr` methods support to `OpMethod`. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D97390
1 parent e3b350c commit 5d613e4

File tree

8 files changed

+44
-9
lines changed

8 files changed

+44
-9
lines changed

mlir/include/mlir/TableGen/OpClass.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ class OpMethod {
238238
MP_Constructor = 0x2,
239239
MP_Private = 0x4,
240240
MP_Declaration = 0x8,
241+
MP_Inline = 0x10,
242+
MP_Constexpr = 0x20 | MP_Inline,
241243
MP_StaticDeclaration = MP_Static | MP_Declaration,
242244
};
243245

@@ -260,6 +262,9 @@ class OpMethod {
260262
// Returns true if this is a private method.
261263
bool isPrivate() const { return properties & MP_Private; }
262264

265+
// Returns true if this is an inline method.
266+
bool isInline() const { return properties & MP_Inline; }
267+
263268
// Returns the name of this method.
264269
StringRef getName() const { return methodSignature.getName(); }
265270

mlir/lib/TableGen/OpClass.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,25 @@ void OpMethod::writeDeclTo(raw_ostream &os) const {
201201
os.indent(2);
202202
if (isStatic())
203203
os << "static ";
204+
if (properties & MP_Constexpr)
205+
os << "constexpr ";
204206
methodSignature.writeDeclTo(os);
205-
os << ";";
207+
if (!isInline())
208+
os << ";";
209+
else {
210+
os << " {\n";
211+
methodBody.writeTo(os);
212+
os << "}";
213+
}
206214
}
207215

208216
void OpMethod::writeDefTo(raw_ostream &os, StringRef namePrefix) const {
209217
// Do not write definition if the method is decl only.
210218
if (properties & MP_Declaration)
211219
return;
220+
// Do not generate separate definition for inline method
221+
if (isInline())
222+
return;
212223
methodSignature.writeDefTo(os, namePrefix);
213224
os << " {\n";
214225
methodBody.writeTo(os);

mlir/test/mlir-tblgen/op-decl-and-defs.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def NS_AOp : NS_Op<"a_op", [IsolatedFromAbove, IsolatedFromAbove]> {
7171
// CHECK: public:
7272
// CHECK: using Op::Op;
7373
// CHECK: using Adaptor = AOpAdaptor;
74-
// CHECK: static ::llvm::StringRef getOperationName();
74+
// CHECK: static constexpr ::llvm::StringLiteral getOperationName() {
75+
// CHECK: return ::llvm::StringLiteral("test.a_op");
76+
// CHECK: }
7577
// CHECK: ::mlir::Operation::operand_range getODSOperands(unsigned index);
7678
// CHECK: ::mlir::Value a();
7779
// CHECK: ::mlir::Operation::operand_range b();

mlir/test/mlir-tblgen/typedefs.td

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def B_CompoundTypeA : TestType<"CompoundA"> {
5959
// DECL-LABEL: class CompoundAType : public ::mlir::Type
6060
// DECL: static CompoundAType getChecked(llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::MLIRContext *context, int widthOfSomething, ::mlir::test::SimpleTypeA exampleTdType, SomeCppStruct exampleCppType, ::llvm::ArrayRef<int> dims, ::mlir::Type inner);
6161
// DECL: static ::mlir::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, int widthOfSomething, ::mlir::test::SimpleTypeA exampleTdType, SomeCppStruct exampleCppType, ::llvm::ArrayRef<int> dims, ::mlir::Type inner);
62-
// DECL: static ::llvm::StringRef getMnemonic() { return "cmpnd_a"; }
62+
// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
63+
// DECL: return ::llvm::StringLiteral("cmpnd_a");
64+
// DECL: }
6365
// DECL: static ::mlir::Type parse(::mlir::MLIRContext *context,
6466
// DECL-NEXT: ::mlir::DialectAsmParser &parser);
6567
// DECL: void print(::mlir::DialectAsmPrinter &printer) const;
@@ -77,7 +79,9 @@ def C_IndexType : TestType<"Index"> {
7779
);
7880

7981
// DECL-LABEL: class IndexType : public ::mlir::Type
80-
// DECL: static ::llvm::StringRef getMnemonic() { return "index"; }
82+
// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
83+
// DECL: return ::llvm::StringLiteral("index");
84+
// DECL: }
8185
// DECL: static ::mlir::Type parse(::mlir::MLIRContext *context,
8286
// DECL-NEXT: ::mlir::DialectAsmParser &parser);
8387
// DECL: void print(::mlir::DialectAsmPrinter &printer) const;

mlir/tools/mlir-tblgen/DialectGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ class {0} : public ::mlir::Dialect {
7575
void initialize();
7676
friend class ::mlir::MLIRContext;
7777
public:
78-
static ::llvm::StringRef getDialectNamespace() { return "{1}"; }
78+
static constexpr ::llvm::StringLiteral getDialectNamespace() {
79+
return ::llvm::StringLiteral("{1}");
80+
}
7981
)";
8082

8183
/// Registration for a single dependent dialect: to be inserted in the ctor

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,8 +2181,10 @@ void OpEmitter::genTraits() {
21812181

21822182
void OpEmitter::genOpNameGetter() {
21832183
auto *method = opClass.addMethodAndPrune(
2184-
"::llvm::StringRef", "getOperationName", OpMethod::MP_Static);
2185-
method->body() << " return \"" << op.getOperationName() << "\";\n";
2184+
"::llvm::StringLiteral", "getOperationName",
2185+
OpMethod::Property(OpMethod::MP_Static | OpMethod::MP_Constexpr));
2186+
method->body() << " return ::llvm::StringLiteral(\"" << op.getOperationName()
2187+
<< "\");";
21862188
}
21872189

21882190
void OpEmitter::genOpAsmInterface() {

mlir/tools/mlir-tblgen/PassGen.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ const char *const passDeclBegin = R"(
4545
template <typename DerivedT>
4646
class {0}Base : public {1} {
4747
public:
48+
using Base = {0}Base;
49+
4850
{0}Base() : {1}(::mlir::TypeID::get<DerivedT>()) {{}
4951
{0}Base(const {0}Base &) : {1}(::mlir::TypeID::get<DerivedT>()) {{}
5052
5153
/// Returns the command-line argument attached to this pass.
54+
static constexpr ::llvm::StringLiteral getArgumentName() {
55+
return ::llvm::StringLiteral("{2}");
56+
}
5257
::llvm::StringRef getArgument() const override { return "{2}"; }
5358
5459
/// Returns the derived pass name.
60+
static constexpr ::llvm::StringLiteral getPassName() {
61+
return ::llvm::StringLiteral("{0}");
62+
}
5563
::llvm::StringRef getName() const override { return "{0}"; }
5664
5765
/// Support isa/dyn_cast functionality for the derived pass class.

mlir/tools/mlir-tblgen/TypeDefGen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,9 @@ static void emitTypeDefDecl(const TypeDef &typeDef, raw_ostream &os) {
277277

278278
// Emit the mnenomic, if specified.
279279
if (auto mnenomic = typeDef.getMnemonic()) {
280-
os << " static ::llvm::StringRef getMnemonic() { return \"" << mnenomic
281-
<< "\"; }\n";
280+
os << " static constexpr ::llvm::StringLiteral getMnemonic() {\n"
281+
<< " return ::llvm::StringLiteral(\"" << mnenomic << "\");\n"
282+
<< " }\n";
282283

283284
// If mnemonic specified, emit print/parse declarations.
284285
if (typeDef.getParserCode() || typeDef.getPrinterCode() || !params.empty())

0 commit comments

Comments
 (0)