| 1 | //===- TestAvailability.cpp - Pass to test SPIR-V op availability ---------===// |
| 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 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 10 | #include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h" |
| 11 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
| 12 | #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" |
| 13 | #include "mlir/Pass/Pass.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | // Printing op availability pass |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | namespace { |
| 22 | /// A pass for testing SPIR-V op availability. |
| 23 | struct PrintOpAvailability |
| 24 | : public PassWrapper<PrintOpAvailability, OperationPass<mlir::ModuleOp>> { |
| 25 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(PrintOpAvailability) |
| 26 | |
| 27 | void runOnOperation() override; |
| 28 | StringRef getArgument() const final { return "test-spirv-op-availability" ; } |
| 29 | StringRef getDescription() const final { |
| 30 | return "Test SPIR-V op availability" ; |
| 31 | } |
| 32 | }; |
| 33 | } // namespace |
| 34 | |
| 35 | void PrintOpAvailability::runOnOperation() { |
| 36 | mlir::ModuleOp moduleOp = getOperation(); |
| 37 | Dialect *spirvDialect = getContext().getLoadedDialect(name: "spirv" ); |
| 38 | |
| 39 | auto opCallback = [&](Operation *op) { |
| 40 | if (op->getDialect() != spirvDialect) |
| 41 | return WalkResult::advance(); |
| 42 | |
| 43 | auto opName = op->getName(); |
| 44 | auto &os = llvm::outs(); |
| 45 | |
| 46 | if (auto minVersionIfx = dyn_cast<spirv::QueryMinVersionInterface>(Val: op)) { |
| 47 | std::optional<spirv::Version> minVersion = minVersionIfx.getMinVersion(); |
| 48 | os << opName << " min version: " ; |
| 49 | if (minVersion) |
| 50 | os << spirv::stringifyVersion(*minVersion) << "\n" ; |
| 51 | else |
| 52 | os << "None\n" ; |
| 53 | } |
| 54 | |
| 55 | if (auto maxVersionIfx = dyn_cast<spirv::QueryMaxVersionInterface>(Val: op)) { |
| 56 | std::optional<spirv::Version> maxVersion = maxVersionIfx.getMaxVersion(); |
| 57 | os << opName << " max version: " ; |
| 58 | if (maxVersion) |
| 59 | os << spirv::stringifyVersion(*maxVersion) << "\n" ; |
| 60 | else |
| 61 | os << "None\n" ; |
| 62 | } |
| 63 | |
| 64 | if (auto extension = dyn_cast<spirv::QueryExtensionInterface>(Val: op)) { |
| 65 | os << opName << " extensions: [" ; |
| 66 | for (const auto &exts : extension.getExtensions()) { |
| 67 | os << " [" ; |
| 68 | llvm::interleaveComma(c: exts, os, each_fn: [&](spirv::Extension ext) { |
| 69 | os << spirv::stringifyExtension(ext); |
| 70 | }); |
| 71 | os << "]" ; |
| 72 | } |
| 73 | os << " ]\n" ; |
| 74 | } |
| 75 | |
| 76 | if (auto capability = dyn_cast<spirv::QueryCapabilityInterface>(Val: op)) { |
| 77 | os << opName << " capabilities: [" ; |
| 78 | for (const auto &caps : capability.getCapabilities()) { |
| 79 | os << " [" ; |
| 80 | llvm::interleaveComma(c: caps, os, each_fn: [&](spirv::Capability cap) { |
| 81 | os << spirv::stringifyCapability(cap); |
| 82 | }); |
| 83 | os << "]" ; |
| 84 | } |
| 85 | os << " ]\n" ; |
| 86 | } |
| 87 | os.flush(); |
| 88 | |
| 89 | return WalkResult::advance(); |
| 90 | }; |
| 91 | |
| 92 | moduleOp.walk(callback: [&](func::FuncOp f) { |
| 93 | llvm::outs() << f.getName() << "\n" ; |
| 94 | f->walk(callback&: opCallback); |
| 95 | }); |
| 96 | |
| 97 | moduleOp.walk(callback: [&](spirv::GraphARMOp g) { |
| 98 | llvm::outs() << g.getName() << "\n" ; |
| 99 | g->walk(callback&: opCallback); |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | namespace mlir { |
| 104 | void registerPrintSpirvAvailabilityPass() { |
| 105 | PassRegistration<PrintOpAvailability>(); |
| 106 | } |
| 107 | } // namespace mlir |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // Converting target environment pass |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | namespace { |
| 114 | /// A pass for testing SPIR-V op availability. |
| 115 | struct ConvertToTargetEnv |
| 116 | : public PassWrapper<ConvertToTargetEnv, OperationPass<func::FuncOp>> { |
| 117 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ConvertToTargetEnv) |
| 118 | |
| 119 | StringRef getArgument() const override { return "test-spirv-target-env" ; } |
| 120 | StringRef getDescription() const override { |
| 121 | return "Test SPIR-V target environment" ; |
| 122 | } |
| 123 | void runOnOperation() override; |
| 124 | }; |
| 125 | |
| 126 | struct ConvertToAtomCmpExchangeWeak : RewritePattern { |
| 127 | ConvertToAtomCmpExchangeWeak(MLIRContext *context) |
| 128 | : RewritePattern("test.convert_to_atomic_compare_exchange_weak_op" , 1, |
| 129 | context, {"spirv.AtomicCompareExchangeWeak" }) {} |
| 130 | |
| 131 | LogicalResult matchAndRewrite(Operation *op, |
| 132 | PatternRewriter &rewriter) const override { |
| 133 | Value ptr = op->getOperand(idx: 0); |
| 134 | Value value = op->getOperand(idx: 1); |
| 135 | Value comparator = op->getOperand(idx: 2); |
| 136 | |
| 137 | // Create a spirv.AtomicCompareExchangeWeak op with AtomicCounterMemory bits |
| 138 | // in memory semantics to additionally require AtomicStorage capability. |
| 139 | rewriter.replaceOpWithNewOp<spirv::AtomicCompareExchangeWeakOp>( |
| 140 | op, args: value.getType(), args&: ptr, args: spirv::Scope::Workgroup, |
| 141 | args: spirv::MemorySemantics::AcquireRelease | |
| 142 | spirv::MemorySemantics::AtomicCounterMemory, |
| 143 | args: spirv::MemorySemantics::Acquire, args&: value, args&: comparator); |
| 144 | return success(); |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | struct ConvertToBitReverse : RewritePattern { |
| 149 | ConvertToBitReverse(MLIRContext *context) |
| 150 | : RewritePattern("test.convert_to_bit_reverse_op" , 1, context, |
| 151 | {"spirv.BitReverse" }) {} |
| 152 | |
| 153 | LogicalResult matchAndRewrite(Operation *op, |
| 154 | PatternRewriter &rewriter) const override { |
| 155 | Value predicate = op->getOperand(idx: 0); |
| 156 | rewriter.replaceOpWithNewOp<spirv::BitReverseOp>( |
| 157 | op, args: op->getResult(idx: 0).getType(), args&: predicate); |
| 158 | return success(); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | struct ConvertToGroupNonUniformBallot : RewritePattern { |
| 163 | ConvertToGroupNonUniformBallot(MLIRContext *context) |
| 164 | : RewritePattern("test.convert_to_group_non_uniform_ballot_op" , 1, |
| 165 | context, {"spirv.GroupNonUniformBallot" }) {} |
| 166 | |
| 167 | LogicalResult matchAndRewrite(Operation *op, |
| 168 | PatternRewriter &rewriter) const override { |
| 169 | Value predicate = op->getOperand(idx: 0); |
| 170 | rewriter.replaceOpWithNewOp<spirv::GroupNonUniformBallotOp>( |
| 171 | op, args: op->getResult(idx: 0).getType(), args: spirv::Scope::Subgroup, args&: predicate); |
| 172 | return success(); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | struct ConvertToModule : RewritePattern { |
| 177 | ConvertToModule(MLIRContext *context) |
| 178 | : RewritePattern("test.convert_to_module_op" , 1, context, |
| 179 | {"spirv.module" }) {} |
| 180 | |
| 181 | LogicalResult matchAndRewrite(Operation *op, |
| 182 | PatternRewriter &rewriter) const override { |
| 183 | rewriter.replaceOpWithNewOp<spirv::ModuleOp>( |
| 184 | op, args: spirv::AddressingModel::PhysicalStorageBuffer64, |
| 185 | args: spirv::MemoryModel::Vulkan); |
| 186 | return success(); |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | struct ConvertToSubgroupBallot : RewritePattern { |
| 191 | ConvertToSubgroupBallot(MLIRContext *context) |
| 192 | : RewritePattern("test.convert_to_subgroup_ballot_op" , 1, context, |
| 193 | {"spirv.KHR.SubgroupBallot" }) {} |
| 194 | |
| 195 | LogicalResult matchAndRewrite(Operation *op, |
| 196 | PatternRewriter &rewriter) const override { |
| 197 | Value predicate = op->getOperand(idx: 0); |
| 198 | rewriter.replaceOpWithNewOp<spirv::KHRSubgroupBallotOp>( |
| 199 | op, args: op->getResult(idx: 0).getType(), args&: predicate); |
| 200 | return success(); |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | template <const char *TestOpName, typename SPIRVOp> |
| 205 | struct ConvertToIntegerDotProd : RewritePattern { |
| 206 | ConvertToIntegerDotProd(MLIRContext *context) |
| 207 | : RewritePattern(TestOpName, 1, context, {SPIRVOp::getOperationName()}) {} |
| 208 | |
| 209 | LogicalResult matchAndRewrite(Operation *op, |
| 210 | PatternRewriter &rewriter) const override { |
| 211 | typename SPIRVOp::Properties properties{}; |
| 212 | SPIRVOp::populateDefaultProperties( |
| 213 | OperationName(SPIRVOp::getOperationName(), rewriter.getContext()), |
| 214 | properties); |
| 215 | if (failed(SPIRVOp::setPropertiesFromAttr( |
| 216 | properties, op->getDiscardableAttrDictionary(), |
| 217 | [&]() { return op->emitError(message: "invalid SPIR-V properties" ); }))) |
| 218 | return failure(); |
| 219 | auto propertiesAttr = dyn_cast_or_null<DictionaryAttr>( |
| 220 | SPIRVOp::getPropertiesAsAttr(rewriter.getContext(), properties)); |
| 221 | SmallVector<NamedAttribute> discardableAttrs; |
| 222 | for (NamedAttribute attr : op->getDiscardableAttrDictionary().getValue()) { |
| 223 | if (!propertiesAttr || !propertiesAttr.contains(attr.getName())) |
| 224 | discardableAttrs.push_back(Elt: attr); |
| 225 | } |
| 226 | rewriter.replaceOpWithNewOp<SPIRVOp>(op, op->getResultTypes(), |
| 227 | op->getOperands(), properties, |
| 228 | discardableAttrs); |
| 229 | return success(); |
| 230 | } |
| 231 | }; |
| 232 | } // namespace |
| 233 | |
| 234 | void ConvertToTargetEnv::runOnOperation() { |
| 235 | MLIRContext *context = &getContext(); |
| 236 | func::FuncOp fn = getOperation(); |
| 237 | |
| 238 | auto targetEnv = dyn_cast_or_null<spirv::TargetEnvAttr>( |
| 239 | Val: fn.getOperation()->getDiscardableAttr(name: spirv::getTargetEnvAttrName())); |
| 240 | if (!targetEnv) { |
| 241 | fn.emitError(message: "missing 'spirv.target_env' attribute" ); |
| 242 | return signalPassFailure(); |
| 243 | } |
| 244 | |
| 245 | auto target = SPIRVConversionTarget::get(targetAttr: targetEnv); |
| 246 | |
| 247 | static constexpr char sDotTestOpName[] = "test.convert_to_sdot_op" ; |
| 248 | static constexpr char suDotTestOpName[] = "test.convert_to_sudot_op" ; |
| 249 | static constexpr char uDotTestOpName[] = "test.convert_to_udot_op" ; |
| 250 | static constexpr char sDotAccSatTestOpName[] = |
| 251 | "test.convert_to_sdot_acc_sat_op" ; |
| 252 | static constexpr char suDotAccSatTestOpName[] = |
| 253 | "test.convert_to_sudot_acc_sat_op" ; |
| 254 | static constexpr char uDotAccSatTestOpName[] = |
| 255 | "test.convert_to_udot_acc_sat_op" ; |
| 256 | |
| 257 | RewritePatternSet patterns(context); |
| 258 | patterns.add< |
| 259 | ConvertToAtomCmpExchangeWeak, ConvertToBitReverse, |
| 260 | ConvertToGroupNonUniformBallot, ConvertToModule, ConvertToSubgroupBallot, |
| 261 | ConvertToIntegerDotProd<sDotTestOpName, spirv::SDotOp>, |
| 262 | ConvertToIntegerDotProd<suDotTestOpName, spirv::SUDotOp>, |
| 263 | ConvertToIntegerDotProd<uDotTestOpName, spirv::UDotOp>, |
| 264 | ConvertToIntegerDotProd<sDotAccSatTestOpName, spirv::SDotAccSatOp>, |
| 265 | ConvertToIntegerDotProd<suDotAccSatTestOpName, spirv::SUDotAccSatOp>, |
| 266 | ConvertToIntegerDotProd<uDotAccSatTestOpName, spirv::UDotAccSatOp>>( |
| 267 | arg&: context); |
| 268 | |
| 269 | if (failed(Result: applyPartialConversion(op: fn, target: *target, patterns: std::move(patterns)))) |
| 270 | return signalPassFailure(); |
| 271 | } |
| 272 | |
| 273 | namespace mlir { |
| 274 | void registerConvertToTargetEnvPass() { |
| 275 | PassRegistration<ConvertToTargetEnv>(); |
| 276 | } |
| 277 | } // namespace mlir |
| 278 | |