Skip to content

Commit f88ae0d

Browse files
committed
refactor / cleanup
1 parent d76f47b commit f88ae0d

File tree

6 files changed

+29
-30
lines changed

6 files changed

+29
-30
lines changed

mlir/include/mlir/Bytecode/BytecodeOpInterface.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def FallbackBytecodeOpInterface : OpInterface<"FallbackBytecodeOpInterface"> {
5353
Set the original name for this operation from the bytecode.
5454
}],
5555
"void", "setOriginalOperationName", (ins
56-
"::mlir::StringRef":$opName,
56+
"const ::mlir::Twine&":$opName,
5757
"::mlir::OperationState &":$state)
5858
>,
5959
InterfaceMethod<[{

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/ScopeExit.h"
2222
#include "llvm/ADT/StringExtras.h"
2323
#include "llvm/ADT/StringRef.h"
24+
#include "llvm/ADT/Twine.h"
2425
#include "llvm/Support/Endian.h"
2526
#include "llvm/Support/MemoryBufferRef.h"
2627
#include "llvm/Support/SourceMgr.h"
@@ -2257,8 +2258,11 @@ BytecodeReader::Impl::parseOpWithoutRegions(EncodingReader &reader,
22572258
// state.
22582259
OperationState opState(opLoc, *opName);
22592260
// If this is a fallback op, provide the original name of the operation.
2260-
if (auto *iface = opName->getInterface<FallbackBytecodeOpInterface>())
2261-
iface->setOriginalOperationName((*bytecodeOp)->name, opState);
2261+
if (auto *iface = opName->getInterface<FallbackBytecodeOpInterface>()) {
2262+
const Twine originalName =
2263+
opName->getDialect()->getNamespace() + "." + (*bytecodeOp)->name;
2264+
iface->setOriginalOperationName(originalName, opState);
2265+
}
22622266

22632267
// Parse the attributes of the operation.
22642268
if (opMask & bytecode::OpEncodingMask::kHasAttrs) {

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,12 @@ void BytecodeWriter::writeDialectSection(EncodingEmitter &emitter) {
848848

849849
// Emit the referenced operation names grouped by dialect.
850850
auto emitOpName = [&](OpNameNumbering &name) {
851-
bool isRegistered = name.name.isRegistered();
852-
// If we're writing a fallback op, write it as if it were a registered op.
853-
if (name.name.hasInterface<FallbackBytecodeOpInterface>())
854-
isRegistered = true;
855-
851+
const bool isKnownOp = name.isOpaqueEntry || name.name.isRegistered();
856852
size_t stringId = stringSection.insert(name.name.stripDialect());
857853
if (config.bytecodeVersion < bytecode::kNativePropertiesEncoding)
858854
dialectEmitter.emitVarInt(stringId, "dialect op name");
859855
else
860-
dialectEmitter.emitVarIntWithFlag(stringId, isRegistered,
861-
"dialect op name");
856+
dialectEmitter.emitVarIntWithFlag(stringId, isKnownOp, "dialect op name");
862857
};
863858
writeDialectGrouping(dialectEmitter, numberingState.getOpNames(), emitOpName);
864859

@@ -1000,10 +995,8 @@ LogicalResult BytecodeWriter::writeOp(EncodingEmitter &emitter, Operation *op) {
1000995
// For fallback ops, create a new operation name referencing the original op
1001996
// instead.
1002997
if (auto fallback = dyn_cast<FallbackBytecodeOpInterface>(op))
1003-
opName = OperationName((fallback->getDialect()->getNamespace() + "." +
1004-
fallback.getOriginalOperationName())
1005-
.str(),
1006-
op->getContext());
998+
opName =
999+
OperationName(fallback.getOriginalOperationName(), op->getContext());
10071000

10081001
emitter.emitVarInt(numberingState.getNumber(opName), "op name ID");
10091002

mlir/lib/Bytecode/Writer/IRNumbering.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,14 @@ void IRNumberingState::number(Operation &op) {
420420
// Number the components of an operation that won't be numbered elsewhere
421421
// (e.g. we don't number operands, regions, or successors here).
422422

423-
OperationName opName = op.getName();
424-
// For fallback ops, create a new operation name referencing the original op
423+
// For fallback ops, create a new OperationName referencing the original op
425424
// instead.
426-
if (auto fallback = dyn_cast<FallbackBytecodeOpInterface>(op))
427-
opName = OperationName((fallback->getDialect()->getNamespace() + "." +
428-
fallback.getOriginalOperationName())
429-
.str(),
430-
op.getContext());
431-
number(opName);
425+
if (auto fallback = dyn_cast<FallbackBytecodeOpInterface>(op)) {
426+
OperationName opName(fallback.getOriginalOperationName(), op.getContext());
427+
number(opName, /*isOpaque=*/true);
428+
} else {
429+
number(op.getName(), /*isOpaque=*/false);
430+
}
432431

433432
for (OpResult result : op.getResults()) {
434433
valueIDs.try_emplace(result, nextValueID++);
@@ -467,7 +466,7 @@ void IRNumberingState::number(Operation &op) {
467466
number(op.getLoc());
468467
}
469468

470-
void IRNumberingState::number(OperationName opName) {
469+
void IRNumberingState::number(OperationName opName, bool isOpaque) {
471470
OpNameNumbering *&numbering = opNames[opName];
472471
if (numbering) {
473472
++numbering->refCount;
@@ -479,8 +478,8 @@ void IRNumberingState::number(OperationName opName) {
479478
else
480479
dialectNumber = &numberDialect(opName.getDialectNamespace());
481480

482-
numbering =
483-
new (opNameAllocator.Allocate()) OpNameNumbering(dialectNumber, opName);
481+
numbering = new (opNameAllocator.Allocate())
482+
OpNameNumbering(dialectNumber, opName, isOpaque);
484483
orderedOpNames.push_back(numbering);
485484
}
486485

mlir/lib/Bytecode/Writer/IRNumbering.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ struct TypeNumbering : public AttrTypeNumbering {
6363

6464
/// This class represents the numbering entry of an operation name.
6565
struct OpNameNumbering {
66-
OpNameNumbering(DialectNumbering *dialect, OperationName name)
67-
: dialect(dialect), name(name) {}
66+
OpNameNumbering(DialectNumbering *dialect, OperationName name, bool isOpaque)
67+
: dialect(dialect), name(name), isOpaqueEntry(isOpaque) {}
6868

6969
/// The dialect of this value.
7070
DialectNumbering *dialect;
7171

7272
/// The concrete name.
7373
OperationName name;
7474

75+
/// This entry represents an opaque operation entry.
76+
bool isOpaqueEntry = false;
77+
7578
/// The number assigned to this name.
7679
unsigned number = 0;
7780

@@ -210,7 +213,7 @@ class IRNumberingState {
210213

211214
/// Get the set desired bytecode version to emit.
212215
int64_t getDesiredBytecodeVersion() const;
213-
216+
214217
private:
215218
/// This class is used to provide a fake dialect writer for numbering nested
216219
/// attributes and types.
@@ -225,7 +228,7 @@ class IRNumberingState {
225228
DialectNumbering &numberDialect(Dialect *dialect);
226229
DialectNumbering &numberDialect(StringRef dialect);
227230
void number(Operation &op);
228-
void number(OperationName opName);
231+
void number(OperationName opName, bool isOpaque);
229232
void number(Region &region);
230233
void number(Type type);
231234

mlir/test/lib/Dialect/Test/TestOpDefs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ void TestVersionedOpA::writeProperties(mlir::DialectBytecodeWriter &writer) {
12611261
// TestBytecodeFallbackOp
12621262
//===----------------------------------------------------------------------===//
12631263

1264-
void TestBytecodeFallbackOp::setOriginalOperationName(StringRef name,
1264+
void TestBytecodeFallbackOp::setOriginalOperationName(const Twine &name,
12651265
OperationState &state) {
12661266
state.getOrAddProperties<Properties>().setOpname(
12671267
StringAttr::get(state.getContext(), name));

0 commit comments

Comments
 (0)