Skip to content

[mlir][memref] Check memory space before lowering alloc ops #134427

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
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
4 changes: 2 additions & 2 deletions mlir/include/mlir/Conversion/LLVMCommon/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class ConvertToLLVMPattern : public ConversionPattern {
ValueRange indices,
ConversionPatternRewriter &rewriter) const;

/// Returns if the given memref has identity maps and the element type is
/// convertible to LLVM.
/// Returns if the given memref type is convertible to LLVM and has an
/// identity layout map.
bool isConvertibleAndHasIdentityMaps(MemRefType type) const;

/// Returns the type of a pointer to an element of the memref.
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Conversion/LLVMCommon/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ Value ConvertToLLVMPattern::getStridedElementPtr(
// only support memrefs with identity maps.
bool ConvertToLLVMPattern::isConvertibleAndHasIdentityMaps(
MemRefType type) const {
if (!typeConverter->convertType(type.getElementType()))
if (!type.getLayout().isIdentity())
return false;
return type.getLayout().isIdentity();
return static_cast<bool>(typeConverter->convertType(type));
}

Type ConvertToLLVMPattern::getElementPtrType(MemRefType type) const {
Expand Down
7 changes: 1 addition & 6 deletions mlir/lib/Conversion/MemRefToLLVM/AllocLikeConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ std::tuple<Value, Value> AllocationOpLLVMLowering::allocateBufferManuallyAlign(
MemRefType memRefType = getMemRefResultType(op);
// Allocate the underlying buffer.
Type elementPtrType = this->getElementPtrType(memRefType);
if (!elementPtrType) {
emitError(loc, "conversion of memref memory space ")
<< memRefType.getMemorySpace()
<< " to integer address space "
"failed. Consider adding memory space conversions.";
}
assert(elementPtrType && "could not compute element ptr type");
FailureOr<LLVM::LLVMFuncOp> allocFuncOp = getNotalignedAllocFn(
getTypeConverter(), op->getParentWithTrait<OpTrait::SymbolTable>(),
getIndexType());
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Conversion/MemRefToLLVM/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func.func @bad_address_space(%a: memref<2xindex, "foo">) {

// CHECK-LABEL: @invalid_int_conversion
func.func @invalid_int_conversion() {
// expected-error@+1 {{conversion of memref memory space 1 : ui64 to integer address space failed. Consider adding memory space conversions.}}
// expected-error@unknown{{conversion of memref memory space 1 : ui64 to integer address space failed. Consider adding memory space conversions.}}
Copy link
Member

Choose a reason for hiding this comment

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

(Non actionable)
This is a bit unfortunate regarding the quality of the error reporting, but is more of a pre-existing issue with the conversion functions (and the type converter infrastructure). I like the fact all type conversion related error handling is actually in the type conversions now.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm thinking of maybe even removing this error entirely (not in this PR). This error is produced in the type converter. We don't usually produce errors there.

There are other ways to signal this to the user: when the type converter fails, the pattern does not apply. Either because a hand-written converter.convertType fails or because the values of the adaptor could not be computed. In either case, -debug would show that (notifyMatchFailure).

Copy link
Member

Choose a reason for hiding this comment

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

That sounds great to me, I'd support that

%alloc = memref.alloc() {alignment = 64 : i64} : memref<10xf32, 1 : ui64>
return
}
Expand All @@ -32,7 +32,6 @@ func.func @invalid_int_conversion() {
// expected-error@unknown{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
// CHECK-LABEL: @issue_70160
func.func @issue_70160() {
// expected-error@+1{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
%alloc = memref.alloc() : memref<1x32x33xi32, #gpu.address_space<workgroup>>
%alloc1 = memref.alloc() : memref<i32>
%c0 = arith.constant 0 : index
Expand Down