MLIR 23.0.0git
TargetAndABI.h
Go to the documentation of this file.
1//===- TargetAndABI.h - SPIR-V target and ABI utilities --------*- 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 file declares utilities for SPIR-V target and shader interface ABI.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
14#define MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
15
17#include "mlir/Support/LLVM.h"
18#include "llvm/ADT/SmallSet.h"
19#include <optional>
20
21namespace mlir {
22class Operation;
23
24namespace spirv {
25enum class StorageClass : uint32_t;
26
27/// A wrapper class around a spirv::TargetEnvAttr to provide query methods for
28/// allowed version/capabilities/extensions.
29class TargetEnv {
30public:
31 explicit TargetEnv(TargetEnvAttr targetAttr);
32
33 Version getVersion() const;
34
35 /// Returns true if the given capability is allowed.
36 bool allows(Capability) const;
37 /// Returns the first allowed one if any of the given capabilities is allowed.
38 /// Returns std::nullopt otherwise.
39 std::optional<Capability> allows(ArrayRef<Capability>) const;
40
41 /// Returns true if the given extension is allowed.
42 bool allows(Extension) const;
43 /// Returns the first allowed one if any of the given extensions is allowed.
44 /// Returns std::nullopt otherwise.
45 std::optional<Extension> allows(ArrayRef<Extension>) const;
46
47 /// Returns the vendor ID.
48 Vendor getVendorID() const;
49
50 /// Returns the device type.
51 DeviceType getDeviceType() const;
52
53 /// Returns the device ID.
54 uint32_t getDeviceID() const;
55
56 /// Returns the MLIRContext.
57 MLIRContext *getContext() const;
58
59 /// Returns the target resource limits.
60 ResourceLimitsAttr getResourceLimits() const;
61
62 TargetEnvAttr getAttr() const { return targetAttr; }
63
64 /// Allows implicity converting to the underlying spirv::TargetEnvAttr.
65 operator TargetEnvAttr() const { return targetAttr; }
66
67private:
68 TargetEnvAttr targetAttr;
69 llvm::SmallSet<Extension, 4> givenExtensions; /// Allowed extensions
70 llvm::SmallSet<Capability, 8> givenCapabilities; /// Allowed capabilities
71};
72
73/// Returns the attribute name for specifying argument ABI information.
75
76/// Gets the InterfaceVarABIAttr given its fields.
77InterfaceVarABIAttr
78getInterfaceVarABIAttr(unsigned descriptorSet, unsigned binding,
79 std::optional<StorageClass> storageClass,
80 MLIRContext *context);
81
82/// Returns whether the given SPIR-V target (described by TargetEnvAttr) needs
83/// ABI attributes for interface variables (spirv.interface_var_abi).
85
86/// Returns the attribute name for specifying entry point information.
87StringRef getEntryPointABIAttrName();
88
89/// Gets the EntryPointABIAttr given its fields.
90/// targetWidth is used by several execution modes. It is the element width
91/// of floating-point operations.
92/// Refer to Execution Mode in SPIR-V specification.
93/// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_execution_mode
94EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context,
95 ArrayRef<int32_t> workgroupSize = {},
96 std::optional<int> subgroupSize = {},
97 std::optional<int> targetWidth = {});
98
99/// Queries the entry point ABI on the nearest function-like op containing the
100/// given `op`. Returns null attribute if not found.
101EntryPointABIAttr lookupEntryPointABI(Operation *op);
102
103/// Queries the local workgroup size from entry point ABI on the nearest
104/// function-like op containing the given `op`. Returns null attribute if not
105/// found.
107
108/// Returns a default resource limits attribute that uses numbers from
109/// "Table 46. Required Limits" of the Vulkan spec.
110ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context);
111
112/// Returns the attribute name for specifying loop control.
113StringRef getLoopControlAttrName();
114
115/// Returns the attribute name for specifying SPIR-V target environment.
116StringRef getTargetEnvAttrName();
117
118/// Returns the default target environment: SPIR-V 1.0 with Shader capability
119/// and no extra extensions.
120TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
121
122/// Queries the target environment recursively from enclosing symbol table ops
123/// containing the given `op`.
124TargetEnvAttr lookupTargetEnv(Operation *op);
125
126/// Queries the target environment recursively from enclosing symbol table ops
127/// containing the given `op` or returns the default target environment as
128/// returned by getDefaultTargetEnv() if not provided.
130
131/// Returns addressing model selected based on target environment.
132AddressingModel getAddressingModel(TargetEnvAttr targetAttr,
133 bool use64bitAddress);
134
135/// Returns execution model selected based on target environment.
136/// Returns failure if it cannot be selected.
137FailureOr<ExecutionModel> getExecutionModel(TargetEnvAttr targetAttr);
138
139/// Returns memory model selected based on target environment.
140/// Returns failure if it cannot be selected.
141FailureOr<MemoryModel> getMemoryModel(TargetEnvAttr targetAttr);
142
143} // namespace spirv
144} // namespace mlir
145
146#endif // MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
An attribute that specifies the target version, allowed extensions and capabilities,...
bool allows(Extension) const
Returns true if the given extension is allowed.
std::optional< Extension > allows(ArrayRef< Extension >) const
Returns the first allowed one if any of the given extensions is allowed.
DeviceType getDeviceType() const
Returns the device type.
Version getVersion() const
Vendor getVendorID() const
Returns the vendor ID.
std::optional< Capability > allows(ArrayRef< Capability >) const
Returns the first allowed one if any of the given capabilities is allowed.
bool allows(Capability) const
Returns true if the given capability is allowed.
ResourceLimitsAttr getResourceLimits() const
Returns the target resource limits.
TargetEnv(TargetEnvAttr targetAttr)
TargetEnvAttr getAttr() const
MLIRContext * getContext() const
Returns the MLIRContext.
uint32_t getDeviceID() const
Returns the device ID.
StringRef getInterfaceVarABIAttrName()
Returns the attribute name for specifying argument ABI information.
bool needsInterfaceVarABIAttrs(TargetEnvAttr targetAttr)
Returns whether the given SPIR-V target (described by TargetEnvAttr) needs ABI attributes for interfa...
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
InterfaceVarABIAttr getInterfaceVarABIAttr(unsigned descriptorSet, unsigned binding, std::optional< StorageClass > storageClass, MLIRContext *context)
Gets the InterfaceVarABIAttr given its fields.
EntryPointABIAttr lookupEntryPointABI(Operation *op)
Queries the entry point ABI on the nearest function-like op containing the given op.
StringRef getLoopControlAttrName()
Returns the attribute name for specifying loop control.
EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context, ArrayRef< int32_t > workgroupSize={}, std::optional< int > subgroupSize={}, std::optional< int > targetWidth={})
Gets the EntryPointABIAttr given its fields.
TargetEnvAttr lookupTargetEnv(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op.
StringRef getTargetEnvAttrName()
Returns the attribute name for specifying SPIR-V target environment.
DenseI32ArrayAttr lookupLocalWorkGroupSize(Operation *op)
Queries the local workgroup size from entry point ABI on the nearest function-like op containing the ...
AddressingModel getAddressingModel(TargetEnvAttr targetAttr, bool use64bitAddress)
Returns addressing model selected based on target environment.
FailureOr< ExecutionModel > getExecutionModel(TargetEnvAttr targetAttr)
Returns execution model selected based on target environment.
FailureOr< MemoryModel > getMemoryModel(TargetEnvAttr targetAttr)
Returns memory model selected based on target environment.
ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context)
Returns a default resource limits attribute that uses numbers from "Table 46. Required Limits" of the...
TargetEnvAttr getDefaultTargetEnv(MLIRContext *context)
Returns the default target environment: SPIR-V 1.0 with Shader capability and no extra extensions.
StringRef getEntryPointABIAttrName()
Returns the attribute name for specifying entry point information.
Include the generated interface declarations.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr