MLIR 22.0.0git
GlobalIdRewriter.cpp
Go to the documentation of this file.
1//===- GlobalIdRewriter.cpp - Implementation of GlobalId rewriting -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements in-dialect rewriting of the global_id op for archs
10// where global_id.x = threadId.x + blockId.x * blockDim.x
11//
12//===----------------------------------------------------------------------===//
13
18
19using namespace mlir;
20
21namespace {
22struct GpuGlobalIdRewriter : public OpRewritePattern<gpu::GlobalIdOp> {
23 using OpRewritePattern<gpu::GlobalIdOp>::OpRewritePattern;
24
25 LogicalResult matchAndRewrite(gpu::GlobalIdOp op,
26 PatternRewriter &rewriter) const override {
27 Location loc = op.getLoc();
28 auto dim = op.getDimension();
29 Value blockId = gpu::BlockIdOp::create(rewriter, loc, dim);
30 Value blockDim = gpu::BlockDimOp::create(rewriter, loc, dim);
31 auto indexType = rewriter.getIndexType();
32 // Compute blockId.x * blockDim.x
33 Value tmp =
34 arith::MulIOp::create(rewriter, loc, indexType, blockId, blockDim);
35 Value threadId = gpu::ThreadIdOp::create(rewriter, loc, dim);
36 // Compute threadId.x + blockId.x * blockDim.x
37 rewriter.replaceOpWithNewOp<arith::AddIOp>(op, indexType, threadId, tmp);
38 return success();
39 }
40};
41} // namespace
42
44 patterns.add<GpuGlobalIdRewriter>(patterns.getContext());
45}
return success()
IndexType getIndexType()
Definition Builders.cpp:51
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Include the generated interface declarations.
void populateGpuGlobalIdPatterns(RewritePatternSet &patterns)
Collect a set of patterns to rewrite GlobalIdOp op within the GPU dialect.
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...