77#define GEN_PASS_DEF_ACCSPECIALIZEFORDEVICE
78#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
89class FoldAccOnDeviceOpConversion :
public OpRewritePattern<acc::OnDeviceOp> {
90 using OpRewritePattern<acc::OnDeviceOp>::OpRewritePattern;
95 FoldAccOnDeviceOpConversion(MLIRContext *context,
97 : OpRewritePattern<acc::OnDeviceOp>(context),
98 theDeviceTypes(theDeviceTypes) {}
100 LogicalResult matchAndRewrite(acc::OnDeviceOp op,
101 PatternRewriter &rewriter)
const override {
102 Value deviceType = op.getDeviceType();
105 bool result = theDeviceTypes.contains(constVal.getSExtValue());
113 Location loc = op.getLoc();
115 for (int64_t type : theDeviceTypes) {
116 Value typeValue = arith::ConstantOp::create(
117 rewriter, loc, deviceType.
getType(),
119 Value isType = arith::CmpIOp::create(
120 rewriter, loc, arith::CmpIPredicate::eq, deviceType, typeValue);
122 result = arith::OrIOp::create(rewriter, loc,
result, isType);
126 assert(
result &&
"At least one device type expected.");
132class ACCSpecializeForDevice
135 using ACCSpecializeForDeviceBase<
136 ACCSpecializeForDevice>::ACCSpecializeForDeviceBase;
138 void getDependentDialects(DialectRegistry ®istry)
const override {
139 ACCSpecializeForDeviceBase<ACCSpecializeForDevice>::getDependentDialects(
144 void runOnOperation()
override {
145 func::FuncOp func = getOperation();
147 TypesForDevice types{theDeviceTypes.begin(), theDeviceTypes.end()};
151 GreedyRewriteConfig config;
165 SmallVector<Operation *> opsToTransform;
166 func.walk([&](Operation *op) {
167 if (isa<ACC_COMPUTE_CONSTRUCT_OPS>(op)) {
169 op->
walk([&](Operation *innerOp) {
173 if (isa<acc::OpenACCDialect>(innerOp->
getDialect()))
174 opsToTransform.push_back(innerOp);
178 if (!opsToTransform.empty())
241 patterns.
insert<FoldAccOnDeviceOpConversion>(context, theDeviceTypes);
IntegerAttr getIntegerAttr(Type type, int64_t value)
BoolAttr getBoolAttr(bool value)
GreedyRewriteConfig & setUseTopDownTraversal(bool use=true)
GreedyRewriteConfig & setStrictness(GreedyRewriteStrictness mode)
MLIRContext is the top-level object for a collection of MLIR operations.
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
RewritePatternSet & insert(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
MLIRContext * getContext() const
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Type getType() const
Return the type of this value.
Pattern to erase acc.declare_enter and its associated acc.declare_exit.
Pattern to simply erase an ACC op (for ops with no results).
Pattern to replace an ACC op with its var operand.
Pattern to unwrap a region from an ACC op and erase the wrapper.
llvm::SmallSetVector< int64_t, 3 > TypesForDevice
Holds information for which integers represent a device type in the runtime.
bool isSpecializedAccRoutine(mlir::Operation *op)
Used to check whether this is a specialized accelerator version of acc routine function.
void registerAllExtensions(DialectRegistry ®istry)
Register the standard OpenACC models for GPU, LLVM and MemRef IR.
void populateACCSpecializeForDevicePatterns(RewritePatternSet &patterns, const TypesForDevice &theDeviceTypes)
Populates all patterns for device specialization.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
LogicalResult applyPatternsGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...