MLIR  18.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 
21 namespace mlir {
22 class Operation;
23 
24 namespace spirv {
25 enum class StorageClass : uint32_t;
26 
27 /// A wrapper class around a spirv::TargetEnvAttr to provide query methods for
28 /// allowed version/capabilities/extensions.
29 class TargetEnv {
30 public:
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 
67 private:
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.
74 StringRef getInterfaceVarABIAttrName();
75 
76 /// Gets the InterfaceVarABIAttr given its fields.
77 InterfaceVarABIAttr
78 getInterfaceVarABIAttr(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.
87 StringRef getEntryPointABIAttrName();
88 
89 /// Gets the EntryPointABIAttr given its fields.
90 EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context,
91  ArrayRef<int32_t> workgroupSize = {},
92  std::optional<int> subgroupSize = {});
93 
94 /// Queries the entry point ABI on the nearest function-like op containing the
95 /// given `op`. Returns null attribute if not found.
96 EntryPointABIAttr lookupEntryPointABI(Operation *op);
97 
98 /// Queries the local workgroup size from entry point ABI on the nearest
99 /// function-like op containing the given `op`. Returns null attribute if not
100 /// found.
102 
103 /// Returns a default resource limits attribute that uses numbers from
104 /// "Table 46. Required Limits" of the Vulkan spec.
105 ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context);
106 
107 /// Returns the attribute name for specifying SPIR-V target environment.
108 StringRef getTargetEnvAttrName();
109 
110 /// Returns the default target environment: SPIR-V 1.0 with Shader capability
111 /// and no extra extensions.
112 TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
113 
114 /// Queries the target environment recursively from enclosing symbol table ops
115 /// containing the given `op`.
116 TargetEnvAttr lookupTargetEnv(Operation *op);
117 
118 /// Queries the target environment recursively from enclosing symbol table ops
119 /// containing the given `op` or returns the default target environment as
120 /// returned by getDefaultTargetEnv() if not provided.
121 TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
122 
123 /// Returns addressing model selected based on target environment.
124 AddressingModel getAddressingModel(TargetEnvAttr targetAttr,
125  bool use64bitAddress);
126 
127 /// Returns execution model selected based on target environment.
128 /// Returns failure if it cannot be selected.
129 FailureOr<ExecutionModel> getExecutionModel(TargetEnvAttr targetAttr);
130 
131 /// Returns memory model selected based on target environment.
132 /// Returns failure if it cannot be selected.
133 FailureOr<MemoryModel> getMemoryModel(TargetEnvAttr targetAttr);
134 
135 } // namespace spirv
136 } // namespace mlir
137 
138 #endif // MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
static MLIRContext * getContext(OpFoldResult val)
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
An attribute that specifies the target version, allowed extensions and capabilities,...
A wrapper class around a spirv::TargetEnvAttr to provide query methods for allowed version/capabiliti...
Definition: TargetAndABI.h:29
std::optional< Capability > allows(ArrayRef< Capability >) const
Returns the first allowed one if any of the given capabilities is allowed.
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.
bool allows(Capability) const
Returns true if the given capability is allowed.
TargetEnvAttr getAttr() const
Definition: TargetAndABI.h:62
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...
EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context, ArrayRef< int32_t > workgroupSize={}, std::optional< int > subgroupSize={})
Gets the EntryPointABIAttr given its fields.
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.
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