Skip to content

[MLIR][Arith] Add ExpandOps to convertArithToLLVM #117305

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

Merged
merged 6 commits into from
Dec 4, 2024
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
5 changes: 4 additions & 1 deletion mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/VectorPattern.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Arith/Transforms/Passes.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/TypeUtilities.h"
Expand Down Expand Up @@ -477,7 +478,8 @@ struct ArithToLLVMConversionPass
options.overrideIndexBitwidth(indexBitwidth);

LLVMTypeConverter converter(&getContext(), options);
mlir::arith::populateArithToLLVMConversionPatterns(converter, patterns);
arith::populateCeilFloorDivExpandOpsPatterns(patterns);
arith::populateArithToLLVMConversionPatterns(converter, patterns);

if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
Expand All @@ -503,6 +505,7 @@ struct ArithToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
void populateConvertToLLVMConversionPatterns(
ConversionTarget &target, LLVMTypeConverter &typeConverter,
RewritePatternSet &patterns) const final {
arith::populateCeilFloorDivExpandOpsPatterns(patterns);
arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pass is kind of deprecated: we're supposed to migrate to --convert-to-llvm.
I'm concerned by additions here that wouldn't carry over to --convert-to-llvm!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not completely sure of what you mean.
The tests mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir calls both convert-arith-to-llvm and --convert-to-llvm. If either arith-to-llvm or convert-to-llvm is updated, the other must be updated as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nujaa , wouldn't adding this in "ConvertToLLVMPass.cpp" instead also work?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused because you're only changing the ArithToLLVMConversionPass here, so was this behavior already implemented in the convert-to-llvm flow and you're just catching up?

(I guess that makes my point as well somehow: we should remove the convert-arith-to-llvm pass: it does not provide anything that convert-to-llvm can't do, hopefully).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nujaa , wouldn't adding this in "ConvertToLLVMPass.cpp" instead also work?

I think it is Arith specific and should not be part of generic ConvertToLLVMPass

Copy link
Contributor Author

@nujaa nujaa Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so was this behavior already implemented in the convert-to-llvm flow and you're just catching up?

Neither arith-to-llvm nor convert-to-llvm was not expanding ops. As the test runs both with the same CHECK-lines.

Expand Down
62 changes: 62 additions & 0 deletions mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,68 @@ func.func @select(%arg0 : i1, %arg1 : i32, %arg2 : i32) -> i32 {

// -----

// CHECK-LABEL: @ceildivsi
// CHECK-SAME: %[[ARG0:.*]]: i64) -> i64
func.func @ceildivsi(%arg0 : i64) -> i64 {
// CHECK: %[[CST0:.*]] = llvm.mlir.constant(1 : i64) : i64
// CHECK: %[[CST1:.*]] = llvm.mlir.constant(0 : i64) : i64
// CHECK: %[[CST2:.*]] = llvm.mlir.constant(-1 : i64) : i64
// CHECK: %[[CMP0:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[SEL0:.*]] = llvm.select %[[CMP0]], %[[CST2]], %[[CST0]] : i1, i64
// CHECK: %[[ADD0:.*]] = llvm.add %[[SEL0]], %[[ARG0]] : i64
// CHECK: %[[DIV0:.*]] = llvm.sdiv %[[ADD0]], %[[ARG0]] : i64
// CHECK: %[[ADD1:.*]] = llvm.add %[[DIV0]], %[[CST0]] : i64
// CHECK: %[[SUB0:.*]] = llvm.sub %[[CST1]], %[[ARG0]] : i64
// CHECK: %[[DIV1:.*]] = llvm.sdiv %[[SUB0]], %[[ARG0]] : i64
// CHECK: %[[SUB1:.*]] = llvm.sub %[[CST1]], %[[DIV1]] : i64
// CHECK: %[[CMP1:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[CMP2:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[CMP3:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[CMP4:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[AND0:.*]] = llvm.and %[[CMP1]], %[[CMP3]] : i1
// CHECK: %[[AND1:.*]] = llvm.and %[[CMP2]], %[[CMP4]] : i1
// CHECK: %[[OR:.*]] = llvm.or %[[AND0]], %[[AND1]] : i1
// CHECK: %[[SEL1:.*]] = llvm.select %[[OR]], %[[ADD1]], %[[SUB1]] : i1, i64
%0 = arith.ceildivsi %arg0, %arg0 : i64
return %0: i64
}

// CHECK-LABEL: @ceildivui
// CHECK-SAME: %[[ARG0:.*]]: i32) -> i32
func.func @ceildivui(%arg0 : i32) -> i32 {
// CHECK: %[[CST0:.*]] = llvm.mlir.constant(0 : i32) : i32
// CHECK: %[[CMP0:.*]] = llvm.icmp "eq" %[[ARG0]], %[[CST0]] : i32
// CHECK: %[[CST1:.*]] = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[SUB0:.*]] = llvm.sub %[[ARG0]], %[[CST1]] : i32
// CHECK: %[[DIV0:.*]] = llvm.udiv %[[SUB0]], %[[ARG0]] : i32
// CHECK: %[[ADD0:.*]] = llvm.add %[[DIV0]], %[[CST1]] : i32
// CHECK: %[[SEL0:.*]] = llvm.select %[[CMP0]], %[[CST0]], %[[ADD0]] : i1, i32
%0 = arith.ceildivui %arg0, %arg0 : i32
return %0: i32
}

// -----

// CHECK-LABEL: @floordivsi
// CHECK-SAME: %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32) -> i32
func.func @floordivsi(%arg0 : i32, %arg1 : i32) -> i32 {
// CHECK: %[[SDIV:.*]] = llvm.sdiv %[[ARG0]], %[[ARG1]] : i32
// CHECK: %[[MUL0:.*]] = llvm.mul %[[SDIV]], %[[ARG1]] : i32
// CHECK: %[[CMP0:.*]] = llvm.icmp "ne" %[[ARG0]], %[[MUL0]] : i32
// CHECK: %[[CST0:.*]] = llvm.mlir.constant(0 : i32) : i32
// CHECK: %[[CMP1:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST0]] : i32
// CHECK: %[[CMP2:.*]] = llvm.icmp "slt" %[[ARG1]], %[[CST0]] : i32
// CHECK: %[[CMP3:.*]] = llvm.icmp "ne" %[[CMP1]], %[[CMP2]] : i1
// CHECK: %[[AND:.*]] = llvm.and %[[CMP0]], %[[CMP3]] : i1
// CHECK: %[[CST1:.*]] = llvm.mlir.constant(-1 : i32) : i32
// CHECK: %[[ADD:.*]] = llvm.add %[[SDIV]], %[[CST1]] : i32
// CHECK: %[[SEL:.*]] = llvm.select %[[AND]], %[[ADD]], %[[SDIV]] : i1, i32
%0 = arith.floordivsi %arg0, %arg1 : i32
return %0 : i32
}

// -----

// CHECK-LABEL: @minmaxi
func.func @minmaxi(%arg0 : i32, %arg1 : i32) -> i32 {
// CHECK: = llvm.intr.smin(%arg0, %arg1) : (i32, i32) -> i32
Expand Down
Loading