Skip to content

[mlir][ArmSME] Use ArmSMETypeConverter for all VectorToLLVM patterns #65261

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

Closed
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
7 changes: 4 additions & 3 deletions mlir/include/mlir/Conversion/LLVMCommon/TypeConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,15 @@ class LLVMTypeConverter : public TypeConverter {
/// Convert a memref type to a bare pointer to the memref element type.
Type convertMemRefToBarePtr(BaseMemRefType type) const;

/// Convert a 1D vector type into an LLVM vector type.
Type convertVectorType(VectorType type) const;

/// Options for customizing the llvm lowering.
LowerToLLVMOptions options;

/// Data layout analysis mapping scopes to layouts active in them.
const DataLayoutAnalysis *dataLayoutAnalysis;

protected:
/// Convert a 1D vector type into an LLVM vector type.
Type convertVectorType(VectorType type) const;
};

/// Callback to convert function argument types. It converts a MemRef function
Expand Down
13 changes: 13 additions & 0 deletions mlir/include/mlir/Dialect/ArmSME/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ std::unique_ptr<Pass> createTileAllocationPass();
class ArmSMETypeConverter : public LLVMTypeConverter {
public:
ArmSMETypeConverter(MLIRContext *ctx, const LowerToLLVMOptions &options);

protected:
/// Convert an n-D vector type to an LLVM vector type.
///
/// Disables type conversion of legal 2-D scalable vector types such as
/// `vector<[16]x[16]xi8>` for ArmSME, since LLVM does not support arrays of
/// scalable vectors and the LLVM type converter asserts on such types to
/// prevent generation of illegal LLVM IR. When lowering to ArmSME these types
/// should be eliminated before lowering to LLVM.
///
/// Types unrelated to ArmSME are converted by
/// `LLVMTypeConverter::convertVectorType`.
Type convertVectorType(VectorType type) const;
};

//===----------------------------------------------------------------------===//
Expand Down
23 changes: 14 additions & 9 deletions mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVMPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,26 @@ void LowerVectorToLLVMPass::runOnOperation() {
// Convert to the LLVM IR dialect.
LowerToLLVMOptions options(&getContext());
options.useOpaquePointers = useOpaquePointers;
LLVMTypeConverter converter(&getContext(), options);

LLVMTypeConverter *converter;
if (armSME)
converter = new arm_sme::ArmSMETypeConverter(&getContext(), options);
else
converter = new LLVMTypeConverter(&getContext(), options);
Comment on lines +87 to +91
Copy link
Member

Choose a reason for hiding this comment

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

This is a memory leak. You should at the very least use std::unique_ptr<LLVMTypeConverter> here and use make_unique in the logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, thanks for heads up! I'll push a fix soon


RewritePatternSet patterns(&getContext());
populateVectorMaskMaterializationPatterns(patterns, force32BitVectorIndices);
populateVectorTransferLoweringPatterns(patterns);
populateVectorToLLVMMatrixConversionPatterns(converter, patterns);
populateVectorToLLVMMatrixConversionPatterns(*converter, patterns);
populateVectorToLLVMConversionPatterns(
converter, patterns, reassociateFPReductions, force32BitVectorIndices);
populateVectorToLLVMMatrixConversionPatterns(converter, patterns);
*converter, patterns, reassociateFPReductions, force32BitVectorIndices);
populateVectorToLLVMMatrixConversionPatterns(*converter, patterns);

// Architecture specific augmentations.
LLVMConversionTarget target(getContext());
target.addLegalDialect<arith::ArithDialect>();
target.addLegalDialect<memref::MemRefDialect>();
target.addLegalOp<UnrealizedConversionCastOp>();
arm_sme::ArmSMETypeConverter armSMEConverter(&getContext(), options);

if (armNeon) {
// TODO: we may or may not want to include in-dialect lowering to
Expand All @@ -107,19 +112,19 @@ void LowerVectorToLLVMPass::runOnOperation() {
}
if (armSVE) {
configureArmSVELegalizeForExportTarget(target);
populateArmSVELegalizeForLLVMExportPatterns(converter, patterns);
populateArmSVELegalizeForLLVMExportPatterns(*converter, patterns);
}
if (armSME) {
configureArmSMELegalizeForExportTarget(target);
populateArmSMELegalizeForLLVMExportPatterns(armSMEConverter, patterns);
populateArmSMELegalizeForLLVMExportPatterns(*converter, patterns);
}
if (amx) {
configureAMXLegalizeForExportTarget(target);
populateAMXLegalizeForLLVMExportPatterns(converter, patterns);
populateAMXLegalizeForLLVMExportPatterns(*converter, patterns);
}
if (x86Vector) {
configureX86VectorLegalizeForExportTarget(target);
populateX86VectorLegalizeForLLVMExportPatterns(converter, patterns);
populateX86VectorLegalizeForLLVMExportPatterns(*converter, patterns);
}

if (failed(
Expand Down
15 changes: 8 additions & 7 deletions mlir/lib/Dialect/ArmSME/Transforms/ArmSMETypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/ArmSME/Transforms/Passes.h"
#include "mlir/Dialect/ArmSME/Utils/Utils.h"

using namespace mlir;
arm_sme::ArmSMETypeConverter::ArmSMETypeConverter(
MLIRContext *ctx, const LowerToLLVMOptions &options)
: LLVMTypeConverter(ctx, options) {
// Disable LLVM type conversion for vectors. This is to prevent 2-d scalable
// vectors (common in the context of ArmSME), e.g.
// `vector<[16]x[16]xi8>`,
// entering the LLVM Type converter. LLVM does not support arrays of scalable
// vectors, but in the case of SME such types are effectively eliminated when
// emitting ArmSME LLVM IR intrinsics.
addConversion([&](VectorType type) { return type; });
addConversion([&](VectorType type) { return convertVectorType(type); });
}

Type arm_sme::ArmSMETypeConverter::convertVectorType(VectorType type) const {
if (arm_sme::isValidSMETileVectorType(type))
return type;
return LLVMTypeConverter::convertVectorType(type);
}
1 change: 1 addition & 0 deletions mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: mlir-opt %s -convert-vector-to-llvm='use-opaque-pointers=1' -split-input-file | FileCheck %s
// RUN: mlir-opt %s -convert-vector-to-llvm='use-opaque-pointers=1 enable-arm-sme' -split-input-file | FileCheck %s


func.func @bitcast_f32_to_i32_vector_0d(%input: vector<f32>) -> vector<i32> {
Expand Down