MLIR 24.0.0git
AttachSelectObject.cpp
Go to the documentation of this file.
1//===- AttachSelectObject.cpp - Attach a select object handler ------------===//
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 the `GpuAttachSelectObjectPass` pass, attaching
10// `#gpu.select_object` offloading handlers to GPU modules.
11//
12//===----------------------------------------------------------------------===//
13
15
17#include "mlir/IR/Builders.h"
18#include "llvm/Support/Regex.h"
19#include <limits>
20
21using namespace mlir;
22using namespace mlir::gpu;
23
24namespace mlir {
25#define GEN_PASS_DEF_GPUATTACHSELECTOBJECTPASS
26#include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
27} // namespace mlir
28
29namespace {
30class GpuAttachSelectObjectPass
31 : public impl::GpuAttachSelectObjectPassBase<GpuAttachSelectObjectPass> {
32public:
33 using Base::Base;
34 void runOnOperation() final;
35};
36} // namespace
37
38void GpuAttachSelectObjectPass::runOnOperation() {
39 if (priority > std::numeric_limits<uint16_t>::max()) {
40 getOperation()->emitError()
41 << "Invalid priority specified: " << priority.getValue()
42 << " (expected a value in the range [0, 65535])";
43 return signalPassFailure();
44 }
45 // The default priority is the one of modules with no offloading handler.
46 if (priority == SelectObjectAttr::kDefaultPriority)
47 return;
48
49 Builder builder(&getContext());
50 llvm::Regex matcher(moduleMatcher);
51 for (Region &region : getOperation()->getRegions()) {
52 for (Block &block : region.getBlocks()) {
53 for (GPUModuleOp module : block.getOps<GPUModuleOp>()) {
54 // Check if the name of the module matches.
55 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
56 continue;
57 // Leave other offloading handlers, and handlers that already specify
58 // a priority, unchanged.
59 Attribute handler = module.getOffloadingHandlerAttr();
60 auto selectObject = dyn_cast_or_null<SelectObjectAttr>(handler);
61 if (handler && !selectObject)
62 continue;
63 if (selectObject &&
64 selectObject.getPriority() != SelectObjectAttr::kDefaultPriority)
65 continue;
66 // A null target selects the first object.
67 Attribute target =
68 selectObject ? selectObject.getTarget() : Attribute();
69 if (!target)
70 target = builder.getI64IntegerAttr(0);
71 module.setOffloadingHandlerAttr(
72 SelectObjectAttr::get(&getContext(), target, priority));
73 }
74 }
75 }
76}
b getContext())
Include the generated interface declarations.