| 1 | //===- ConvertToReplicatedConstantCompositePass.cpp -----------------------===// |
| 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 a pass to convert a splat composite spirv.Constant and |
| 10 | // spirv.SpecConstantComposite to spirv.EXT.ConstantCompositeReplicate and |
| 11 | // spirv.EXT.SpecConstantCompositeReplicate respectively. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
| 16 | #include "mlir/Dialect/SPIRV/Transforms/Passes.h" |
| 17 | #include "mlir/Transforms/WalkPatternRewriteDriver.h" |
| 18 | |
| 19 | namespace mlir::spirv { |
| 20 | #define GEN_PASS_DEF_SPIRVREPLICATEDCONSTANTCOMPOSITEPASS |
| 21 | #include "mlir/Dialect/SPIRV/Transforms/Passes.h.inc" |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | static Type getArrayElemType(Attribute attr) { |
| 26 | if (auto typedAttr = dyn_cast<TypedAttr>(Val&: attr)) { |
| 27 | return typedAttr.getType(); |
| 28 | } |
| 29 | |
| 30 | if (auto arrayAttr = dyn_cast<ArrayAttr>(Val&: attr)) { |
| 31 | if (arrayAttr.empty()) |
| 32 | return nullptr; |
| 33 | return ArrayType::get(elementType: getArrayElemType(attr: arrayAttr[0]), elementCount: arrayAttr.size()); |
| 34 | } |
| 35 | |
| 36 | return nullptr; |
| 37 | } |
| 38 | |
| 39 | static std::pair<Attribute, uint32_t> |
| 40 | getSplatAttrAndNumElements(Attribute valueAttr, Type valueType) { |
| 41 | auto compositeType = dyn_cast_or_null<spirv::CompositeType>(Val&: valueType); |
| 42 | if (!compositeType) |
| 43 | return {nullptr, 1}; |
| 44 | |
| 45 | if (auto splatAttr = dyn_cast<SplatElementsAttr>(Val&: valueAttr)) { |
| 46 | return {splatAttr.getSplatValue<Attribute>(), splatAttr.size()}; |
| 47 | } |
| 48 | |
| 49 | if (auto arrayAttr = dyn_cast<ArrayAttr>(Val&: valueAttr)) { |
| 50 | if (!arrayAttr.empty() && llvm::all_equal(Range&: arrayAttr)) { |
| 51 | Attribute attr = arrayAttr[0]; |
| 52 | uint32_t numElements = arrayAttr.size(); |
| 53 | |
| 54 | // Find the inner-most splat value for array of composites |
| 55 | auto [newAttr, newNumElements] = |
| 56 | getSplatAttrAndNumElements(valueAttr: attr, valueType: getArrayElemType(attr)); |
| 57 | if (newAttr) { |
| 58 | attr = newAttr; |
| 59 | numElements *= newNumElements; |
| 60 | } |
| 61 | return {attr, numElements}; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return {nullptr, 1}; |
| 66 | } |
| 67 | |
| 68 | struct ConstantOpConversion final : OpRewritePattern<spirv::ConstantOp> { |
| 69 | using Base::Base; |
| 70 | |
| 71 | LogicalResult matchAndRewrite(spirv::ConstantOp op, |
| 72 | PatternRewriter &rewriter) const override { |
| 73 | auto [attr, numElements] = |
| 74 | getSplatAttrAndNumElements(valueAttr: op.getValue(), valueType: op.getType()); |
| 75 | if (!attr) |
| 76 | return rewriter.notifyMatchFailure(arg&: op, msg: "composite is not splat" ); |
| 77 | |
| 78 | if (numElements == 1) |
| 79 | return rewriter.notifyMatchFailure(arg&: op, |
| 80 | msg: "composite has only one constituent" ); |
| 81 | |
| 82 | rewriter.replaceOpWithNewOp<spirv::EXTConstantCompositeReplicateOp>( |
| 83 | op, args: op.getType(), args&: attr); |
| 84 | return success(); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | struct SpecConstantCompositeOpConversion final |
| 89 | : OpRewritePattern<spirv::SpecConstantCompositeOp> { |
| 90 | using Base::Base; |
| 91 | |
| 92 | LogicalResult matchAndRewrite(spirv::SpecConstantCompositeOp op, |
| 93 | PatternRewriter &rewriter) const override { |
| 94 | auto compositeType = dyn_cast_or_null<spirv::CompositeType>(Val: op.getType()); |
| 95 | if (!compositeType) |
| 96 | return rewriter.notifyMatchFailure(arg&: op, msg: "not a composite constant" ); |
| 97 | |
| 98 | ArrayAttr constituents = op.getConstituents(); |
| 99 | if (constituents.size() <= 1) |
| 100 | return rewriter.notifyMatchFailure( |
| 101 | arg&: op, msg: "composite has zero or one consituent" ); |
| 102 | |
| 103 | if (!llvm::all_equal(Range&: constituents)) |
| 104 | return rewriter.notifyMatchFailure(arg&: op, msg: "composite is not splat" ); |
| 105 | |
| 106 | auto splatConstituent = dyn_cast<FlatSymbolRefAttr>(Val: constituents[0]); |
| 107 | if (!splatConstituent) |
| 108 | return rewriter.notifyMatchFailure( |
| 109 | arg&: op, msg: "expected flat symbol reference for splat constituent" ); |
| 110 | |
| 111 | rewriter.replaceOpWithNewOp<spirv::EXTSpecConstantCompositeReplicateOp>( |
| 112 | op, args: TypeAttr::get(type: op.getType()), args: op.getSymNameAttr(), args&: splatConstituent, |
| 113 | args: op.getSymVisibilityAttr()); |
| 114 | |
| 115 | return success(); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | struct ConvertToReplicatedConstantCompositePass final |
| 120 | : spirv::impl::SPIRVReplicatedConstantCompositePassBase< |
| 121 | ConvertToReplicatedConstantCompositePass> { |
| 122 | void runOnOperation() override { |
| 123 | MLIRContext *context = &getContext(); |
| 124 | RewritePatternSet patterns(context); |
| 125 | patterns.add<ConstantOpConversion, SpecConstantCompositeOpConversion>( |
| 126 | arg&: context); |
| 127 | walkAndApplyPatterns(op: getOperation(), patterns: std::move(patterns)); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | } // namespace |
| 132 | } // namespace mlir::spirv |
| 133 | |