-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][memref][llvm] Infer llvm alias scopes attrs from memref.distinct_objects
#160512
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
base: main
Are you sure you want to change the base?
Changes from all commits
7a513df
1e24829
4b943e8
98af921
f492b57
aceff37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//==- InferAliasScopeAttrs.h - Infer LLVM alias scope attributes -*- C++ -*-==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_LLVMIR_TRANSFORMS_INFERALIASSCOPEATTRS_H | ||
#define MLIR_DIALECT_LLVMIR_TRANSFORMS_INFERALIASSCOPEATTRS_H | ||
|
||
#include <memory> | ||
|
||
namespace mlir { | ||
class Pass; | ||
namespace LLVM { | ||
#define GEN_PASS_DECL_LLVMINFERALIASSCOPEATTRIBUTES | ||
#include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc" | ||
} // namespace LLVM | ||
} // namespace mlir | ||
|
||
#endif // MLIR_DIALECT_LLVMIR_TRANSFORMS_INFERALIASSCOPEATTRS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -465,6 +465,50 @@ struct AssumeAlignmentOpLowering | |
} | ||
}; | ||
|
||
struct DistinctObjectsOpLowering | ||
: public ConvertOpToLLVMPattern<memref::DistinctObjectsOp> { | ||
using ConvertOpToLLVMPattern< | ||
memref::DistinctObjectsOp>::ConvertOpToLLVMPattern; | ||
explicit DistinctObjectsOpLowering(const LLVMTypeConverter &converter) | ||
: ConvertOpToLLVMPattern<memref::DistinctObjectsOp>(converter) {} | ||
|
||
LogicalResult | ||
matchAndRewrite(memref::DistinctObjectsOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
ValueRange operands = adaptor.getOperands(); | ||
if (operands.size() <= 1) { | ||
// Fast path. | ||
rewriter.replaceOp(op, operands); | ||
return success(); | ||
} | ||
|
||
Location loc = op.getLoc(); | ||
SmallVector<Value> ptrs; | ||
for (auto [origOperand, newOperand] : | ||
llvm::zip_equal(op.getOperands(), operands)) { | ||
auto memrefType = cast<MemRefType>(origOperand.getType()); | ||
Value ptr = getStridedElementPtr(rewriter, loc, memrefType, newOperand, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this is the right thing? I think we want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (this code is taken from original There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah - you haven't pushed to a users/ to set up a stacked branch Can you pull my comment over? |
||
/*indices=*/{}); | ||
ptrs.push_back(ptr); | ||
} | ||
|
||
auto cond = | ||
LLVM::ConstantOp::create(rewriter, loc, rewriter.getI1Type(), 1); | ||
// Generate separate_storage assumptions for each pair of pointers. | ||
for (auto i : llvm::seq<size_t>(ptrs.size() - 1)) { | ||
for (auto j : llvm::seq<size_t>(i + 1, ptrs.size())) { | ||
Value ptr1 = ptrs[i]; | ||
Value ptr2 = ptrs[j]; | ||
LLVM::AssumeOp::create(rewriter, loc, cond, | ||
LLVM::AssumeSeparateStorageTag{}, ptr1, ptr2); | ||
} | ||
} | ||
|
||
rewriter.replaceOp(op, operands); | ||
return success(); | ||
} | ||
}; | ||
|
||
// A `dealloc` is converted into a call to `free` on the underlying data buffer. | ||
// The memref descriptor being an SSA value, there is no need to clean it up | ||
// in any way. | ||
|
@@ -878,6 +922,18 @@ struct GetGlobalMemrefOpLowering | |
} | ||
}; | ||
|
||
static SmallVector<std::pair<StringAttr, Attribute>> | ||
copyImportantAttrs(Operation *op) { | ||
SmallVector<std::pair<StringAttr, Attribute>> attrs; | ||
for (StringRef attrName : {LLVM::LLVMDialect::getAliasScopesAttrName(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just unconditionally pass through LLVM dialect attributes here? |
||
LLVM::LLVMDialect::getNoAliasAttrName()}) { | ||
auto nameAttr = StringAttr::get(op->getContext(), attrName); | ||
if (auto attr = op->getAttr(nameAttr)) | ||
attrs.emplace_back(nameAttr, attr); | ||
} | ||
return attrs; | ||
} | ||
|
||
// Load operation is lowered to obtaining a pointer to the indexed element | ||
// and loading it. | ||
struct LoadOpLowering : public LoadStoreOpLowering<memref::LoadOp> { | ||
|
@@ -888,15 +944,22 @@ struct LoadOpLowering : public LoadStoreOpLowering<memref::LoadOp> { | |
ConversionPatternRewriter &rewriter) const override { | ||
auto type = loadOp.getMemRefType(); | ||
|
||
SmallVector<std::pair<StringAttr, Attribute>> importantAttrs = | ||
copyImportantAttrs(loadOp); | ||
|
||
// Per memref.load spec, the indices must be in-bounds: | ||
// 0 <= idx < dim_size, and additionally all offsets are non-negative, | ||
// hence inbounds and nuw are used when lowering to llvm.getelementptr. | ||
Value dataPtr = getStridedElementPtr(rewriter, loadOp.getLoc(), type, | ||
adaptor.getMemref(), | ||
adaptor.getIndices(), kNoWrapFlags); | ||
rewriter.replaceOpWithNewOp<LLVM::LoadOp>( | ||
auto newOp = rewriter.replaceOpWithNewOp<LLVM::LoadOp>( | ||
loadOp, typeConverter->convertType(type.getElementType()), dataPtr, | ||
loadOp.getAlignment().value_or(0), false, loadOp.getNontemporal()); | ||
|
||
for (auto [nameAttr, attr] : importantAttrs) | ||
newOp->setAttr(nameAttr, attr); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
@@ -911,15 +974,22 @@ struct StoreOpLowering : public LoadStoreOpLowering<memref::StoreOp> { | |
ConversionPatternRewriter &rewriter) const override { | ||
auto type = op.getMemRefType(); | ||
|
||
SmallVector<std::pair<StringAttr, Attribute>> importantAttrs = | ||
copyImportantAttrs(op); | ||
|
||
// Per memref.store spec, the indices must be in-bounds: | ||
// 0 <= idx < dim_size, and additionally all offsets are non-negative, | ||
// hence inbounds and nuw are used when lowering to llvm.getelementptr. | ||
Value dataPtr = | ||
getStridedElementPtr(rewriter, op.getLoc(), type, adaptor.getMemref(), | ||
adaptor.getIndices(), kNoWrapFlags); | ||
rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op, adaptor.getValue(), dataPtr, | ||
op.getAlignment().value_or(0), | ||
false, op.getNontemporal()); | ||
auto newOp = rewriter.replaceOpWithNewOp<LLVM::StoreOp>( | ||
op, adaptor.getValue(), dataPtr, op.getAlignment().value_or(0), false, | ||
op.getNontemporal()); | ||
|
||
for (auto [nameAttr, attr] : importantAttrs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the for loop, I think, or putting the extraction beforehand. You could just |
||
newOp->setAttr(nameAttr, attr); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
@@ -1997,22 +2067,23 @@ void mlir::populateFinalizeMemRefToLLVMConversionPatterns( | |
patterns.add< | ||
AllocaOpLowering, | ||
AllocaScopeOpLowering, | ||
AtomicRMWOpLowering, | ||
AssumeAlignmentOpLowering, | ||
AtomicRMWOpLowering, | ||
ConvertExtractAlignedPointerAsIndex, | ||
DimOpLowering, | ||
DistinctObjectsOpLowering, | ||
ExtractStridedMetadataOpLowering, | ||
GenericAtomicRMWOpLowering, | ||
GetGlobalMemrefOpLowering, | ||
LoadOpLowering, | ||
MemRefCastOpLowering, | ||
MemorySpaceCastOpLowering, | ||
MemRefReinterpretCastOpLowering, | ||
MemRefReshapeOpLowering, | ||
MemorySpaceCastOpLowering, | ||
PrefetchOpLowering, | ||
RankOpLowering, | ||
ReassociatingReshapeOpConversion<memref::ExpandShapeOp>, | ||
ReassociatingReshapeOpConversion<memref::CollapseShapeOp>, | ||
ReassociatingReshapeOpConversion<memref::ExpandShapeOp>, | ||
StoreOpLowering, | ||
SubViewOpLowering, | ||
TransposeOpLowering, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to preemptively define an interface here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an interface so MLIR
LocalAliasAnalysis
won't depend directly on memref dialect.