1//===- SPIRVAttachTarget.cpp - Attach an SPIR-V target --------------------===//
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 `GPUSPIRVAttachTarget` pass, attaching
10// `#spirv.target_env` attributes to GPU modules.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/GPU/Transforms/SPIRV/Passes.h"
15
16#include "mlir/Dialect/GPU/IR/GPUDialect.h"
17#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
18#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
19#include "mlir/Dialect/SPIRV/IR/TargetAndABI.h"
20#include "mlir/IR/Builders.h"
21#include "mlir/Pass/Pass.h"
22#include "llvm/Support/Regex.h"
23
24namespace mlir {
25#define GEN_PASS_DEF_GPUSPIRVATTACHTARGET
26#include "mlir/Dialect/GPU/Transforms/SPIRV/Passes.h.inc"
27} // namespace mlir
28
29using namespace mlir;
30using namespace mlir::spirv;
31
32namespace {
33struct SPIRVAttachTarget
34 : public impl::GpuSPIRVAttachTargetBase<SPIRVAttachTarget> {
35 using Base::Base;
36
37 void runOnOperation() override;
38
39 void getDependentDialects(DialectRegistry &registry) const override {
40 registry.insert<spirv::SPIRVDialect>();
41 }
42};
43} // namespace
44
45void SPIRVAttachTarget::runOnOperation() {
46 OpBuilder builder(&getContext());
47 auto versionSymbol = symbolizeVersion(spirvVersion);
48 if (!versionSymbol)
49 return signalPassFailure();
50 auto apiSymbol = symbolizeClientAPI(clientApi);
51 if (!apiSymbol)
52 return signalPassFailure();
53 auto vendorSymbol = symbolizeVendor(deviceVendor);
54 if (!vendorSymbol)
55 return signalPassFailure();
56 auto deviceTypeSymbol = symbolizeDeviceType(deviceType);
57 if (!deviceTypeSymbol)
58 return signalPassFailure();
59 // Set the default device ID if none was given
60 if (!deviceId.hasValue())
61 deviceId = mlir::spirv::TargetEnvAttr::kUnknownDeviceID;
62
63 Version version = versionSymbol.value();
64 SmallVector<Capability, 4> capabilities;
65 SmallVector<Extension, 8> extensions;
66 for (const auto &cap : spirvCapabilities) {
67 auto capSymbol = symbolizeCapability(cap);
68 if (capSymbol)
69 capabilities.push_back(Elt: capSymbol.value());
70 }
71 ArrayRef<Capability> caps(capabilities);
72 for (const auto &ext : spirvExtensions) {
73 auto extSymbol = symbolizeExtension(ext);
74 if (extSymbol)
75 extensions.push_back(Elt: extSymbol.value());
76 }
77 ArrayRef<Extension> exts(extensions);
78 VerCapExtAttr vce = VerCapExtAttr::get(version, capabilities: caps, extensions: exts, context: &getContext());
79 auto target = TargetEnvAttr::get(triple: vce, limits: getDefaultResourceLimits(context: &getContext()),
80 clientAPI: apiSymbol.value(), vendorID: vendorSymbol.value(),
81 deviceType: deviceTypeSymbol.value(), deviceId);
82 llvm::Regex matcher(moduleMatcher);
83 getOperation()->walk(callback: [&](gpu::GPUModuleOp gpuModule) {
84 // Check if the name of the module matches.
85 if (!moduleMatcher.empty() && !matcher.match(String: gpuModule.getName()))
86 return;
87 // Create the target array.
88 SmallVector<Attribute> targets;
89 if (std::optional<ArrayAttr> attrs = gpuModule.getTargets())
90 targets.append(in_start: attrs->getValue().begin(), in_end: attrs->getValue().end());
91 targets.push_back(Elt: target);
92 // Remove any duplicate targets.
93 targets.erase(CS: llvm::unique(R&: targets), CE: targets.end());
94 // Update the target attribute array.
95 gpuModule.setTargetsAttr(builder.getArrayAttr(value: targets));
96 });
97}
98