MCPcopy Create free account
hub / github.com/NVIDIA/cuda-tile / DenseElementsAttrCache

Class DenseElementsAttrCache

lib/Bytecode/Reader/BytecodeReader.cpp:664–723  ·  view source on GitHub ↗

A cache for deduplicating constant attributes during parsing.

Source from the content-addressed store, hash-verified

662namespace {
663/// A cache for deduplicating constant attributes during parsing.
664class DenseElementsAttrCache {
665public:
666 FailureOr<DenseElementsAttr> getOrCreate(Type type, ArrayRef<uint8_t> data,
667 MLIRContext &context) {
668 // The key is a combination of the expected type and the raw data blob.
669 std::pair<Type, ArrayRef<uint8_t>> key = {type, data};
670 auto it = cache.find(key);
671 if (it != cache.end())
672 return it->second;
673
674 // Create a reader for the constant data blob.
675 EncodingReader reader(data, context);
676
677 // Cast to TileType to get element type and shape info.
678 if (!type)
679 return reader.emitError() << "provided type is null";
680 auto tileType = mlir::dyn_cast<cuda_tile::TileType>(type);
681 if (!tileType || !tileType.getElementType().isIntOrFloat())
682 return reader.emitError()
683 << "expect Cuda Tile integer or float type but got: " << tileType;
684
685 // Read the size of the raw data buffer.
686 uint64_t rawDataSize;
687 if (failed(reader.readVarInt(rawDataSize)))
688 return reader.emitError() << "failed to read the size of the data buffer";
689
690 // Read the raw byte data.
691 ArrayRef<uint8_t> rawUint8Data;
692 if (failed(reader.readBytes(rawDataSize, rawUint8Data)))
693 return reader.emitError() << "failed to read the raw byte data";
694
695 // Convert ArrayRef<uint8_t> to ArrayRef<char>.
696 ArrayRef<char> rawData(reinterpret_cast<const char *>(rawUint8Data.data()),
697 rawUint8Data.size());
698 // Validate the buffer size and format.
699 if (!DenseElementsAttr::isValidRawBuffer(tileType, rawData)) {
700 return reader.emitError() << "failed to validate buffer size and format";
701 }
702
703 DenseElementsAttr attr = nullptr;
704 // Handle endianness conversion.
705 if (llvm::endianness::native == llvm::endianness::big) {
706 // Convert endianess.
707 SmallVector<char, 64> outDataVec(rawData.size());
708 MutableArrayRef<char> convRawData(outDataVec);
709 DenseTypedElementsAttr::convertEndianOfArrayRefForBEmachine(
710 rawData, convRawData, tileType);
711 attr = DenseElementsAttr::getFromRawBuffer(tileType, convRawData);
712 } else {
713 attr = DenseElementsAttr::getFromRawBuffer(tileType, rawData);
714 }
715
716 if (attr)
717 cache.insert({key, attr});
718 return attr;
719 }
720
721private:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected