Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Arith to Mod Arith Covnersion pass to use latest LLVM/MLIR #1246

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions lib/Dialect/Arith/Conversions/ArithToModArith/ArithToModArith.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,35 @@ struct ConvertExtUI : public OpConversionPattern<mlir::arith::ExtUIOp> {
}
};

struct ConvertLoadOp : public OpConversionPattern<mlir::memref::LoadOp> {
ConvertLoadOp(mlir::MLIRContext *context)
: OpConversionPattern<mlir::memref::LoadOp>(context) {}

using OpConversionPattern::OpConversionPattern;

LogicalResult matchAndRewrite(
::mlir::memref::LoadOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
ImplicitLocOpBuilder b(op.getLoc(), rewriter);

auto defineOp = op.getMemRef().getDefiningOp();

if (op.getMemRef().getDefiningOp()) {
if (isa<memref::GetGlobalOp>(defineOp)) {
// skip global memref
return success();
}
}

auto result = rewriter.create<memref::LoadOp>(
op.getLoc(), convertArithType(op.getType()), adaptor.getOperands()[0],
op.getIndices());

rewriter.replaceOp(op, result);
return success();
}
};

struct ArithToModArith : impl::ArithToModArithBase<ArithToModArith> {
using ArithToModArithBase::ArithToModArithBase;

Expand All @@ -156,7 +185,19 @@ void ArithToModArith::runOnOperation() {
});

target.addDynamicallyLegalOp<memref::LoadOp>([&](Operation *op) {
return cast<memref::LoadOp>(op).getMemRef().getDefiningOp();
auto users = cast<memref::LoadOp>(op).getResult().getUsers();
if (cast<memref::LoadOp>(op).getResult().getDefiningOp()) {
if (isa<memref::GetGlobalOp>(
cast<memref::LoadOp>(op).getResult().getDefiningOp())) {
auto detectable = llvm::any_of(users, [](Operation *user) {
return isa<mod_arith::EncapsulateOp>(user);
});
return detectable;
}
}

return (typeConverter.isLegal(op->getOperandTypes()) &&
typeConverter.isLegal(op->getResultTypes()));
});

target.addDynamicallyLegalOp<
Expand All @@ -172,13 +213,12 @@ void ArithToModArith::runOnOperation() {
.add<ConvertConstant, ConvertExtSI, ConvertExtUI,
ConvertBinOp<mlir::arith::AddIOp, mod_arith::AddOp>,
ConvertBinOp<mlir::arith::SubIOp, mod_arith::SubOp>,
ConvertBinOp<mlir::arith::MulIOp, mod_arith::MulOp>,
ConvertAny<memref::LoadOp>, ConvertAny<memref::AllocOp>,
ConvertAny<memref::DeallocOp>, ConvertAny<memref::StoreOp>,
ConvertAny<memref::SubViewOp>, ConvertAny<memref::CopyOp>,
ConvertAny<tensor::FromElementsOp>, ConvertAny<tensor::ExtractOp>,
ConvertAny<affine::AffineStoreOp>, ConvertAny<affine::AffineLoadOp>>(
typeConverter, context);
ConvertBinOp<mlir::arith::MulIOp, mod_arith::MulOp>, ConvertLoadOp,
ConvertAny<memref::AllocOp>, ConvertAny<memref::DeallocOp>,
ConvertAny<memref::StoreOp>, ConvertAny<memref::SubViewOp>,
ConvertAny<memref::CopyOp>, ConvertAny<tensor::FromElementsOp>,
ConvertAny<tensor::ExtractOp>, ConvertAny<affine::AffineStoreOp>,
ConvertAny<affine::AffineLoadOp>>(typeConverter, context);

addStructuralConversionPatterns(typeConverter, patterns, target);

Expand Down
3 changes: 0 additions & 3 deletions tests/Dialect/Arith/Conversions/ArithToModArith/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ glob_lit_tests(
name = "all_tests",
data = ["@heir//tests:test_utilities"],
driver = "@heir//tests:run_lit.sh",
exclude = [
"arith-to-mod-arith.mlir", # TODO(b/386761235): re-enable it
],
test_file_exts = ["mlir"],
)
2 changes: 1 addition & 1 deletion tests/Dialect/Arith/Transforms/runner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package(default_applicable_licenses = ["@heir//:license"])
glob_lit_tests(
name = "all_tests",
data = [
"//tests/Dialect/Arith/Transforms/runner:quarter_to_llvm.c",
":quarter_to_llvm",
"@heir//tests:test_utilities",
"@llvm-project//clang",
"@llvm-project//llvm:llc",
Expand Down
Loading