Files
TheRock/compiler/amd-llvm/mlir/lib/Dialect/Linalg/Transforms/InlineScalarOperands.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

120 lines
4.5 KiB
C++

//===- InlineScalarOperands.cpp - Pass to inline scalar operands =============//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements patterns/pass to inline scalar operands into a generic
// operation. A scalar operand is an operand whose indexing map has a constant
// rhs.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Linalg/Passes.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
namespace mlir {
#define GEN_PASS_DEF_LINALGINLINESCALAROPERANDSPASS
#include "mlir/Dialect/Linalg/Passes.h.inc"
} // namespace mlir
using namespace mlir;
using namespace mlir::linalg;
namespace {
struct InlineScalarOperands : public OpRewritePattern<GenericOp> {
using OpRewritePattern<GenericOp>::OpRewritePattern;
LogicalResult matchAndRewrite(GenericOp genericOp,
PatternRewriter &rewriter) const override {
if (!genericOp.hasPureTensorSemantics())
return failure();
SmallVector<size_t> scalarOperands;
SmallVector<AffineMap> newIndexingMaps;
SmallVector<Value> newOperands;
for (OpOperand *opOperand : genericOp.getDpsInputOperands()) {
AffineMap map = genericOp.getMatchingIndexingMap(opOperand);
if (genericOp.isDpsInput(opOperand) && map.isConstant()) {
scalarOperands.emplace_back(opOperand->getOperandNumber());
} else {
newIndexingMaps.emplace_back(map);
newOperands.emplace_back(opOperand->get());
}
}
if (scalarOperands.empty())
return failure();
for (OpOperand &opOperand : genericOp.getDpsInitsMutable())
newIndexingMaps.emplace_back(
genericOp.getMatchingIndexingMap(&opOperand));
Location loc = genericOp->getLoc();
SmallVector<Value> outputOperands = genericOp.getOutputs();
auto newOp = GenericOp::create(rewriter, loc, genericOp->getResultTypes(),
newOperands, outputOperands, newIndexingMaps,
genericOp.getIteratorTypesArray());
rewriter.cloneRegionBefore(genericOp.getRegion(), newOp.getRegion(),
newOp.getRegion().begin());
Block *body = newOp.getBody();
PatternRewriter::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(body);
for (auto idx : llvm::reverse(scalarOperands)) {
OpOperand *opOperand = genericOp.getDpsInputOperand(idx);
AffineMap map = genericOp.getMatchingIndexingMap(opOperand);
SmallVector<int64_t> indices = map.getConstantResults();
SmallVector<Value> indicesValues;
for (auto idx : indices)
indicesValues.emplace_back(
arith::ConstantIndexOp::create(rewriter, loc, idx));
Value scalarValue = opOperand->get();
if (isa<RankedTensorType>(scalarValue.getType())) {
scalarValue = tensor::ExtractOp::create(rewriter, loc, scalarValue,
indicesValues);
}
body->getArgument(idx).replaceAllUsesWith(scalarValue);
body->eraseArgument(idx);
}
rewriter.replaceOp(genericOp, newOp->getResults());
return success();
}
};
} // namespace
/// Patterns that are used to inline constant operands into linalg generic
/// ops.
void mlir::linalg::populateInlineConstantOperandsPatterns(
RewritePatternSet &patterns) {
auto *context = patterns.getContext();
patterns.add<InlineScalarOperands>(context);
}
namespace {
/// Pass that removes unit-extent dims within generic ops.
struct LinalgInlineScalarOperandsPass
: public impl::LinalgInlineScalarOperandsPassBase<
LinalgInlineScalarOperandsPass> {
using impl::LinalgInlineScalarOperandsPassBase<
LinalgInlineScalarOperandsPass>::LinalgInlineScalarOperandsPassBase;
void runOnOperation() override {
Operation *op = getOperation();
MLIRContext &ctx = getContext();
RewritePatternSet patterns(&ctx);
populateInlineConstantOperandsPatterns(patterns);
(void)applyPatternsGreedily(op, std::move(patterns));
}
};
} // namespace