1 //===- SPIRVConversion.cpp - SPIR-V Conversion Utilities ------------------===//
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 implements utilities used to lower to SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
14 #include "mlir/Dialect/Func/IR/FuncOps.h"
15 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
16 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
17 #include "mlir/Transforms/DialectConversion.h"
18 #include "llvm/ADT/Sequence.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/Debug.h"
21 
22 #include <functional>
23 
24 #define DEBUG_TYPE "mlir-spirv-conversion"
25 
26 using namespace mlir;
27 
28 //===----------------------------------------------------------------------===//
29 // Utility functions
30 //===----------------------------------------------------------------------===//
31 
32 /// Checks that `candidates` extension requirements are possible to be satisfied
33 /// with the given `targetEnv`.
34 ///
35 ///  `candidates` is a vector of vector for extension requirements following
36 /// ((Extension::A OR Extension::B) AND (Extension::C OR Extension::D))
37 /// convention.
38 template <typename LabelT>
39 static LogicalResult checkExtensionRequirements(
40     LabelT label, const spirv::TargetEnv &targetEnv,
41     const spirv::SPIRVType::ExtensionArrayRefVector &candidates) {
42   for (const auto &ors : candidates) {
43     if (targetEnv.allows(ors))
44       continue;
45 
46     LLVM_DEBUG({
47       SmallVector<StringRef> extStrings;
48       for (spirv::Extension ext : ors)
49         extStrings.push_back(spirv::stringifyExtension(ext));
50 
51       llvm::dbgs() << label << " illegal: requires at least one extension in ["
52                    << llvm::join(extStrings, ", ")
53                    << "] but none allowed in target environment\n";
54     });
55     return failure();
56   }
57   return success();
58 }
59 
60 /// Checks that `candidates`capability requirements are possible to be satisfied
61 /// with the given `isAllowedFn`.
62 ///
63 ///  `candidates` is a vector of vector for capability requirements following
64 /// ((Capability::A OR Capability::B) AND (Capability::C OR Capability::D))
65 /// convention.
66 template <typename LabelT>
67 static LogicalResult checkCapabilityRequirements(
68     LabelT label, const spirv::TargetEnv &targetEnv,
69     const spirv::SPIRVType::CapabilityArrayRefVector &candidates) {
70   for (const auto &ors : candidates) {
71     if (targetEnv.allows(ors))
72       continue;
73 
74     LLVM_DEBUG({
75       SmallVector<StringRef> capStrings;
76       for (spirv::Capability cap : ors)
77         capStrings.push_back(spirv::stringifyCapability(cap));
78 
79       llvm::dbgs() << label << " illegal: requires at least one capability in ["
80                    << llvm::join(capStrings, ", ")
81                    << "] but none allowed in target environment\n";
82     });
83     return failure();
84   }
85   return success();
86 }
87 
88 /// Returns true if the given `storageClass` needs explicit layout when used in
89 /// Shader environments.
90 static bool needsExplicitLayout(spirv::StorageClass storageClass) {
91   switch (storageClass) {
92   case spirv::StorageClass::PhysicalStorageBuffer:
93   case spirv::StorageClass::PushConstant:
94   case spirv::StorageClass::StorageBuffer:
95   case spirv::StorageClass::Uniform:
96     return true;
97   default:
98     return false;
99   }
100 }
101 
102 /// Wraps the given `elementType` in a struct and gets the pointer to the
103 /// struct. This is used to satisfy Vulkan interface requirements.
104 static spirv::PointerType
105 wrapInStructAndGetPointer(Type elementType, spirv::StorageClass storageClass) {
106   auto structType = needsExplicitLayout(storageClass)
107                         ? spirv::StructType::get(elementType, /*offsetInfo=*/0)
108                         : spirv::StructType::get(elementType);
109   return spirv::PointerType::get(structType, storageClass);
110 }
111 
112 //===----------------------------------------------------------------------===//
113 // Type Conversion
114 //===----------------------------------------------------------------------===//
115 
116 Type SPIRVTypeConverter::getIndexType() const {
117   return IntegerType::get(getContext(), options.use64bitIndex ? 64 : 32);
118 }
119 
120 /// Mapping between SPIR-V storage classes to memref memory spaces.
121 ///
122 /// Note: memref does not have a defined semantics for each memory space; it
123 /// depends on the context where it is used. There are no particular reasons
124 /// behind the number assignments; we try to follow NVVM conventions and largely
125 /// give common storage classes a smaller number. The hope is use symbolic
126 /// memory space representation eventually after memref supports it.
127 // TODO: swap Generic and StorageBuffer assignment to be more akin
128 // to NVVM.
129 #define STORAGE_SPACE_MAP_LIST(MAP_FN)                                         \
130   MAP_FN(spirv::StorageClass::Generic, 1)                                      \
131   MAP_FN(spirv::StorageClass::StorageBuffer, 0)                                \
132   MAP_FN(spirv::StorageClass::Workgroup, 3)                                    \
133   MAP_FN(spirv::StorageClass::Uniform, 4)                                      \
134   MAP_FN(spirv::StorageClass::Private, 5)                                      \
135   MAP_FN(spirv::StorageClass::Function, 6)                                     \
136   MAP_FN(spirv::StorageClass::PushConstant, 7)                                 \
137   MAP_FN(spirv::StorageClass::UniformConstant, 8)                              \
138   MAP_FN(spirv::StorageClass::Input, 9)                                        \
139   MAP_FN(spirv::StorageClass::Output, 10)                                      \
140   MAP_FN(spirv::StorageClass::CrossWorkgroup, 11)                              \
141   MAP_FN(spirv::StorageClass::AtomicCounter, 12)                               \
142   MAP_FN(spirv::StorageClass::Image, 13)                                       \
143   MAP_FN(spirv::StorageClass::CallableDataKHR, 14)                             \
144   MAP_FN(spirv::StorageClass::IncomingCallableDataKHR, 15)                     \
145   MAP_FN(spirv::StorageClass::RayPayloadKHR, 16)                               \
146   MAP_FN(spirv::StorageClass::HitAttributeKHR, 17)                             \
147   MAP_FN(spirv::StorageClass::IncomingRayPayloadKHR, 18)                       \
148   MAP_FN(spirv::StorageClass::ShaderRecordBufferKHR, 19)                       \
149   MAP_FN(spirv::StorageClass::PhysicalStorageBuffer, 20)                       \
150   MAP_FN(spirv::StorageClass::CodeSectionINTEL, 21)                            \
151   MAP_FN(spirv::StorageClass::DeviceOnlyINTEL, 22)                             \
152   MAP_FN(spirv::StorageClass::HostOnlyINTEL, 23)
153 
154 unsigned
155 SPIRVTypeConverter::getMemorySpaceForStorageClass(spirv::StorageClass storage) {
156 #define STORAGE_SPACE_MAP_FN(storage, space)                                   \
157   case storage:                                                                \
158     return space;
159 
160   switch (storage) { STORAGE_SPACE_MAP_LIST(STORAGE_SPACE_MAP_FN) }
161 #undef STORAGE_SPACE_MAP_FN
162   llvm_unreachable("unhandled storage class!");
163 }
164 
165 Optional<spirv::StorageClass>
166 SPIRVTypeConverter::getStorageClassForMemorySpace(unsigned space) {
167 #define STORAGE_SPACE_MAP_FN(storage, space)                                   \
168   case space:                                                                  \
169     return storage;
170 
171   switch (space) {
172     STORAGE_SPACE_MAP_LIST(STORAGE_SPACE_MAP_FN)
173   default:
174     return llvm::None;
175   }
176 #undef STORAGE_SPACE_MAP_FN
177 }
178 
179 const SPIRVTypeConverter::Options &SPIRVTypeConverter::getOptions() const {
180   return options;
181 }
182 
183 MLIRContext *SPIRVTypeConverter::getContext() const {
184   return targetEnv.getAttr().getContext();
185 }
186 
187 #undef STORAGE_SPACE_MAP_LIST
188 
189 // TODO: This is a utility function that should probably be exposed by the
190 // SPIR-V dialect. Keeping it local till the use case arises.
191 static Optional<int64_t>
192 getTypeNumBytes(const SPIRVTypeConverter::Options &options, Type type) {
193   if (type.isa<spirv::ScalarType>()) {
194     auto bitWidth = type.getIntOrFloatBitWidth();
195     // According to the SPIR-V spec:
196     // "There is no physical size or bit pattern defined for values with boolean
197     // type. If they are stored (in conjunction with OpVariable), they can only
198     // be used with logical addressing operations, not physical, and only with
199     // non-externally visible shader Storage Classes: Workgroup, CrossWorkgroup,
200     // Private, Function, Input, and Output."
201     if (bitWidth == 1)
202       return llvm::None;
203     return bitWidth / 8;
204   }
205 
206   if (auto vecType = type.dyn_cast<VectorType>()) {
207     auto elementSize = getTypeNumBytes(options, vecType.getElementType());
208     if (!elementSize)
209       return llvm::None;
210     return vecType.getNumElements() * elementSize.getValue();
211   }
212 
213   if (auto memRefType = type.dyn_cast<MemRefType>()) {
214     // TODO: Layout should also be controlled by the ABI attributes. For now
215     // using the layout from MemRef.
216     int64_t offset;
217     SmallVector<int64_t, 4> strides;
218     if (!memRefType.hasStaticShape() ||
219         failed(getStridesAndOffset(memRefType, strides, offset)))
220       return llvm::None;
221 
222     // To get the size of the memref object in memory, the total size is the
223     // max(stride * dimension-size) computed for all dimensions times the size
224     // of the element.
225     auto elementSize = getTypeNumBytes(options, memRefType.getElementType());
226     if (!elementSize)
227       return llvm::None;
228 
229     if (memRefType.getRank() == 0)
230       return elementSize;
231 
232     auto dims = memRefType.getShape();
233     if (llvm::is_contained(dims, ShapedType::kDynamicSize) ||
234         offset == MemRefType::getDynamicStrideOrOffset() ||
235         llvm::is_contained(strides, MemRefType::getDynamicStrideOrOffset()))
236       return llvm::None;
237 
238     int64_t memrefSize = -1;
239     for (const auto &shape : enumerate(dims))
240       memrefSize = std::max(memrefSize, shape.value() * strides[shape.index()]);
241 
242     return (offset + memrefSize) * elementSize.getValue();
243   }
244 
245   if (auto tensorType = type.dyn_cast<TensorType>()) {
246     if (!tensorType.hasStaticShape())
247       return llvm::None;
248 
249     auto elementSize = getTypeNumBytes(options, tensorType.getElementType());
250     if (!elementSize)
251       return llvm::None;
252 
253     int64_t size = elementSize.getValue();
254     for (auto shape : tensorType.getShape())
255       size *= shape;
256 
257     return size;
258   }
259 
260   // TODO: Add size computation for other types.
261   return llvm::None;
262 }
263 
264 /// Converts a scalar `type` to a suitable type under the given `targetEnv`.
265 static Type convertScalarType(const spirv::TargetEnv &targetEnv,
266                               const SPIRVTypeConverter::Options &options,
267                               spirv::ScalarType type,
268                               Optional<spirv::StorageClass> storageClass = {}) {
269   // Get extension and capability requirements for the given type.
270   SmallVector<ArrayRef<spirv::Extension>, 1> extensions;
271   SmallVector<ArrayRef<spirv::Capability>, 2> capabilities;
272   type.getExtensions(extensions, storageClass);
273   type.getCapabilities(capabilities, storageClass);
274 
275   // If all requirements are met, then we can accept this type as-is.
276   if (succeeded(checkCapabilityRequirements(type, targetEnv, capabilities)) &&
277       succeeded(checkExtensionRequirements(type, targetEnv, extensions)))
278     return type;
279 
280   // Otherwise we need to adjust the type, which really means adjusting the
281   // bitwidth given this is a scalar type.
282 
283   if (!options.emulateNon32BitScalarTypes)
284     return nullptr;
285 
286   if (auto floatType = type.dyn_cast<FloatType>()) {
287     LLVM_DEBUG(llvm::dbgs() << type << " converted to 32-bit for SPIR-V\n");
288     return Builder(targetEnv.getContext()).getF32Type();
289   }
290 
291   auto intType = type.cast<IntegerType>();
292   LLVM_DEBUG(llvm::dbgs() << type << " converted to 32-bit for SPIR-V\n");
293   return IntegerType::get(targetEnv.getContext(), /*width=*/32,
294                           intType.getSignedness());
295 }
296 
297 /// Converts a vector `type` to a suitable type under the given `targetEnv`.
298 static Type convertVectorType(const spirv::TargetEnv &targetEnv,
299                               const SPIRVTypeConverter::Options &options,
300                               VectorType type,
301                               Optional<spirv::StorageClass> storageClass = {}) {
302   if (type.getRank() == 1 && type.getNumElements() == 1)
303     return type.getElementType();
304 
305   if (!spirv::CompositeType::isValid(type)) {
306     // TODO: Vector types with more than four elements can be translated into
307     // array types.
308     LLVM_DEBUG(llvm::dbgs() << type << " illegal: > 4-element unimplemented\n");
309     return nullptr;
310   }
311 
312   // Get extension and capability requirements for the given type.
313   SmallVector<ArrayRef<spirv::Extension>, 1> extensions;
314   SmallVector<ArrayRef<spirv::Capability>, 2> capabilities;
315   type.cast<spirv::CompositeType>().getExtensions(extensions, storageClass);
316   type.cast<spirv::CompositeType>().getCapabilities(capabilities, storageClass);
317 
318   // If all requirements are met, then we can accept this type as-is.
319   if (succeeded(checkCapabilityRequirements(type, targetEnv, capabilities)) &&
320       succeeded(checkExtensionRequirements(type, targetEnv, extensions)))
321     return type;
322 
323   auto elementType = convertScalarType(
324       targetEnv, options, type.getElementType().cast<spirv::ScalarType>(),
325       storageClass);
326   if (elementType)
327     return VectorType::get(type.getShape(), elementType);
328   return nullptr;
329 }
330 
331 /// Converts a tensor `type` to a suitable type under the given `targetEnv`.
332 ///
333 /// Note that this is mainly for lowering constant tensors. In SPIR-V one can
334 /// create composite constants with OpConstantComposite to embed relative large
335 /// constant values and use OpCompositeExtract and OpCompositeInsert to
336 /// manipulate, like what we do for vectors.
337 static Type convertTensorType(const spirv::TargetEnv &targetEnv,
338                               const SPIRVTypeConverter::Options &options,
339                               TensorType type) {
340   // TODO: Handle dynamic shapes.
341   if (!type.hasStaticShape()) {
342     LLVM_DEBUG(llvm::dbgs()
343                << type << " illegal: dynamic shape unimplemented\n");
344     return nullptr;
345   }
346 
347   auto scalarType = type.getElementType().dyn_cast<spirv::ScalarType>();
348   if (!scalarType) {
349     LLVM_DEBUG(llvm::dbgs()
350                << type << " illegal: cannot convert non-scalar element type\n");
351     return nullptr;
352   }
353 
354   Optional<int64_t> scalarSize = getTypeNumBytes(options, scalarType);
355   Optional<int64_t> tensorSize = getTypeNumBytes(options, type);
356   if (!scalarSize || !tensorSize) {
357     LLVM_DEBUG(llvm::dbgs()
358                << type << " illegal: cannot deduce element count\n");
359     return nullptr;
360   }
361 
362   auto arrayElemCount = *tensorSize / *scalarSize;
363   auto arrayElemType = convertScalarType(targetEnv, options, scalarType);
364   if (!arrayElemType)
365     return nullptr;
366   Optional<int64_t> arrayElemSize = getTypeNumBytes(options, arrayElemType);
367   if (!arrayElemSize) {
368     LLVM_DEBUG(llvm::dbgs()
369                << type << " illegal: cannot deduce converted element size\n");
370     return nullptr;
371   }
372 
373   return spirv::ArrayType::get(arrayElemType, arrayElemCount, *arrayElemSize);
374 }
375 
376 static Type convertBoolMemrefType(const spirv::TargetEnv &targetEnv,
377                                   const SPIRVTypeConverter::Options &options,
378                                   MemRefType type) {
379   Optional<spirv::StorageClass> storageClass =
380       SPIRVTypeConverter::getStorageClassForMemorySpace(
381           type.getMemorySpaceAsInt());
382   if (!storageClass) {
383     LLVM_DEBUG(llvm::dbgs()
384                << type << " illegal: cannot convert memory space\n");
385     return nullptr;
386   }
387 
388   unsigned numBoolBits = options.boolNumBits;
389   if (numBoolBits != 8) {
390     LLVM_DEBUG(llvm::dbgs()
391                << "using non-8-bit storage for bool types unimplemented");
392     return nullptr;
393   }
394   auto elementType = IntegerType::get(type.getContext(), numBoolBits)
395                          .dyn_cast<spirv::ScalarType>();
396   if (!elementType)
397     return nullptr;
398   Type arrayElemType =
399       convertScalarType(targetEnv, options, elementType, storageClass);
400   if (!arrayElemType)
401     return nullptr;
402   Optional<int64_t> arrayElemSize = getTypeNumBytes(options, arrayElemType);
403   if (!arrayElemSize) {
404     LLVM_DEBUG(llvm::dbgs()
405                << type << " illegal: cannot deduce converted element size\n");
406     return nullptr;
407   }
408 
409   if (!type.hasStaticShape()) {
410     auto arrayType =
411         spirv::RuntimeArrayType::get(arrayElemType, *arrayElemSize);
412     return wrapInStructAndGetPointer(arrayType, *storageClass);
413   }
414 
415   int64_t memrefSize = (type.getNumElements() * numBoolBits + 7) / 8;
416   auto arrayElemCount = (memrefSize + *arrayElemSize - 1) / *arrayElemSize;
417   auto arrayType =
418       spirv::ArrayType::get(arrayElemType, arrayElemCount, *arrayElemSize);
419 
420   return wrapInStructAndGetPointer(arrayType, *storageClass);
421 }
422 
423 static Type convertMemrefType(const spirv::TargetEnv &targetEnv,
424                               const SPIRVTypeConverter::Options &options,
425                               MemRefType type) {
426   if (type.getElementType().isa<IntegerType>() &&
427       type.getElementTypeBitWidth() == 1) {
428     return convertBoolMemrefType(targetEnv, options, type);
429   }
430 
431   Optional<spirv::StorageClass> storageClass =
432       SPIRVTypeConverter::getStorageClassForMemorySpace(
433           type.getMemorySpaceAsInt());
434   if (!storageClass) {
435     LLVM_DEBUG(llvm::dbgs()
436                << type << " illegal: cannot convert memory space\n");
437     return nullptr;
438   }
439 
440   Type arrayElemType;
441   Type elementType = type.getElementType();
442   if (auto vecType = elementType.dyn_cast<VectorType>()) {
443     arrayElemType =
444         convertVectorType(targetEnv, options, vecType, storageClass);
445   } else if (auto scalarType = elementType.dyn_cast<spirv::ScalarType>()) {
446     arrayElemType =
447         convertScalarType(targetEnv, options, scalarType, storageClass);
448   } else {
449     LLVM_DEBUG(
450         llvm::dbgs()
451         << type
452         << " unhandled: can only convert scalar or vector element type\n");
453     return nullptr;
454   }
455   if (!arrayElemType)
456     return nullptr;
457 
458   Optional<int64_t> elementSize = getTypeNumBytes(options, elementType);
459   if (!elementSize) {
460     LLVM_DEBUG(llvm::dbgs()
461                << type << " illegal: cannot deduce element size\n");
462     return nullptr;
463   }
464 
465   Optional<int64_t> arrayElemSize = getTypeNumBytes(options, arrayElemType);
466   if (!arrayElemSize) {
467     LLVM_DEBUG(llvm::dbgs()
468                << type << " illegal: cannot deduce converted element size\n");
469     return nullptr;
470   }
471 
472   if (!type.hasStaticShape()) {
473     auto arrayType =
474         spirv::RuntimeArrayType::get(arrayElemType, *arrayElemSize);
475     return wrapInStructAndGetPointer(arrayType, *storageClass);
476   }
477 
478   Optional<int64_t> memrefSize = getTypeNumBytes(options, type);
479   if (!memrefSize) {
480     LLVM_DEBUG(llvm::dbgs()
481                << type << " illegal: cannot deduce element count\n");
482     return nullptr;
483   }
484 
485   auto arrayElemCount = *memrefSize / *elementSize;
486 
487 
488   auto arrayType =
489       spirv::ArrayType::get(arrayElemType, arrayElemCount, *arrayElemSize);
490 
491   return wrapInStructAndGetPointer(arrayType, *storageClass);
492 }
493 
494 SPIRVTypeConverter::SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr,
495                                        Options options)
496     : targetEnv(targetAttr), options(options) {
497   // Add conversions. The order matters here: later ones will be tried earlier.
498 
499   // Allow all SPIR-V dialect specific types. This assumes all builtin types
500   // adopted in the SPIR-V dialect (i.e., IntegerType, FloatType, VectorType)
501   // were tried before.
502   //
503   // TODO: this assumes that the SPIR-V types are valid to use in
504   // the given target environment, which should be the case if the whole
505   // pipeline is driven by the same target environment. Still, we probably still
506   // want to validate and convert to be safe.
507   addConversion([](spirv::SPIRVType type) { return type; });
508 
509   addConversion([this](IndexType /*indexType*/) { return getIndexType(); });
510 
511   addConversion([this](IntegerType intType) -> Optional<Type> {
512     if (auto scalarType = intType.dyn_cast<spirv::ScalarType>())
513       return convertScalarType(this->targetEnv, this->options, scalarType);
514     return Type();
515   });
516 
517   addConversion([this](FloatType floatType) -> Optional<Type> {
518     if (auto scalarType = floatType.dyn_cast<spirv::ScalarType>())
519       return convertScalarType(this->targetEnv, this->options, scalarType);
520     return Type();
521   });
522 
523   addConversion([this](VectorType vectorType) {
524     return convertVectorType(this->targetEnv, this->options, vectorType);
525   });
526 
527   addConversion([this](TensorType tensorType) {
528     return convertTensorType(this->targetEnv, this->options, tensorType);
529   });
530 
531   addConversion([this](MemRefType memRefType) {
532     return convertMemrefType(this->targetEnv, this->options, memRefType);
533   });
534 }
535 
536 //===----------------------------------------------------------------------===//
537 // func::FuncOp Conversion Patterns
538 //===----------------------------------------------------------------------===//
539 
540 namespace {
541 /// A pattern for rewriting function signature to convert arguments of functions
542 /// to be of valid SPIR-V types.
543 class FuncOpConversion final : public OpConversionPattern<func::FuncOp> {
544 public:
545   using OpConversionPattern<func::FuncOp>::OpConversionPattern;
546 
547   LogicalResult
548   matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor,
549                   ConversionPatternRewriter &rewriter) const override;
550 };
551 } // namespace
552 
553 LogicalResult
554 FuncOpConversion::matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor,
555                                   ConversionPatternRewriter &rewriter) const {
556   auto fnType = funcOp.getFunctionType();
557   if (fnType.getNumResults() > 1)
558     return failure();
559 
560   TypeConverter::SignatureConversion signatureConverter(fnType.getNumInputs());
561   for (const auto &argType : enumerate(fnType.getInputs())) {
562     auto convertedType = getTypeConverter()->convertType(argType.value());
563     if (!convertedType)
564       return failure();
565     signatureConverter.addInputs(argType.index(), convertedType);
566   }
567 
568   Type resultType;
569   if (fnType.getNumResults() == 1) {
570     resultType = getTypeConverter()->convertType(fnType.getResult(0));
571     if (!resultType)
572       return failure();
573   }
574 
575   // Create the converted spv.func op.
576   auto newFuncOp = rewriter.create<spirv::FuncOp>(
577       funcOp.getLoc(), funcOp.getName(),
578       rewriter.getFunctionType(signatureConverter.getConvertedTypes(),
579                                resultType ? TypeRange(resultType)
580                                           : TypeRange()));
581 
582   // Copy over all attributes other than the function name and type.
583   for (const auto &namedAttr : funcOp->getAttrs()) {
584     if (namedAttr.getName() != FunctionOpInterface::getTypeAttrName() &&
585         namedAttr.getName() != SymbolTable::getSymbolAttrName())
586       newFuncOp->setAttr(namedAttr.getName(), namedAttr.getValue());
587   }
588 
589   rewriter.inlineRegionBefore(funcOp.getBody(), newFuncOp.getBody(),
590                               newFuncOp.end());
591   if (failed(rewriter.convertRegionTypes(
592           &newFuncOp.getBody(), *getTypeConverter(), &signatureConverter)))
593     return failure();
594   rewriter.eraseOp(funcOp);
595   return success();
596 }
597 
598 void mlir::populateBuiltinFuncToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
599                                               RewritePatternSet &patterns) {
600   patterns.add<FuncOpConversion>(typeConverter, patterns.getContext());
601 }
602 
603 //===----------------------------------------------------------------------===//
604 // Builtin Variables
605 //===----------------------------------------------------------------------===//
606 
607 static spirv::GlobalVariableOp getBuiltinVariable(Block &body,
608                                                   spirv::BuiltIn builtin) {
609   // Look through all global variables in the given `body` block and check if
610   // there is a spv.GlobalVariable that has the same `builtin` attribute.
611   for (auto varOp : body.getOps<spirv::GlobalVariableOp>()) {
612     if (auto builtinAttr = varOp->getAttrOfType<StringAttr>(
613             spirv::SPIRVDialect::getAttributeName(
614                 spirv::Decoration::BuiltIn))) {
615       auto varBuiltIn = spirv::symbolizeBuiltIn(builtinAttr.getValue());
616       if (varBuiltIn && varBuiltIn.getValue() == builtin) {
617         return varOp;
618       }
619     }
620   }
621   return nullptr;
622 }
623 
624 /// Gets name of global variable for a builtin.
625 static std::string getBuiltinVarName(spirv::BuiltIn builtin) {
626   return std::string("__builtin_var_") + stringifyBuiltIn(builtin).str() + "__";
627 }
628 
629 /// Gets or inserts a global variable for a builtin within `body` block.
630 static spirv::GlobalVariableOp
631 getOrInsertBuiltinVariable(Block &body, Location loc, spirv::BuiltIn builtin,
632                            Type integerType, OpBuilder &builder) {
633   if (auto varOp = getBuiltinVariable(body, builtin))
634     return varOp;
635 
636   OpBuilder::InsertionGuard guard(builder);
637   builder.setInsertionPointToStart(&body);
638 
639   spirv::GlobalVariableOp newVarOp;
640   switch (builtin) {
641   case spirv::BuiltIn::NumWorkgroups:
642   case spirv::BuiltIn::WorkgroupSize:
643   case spirv::BuiltIn::WorkgroupId:
644   case spirv::BuiltIn::LocalInvocationId:
645   case spirv::BuiltIn::GlobalInvocationId: {
646     auto ptrType = spirv::PointerType::get(VectorType::get({3}, integerType),
647                                            spirv::StorageClass::Input);
648     std::string name = getBuiltinVarName(builtin);
649     newVarOp =
650         builder.create<spirv::GlobalVariableOp>(loc, ptrType, name, builtin);
651     break;
652   }
653   case spirv::BuiltIn::SubgroupId:
654   case spirv::BuiltIn::NumSubgroups:
655   case spirv::BuiltIn::SubgroupSize: {
656     auto ptrType =
657         spirv::PointerType::get(integerType, spirv::StorageClass::Input);
658     std::string name = getBuiltinVarName(builtin);
659     newVarOp =
660         builder.create<spirv::GlobalVariableOp>(loc, ptrType, name, builtin);
661     break;
662   }
663   default:
664     emitError(loc, "unimplemented builtin variable generation for ")
665         << stringifyBuiltIn(builtin);
666   }
667   return newVarOp;
668 }
669 
670 Value mlir::spirv::getBuiltinVariableValue(Operation *op,
671                                            spirv::BuiltIn builtin,
672                                            Type integerType,
673                                            OpBuilder &builder) {
674   Operation *parent = SymbolTable::getNearestSymbolTable(op->getParentOp());
675   if (!parent) {
676     op->emitError("expected operation to be within a module-like op");
677     return nullptr;
678   }
679 
680   spirv::GlobalVariableOp varOp =
681       getOrInsertBuiltinVariable(*parent->getRegion(0).begin(), op->getLoc(),
682                                  builtin, integerType, builder);
683   Value ptr = builder.create<spirv::AddressOfOp>(op->getLoc(), varOp);
684   return builder.create<spirv::LoadOp>(op->getLoc(), ptr);
685 }
686 
687 //===----------------------------------------------------------------------===//
688 // Push constant storage
689 //===----------------------------------------------------------------------===//
690 
691 /// Returns the pointer type for the push constant storage containing
692 /// `elementCount` 32-bit integer values.
693 static spirv::PointerType getPushConstantStorageType(unsigned elementCount,
694                                                      Builder &builder,
695                                                      Type indexType) {
696   auto arrayType = spirv::ArrayType::get(indexType, elementCount,
697                                          /*stride=*/4);
698   auto structType = spirv::StructType::get({arrayType}, /*offsetInfo=*/0);
699   return spirv::PointerType::get(structType, spirv::StorageClass::PushConstant);
700 }
701 
702 /// Returns the push constant varible containing `elementCount` 32-bit integer
703 /// values in `body`. Returns null op if such an op does not exit.
704 static spirv::GlobalVariableOp getPushConstantVariable(Block &body,
705                                                        unsigned elementCount) {
706   for (auto varOp : body.getOps<spirv::GlobalVariableOp>()) {
707     auto ptrType = varOp.type().dyn_cast<spirv::PointerType>();
708     if (!ptrType)
709       continue;
710 
711     // Note that Vulkan requires "There must be no more than one push constant
712     // block statically used per shader entry point." So we should always reuse
713     // the existing one.
714     if (ptrType.getStorageClass() == spirv::StorageClass::PushConstant) {
715       auto numElements = ptrType.getPointeeType()
716                              .cast<spirv::StructType>()
717                              .getElementType(0)
718                              .cast<spirv::ArrayType>()
719                              .getNumElements();
720       if (numElements == elementCount)
721         return varOp;
722     }
723   }
724   return nullptr;
725 }
726 
727 /// Gets or inserts a global variable for push constant storage containing
728 /// `elementCount` 32-bit integer values in `block`.
729 static spirv::GlobalVariableOp
730 getOrInsertPushConstantVariable(Location loc, Block &block,
731                                 unsigned elementCount, OpBuilder &b,
732                                 Type indexType) {
733   if (auto varOp = getPushConstantVariable(block, elementCount))
734     return varOp;
735 
736   auto builder = OpBuilder::atBlockBegin(&block, b.getListener());
737   auto type = getPushConstantStorageType(elementCount, builder, indexType);
738   const char *name = "__push_constant_var__";
739   return builder.create<spirv::GlobalVariableOp>(loc, type, name,
740                                                  /*initializer=*/nullptr);
741 }
742 
743 Value spirv::getPushConstantValue(Operation *op, unsigned elementCount,
744                                   unsigned offset, Type integerType,
745                                   OpBuilder &builder) {
746   Location loc = op->getLoc();
747   Operation *parent = SymbolTable::getNearestSymbolTable(op->getParentOp());
748   if (!parent) {
749     op->emitError("expected operation to be within a module-like op");
750     return nullptr;
751   }
752 
753   spirv::GlobalVariableOp varOp = getOrInsertPushConstantVariable(
754       loc, parent->getRegion(0).front(), elementCount, builder, integerType);
755 
756   Value zeroOp = spirv::ConstantOp::getZero(integerType, loc, builder);
757   Value offsetOp = builder.create<spirv::ConstantOp>(
758       loc, integerType, builder.getI32IntegerAttr(offset));
759   auto addrOp = builder.create<spirv::AddressOfOp>(loc, varOp);
760   auto acOp = builder.create<spirv::AccessChainOp>(
761       loc, addrOp, llvm::makeArrayRef({zeroOp, offsetOp}));
762   return builder.create<spirv::LoadOp>(loc, acOp);
763 }
764 
765 //===----------------------------------------------------------------------===//
766 // Index calculation
767 //===----------------------------------------------------------------------===//
768 
769 Value mlir::spirv::linearizeIndex(ValueRange indices, ArrayRef<int64_t> strides,
770                                   int64_t offset, Type integerType,
771                                   Location loc, OpBuilder &builder) {
772   assert(indices.size() == strides.size() &&
773          "must provide indices for all dimensions");
774 
775   // TODO: Consider moving to use affine.apply and patterns converting
776   // affine.apply to standard ops. This needs converting to SPIR-V passes to be
777   // broken down into progressive small steps so we can have intermediate steps
778   // using other dialects. At the moment SPIR-V is the final sink.
779 
780   Value linearizedIndex = builder.create<spirv::ConstantOp>(
781       loc, integerType, IntegerAttr::get(integerType, offset));
782   for (const auto &index : llvm::enumerate(indices)) {
783     Value strideVal = builder.create<spirv::ConstantOp>(
784         loc, integerType,
785         IntegerAttr::get(integerType, strides[index.index()]));
786     Value update = builder.create<spirv::IMulOp>(loc, strideVal, index.value());
787     linearizedIndex =
788         builder.create<spirv::IAddOp>(loc, linearizedIndex, update);
789   }
790   return linearizedIndex;
791 }
792 
793 spirv::AccessChainOp mlir::spirv::getElementPtr(
794     SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr,
795     ValueRange indices, Location loc, OpBuilder &builder) {
796   // Get base and offset of the MemRefType and verify they are static.
797 
798   int64_t offset;
799   SmallVector<int64_t, 4> strides;
800   if (failed(getStridesAndOffset(baseType, strides, offset)) ||
801       llvm::is_contained(strides, MemRefType::getDynamicStrideOrOffset()) ||
802       offset == MemRefType::getDynamicStrideOrOffset()) {
803     return nullptr;
804   }
805 
806   auto indexType = typeConverter.getIndexType();
807 
808   SmallVector<Value, 2> linearizedIndices;
809   auto zero = spirv::ConstantOp::getZero(indexType, loc, builder);
810 
811   // Add a '0' at the start to index into the struct.
812   linearizedIndices.push_back(zero);
813 
814   if (baseType.getRank() == 0) {
815     linearizedIndices.push_back(zero);
816   } else {
817     linearizedIndices.push_back(
818         linearizeIndex(indices, strides, offset, indexType, loc, builder));
819   }
820   return builder.create<spirv::AccessChainOp>(loc, basePtr, linearizedIndices);
821 }
822 
823 //===----------------------------------------------------------------------===//
824 // SPIR-V ConversionTarget
825 //===----------------------------------------------------------------------===//
826 
827 std::unique_ptr<SPIRVConversionTarget>
828 SPIRVConversionTarget::get(spirv::TargetEnvAttr targetAttr) {
829   std::unique_ptr<SPIRVConversionTarget> target(
830       // std::make_unique does not work here because the constructor is private.
831       new SPIRVConversionTarget(targetAttr));
832   SPIRVConversionTarget *targetPtr = target.get();
833   target->addDynamicallyLegalDialect<spirv::SPIRVDialect>(
834       // We need to capture the raw pointer here because it is stable:
835       // target will be destroyed once this function is returned.
836       [targetPtr](Operation *op) { return targetPtr->isLegalOp(op); });
837   return target;
838 }
839 
840 SPIRVConversionTarget::SPIRVConversionTarget(spirv::TargetEnvAttr targetAttr)
841     : ConversionTarget(*targetAttr.getContext()), targetEnv(targetAttr) {}
842 
843 bool SPIRVConversionTarget::isLegalOp(Operation *op) {
844   // Make sure this op is available at the given version. Ops not implementing
845   // QueryMinVersionInterface/QueryMaxVersionInterface are available to all
846   // SPIR-V versions.
847   if (auto minVersionIfx = dyn_cast<spirv::QueryMinVersionInterface>(op)) {
848     Optional<spirv::Version> minVersion = minVersionIfx.getMinVersion();
849     if (minVersion && *minVersion > this->targetEnv.getVersion()) {
850       LLVM_DEBUG(llvm::dbgs()
851                  << op->getName() << " illegal: requiring min version "
852                  << spirv::stringifyVersion(*minVersion) << "\n");
853       return false;
854     }
855   }
856   if (auto maxVersionIfx = dyn_cast<spirv::QueryMaxVersionInterface>(op)) {
857     Optional<spirv::Version> maxVersion = maxVersionIfx.getMaxVersion();
858     if (maxVersion && *maxVersion < this->targetEnv.getVersion()) {
859       LLVM_DEBUG(llvm::dbgs()
860                  << op->getName() << " illegal: requiring max version "
861                  << spirv::stringifyVersion(*maxVersion) << "\n");
862       return false;
863     }
864   }
865 
866   // Make sure this op's required extensions are allowed to use. Ops not
867   // implementing QueryExtensionInterface do not require extensions to be
868   // available.
869   if (auto extensions = dyn_cast<spirv::QueryExtensionInterface>(op))
870     if (failed(checkExtensionRequirements(op->getName(), this->targetEnv,
871                                           extensions.getExtensions())))
872       return false;
873 
874   // Make sure this op's required extensions are allowed to use. Ops not
875   // implementing QueryCapabilityInterface do not require capabilities to be
876   // available.
877   if (auto capabilities = dyn_cast<spirv::QueryCapabilityInterface>(op))
878     if (failed(checkCapabilityRequirements(op->getName(), this->targetEnv,
879                                            capabilities.getCapabilities())))
880       return false;
881 
882   SmallVector<Type, 4> valueTypes;
883   valueTypes.append(op->operand_type_begin(), op->operand_type_end());
884   valueTypes.append(op->result_type_begin(), op->result_type_end());
885 
886   // Ensure that all types have been converted to SPIRV types.
887   if (llvm::any_of(valueTypes,
888                    [](Type t) { return !t.isa<spirv::SPIRVType>(); }))
889     return false;
890 
891   // Special treatment for global variables, whose type requirements are
892   // conveyed by type attributes.
893   if (auto globalVar = dyn_cast<spirv::GlobalVariableOp>(op))
894     valueTypes.push_back(globalVar.type());
895 
896   // Make sure the op's operands/results use types that are allowed by the
897   // target environment.
898   SmallVector<ArrayRef<spirv::Extension>, 4> typeExtensions;
899   SmallVector<ArrayRef<spirv::Capability>, 8> typeCapabilities;
900   for (Type valueType : valueTypes) {
901     typeExtensions.clear();
902     valueType.cast<spirv::SPIRVType>().getExtensions(typeExtensions);
903     if (failed(checkExtensionRequirements(op->getName(), this->targetEnv,
904                                           typeExtensions)))
905       return false;
906 
907     typeCapabilities.clear();
908     valueType.cast<spirv::SPIRVType>().getCapabilities(typeCapabilities);
909     if (failed(checkCapabilityRequirements(op->getName(), this->targetEnv,
910                                            typeCapabilities)))
911       return false;
912   }
913 
914   return true;
915 }
916