• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TARGETANDABI_H
14 #define MLIR_DIALECT_SPIRV_TARGETANDABI_H
15 
16 #include "mlir/Dialect/SPIRV/SPIRVAttributes.h"
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.
38   Optional<Capability> allows(ArrayRef<Capability>) const;
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.
44   Optional<Extension> allows(ArrayRef<Extension>) const;
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 
getAttr()61   TargetEnvAttr getAttr() const { return targetAttr; }
62 
63   /// Allows implicity converting to the underlying spirv::TargetEnvAttr.
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).
83 bool needsInterfaceVarABIAttrs(TargetEnvAttr targetAttr);
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.
99 DenseIntElementsAttr lookupLocalWorkGroupSize(Operation *op);
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.
110 TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
111 
112 /// Queries the target environment recursively from enclosing symbol table ops
113 /// containing the given `op`.
114 TargetEnvAttr lookupTargetEnv(Operation *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.
119 TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
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.
126 FailureOr<ExecutionModel> getExecutionModel(TargetEnvAttr targetAttr);
127 
128 /// Returns memory model selected based on target environment.
129 /// Returns failure if it cannot be selected.
130 FailureOr<MemoryModel> getMemoryModel(TargetEnvAttr targetAttr);
131 
132 } // namespace spirv
133 } // namespace mlir
134 
135 #endif // MLIR_DIALECT_SPIRV_TARGETANDABI_H
136