#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Arith/Utils/Utils.h"
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/Passes.h"
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineExprVisitor.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/ImplicitLocOpBuilder.h"
#include "mlir/Interfaces/ValueBoundsOpInterface.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Transforms/FoldUtils.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
using namespace mlir;
using namespace mlir::linalg;
using namespace mlir::scf;
using llvm::MapVector;
#define DEBUG_TYPE "linalg-promotion"
static Value allocBuffer(ImplicitLocOpBuilder &b,
const LinalgPromotionOptions &options,
Type elementType, Value allocSize, DataLayout &layout,
std::optional<unsigned> alignment = std::nullopt) {
llvm::TypeSize width = layout.getTypeSize(elementType);
assert(!width.isScalable() && "cannot allocate buffer for a scalable vector");
IntegerAttr alignmentAttr;
if (alignment.has_value())
alignmentAttr = b.getI64IntegerAttr(alignment.value());
Attribute memorySpaceAttr;
if (options.memorySpace.has_value())
memorySpaceAttr = *options.memorySpace;
if (std::optional<int64_t> cst = getConstantIntValue(allocSize)) {
auto staticBufferType = MemRefType::get(width.getFixedValue() * cst.value(),
b.getIntegerType(8));
staticBufferType =
MemRefType::Builder(staticBufferType).setMemorySpace(memorySpaceAttr);
if (options.useAlloca) {
return b.create<memref::AllocaOp>(staticBufferType, ValueRange{},
alignmentAttr);
}
return b.create<memref::AllocOp>(staticBufferType, ValueRange{},
alignmentAttr);
}
auto dynamicBufferType =
MemRefType::get(ShapedType::kDynamic, b.getIntegerType(8));
dynamicBufferType =
MemRefType::Builder(dynamicBufferType).setMemorySpace(memorySpaceAttr);
Value mul = b.createOrFold<arith::MulIOp>(
b.create<arith::ConstantIndexOp>(width), allocSize);
if (options.useAlloca)
return b.create<memref::AllocaOp>(dynamicBufferType, mul, alignmentAttr);
return b.create<memref::AllocOp>(dynamicBufferType, mul, alignmentAttr);
}
static std::optional<Value> defaultAllocBufferCallBack(
const LinalgPromotionOptions &options, OpBuilder &builder,
memref::SubViewOp subView, ArrayRef<Value> boundingSubViewSize,
std::optional<unsigned> alignment, DataLayout &layout) {
ShapedType viewType = subView.getType();
ImplicitLocOpBuilder b(subView.getLoc(), builder);
auto zero = b.create<arith::ConstantIndexOp>(0);
auto one = b.create<arith::ConstantIndexOp>(1);
Attribute memorySpaceAttr;
if (options.memorySpace.has_value())
memorySpaceAttr = *options.memorySpace;
Value allocSize = one;
for (const auto &size : llvm::enumerate(boundingSubViewSize))
allocSize = b.createOrFold<arith::MulIOp>(allocSize, size.value());
Value buffer = allocBuffer(b, options, viewType.getElementType(), allocSize,
layout, alignment);
SmallVector<int64_t, 4> dynSizes(boundingSubViewSize.size(),
ShapedType::kDynamic);
auto viewMemRefType = MemRefType::get(dynSizes, viewType.getElementType());
viewMemRefType =
MemRefType::Builder(viewMemRefType).setMemorySpace(memorySpaceAttr);
Value view = b.createOrFold<memref::ViewOp>(viewMemRefType, buffer, zero,
boundingSubViewSize);
return view;
}
static LogicalResult
defaultDeallocBufferCallBack(const LinalgPromotionOptions &options,
OpBuilder &b, Value fullLocalView) {
if (!options.useAlloca) {
auto viewOp = cast<memref::ViewOp>(fullLocalView.getDefiningOp());
b.create<memref::DeallocOp>(viewOp.getSource().getLoc(),
viewOp.getSource());
}
return success();
}
namespace {
struct LinalgOpInstancePromotionOptions {
LinalgOpInstancePromotionOptions(LinalgOp op,
const LinalgPromotionOptions &options);
MapVector<int64_t, Value> subViews;
llvm::SmallSet<int64_t, 4> operandsNumbersToCopyIn;
DenseMap<Value, bool> useFullTileBuffers;
AllocBufferCallbackFn allocationFn;
DeallocBufferCallbackFn deallocationFn;
CopyCallbackFn copyInFn;
CopyCallbackFn copyOutFn;
std::optional<unsigned> alignment;
};
}
LinalgOpInstancePromotionOptions::LinalgOpInstancePromotionOptions(
LinalgOp linalgOp, const LinalgPromotionOptions &options)
: subViews(), alignment(options.alignment) {
assert(linalgOp.hasPureBufferSemantics() &&
"revisit usage of shaped operand");
auto vUseFullTileBuffers =
options.useFullTileBuffers.value_or(llvm::SmallBitVector());
vUseFullTileBuffers.resize(linalgOp->getNumOperands(),
options.useFullTileBuffersDefault);
for (OpOperand &opOperand : linalgOp->getOpOperands()) {
int64_t operandNumber = opOperand.getOperandNumber();
if (options.operandsToPromote &&
!options.operandsToPromote->count(operandNumber))
continue;
Operation *op = opOperand.get().getDefiningOp();
if (auto sv = dyn_cast_or_null<memref::SubViewOp>(op)) {
subViews[operandNumber] = sv;
if (!isa<linalg::GenericOp>(linalgOp) ||
linalgOp.payloadUsesValueFromOperand(&opOperand))
operandsNumbersToCopyIn.insert(operandNumber);
useFullTileBuffers[sv] = vUseFullTileBuffers[operandNumber];
}
}
if (options.allocationFn) {
allocationFn = *options.allocationFn;
} else {
allocationFn = [&](OpBuilder &b, memref::SubViewOp subViewOp,
ArrayRef<Value> boundingSubViewSize,
DataLayout &layout) -> std::optional<Value> {
return defaultAllocBufferCallBack(options, b, subViewOp,
boundingSubViewSize, alignment, layout);
};
}
if (options.deallocationFn) {
deallocationFn = *options.deallocationFn;
} else {
deallocationFn = [&](OpBuilder &b, Value buffer) {
return defaultDeallocBufferCallBack(options, b, buffer);
};
}
Location loc = linalgOp.getLoc();
auto defaultCopyCallBack = [loc](OpBuilder &b, Value src,
Value dst) -> LogicalResult {
b.create<linalg::CopyOp>(loc, src, dst);
return success();
};
copyInFn = (options.copyInFn ? *(options.copyInFn) : defaultCopyCallBack);
copyOutFn = (options.copyOutFn ? *(options.copyOutFn) : defaultCopyCallBack);
}
FailureOr<PromotionInfo> mlir::linalg::promoteSubviewAsNewBuffer(
OpBuilder &b, Location loc, memref::SubViewOp subView,
const AllocBufferCallbackFn &allocationFn, DataLayout &layout) {
auto viewType = subView.getType();
auto rank = viewType.getRank();
SmallVector<Value, 4> fullSizes;
SmallVector<OpFoldResult> partialSizes;
fullSizes.reserve(rank);
partialSizes.reserve(rank);
llvm::SmallBitVector droppedDims = subView.getDroppedDims();
int64_t resultDimIdx = 0;
for (const auto &en : llvm::enumerate(subView.getOrCreateRanges(b, loc))) {
if (droppedDims[en.index()])
continue;
auto rangeValue = en.value();
LLVM_DEBUG(llvm::dbgs() << "Extract tightest: " << rangeValue.size << "\n");
Value size;
if (auto attr = llvm::dyn_cast_if_present<Attribute>(rangeValue.size)) {
size = getValueOrCreateConstantIndexOp(b, loc, rangeValue.size);
} else {
FailureOr<int64_t> upperBound =
ValueBoundsConstraintSet::computeConstantBound(
presburger::BoundType::UB, rangeValue.size,
nullptr, true);
size = failed(upperBound)
? getValueOrCreateConstantIndexOp(b, loc, rangeValue.size)
: b.create<arith::ConstantIndexOp>(loc, *upperBound);
}
LLVM_DEBUG(llvm::dbgs() << "Extracted tightest: " << size << "\n");
fullSizes.push_back(size);
partialSizes.push_back(
b.createOrFold<memref::DimOp>(loc, subView, resultDimIdx++));
}
SmallVector<int64_t, 4> dynSizes(fullSizes.size(), ShapedType::kDynamic);
std::optional<Value> fullLocalView =
allocationFn(b, subView, fullSizes, layout);
if (!fullLocalView)
return failure();
SmallVector<OpFoldResult, 4> zeros(fullSizes.size(), b.getIndexAttr(0));
SmallVector<OpFoldResult, 4> ones(fullSizes.size(), b.getIndexAttr(1));
auto partialLocalView = b.createOrFold<memref::SubViewOp>(
loc, *fullLocalView, zeros, partialSizes, ones);
return PromotionInfo{*fullLocalView, partialLocalView};
}
static FailureOr<MapVector<int64_t, PromotionInfo>>
promoteSubViews(ImplicitLocOpBuilder &b,
LinalgOpInstancePromotionOptions options, DataLayout &layout) {
if (options.subViews.empty())
return failure();
MapVector<int64_t, PromotionInfo> promotionInfoMap;
for (auto v : options.subViews) {
memref::SubViewOp subView =
cast<memref::SubViewOp>(v.second.getDefiningOp());
auto promotionInfo = promoteSubviewAsNewBuffer(
b, b.getLoc(), subView, options.allocationFn, layout);
if (failed(promotionInfo))
return failure();
promotionInfoMap[v.first] = *promotionInfo;
if (!options.useFullTileBuffers[v.second])
continue;
Type subviewEltType = subView.getType().getElementType();
Value fillVal =
llvm::TypeSwitch<Type, Value>(subviewEltType)
.Case([&](FloatType t) {
return b.create<arith::ConstantOp>(FloatAttr::get(t, 0.0));
})
.Case([&](IntegerType t) {
return b.create<arith::ConstantOp>(IntegerAttr::get(t, 0));
})
.Case([&](ComplexType t) {
Value tmp;
if (auto et = dyn_cast<FloatType>(t.getElementType()))
tmp = b.create<arith::ConstantOp>(FloatAttr::get(et, 0.0));
else if (auto et = cast<IntegerType>(t.getElementType()))
tmp = b.create<arith::ConstantOp>(IntegerAttr::get(et, 0));
return b.create<complex::CreateOp>(t, tmp, tmp);
})
.Default([](auto) { return Value(); });
if (!fillVal)
return failure();
b.create<linalg::FillOp>(fillVal, promotionInfo->fullLocalView);
}
for (auto v : options.subViews) {
auto *info = promotionInfoMap.find(v.first);
if (info == promotionInfoMap.end())
continue;
if (options.operandsNumbersToCopyIn.count(v.first) == 0)
continue;
if (failed(options.copyInFn(
b, cast<memref::SubViewOp>(v.second.getDefiningOp()),
info->second.partialLocalView)))
return failure();
}
return promotionInfoMap;
}
static FailureOr<LinalgOp>
promoteSubViews(ImplicitLocOpBuilder &b, LinalgOp op,
LinalgOpInstancePromotionOptions options, DataLayout &layout) {
assert(op.hasPureBufferSemantics() &&
"expected linalg op with buffer semantics");
auto promotedBuffersAndViews = promoteSubViews(b, options, layout);
if (failed(promotedBuffersAndViews) ||
promotedBuffersAndViews->size() != options.subViews.size())
return failure();
SmallVector<Value, 8> opViews;
opViews.reserve(op->getNumOperands());
SmallVector<std::pair<Value, Value>, 8> writebackViews;
writebackViews.reserve(promotedBuffersAndViews->size());
for (OpOperand &opOperand : op->getOpOperands()) {
int64_t operandNumber = opOperand.getOperandNumber();
if (options.subViews.count(operandNumber) != 0) {
if (options.useFullTileBuffers[opOperand.get()])
opViews.push_back(
(*promotedBuffersAndViews)[operandNumber].fullLocalView);
else
opViews.push_back(
(*promotedBuffersAndViews)[operandNumber].partialLocalView);
if (operandNumber >= op.getNumDpsInputs())
writebackViews.emplace_back(std::make_pair(
opOperand.get(),
(*promotedBuffersAndViews)[operandNumber].partialLocalView));
} else {
opViews.push_back(opOperand.get());
}
}
op->setOperands(0, opViews.size(), opViews);
OpBuilder::InsertionGuard guard(b);
b.setInsertionPointAfter(op);
for (auto viewAndPartialLocalView : writebackViews) {
if (failed(options.copyOutFn(b, viewAndPartialLocalView.second,
viewAndPartialLocalView.first)))
return failure();
}
for (const auto &pi : *promotedBuffersAndViews)
(void)options.deallocationFn(b, pi.second.fullLocalView);
return op;
}
LogicalResult
mlir::linalg::promoteSubviewsPrecondition(Operation *op,
LinalgPromotionOptions options) {
LinalgOp linalgOp = dyn_cast<LinalgOp>(op);
if (!linalgOp || !linalgOp.hasPureBufferSemantics())
return failure();
for (OpOperand &opOperand : linalgOp->getOpOperands()) {
auto sv =
isa_and_nonnull<memref::SubViewOp>(opOperand.get().getDefiningOp());
if (sv) {
if (!options.operandsToPromote ||
options.operandsToPromote->count(opOperand.getOperandNumber()))
return success();
}
}
return failure();
}
FailureOr<LinalgOp>
mlir::linalg::promoteSubViews(OpBuilder &builder, LinalgOp linalgOp,
const LinalgPromotionOptions &options) {
LinalgOpInstancePromotionOptions linalgOptions(linalgOp, options);
auto layout = DataLayout::closest(linalgOp);
ImplicitLocOpBuilder b(linalgOp.getLoc(), builder);
auto res = ::promoteSubViews(b, linalgOp, linalgOptions, layout);
if (failed(res))
return failure();
return res;
}
static std::optional<Value> allocateSubviewGPUMemoryInAddressSpace(
OpBuilder &builder, memref::SubViewOp subview, ArrayRef<Value> sizeBounds,
gpu::AddressSpace addressSpace) {
OpBuilder::InsertionGuard guard(builder);
func::FuncOp funcOp = subview->getParentOfType<func::FuncOp>();
if (!funcOp)
return std::nullopt;
SmallVector<int64_t> shape;
for (Value bound : sizeBounds) {
APInt value;
if (!matchPattern(bound, m_ConstantInt(&value)))
return std::nullopt;
shape.push_back(value.getSExtValue());
}
builder.setInsertionPoint(&funcOp.front(), funcOp.front().begin());
auto type = MemRefType::get(
shape, subview.getType().getElementType(), MemRefLayoutAttrInterface{},
gpu::AddressSpaceAttr::get(builder.getContext(), addressSpace));
Value buffer;
if (addressSpace == gpu::GPUDialect::getWorkgroupAddressSpace()) {
buffer = builder.create<memref::AllocOp>(funcOp.getLoc(), type);
} else if (addressSpace == gpu::GPUDialect::getPrivateAddressSpace()) {
buffer = builder.create<memref::AllocaOp>(funcOp.getLoc(), type);
} else {
return std::nullopt;
}
return buffer;
}
std::optional<Value> mlir::linalg::allocateWorkgroupMemory(
OpBuilder &builder, memref::SubViewOp subview, ArrayRef<Value> sizeBounds,
DataLayout &) {
return allocateSubviewGPUMemoryInAddressSpace(
builder, subview, sizeBounds,
gpu::GPUDialect::getWorkgroupAddressSpace());
}
LogicalResult mlir::linalg::deallocateWorkgroupMemory(OpBuilder &,
Value ) {
return success();
}
LogicalResult mlir::linalg::copyToWorkgroupMemory(OpBuilder &b, Value src,
Value dst) {
b.create<gpu::BarrierOp>(src.getLoc());
Operation *copyOp = b.create<memref::CopyOp>(src.getLoc(), src, dst);
b.create<gpu::BarrierOp>(copyOp->getLoc());
return success();
}
std::optional<Value> mlir::linalg::allocateGPUPrivateMemory(
OpBuilder &builder, memref::SubViewOp subview, ArrayRef<Value> sizeBounds,
DataLayout &) {
return allocateSubviewGPUMemoryInAddressSpace(
builder, subview, sizeBounds, gpu::GPUDialect::getPrivateAddressSpace());
}
LogicalResult mlir::linalg::copyToGPUPrivateMemory(OpBuilder &b, Value src,
Value dst) {
b.create<memref::CopyOp>(src.getLoc(), src, dst);
return success();
}
LogicalResult mlir::linalg::deallocateGPUPrivateMemory(OpBuilder &,
Value ) {
return success();
}