llvm-project/mlir/lib/IR/BuiltinDialectBytecode.cpp
Tobias Gysi 728a8d5a81 [mlir] Add a builtin distinct attribute
A distinct attribute associates a referenced attribute with a unique
identifier. Every call to its create function allocates a new
distinct attribute instance. The address of the attribute instance
temporarily serves as its unique identifier. Similar to the names
of SSA values, the final unique identifiers are generated during
pretty printing.

Examples:
 #distinct = distinct[0]<42.0 : f32>
 #distinct1 = distinct[1]<42.0 : f32>
 #distinct2 = distinct[2]<array<i32: 10, 42>>

This mechanism is meant to generate attributes with a unique
identifier, which can be used to mark groups of operations
that share a common properties such as if they are aliasing.

The design of the distinct attribute ensures minimal memory
footprint per distinct attribute since it only contains a reference
to another attribute. All distinct attributes are stored outside of
the storage uniquer in a thread local store that is part of the
context. It uses one bump pointer allocator per thread to ensure
distinct attributes can be created in-parallel.

Reviewed By: rriddle, Dinistro, zero9178

Differential Revision: https://reviews.llvm.org/D153360
2023-07-11 07:33:16 +00:00

118 lines
4.0 KiB
C++

//===- BuiltinDialectBytecode.cpp - Builtin Bytecode Implementation -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "BuiltinDialectBytecode.h"
#include "AttributeDetail.h"
#include "mlir/Bytecode/BytecodeImplementation.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/DialectResourceBlobManager.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
//===----------------------------------------------------------------------===//
// BuiltinDialectBytecodeInterface
//===----------------------------------------------------------------------===//
namespace {
//===----------------------------------------------------------------------===//
// Utility functions
// TODO: Move these to separate file.
// Returns the bitwidth if known, else return 0.
static unsigned getIntegerBitWidth(DialectBytecodeReader &reader, Type type) {
if (auto intType = dyn_cast<IntegerType>(type)) {
return intType.getWidth();
} else if (llvm::isa<IndexType>(type)) {
return IndexType::kInternalStorageBitWidth;
}
reader.emitError()
<< "expected integer or index type for IntegerAttr, but got: " << type;
return 0;
}
static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
Type type, FailureOr<APInt> &val) {
unsigned bitWidth = getIntegerBitWidth(reader, type);
val = reader.readAPIntWithKnownWidth(bitWidth);
return val;
}
static LogicalResult
readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
FailureOr<APFloat> &val) {
auto ftype = dyn_cast<FloatType>(type);
if (!ftype)
return failure();
val = reader.readAPFloatWithKnownSemantics(ftype.getFloatSemantics());
return success();
}
LogicalResult
readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
bool isSplat,
SmallVectorImpl<StringRef> &rawStringData) {
rawStringData.resize(isSplat ? 1 : type.getNumElements());
for (StringRef &value : rawStringData)
if (failed(reader.readString(value)))
return failure();
return success();
}
void writePotentiallySplatString(DialectBytecodeWriter &writer,
DenseStringElementsAttr attr) {
bool isSplat = attr.isSplat();
if (isSplat)
return writer.writeOwnedString(attr.getRawStringData().front());
for (StringRef str : attr.getRawStringData())
writer.writeOwnedString(str);
}
#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
/// This class implements the bytecode interface for the builtin dialect.
struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
BuiltinDialectBytecodeInterface(Dialect *dialect)
: BytecodeDialectInterface(dialect) {}
//===--------------------------------------------------------------------===//
// Attributes
Attribute readAttribute(DialectBytecodeReader &reader) const override {
return ::readAttribute(getContext(), reader);
}
LogicalResult writeAttribute(Attribute attr,
DialectBytecodeWriter &writer) const override {
return ::writeAttribute(attr, writer);
}
//===--------------------------------------------------------------------===//
// Types
Type readType(DialectBytecodeReader &reader) const override {
return ::readType(getContext(), reader);
}
LogicalResult writeType(Type type,
DialectBytecodeWriter &writer) const override {
return ::writeType(type, writer);
}
};
} // namespace
void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
dialect->addInterfaces<BuiltinDialectBytecodeInterface>();
}