MLIR  15.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.
76 InterfaceVarABIAttr getInterfaceVarABIAttr(unsigned descriptorSet,
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 (spv.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(ArrayRef<int32_t> localSize,
90  MLIRContext *context);
91 
92 /// Queries the entry point ABI on the nearest function-like op containing the
93 /// given `op`. Returns null attribute if not found.
94 EntryPointABIAttr lookupEntryPointABI(Operation *op);
95 
96 /// Queries the local workgroup size from entry point ABI on the nearest
97 /// function-like op containing the given `op`. Returns null attribute if not
98 /// found.
100 
101 /// Returns a default resource limits attribute that uses numbers from
102 /// "Table 46. Required Limits" of the Vulkan spec.
103 ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context);
104 
105 /// Returns the attribute name for specifying SPIR-V target environment.
106 StringRef getTargetEnvAttrName();
107 
108 /// Returns the default target environment: SPIR-V 1.0 with Shader capability
109 /// and no extra extensions.
111 
112 /// Queries the target environment recursively from enclosing symbol table ops
113 /// containing the given `op`.
115 
116 /// Queries the target environment recursively from enclosing symbol table ops
117 /// containing the given `op` or returns the default target environment as
118 /// returned by getDefaultTargetEnv() if not provided.
120 
121 /// Returns addressing model selected based on target environment.
122 AddressingModel getAddressingModel(TargetEnvAttr targetAttr);
123 
124 /// Returns execution model selected based on target environment.
125 /// Returns failure if it cannot be selected.
127 
128 /// Returns memory model selected based on target environment.
129 /// Returns failure if it cannot be selected.
131 
132 } // namespace spirv
133 } // namespace mlir
134 
135 #endif // MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
DenseIntElementsAttr lookupLocalWorkGroupSize(Operation *op)
Queries the local workgroup size from entry point ABI on the nearest function-like op containing the ...
TargetEnvAttr lookupTargetEnv(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op...
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
FailureOr< MemoryModel > getMemoryModel(TargetEnvAttr targetAttr)
Returns memory model selected based on target environment.
An attribute that specifies the information regarding the interface variable: descriptor set...
StringRef getEntryPointABIAttrName()
Returns the attribute name for specifying entry point information.
TargetEnvAttr getAttr() const
Definition: TargetAndABI.h:61
bool needsInterfaceVarABIAttrs(TargetEnvAttr targetAttr)
Returns whether the given SPIR-V target (described by TargetEnvAttr) needs ABI attributes for interfa...
EntryPointABIAttr getEntryPointABIAttr(ArrayRef< int32_t > localSize, MLIRContext *context)
Gets the EntryPointABIAttr given its fields.
ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context)
Returns a default resource limits attribute that uses numbers from "Table 46. Required Limits" of the...
EntryPointABIAttr lookupEntryPointABI(Operation *op)
Queries the entry point ABI on the nearest function-like op containing the given op.
StringRef getTargetEnvAttrName()
Returns the attribute name for specifying SPIR-V target environment.
This class provides support for representing a failure result, or a valid value of type T...
Definition: LogicalResult.h:77
StringRef getInterfaceVarABIAttrName()
Returns the attribute name for specifying argument ABI information.
InterfaceVarABIAttr getInterfaceVarABIAttr(unsigned descriptorSet, unsigned binding, Optional< StorageClass > storageClass, MLIRContext *context)
Gets the InterfaceVarABIAttr given its fields.
AddressingModel getAddressingModel(TargetEnvAttr targetAttr)
Returns addressing model selected based on target environment.
A wrapper class around a spirv::TargetEnvAttr to provide query methods for allowed version/capabiliti...
Definition: TargetAndABI.h:28
FailureOr< ExecutionModel > getExecutionModel(TargetEnvAttr targetAttr)
Returns execution model selected based on target environment.
TargetEnvAttr getDefaultTargetEnv(MLIRContext *context)
Returns the default target environment: SPIR-V 1.0 with Shader capability and no extra extensions...
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
An attribute that specifies the target version, allowed extensions and capabilities, and resource limits.
An attribute that represents a reference to a dense integer vector or tensor object.