Skip to content

Commit bafa2f4

Browse files
[mlir][memref] Check memory space before lowering alloc ops (#134427)
Check the memory space before lowering allocation ops, instead of starting the lowering and then rolling back the pattern when the memory space was found to be incompatible with LLVM. Note: This is in preparation of the One-Shot Dialect Conversion refactoring. Note: `isConvertibleAndHasIdentityMaps` now also checks the memory space.
1 parent 4509bc1 commit bafa2f4

File tree

4 files changed

+6
-12
lines changed

4 files changed

+6
-12
lines changed

mlir/include/mlir/Conversion/LLVMCommon/Pattern.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class ConvertToLLVMPattern : public ConversionPattern {
7575
ValueRange indices,
7676
ConversionPatternRewriter &rewriter) const;
7777

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

8282
/// Returns the type of a pointer to an element of the memref.

mlir/lib/Conversion/LLVMCommon/Pattern.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ Value ConvertToLLVMPattern::getStridedElementPtr(
9999
// only support memrefs with identity maps.
100100
bool ConvertToLLVMPattern::isConvertibleAndHasIdentityMaps(
101101
MemRefType type) const {
102-
if (!typeConverter->convertType(type.getElementType()))
102+
if (!type.getLayout().isIdentity())
103103
return false;
104-
return type.getLayout().isIdentity();
104+
return static_cast<bool>(typeConverter->convertType(type));
105105
}
106106

107107
Type ConvertToLLVMPattern::getElementPtrType(MemRefType type) const {

mlir/lib/Conversion/MemRefToLLVM/AllocLikeConversion.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ std::tuple<Value, Value> AllocationOpLLVMLowering::allocateBufferManuallyAlign(
7373
MemRefType memRefType = getMemRefResultType(op);
7474
// Allocate the underlying buffer.
7575
Type elementPtrType = this->getElementPtrType(memRefType);
76-
if (!elementPtrType) {
77-
emitError(loc, "conversion of memref memory space ")
78-
<< memRefType.getMemorySpace()
79-
<< " to integer address space "
80-
"failed. Consider adding memory space conversions.";
81-
}
76+
assert(elementPtrType && "could not compute element ptr type");
8277
FailureOr<LLVM::LLVMFuncOp> allocFuncOp = getNotalignedAllocFn(
8378
getTypeConverter(), op->getParentWithTrait<OpTrait::SymbolTable>(),
8479
getIndexType());

mlir/test/Conversion/MemRefToLLVM/invalid.mlir

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func.func @bad_address_space(%a: memref<2xindex, "foo">) {
2222

2323
// CHECK-LABEL: @invalid_int_conversion
2424
func.func @invalid_int_conversion() {
25-
// expected-error@+1 {{conversion of memref memory space 1 : ui64 to integer address space failed. Consider adding memory space conversions.}}
25+
// expected-error@unknown{{conversion of memref memory space 1 : ui64 to integer address space failed. Consider adding memory space conversions.}}
2626
%alloc = memref.alloc() {alignment = 64 : i64} : memref<10xf32, 1 : ui64>
2727
return
2828
}
@@ -32,7 +32,6 @@ func.func @invalid_int_conversion() {
3232
// expected-error@unknown{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
3333
// CHECK-LABEL: @issue_70160
3434
func.func @issue_70160() {
35-
// expected-error@+1{{conversion of memref memory space #gpu.address_space<workgroup> to integer address space failed. Consider adding memory space conversions}}
3635
%alloc = memref.alloc() : memref<1x32x33xi32, #gpu.address_space<workgroup>>
3736
%alloc1 = memref.alloc() : memref<i32>
3837
%c0 = arith.constant 0 : index

0 commit comments

Comments
 (0)