1//===- ACCLegalizeSerial.cpp - Legalize ACC Serial region -----------------===//
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 pass converts acc.serial into acc.parallel with num_gangs(1)
10// num_workers(1) vector_length(1).
11//
12// This transformation simplifies processing of acc regions by unifying the
13// handling of serial and parallel constructs. Since an OpenACC serial region
14// executes sequentially (like a parallel region with a single gang, worker, and
15// vector), this conversion is semantically equivalent while enabling code reuse
16// in later compilation stages.
17//
18//===----------------------------------------------------------------------===//
19
20#include "mlir/Dialect/OpenACC/Extensions/AllExtensions.h"
21#include "mlir/Dialect/OpenACC/Transforms/Passes.h"
22
23#include "mlir/Dialect/Arith/IR/Arith.h"
24#include "mlir/Dialect/Func/IR/FuncOps.h"
25#include "mlir/Dialect/OpenACC/OpenACC.h"
26#include "mlir/IR/Builders.h"
27#include "mlir/IR/BuiltinAttributes.h"
28#include "mlir/IR/Location.h"
29#include "mlir/IR/MLIRContext.h"
30#include "mlir/IR/PatternMatch.h"
31#include "mlir/IR/Region.h"
32#include "mlir/IR/Value.h"
33#include "mlir/Support/LLVM.h"
34#include "mlir/Support/LogicalResult.h"
35#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
36#include "llvm/Support/Debug.h"
37
38namespace mlir {
39namespace acc {
40#define GEN_PASS_DEF_ACCLEGALIZESERIAL
41#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
42} // namespace acc
43} // namespace mlir
44
45#define DEBUG_TYPE "acc-legalize-serial"
46
47namespace {
48using namespace mlir;
49
50struct ACCSerialOpConversion : public OpRewritePattern<acc::SerialOp> {
51 using OpRewritePattern<acc::SerialOp>::OpRewritePattern;
52
53 LogicalResult matchAndRewrite(acc::SerialOp serialOp,
54 PatternRewriter &rewriter) const override {
55
56 const Location loc = serialOp.getLoc();
57
58 // Create a container holding the constant value of 1 for use as the
59 // num_gangs, num_workers, and vector_length attributes.
60 llvm::SmallVector<mlir::Value> numValues;
61 auto value = arith::ConstantIntOp::create(builder&: rewriter, location: loc, value: 1, width: 32);
62 numValues.push_back(Elt: value);
63
64 // Since num_gangs is specified as both attributes and values, create a
65 // segment attribute.
66 llvm::SmallVector<int32_t> numGangsSegments;
67 numGangsSegments.push_back(Elt: numValues.size());
68 auto gangSegmentsAttr = rewriter.getDenseI32ArrayAttr(values: numGangsSegments);
69
70 // Create a device_type attribute set to `none` which ensures that
71 // the parallel dimensions specification applies to the default clauses.
72 llvm::SmallVector<mlir::Attribute> crtDeviceTypes;
73 auto crtDeviceTypeAttr = mlir::acc::DeviceTypeAttr::get(
74 context: rewriter.getContext(), value: mlir::acc::DeviceType::None);
75 crtDeviceTypes.push_back(Elt: crtDeviceTypeAttr);
76 auto devTypeAttr =
77 mlir::ArrayAttr::get(context: rewriter.getContext(), value: crtDeviceTypes);
78
79 LLVM_DEBUG(llvm::dbgs() << "acc.serial OP: " << serialOp << "\n");
80
81 // Create a new acc.parallel op with the same operands - except include the
82 // num_gangs, num_workers, and vector_length attributes.
83 acc::ParallelOp parOp = acc::ParallelOp::create(
84 builder&: rewriter, location: loc, asyncOperands: serialOp.getAsyncOperands(),
85 asyncOperandsDeviceType: serialOp.getAsyncOperandsDeviceTypeAttr(), asyncOnly: serialOp.getAsyncOnlyAttr(),
86 waitOperands: serialOp.getWaitOperands(), waitOperandsSegments: serialOp.getWaitOperandsSegmentsAttr(),
87 waitOperandsDeviceType: serialOp.getWaitOperandsDeviceTypeAttr(),
88 hasWaitDevnum: serialOp.getHasWaitDevnumAttr(), waitOnly: serialOp.getWaitOnlyAttr(), numGangs: numValues,
89 numGangsSegments: gangSegmentsAttr, numGangsDeviceType: devTypeAttr, numWorkers: numValues, numWorkersDeviceType: devTypeAttr, vectorLength: numValues,
90 vectorLengthDeviceType: devTypeAttr, ifCond: serialOp.getIfCond(), selfCond: serialOp.getSelfCond(),
91 selfAttr: serialOp.getSelfAttrAttr(), reductionOperands: serialOp.getReductionOperands(),
92 privateOperands: serialOp.getPrivateOperands(), firstprivateOperands: serialOp.getFirstprivateOperands(),
93 dataClauseOperands: serialOp.getDataClauseOperands(), defaultAttr: serialOp.getDefaultAttrAttr(),
94 combined: serialOp.getCombinedAttr());
95
96 parOp.getRegion().takeBody(other&: serialOp.getRegion());
97
98 LLVM_DEBUG(llvm::dbgs() << "acc.parallel OP: " << parOp << "\n");
99 rewriter.replaceOp(op: serialOp, newOp: parOp);
100
101 return success();
102 }
103};
104
105class ACCLegalizeSerial
106 : public mlir::acc::impl::ACCLegalizeSerialBase<ACCLegalizeSerial> {
107public:
108 using ACCLegalizeSerialBase<ACCLegalizeSerial>::ACCLegalizeSerialBase;
109
110 void getDependentDialects(DialectRegistry &registry) const override {
111 ACCLegalizeSerialBase<ACCLegalizeSerial>::getDependentDialects(registry);
112 acc::registerAllExtensions(registry);
113 }
114 void runOnOperation() override {
115 func::FuncOp funcOp = getOperation();
116 MLIRContext *context = funcOp.getContext();
117 RewritePatternSet patterns(context);
118 patterns.insert<ACCSerialOpConversion>(arg&: context);
119 (void)applyPatternsGreedily(op: funcOp, patterns: std::move(patterns));
120 }
121};
122} // namespace
123