MLIR 24.0.0git
Target.cpp
Go to the documentation of this file.
1//===- Target.cpp - MLIR SPIR-V target compilation --------------*- C++ -*-===//
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 files defines SPIR-V target related functions including registration
10// calls for the `#spirv.target_env` compilation attribute.
11//
12//===----------------------------------------------------------------------===//
13
15
21
22#include <cstdlib>
23#include <cstring>
24
25using namespace mlir;
26using namespace mlir::spirv;
27
28namespace {
29// SPIR-V implementation of the gpu:TargetAttrInterface.
30class SPIRVTargetAttrImpl
31 : public gpu::TargetAttrInterface::FallbackModel<SPIRVTargetAttrImpl> {
32public:
33 std::optional<mlir::gpu::SerializedObject>
34 serializeToObject(Attribute attribute, Operation *moduleOp,
35 const gpu::TargetOptions &options) const;
36
37 Attribute createObject(Attribute attribute, Operation *moduleOp,
38 const mlir::gpu::SerializedObject &object,
39 const gpu::TargetOptions &options) const;
40};
41} // namespace
42
43// Register the SPIR-V dialect, the SPIR-V translation & the target interface.
50
57
58// Reuse from existing serializer
59std::optional<mlir::gpu::SerializedObject>
60SPIRVTargetAttrImpl::serializeToObject(
61 Attribute attribute, Operation *moduleOp,
62 const gpu::TargetOptions &options) const {
63 if (!moduleOp)
64 return std::nullopt;
65 auto gpuMod = dyn_cast<gpu::GPUModuleOp>(moduleOp);
66 if (!gpuMod) {
67 moduleOp->emitError("expected to be a gpu.module op");
68 return std::nullopt;
69 }
70 auto spvMods = gpuMod.getOps<spirv::ModuleOp>();
71 if (spvMods.empty())
72 return std::nullopt;
73
74 auto spvMod = *spvMods.begin();
76
77 spvBinary.clear();
78 // Serialize the spirv.module op to SPIR-V blob.
79 if (mlir::failed(spirv::serialize(spvMod, spvBinary))) {
80 spvMod.emitError() << "failed to serialize SPIR-V module";
81 return std::nullopt;
82 }
83
84 SmallVector<char, 0> spvData(spvBinary.size() * sizeof(uint32_t), 0);
85 std::memcpy(spvData.data(), spvBinary.data(), spvData.size());
86
87 spvMod.erase();
88 return gpu::SerializedObject{std::move(spvData)};
89}
90
91// Prepare Attribute for gpu.binary with serialized kernel object
92Attribute
93SPIRVTargetAttrImpl::createObject(Attribute attribute, Operation *moduleOp,
94 const mlir::gpu::SerializedObject &object,
95 const gpu::TargetOptions &options) const {
96 gpu::CompilationTarget format = options.getCompilationTarget();
97 DictionaryAttr objectProps;
98 Builder builder(attribute.getContext());
99 return builder.getAttr<gpu::ObjectAttr>(
100 attribute, format,
101 builder.getStringAttr(
102 StringRef(object.getObject().data(), object.getObject().size())),
103 objectProps, /*kernels=*/nullptr);
104}
static llvm::ManagedStatic< PassManagerOptions > options
Attributes are known-constant values of operations.
Definition Attributes.h:25
MLIRContext * getContext() const
Return the context this attribute belongs to.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
LogicalResult serialize(ModuleOp moduleOp, SmallVectorImpl< uint32_t > &binary, const SerializationOptions &options={})
Serializes the given SPIR-V moduleOp and writes to binary.
void registerSPIRVTargetInterfaceExternalModels(DialectRegistry &registry)
Registers the TargetAttrInterface for the #spirv.target_env attribute in the given registry.
Definition Target.cpp:44
Include the generated interface declarations.