Skip to content

Commit 8b07f22

Browse files
committed
Added PoC name retention for parsing and printing
1 parent 01ba627 commit 8b07f22

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

mlir/include/mlir/IR/MLIRContext.h

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mlir/Support/LLVM.h"
1313
#include "mlir/Support/TypeID.h"
1414
#include "llvm/ADT/ArrayRef.h"
15+
#include "llvm/ADT/DenseMap.h"
1516
#include <functional>
1617
#include <memory>
1718
#include <vector>
@@ -34,6 +35,8 @@ class MLIRContextImpl;
3435
class RegisteredOperationName;
3536
class StorageUniquer;
3637
class IRUnit;
38+
class Value;
39+
class Operation;
3740

3841
/// MLIRContext is the top-level object for a collection of MLIR operations. It
3942
/// holds immortal uniqued objects like types, and the tables used to unique
@@ -240,6 +243,11 @@ class MLIRContext {
240243
/// (attributes, operations, types, etc.).
241244
llvm::hash_code getRegistryHash();
242245

246+
///===--------------------------------------------------------------------===//
247+
llvm::DenseMap<Operation *, llvm::StringRef> valueToAlias;
248+
void setAliasName(Value *value, llvm::StringRef aliasName);
249+
llvm::StringRef getAliasName(Value *value);
250+
243251
//===--------------------------------------------------------------------===//
244252
// Action API
245253
//===--------------------------------------------------------------------===//

mlir/lib/AsmParser/Parser.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,11 @@ ParseResult OperationParser::addDefinition(UnresolvedOperand useInfo,
938938
/// Record this definition for the current scope.
939939
entries[useInfo.number] = {value, useInfo.location};
940940
recordDefinition(useInfo.name);
941+
942+
/// Register the alias name for use in printing
943+
llvm::StringRef modifiedName = useInfo.name.drop_front(1);
944+
getContext()->setAliasName(&value, modifiedName);
945+
941946
return success();
942947
}
943948

mlir/lib/IR/AsmPrinter.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ OpAsmParser::~OpAsmParser() = default;
7272
MLIRContext *AsmParser::getContext() const { return getBuilder().getContext(); }
7373

7474
/// Parse a type list.
75-
/// This is out-of-line to work-around https://github.com/llvm/llvm-project/issues/62918
75+
/// This is out-of-line to work-around
76+
/// https://github.com/llvm/llvm-project/issues/62918
7677
ParseResult AsmParser::parseTypeList(SmallVectorImpl<Type> &result) {
77-
return parseCommaSeparatedList(
78-
[&]() { return parseType(result.emplace_back()); });
79-
}
80-
81-
78+
return parseCommaSeparatedList(
79+
[&]() { return parseType(result.emplace_back()); });
80+
}
8281

8382
//===----------------------------------------------------------------------===//
8483
// DialectAsmPrinter
@@ -1417,7 +1416,9 @@ void SSANameState::printValueID(Value value, bool printResultNo,
14171416
}
14181417

14191418
stream << '%';
1420-
if (it->second != NameSentinel) {
1419+
if (!value.getContext()->getAliasName(&value).empty()) {
1420+
stream << value.getContext()->getAliasName(&value);
1421+
} else if (it->second != NameSentinel) {
14211422
stream << it->second;
14221423
} else {
14231424
auto nameIt = valueNames.find(lookupValue);

mlir/lib/IR/MLIRContext.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ void MLIRContext::executeActionInternal(function_ref<void()> actionFn,
392392

393393
bool MLIRContext::hasActionHandler() { return (bool)getImpl().actionHandler; }
394394

395+
void MLIRContext::setAliasName(Value *value, llvm::StringRef aliasName) {
396+
valueToAlias[value->getDefiningOp()] = aliasName;
397+
}
398+
llvm::StringRef MLIRContext::getAliasName(Value *value) {
399+
auto found = valueToAlias.find(value->getDefiningOp());
400+
return (found != valueToAlias.end()) ? found->second : llvm::StringRef();
401+
}
402+
395403
//===----------------------------------------------------------------------===//
396404
// Diagnostic Handlers
397405
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)