MLIR 24.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 function parameter alignment.
75
76/// Returns the attribute name for specifying function parameter and result
77/// attributes.
78StringRef getFuncParamAttrName();
79
80/// Returns the attribute name for specifying argument ABI information.
82
83/// Gets the InterfaceVarABIAttr given its fields.
84InterfaceVarABIAttr
85getInterfaceVarABIAttr(unsigned descriptorSet, unsigned binding,
86 std::optional<StorageClass> storageClass,
87 MLIRContext *context);
88
89/// Returns whether the given SPIR-V target (described by TargetEnvAttr) needs
90/// ABI attributes for interface variables (spirv.interface_var_abi).
92
93/// Returns the attribute name for specifying entry point information.
94StringRef getEntryPointABIAttrName();
95
96/// Gets the EntryPointABIAttr given its fields.
97/// targetWidth is used by several execution modes. It is the element width
98/// of floating-point operations.
99/// Refer to Execution Mode in SPIR-V specification.
100/// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_execution_mode
101EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context,
102 ArrayRef<int32_t> workgroupSize = {},
103 std::optional<int> subgroupSize = {},
104 std::optional<int> targetWidth = {});
105
106/// Queries the entry point ABI on the nearest function-like op containing the
107/// given `op`. Returns null attribute if not found.
108EntryPointABIAttr lookupEntryPointABI(Operation *op);
109
110/// Queries the local workgroup size from entry point ABI on the nearest
111/// function-like op containing the given `op`. Returns null attribute if not
112/// found.
114
115/// Returns a default resource limits attribute that uses numbers from
116/// "Table 46. Required Limits" of the Vulkan spec.
117ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context);
118
119/// Returns the attribute name for specifying loop control.
120StringRef getLoopControlAttrName();
121
122/// Returns the attribute name for specifying selection control.
124
125/// Returns the attribute name for specifying SPIR-V target environment.
126StringRef getTargetEnvAttrName();
127
128/// Returns the default target environment: SPIR-V 1.0 with Shader capability
129/// and no extra extensions.
130TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
131
132/// Queries the target environment recursively from enclosing symbol table ops
133/// containing the given `op`.
134TargetEnvAttr lookupTargetEnv(Operation *op);
135
136/// Queries the target environment recursively from enclosing symbol table ops
137/// containing the given `op` or returns the default target environment as
138/// returned by getDefaultTargetEnv() if not provided.
140
141/// Returns addressing model selected based on target environment.
142AddressingModel getAddressingModel(TargetEnvAttr targetAttr,
143 bool use64bitAddress);
144
145/// Returns execution model selected based on target environment.
146/// Returns failure if it cannot be selected.
147FailureOr<ExecutionModel> getExecutionModel(TargetEnvAttr targetAttr);
148
149/// Returns memory model selected based on target environment.
150/// Returns failure if it cannot be selected.
151FailureOr<MemoryModel> getMemoryModel(TargetEnvAttr targetAttr);
152
153} // namespace spirv
154} // namespace mlir
155
156#endif // MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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 ...
StringRef getFuncParamAttrName()
Returns the attribute name for specifying function parameter and result attributes.
AddressingModel getAddressingModel(TargetEnvAttr targetAttr, bool use64bitAddress)
Returns addressing model selected based on target environment.
StringRef getFuncParamAlignmentAttrName()
Returns the attribute name for specifying function parameter alignment.
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...
StringRef getSelectionControlAttrName()
Returns the attribute name for specifying selection control.
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