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
100 lines
3.3 KiB
C++
100 lines
3.3 KiB
C++
//===- ExpressionSimplification.cpp - Simplify HLFIR expressions ----------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "flang/Optimizer/Builder/FIRBuilder.h"
|
|
#include "flang/Optimizer/HLFIR/HLFIROps.h"
|
|
#include "flang/Optimizer/HLFIR/Passes.h"
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
|
|
namespace hlfir {
|
|
#define GEN_PASS_DEF_EXPRESSIONSIMPLIFICATION
|
|
#include "flang/Optimizer/HLFIR/Passes.h.inc"
|
|
} // namespace hlfir
|
|
|
|
// Get the first user of `op`.
|
|
// Note that we consider the first user to be the one on the lowest line of
|
|
// the emitted HLFIR. The user iterator considers the opposite.
|
|
template <typename UserOp>
|
|
static UserOp getFirstUser(mlir::Operation *op) {
|
|
auto it = op->user_begin(), end = op->user_end(), prev = it;
|
|
for (; it != end; prev = it++)
|
|
;
|
|
if (prev != end)
|
|
if (auto userOp = mlir::dyn_cast<UserOp>(*prev))
|
|
return userOp;
|
|
return {};
|
|
}
|
|
|
|
// Get the last user of `op`.
|
|
// Note that we consider the last user to be the one on the highest line of
|
|
// the emitted HLFIR. The user iterator considers the opposite.
|
|
template <typename UserOp>
|
|
static UserOp getLastUser(mlir::Operation *op) {
|
|
if (!op->getUsers().empty())
|
|
if (auto userOp = mlir::dyn_cast<UserOp>(*op->user_begin()))
|
|
return userOp;
|
|
return {};
|
|
}
|
|
|
|
namespace {
|
|
|
|
// Trim operations can be erased in certain expressions, such as character
|
|
// comparisons.
|
|
// Since a character comparison appends spaces to the shorter character,
|
|
// calls to trim() that are used only in the comparison can be eliminated.
|
|
//
|
|
// Example:
|
|
// `trim(x) == trim(y)`
|
|
// can be simplified to
|
|
// `x == y`
|
|
class EraseTrim : public mlir::OpRewritePattern<hlfir::CharTrimOp> {
|
|
public:
|
|
using mlir::OpRewritePattern<hlfir::CharTrimOp>::OpRewritePattern;
|
|
|
|
llvm::LogicalResult
|
|
matchAndRewrite(hlfir::CharTrimOp trimOp,
|
|
mlir::PatternRewriter &rewriter) const override {
|
|
int trimUses = std::distance(trimOp->use_begin(), trimOp->use_end());
|
|
auto cmpCharOp = getFirstUser<hlfir::CmpCharOp>(trimOp);
|
|
auto destroyOp = getLastUser<hlfir::DestroyOp>(trimOp);
|
|
if (!cmpCharOp || !destroyOp || trimUses != 2)
|
|
return rewriter.notifyMatchFailure(
|
|
trimOp, "hlfir.char_trim is not used (only) by hlfir.cmpchar");
|
|
|
|
rewriter.eraseOp(destroyOp);
|
|
rewriter.replaceOp(trimOp, trimOp.getChr());
|
|
return mlir::success();
|
|
}
|
|
};
|
|
|
|
class ExpressionSimplificationPass
|
|
: public hlfir::impl::ExpressionSimplificationBase<
|
|
ExpressionSimplificationPass> {
|
|
public:
|
|
void runOnOperation() override {
|
|
mlir::MLIRContext *context = &getContext();
|
|
|
|
mlir::GreedyRewriteConfig config;
|
|
// Prevent the pattern driver from merging blocks.
|
|
config.setRegionSimplificationLevel(
|
|
mlir::GreedySimplifyRegionLevel::Disabled);
|
|
|
|
mlir::RewritePatternSet patterns(context);
|
|
patterns.insert<EraseTrim>(context);
|
|
|
|
if (mlir::failed(mlir::applyPatternsGreedily(
|
|
getOperation(), std::move(patterns), config))) {
|
|
mlir::emitError(getOperation()->getLoc(),
|
|
"failure in HLFIR expression simplification");
|
|
signalPassFailure();
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace
|