26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/TypeSwitch.h"
34bool LLVM::LifetimeStartOp::canUsesBeRemoved(
46bool LLVM::LifetimeEndOp::canUsesBeRemoved(
58bool LLVM::InvariantStartOp::canUsesBeRemoved(
70bool LLVM::InvariantEndOp::canUsesBeRemoved(
82bool LLVM::LaunderInvariantGroupOp::canUsesBeRemoved(
89DeletionKind LLVM::LaunderInvariantGroupOp::removeBlockingUses(
101template <
class MemIntr>
102std::optional<uint64_t> getStaticMemIntrLen(MemIntr op) {
106 if (memIntrLen.getBitWidth() > 64)
108 return memIntrLen.getZExtValue();
116std::optional<uint64_t> getStaticMemIntrLen(LLVM::MemcpyInlineOp op) {
117 APInt memIntrLen = op.getLen();
118 if (memIntrLen.getBitWidth() > 64)
120 return memIntrLen.getZExtValue();
128std::optional<uint64_t> getStaticMemIntrLen(LLVM::MemsetInlineOp op) {
129 APInt memIntrLen = op.getLen();
130 if (memIntrLen.getBitWidth() > 64)
132 return memIntrLen.getZExtValue();
136template <
class MemsetIntr>
137IntegerAttr createMemsetLenAttr(MemsetIntr op) {
138 IntegerAttr memsetLenAttr;
139 bool successfulMatch =
141 (
void)successfulMatch;
142 assert(successfulMatch);
143 return memsetLenAttr;
150IntegerAttr createMemsetLenAttr(LLVM::MemsetInlineOp op) {
151 return op.getLenAttr();
157template <
class MemsetIntr>
158void createMemsetIntr(
OpBuilder &builder, MemsetIntr toReplace,
159 IntegerAttr memsetLenAttr, uint64_t newMemsetSize,
164void createMemsetIntr(
OpBuilder &builder, LLVM::MemsetOp toReplace,
165 IntegerAttr memsetLenAttr, uint64_t newMemsetSize,
168 Value newMemsetSizeValue =
169 LLVM::ConstantOp::create(
170 builder, toReplace.getLen().getLoc(),
171 IntegerAttr::get(memsetLenAttr.getType(), newMemsetSize))
174 LLVM::MemsetOp::create(builder, toReplace.getLoc(), subslots.at(
index).ptr,
175 toReplace.getVal(), newMemsetSizeValue,
176 toReplace.getIsVolatile());
180void createMemsetIntr(
OpBuilder &builder, LLVM::MemsetInlineOp toReplace,
181 IntegerAttr memsetLenAttr, uint64_t newMemsetSize,
184 auto newMemsetSizeValue =
185 IntegerAttr::get(memsetLenAttr.getType(), newMemsetSize);
187 LLVM::MemsetInlineOp::create(builder, toReplace.getLoc(),
188 subslots.at(
index).ptr, toReplace.getVal(),
189 newMemsetSizeValue, toReplace.getIsVolatile());
195template <
class MemIntr>
198 if (!isa<LLVM::LLVMPointerType>(slot.
ptr.
getType()) ||
199 op.getDst() != slot.
ptr)
202 std::optional<uint64_t> memIntrLen = getStaticMemIntrLen(op);
212 auto intIndex = dyn_cast<IntegerAttr>(index);
213 return intIndex && intIndex.getType() == i32;
221template <
class MemsetIntr>
229 if (op.getIsVolatile())
232 if (!cast<DestructurableTypeInterface>(slot.
valueType)
233 .getSubelementIndexMap())
242template <
class MemsetIntr>
247 auto buildMemsetValue = [&](
unsigned width) ->
Value {
248 assert(width % 8 == 0);
249 auto intType = IntegerType::get(op.getContext(), width);
253 IntegerAttr constantPattern;
255 assert(constantPattern.getValue().getBitWidth() == 8);
256 APInt memsetVal(width, 0);
257 for (
unsigned loBit = 0; loBit < width; loBit += 8)
258 memsetVal.insertBits(constantPattern.getValue(), loBit);
259 return LLVM::ConstantOp::create(builder, op.getLoc(),
260 IntegerAttr::get(intType, memsetVal));
269 uint64_t coveredBits = 8;
271 LLVM::ZExtOp::create(builder, op.getLoc(), intType, op.getVal());
272 while (coveredBits < width) {
274 LLVM::ConstantOp::create(builder, op.getLoc(), intType, coveredBits);
276 LLVM::ShlOp::create(builder, op.getLoc(), currentValue, shiftBy);
278 LLVM::OrOp::create(builder, op.getLoc(), currentValue, shifted);
285 .Case([&](IntegerType type) ->
Value {
286 return buildMemsetValue(type.getWidth());
288 .Case([&](FloatType type) ->
Value {
289 Value intVal = buildMemsetValue(type.getWidth());
290 return LLVM::BitcastOp::create(builder, op.getLoc(), type, intVal);
293 "getStored should not be called on memset to unsupported type");
296template <
class MemsetIntr>
302 bool canConvertType =
304 .Case<IntegerType, FloatType>([](
auto type) {
305 return type.getWidth() % 8 == 0 && type.getWidth() > 0;
311 if (op.getIsVolatile())
317template <
class MemsetIntr>
323 std::optional<DenseMap<Attribute, Type>> types =
324 cast<DestructurableTypeInterface>(slot.
valueType).getSubelementIndexMap();
326 IntegerAttr memsetLenAttr = createMemsetLenAttr(op);
328 auto structType = dyn_cast<LLVM::LLVMStructType>(slot.
valueType);
330 Type i32 = IntegerType::get(op.getContext(), 32);
331 uint64_t memsetLen = memsetLenAttr.getValue().getZExtValue();
332 for (
size_t i = 0; i < types->size(); i++) {
336 uint64_t typeSize = dataLayout.
getTypeSize(elemType);
337 uint64_t offset = structType ? structType.getElementOffset(dataLayout, i)
339 if (offset >= memsetLen)
343 if (subslots.contains(
index)) {
344 uint64_t newMemsetSize = std::min(memsetLen - offset, typeSize);
345 createMemsetIntr(builder, op, memsetLenAttr, newMemsetSize, subslots,
353bool LLVM::MemsetOp::loadsFrom(
const MemorySlot &slot) {
return false; }
355bool LLVM::MemsetOp::storesTo(
const MemorySlot &slot) {
356 return getDst() == slot.
ptr;
365bool LLVM::MemsetOp::canUsesBeRemoved(
380LogicalResult LLVM::MemsetOp::ensureOnlySafeAccesses(
398 return memsetRewire(*
this, slot, subslots, builder, dataLayout);
401bool LLVM::MemsetInlineOp::loadsFrom(
const MemorySlot &slot) {
return false; }
403bool LLVM::MemsetInlineOp::storesTo(
const MemorySlot &slot) {
404 return getDst() == slot.
ptr;
413bool LLVM::MemsetInlineOp::canUsesBeRemoved(
428LogicalResult LLVM::MemsetInlineOp::ensureOnlySafeAccesses(
434bool LLVM::MemsetInlineOp::canRewire(
447 return memsetRewire(*
this, slot, subslots, builder, dataLayout);
454template <
class MemcpyLike>
456 return op.getSrc() == slot.
ptr;
459template <
class MemcpyLike>
461 return op.getDst() == slot.
ptr;
464template <
class MemcpyLike>
467 return LLVM::LoadOp::create(builder, op.getLoc(), slot.
valueType,
471template <
class MemcpyLike>
480 if (op.getDst() == op.getSrc())
483 if (op.getIsVolatile())
489template <
class MemcpyLike>
494 if (op.loadsFrom(slot))
495 LLVM::StoreOp::create(builder, op.getLoc(), reachingDefinition,
500template <
class MemcpyLike>
511template <
class MemcpyLike>
516 if (op.getIsVolatile())
519 if (!cast<DestructurableTypeInterface>(slot.
valueType)
520 .getSubelementIndexMap())
530 if (op.getSrc() == slot.
ptr)
531 usedIndices.insert_range(llvm::make_first_range(slot.
subelementTypes));
537template <
class MemcpyLike>
540 Type toCpy,
bool isVolatile) {
542 LLVM::ConstantOp::create(builder, toReplace.getLoc(),
543 IntegerAttr::get(toReplace.getLen().getType(),
545 MemcpyLike::create(builder, toReplace.getLoc(), dst, src, memcpySize,
551 LLVM::MemcpyInlineOp toReplace,
Value dst,
552 Value src,
Type toCpy,
bool isVolatile) {
553 Type lenType = IntegerType::get(toReplace->getContext(),
554 toReplace.getLen().getBitWidth());
555 LLVM::MemcpyInlineOp::create(
556 builder, toReplace.getLoc(), dst, src,
557 IntegerAttr::get(lenType, layout.
getTypeSize(toCpy)), isVolatile);
563template <
class MemcpyLike>
568 if (subslots.empty())
571 assert((slot.
ptr == op.getDst()) != (slot.
ptr == op.getSrc()));
572 bool isDst = slot.
ptr == op.getDst();
575 size_t slotsTreated = 0;
580 Type indexType = cast<IntegerAttr>(subslots.begin()->first).getType();
583 if (!subslots.contains(
index))
594 0,
static_cast<int32_t
>(
595 cast<IntegerAttr>(
index).getValue().getZExtValue())};
596 Value subslotPtrInOther = LLVM::GEPOp::create(
597 builder, op.getLoc(), LLVM::LLVMPointerType::get(op.getContext()),
598 slot.
valueType, isDst ? op.getSrc() : op.getDst(), gepIndices);
601 createMemcpyLikeToReplace(builder, dataLayout, op,
602 isDst ? subslot.
ptr : subslotPtrInOther,
603 isDst ? subslotPtrInOther : subslot.
ptr,
607 assert(subslots.size() == slotsTreated);
612bool LLVM::MemcpyOp::loadsFrom(
const MemorySlot &slot) {
616bool LLVM::MemcpyOp::storesTo(
const MemorySlot &slot) {
626bool LLVM::MemcpyOp::canUsesBeRemoved(
642LogicalResult LLVM::MemcpyOp::ensureOnlySafeAccesses(
660 return memcpyRewire(*
this, slot, subslots, builder, dataLayout);
663bool LLVM::MemcpyInlineOp::loadsFrom(
const MemorySlot &slot) {
667bool LLVM::MemcpyInlineOp::storesTo(
const MemorySlot &slot) {
677bool LLVM::MemcpyInlineOp::canUsesBeRemoved(
693LogicalResult LLVM::MemcpyInlineOp::ensureOnlySafeAccesses(
699bool LLVM::MemcpyInlineOp::canRewire(
712 return memcpyRewire(*
this, slot, subslots, builder, dataLayout);
715bool LLVM::MemmoveOp::loadsFrom(
const MemorySlot &slot) {
719bool LLVM::MemmoveOp::storesTo(
const MemorySlot &slot) {
729bool LLVM::MemmoveOp::canUsesBeRemoved(
745LogicalResult LLVM::MemmoveOp::ensureOnlySafeAccesses(
763 return memcpyRewire(*
this, slot, subslots, builder, dataLayout);
767 return {getDst(), getSrc()};
771 return {getDst(), getSrc()};
775 return {getDst(), getSrc()};
786#define GET_OP_CLASSES
787#include "mlir/Dialect/LLVMIR/LLVMMemoryIntrinsicOps.cpp.inc"
static LogicalResult memcpyEnsureOnlySafeAccesses(MemcpyLike op, const MemorySlot &slot, SmallVectorImpl< MemorySlot > &mustBeSafelyUsed)
static bool areAllIndicesI32(const DestructurableMemorySlot &slot)
Checks whether all indices are i32.
static bool memcpyStoresTo(MemcpyLike op, const MemorySlot &slot)
static DeletionKind memsetRewire(MemsetIntr op, const DestructurableMemorySlot &slot, DenseMap< Attribute, MemorySlot > &subslots, OpBuilder &builder, const DataLayout &dataLayout)
static bool memsetCanUsesBeRemoved(MemsetIntr op, const MemorySlot &slot, const SmallPtrSetImpl< OpOperand * > &blockingUses, SmallVectorImpl< OpOperand * > &newBlockingUses, const DataLayout &dataLayout)
static bool memcpyLoadsFrom(MemcpyLike op, const MemorySlot &slot)
static DeletionKind memcpyRemoveBlockingUses(MemcpyLike op, const MemorySlot &slot, const SmallPtrSetImpl< OpOperand * > &blockingUses, OpBuilder &builder, Value reachingDefinition)
static bool memcpyCanUsesBeRemoved(MemcpyLike op, const MemorySlot &slot, const SmallPtrSetImpl< OpOperand * > &blockingUses, SmallVectorImpl< OpOperand * > &newBlockingUses, const DataLayout &dataLayout)
static Value memcpyGetStored(MemcpyLike op, const MemorySlot &slot, OpBuilder &builder)
static bool memsetCanRewire(MemsetIntr op, const DestructurableMemorySlot &slot, SmallPtrSetImpl< Attribute > &usedIndices, SmallVectorImpl< MemorySlot > &mustBeSafelyUsed, const DataLayout &dataLayout)
static DeletionKind memcpyRewire(MemcpyLike op, const DestructurableMemorySlot &slot, DenseMap< Attribute, MemorySlot > &subslots, OpBuilder &builder, const DataLayout &dataLayout)
Rewires a memcpy-like operation.
static Value memsetGetStored(MemsetIntr op, const MemorySlot &slot, OpBuilder &builder)
static bool definitelyWritesOnlyWithinSlot(MemIntr op, const MemorySlot &slot, const DataLayout &dataLayout)
Returns whether one can be sure the memory intrinsic does not write outside of the bounds of the give...
static bool memcpyCanRewire(MemcpyLike op, const DestructurableMemorySlot &slot, SmallPtrSetImpl< Attribute > &usedIndices, SmallVectorImpl< MemorySlot > &mustBeSafelyUsed, const DataLayout &dataLayout)
Attributes are known-constant values of operations.
The main mechanism for performing data layout queries.
static DataLayout closest(Operation *op)
Returns the layout of the closest parent operation carrying layout info.
llvm::TypeSize getTypeSize(Type t) const
Returns the size of the given type in the current scope.
llvm::TypeSize getTypeAllocSize(Type t) const
Returns the offset in bytes between successive objects of the given type in the current scope,...
This class helps build Operations.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Dialect & getDialect() const
Get the dialect this type is registered to.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
MLIRContext * getContext() const
Utility to get the associated MLIRContext that this value is defined in.
Type getType() const
Return the type of this value.
bool forwardToUsers(Operation *op, SmallVectorImpl< OpOperand * > &newBlockingUses)
Conditions the deletion of the operation to the removal of all its uses.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
DeletionKind
Returned by operation promotion logic requesting the deletion of an operation.
@ Delete
Delete the operation after promotion.
llvm::TypeSwitch< T, ResultT > TypeSwitch
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Memory slot attached with information about its destructuring procedure.
DenseMap< Attribute, Type > subelementTypes
Maps an index within the memory slot to the corresponding subelement type.
Represents a slot in memory.
Type valueType
Type of the value contained in the slot.
Value ptr
Pointer to the memory slot, used by operations to refer to it.