Files
TheRock/compiler/amd-llvm/mlir/test/lib/Dialect/Linalg/TestLinalgDropUnitDims.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

145 lines
5.4 KiB
C++

//===- TestLinalgDropUnitDims.cpp - Test Linalg drop unit dims -----------===//
//
// 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 a pass for testing the transformation to drop unit
// extent dimensions from `linalg.generic` operations.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
using namespace mlir;
namespace {
LogicalResult dropOutermostUnitDims(RewriterBase &rewriter,
linalg::GenericOp genericOp) {
linalg::ControlDropUnitDims options;
options.controlFn = [](Operation *op) { return SmallVector<unsigned>{0}; };
FailureOr<linalg::DropUnitDimsResult> result =
linalg::dropUnitDims(rewriter, genericOp, options);
if (failed(result)) {
return failure();
}
rewriter.replaceOp(genericOp, result->replacements);
return success();
}
LogicalResult dropOutermostUnitDimsWithEncoding(RewriterBase &rewriter,
linalg::GenericOp genericOp) {
linalg::ControlDropUnitDims options;
linalg::ControlDropUnitDims::CollapseFnTy defaultCollapseFn =
options.collapseFn;
linalg::ControlDropUnitDims::ExpandFnTy defaultExpandFn = options.expandFn;
options.controlFn = [](Operation *op) { return SmallVector<unsigned>{0}; };
options.collapseFn =
[=](RewriterBase &rewriter, Location loc, Value operand,
ArrayRef<int64_t> targetShape,
ArrayRef<ReassociationIndices> reassociation,
const linalg::ControlDropUnitDims &control) -> FailureOr<Value> {
if (auto tensorType = dyn_cast<RankedTensorType>(operand.getType())) {
if (tensorType.getEncoding()) {
assert(control.rankReductionStrategy ==
linalg::ControlDropUnitDims::RankReductionStrategy::
ReassociativeReshape &&
"unexpected rank reduction strategy");
auto targetType = RankedTensorType::get(
targetShape, tensorType.getElementType(), tensorType.getEncoding());
return tensor::CollapseShapeOp::create(rewriter, loc, targetType,
operand, reassociation)
.getResult();
}
}
return defaultCollapseFn(rewriter, loc, operand, targetShape, reassociation,
control);
};
options.expandFn =
[=](RewriterBase &rewriter, Location loc, Value result, Value origDest,
ArrayRef<ReassociationIndices> reassociation,
const linalg::ControlDropUnitDims &control) -> FailureOr<Value> {
if (auto tensorType = dyn_cast<RankedTensorType>(origDest.getType())) {
if (tensorType.getEncoding()) {
assert(control.rankReductionStrategy ==
linalg::ControlDropUnitDims::RankReductionStrategy::
ReassociativeReshape &&
"unexpected rank reduction strategy");
return tensor::ExpandShapeOp::create(rewriter, loc, tensorType, result,
reassociation)
.getResult();
}
}
return defaultExpandFn(rewriter, loc, result, origDest, reassociation,
control);
};
FailureOr<linalg::DropUnitDimsResult> result =
linalg::dropUnitDims(rewriter, genericOp, options);
if (failed(result)) {
return failure();
}
rewriter.replaceOp(genericOp, result->replacements);
return success();
}
struct TestLinalgDropUnitDims
: public PassWrapper<TestLinalgDropUnitDims, OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLinalgDropUnitDims)
TestLinalgDropUnitDims() = default;
TestLinalgDropUnitDims(const TestLinalgDropUnitDims &pass)
: PassWrapper(pass) {}
Option<bool> collapseEncoded{
*this, "collapse-encoded",
llvm::cl::desc("Collapse and expand tensors with encodings"),
llvm::cl::init(false)};
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<linalg::LinalgDialect>();
}
StringRef getArgument() const final { return "test-linalg-drop-unit-dims"; }
StringRef getDescriptions() const {
return "Test transformation to drop unit-extent dims from Linalg "
"operations";
}
void runOnOperation() override {
MLIRContext *context = &this->getContext();
func::FuncOp funcOp = this->getOperation();
IRRewriter rewriter(context);
SmallVector<linalg::GenericOp> genericOps;
funcOp.walk(
[&](linalg::GenericOp genericOp) { genericOps.push_back(genericOp); });
for (auto genericOp : genericOps) {
rewriter.setInsertionPoint(genericOp);
if (collapseEncoded) {
(void)dropOutermostUnitDimsWithEncoding(rewriter, genericOp);
continue;
}
(void)dropOutermostUnitDims(rewriter, genericOp);
}
}
};
} // namespace
namespace mlir {
namespace test {
void registerTestLinalgDropUnitDims() {
PassRegistration<TestLinalgDropUnitDims>();
}
} // namespace test
} // namespace mlir