MLIR  16.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 
20 namespace mlir {
21 class Operation;
22 
23 namespace spirv {
24 enum class StorageClass : uint32_t;
25 
26 /// A wrapper class around a spirv::TargetEnvAttr to provide query methods for
27 /// allowed version/capabilities/extensions.
28 class TargetEnv {
29 public:
30  explicit TargetEnv(TargetEnvAttr targetAttr);
31 
32  Version getVersion() const;
33 
34  /// Returns true if the given capability is allowed.
35  bool allows(Capability) const;
36  /// Returns the first allowed one if any of the given capabilities is allowed.
37  /// Returns llvm::None otherwise.
39 
40  /// Returns true if the given extension is allowed.
41  bool allows(Extension) const;
42  /// Returns the first allowed one if any of the given extensions is allowed.
43  /// Returns llvm::None otherwise.
45 
46  /// Returns the vendor ID.
47  Vendor getVendorID() const;
48 
49  /// Returns the device type.
50  DeviceType getDeviceType() const;
51 
52  /// Returns the device ID.
53  uint32_t getDeviceID() const;
54 
55  /// Returns the MLIRContext.
56  MLIRContext *getContext() const;
57 
58  /// Returns the target resource limits.
59  ResourceLimitsAttr getResourceLimits() const;
60 
61  TargetEnvAttr getAttr() const { return targetAttr; }
62 
63  /// Allows implicity converting to the underlying spirv::TargetEnvAttr.
64  operator TargetEnvAttr() const { return targetAttr; }
65 
66 private:
67  TargetEnvAttr targetAttr;
68  llvm::SmallSet<Extension, 4> givenExtensions; /// Allowed extensions
69  llvm::SmallSet<Capability, 8> givenCapabilities; /// Allowed capabilities
70 };
71 
72 /// Returns the attribute name for specifying argument ABI information.
73 StringRef getInterfaceVarABIAttrName();
74 
75 /// Gets the InterfaceVarABIAttr given its fields.
77  unsigned binding,
78  Optional<StorageClass> storageClass,
79  MLIRContext *context);
80 
81 /// Returns whether the given SPIR-V target (described by TargetEnvAttr) needs
82 /// ABI attributes for interface variables (spirv.interface_var_abi).
84 
85 /// Returns the attribute name for specifying entry point information.
86 StringRef getEntryPointABIAttrName();
87 
88 /// Gets the EntryPointABIAttr given its fields.
89 EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context,
90  ArrayRef<int32_t> workgroupSize = {},
91  llvm::Optional<int> subgroupSize = {});
92 
93 /// Queries the entry point ABI on the nearest function-like op containing the
94 /// given `op`. Returns null attribute if not found.
95 EntryPointABIAttr lookupEntryPointABI(Operation *op);
96 
97 /// Queries the local workgroup size from entry point ABI on the nearest
98 /// function-like op containing the given `op`. Returns null attribute if not
99 /// found.
101 
102 /// Returns a default resource limits attribute that uses numbers from
103 /// "Table 46. Required Limits" of the Vulkan spec.
104 ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context);
105 
106 /// Returns the attribute name for specifying SPIR-V target environment.
107 StringRef getTargetEnvAttrName();
108 
109 /// Returns the default target environment: SPIR-V 1.0 with Shader capability
110 /// and no extra extensions.
111 TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
112 
113 /// Queries the target environment recursively from enclosing symbol table ops
114 /// containing the given `op`.
115 TargetEnvAttr lookupTargetEnv(Operation *op);
116 
117 /// Queries the target environment recursively from enclosing symbol table ops
118 /// containing the given `op` or returns the default target environment as
119 /// returned by getDefaultTargetEnv() if not provided.
120 TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
121 
122 /// Returns addressing model selected based on target environment.
123 AddressingModel getAddressingModel(TargetEnvAttr targetAttr);
124 
125 /// Returns execution model selected based on target environment.
126 /// Returns failure if it cannot be selected.
127 FailureOr<ExecutionModel> getExecutionModel(TargetEnvAttr targetAttr);
128 
129 /// Returns memory model selected based on target environment.
130 /// Returns failure if it cannot be selected.
131 FailureOr<MemoryModel> getMemoryModel(TargetEnvAttr targetAttr);
132 
133 } // namespace spirv
134 } // namespace mlir
135 
136 #endif // MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:56
An attribute that specifies the information regarding the interface variable: descriptor set,...
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:28
bool allows(Extension) const
Returns true if the given extension is allowed.
bool allows(Capability) const
Returns true if the given capability is allowed.
TargetEnvAttr getAttr() const
Definition: TargetAndABI.h:61
Optional< Extension > allows(ArrayRef< Extension >) const
Returns the first allowed one if any of the given extensions is allowed.
Optional< Capability > allows(ArrayRef< Capability >) const
Returns the first allowed one if any of the given capabilities is allowed.
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...
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 ...
EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context, ArrayRef< int32_t > workgroupSize={}, llvm::Optional< int > subgroupSize={})
Gets the EntryPointABIAttr given its fields.
FailureOr< ExecutionModel > getExecutionModel(TargetEnvAttr targetAttr)
Returns execution model selected based on target environment.
AddressingModel getAddressingModel(TargetEnvAttr targetAttr)
Returns addressing 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...
InterfaceVarABIAttr getInterfaceVarABIAttr(unsigned descriptorSet, unsigned binding, Optional< StorageClass > storageClass, MLIRContext *context)
Gets the InterfaceVarABIAttr given its fields.
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