Files
TheRock/compiler/amd-llvm/mlir/lib/Reducer/OptReductionPass.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

102 lines
3.2 KiB
C++

//===- OptReductionPass.cpp - Optimization Reduction Pass Wrapper ---------===//
//
// 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 defines the Opt Reduction Pass Wrapper. It creates a MLIR pass to
// run any optimization pass within it and only replaces the output module with
// the transformed version if it is smaller and interesting.
//
//===----------------------------------------------------------------------===//
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "mlir/Reducer/Passes.h"
#include "mlir/Reducer/Tester.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/MemoryBuffer.h"
namespace mlir {
#define GEN_PASS_DEF_OPTREDUCTIONPASS
#include "mlir/Reducer/Passes.h.inc"
} // namespace mlir
#define DEBUG_TYPE "mlir-reduce"
using namespace mlir;
namespace {
class OptReductionPass : public impl::OptReductionPassBase<OptReductionPass> {
public:
using Base::Base;
/// Runs the pass instance in the pass pipeline.
void runOnOperation() override;
};
} // namespace
/// Runs the pass instance in the pass pipeline.
void OptReductionPass::runOnOperation() {
LDBG() << "\nOptimization Reduction pass: ";
Tester test(testerName, testerArgs);
Operation *topOp = this->getOperation();
std::string pipelineStr = optPass;
if (pipelineStr.empty()) {
if (!optPassFile.empty()) {
auto fileOrErr = llvm::MemoryBuffer::getFile(optPassFile);
if (std::error_code ec = fileOrErr.getError()) {
topOp->emitError() << "Could not open pass pipeline file: "
<< optPassFile << " (" << ec.message() << ")";
return signalPassFailure();
}
pipelineStr = fileOrErr.get()->getBuffer().trim().str();
}
}
PassManager passManager(topOp->getName());
if (failed(parsePassPipeline(pipelineStr, passManager))) {
topOp->emitError() << "\nfailed to parse pass pipeline";
return signalPassFailure();
}
std::pair<Tester::Interestingness, int> original = test.isInteresting(topOp);
if (original.first != Tester::Interestingness::True) {
topOp->emitError() << "\nthe original input is not interested";
return signalPassFailure();
}
Operation *topOpVariant = topOp->clone();
LogicalResult pipelineResult = passManager.run(topOpVariant);
if (failed(pipelineResult)) {
topOp->emitError() << "\nfailed to run pass pipeline";
return signalPassFailure();
}
std::pair<Tester::Interestingness, int> reduced =
test.isInteresting(topOpVariant);
if (reduced.first == Tester::Interestingness::True &&
reduced.second < original.second) {
topOp->getRegion(0).getBlocks().clear();
topOp->getRegion(0).getBlocks().splice(
topOp->getRegion(0).getBlocks().begin(),
topOpVariant->getRegion(0).getBlocks());
LDBG() << "\nSuccessful Transformed version\n";
} else {
LDBG() << "\nUnsuccessful Transformed version\n";
}
topOpVariant->destroy();
LDBG() << "Pass Complete\n";
}