|  | 
|  | 1 | +// RUN: iree-opt %s | 
|  | 2 | + | 
|  | 3 | +// PDL pattern spec to annotate operations with specialization ranges. | 
|  | 4 | + | 
|  | 5 | +pdl.pattern @f16_pingpong : benefit(1) { | 
|  | 6 | +  %imaps = pdl.attribute = [ | 
|  | 7 | +    affine_map<(d0, d1, d2) -> (d0, d2)>, | 
|  | 8 | +    affine_map<(d0, d1, d2) -> (d1, d2)>, | 
|  | 9 | +    affine_map<(d0, d1, d2) -> (d0, d1)> | 
|  | 10 | +  ] | 
|  | 11 | +  %elemtypes = pdl.attribute = [f16, f16, f32] | 
|  | 12 | +  %operands = pdl.operands | 
|  | 13 | +  %types = pdl.types | 
|  | 14 | +  %matmul = pdl.operation (%operands : !pdl.range<value>) -> (%types : !pdl.range<type>) | 
|  | 15 | +  pdl.apply_native_constraint "matchContraction"( | 
|  | 16 | +        %matmul, %elemtypes, %imaps | 
|  | 17 | +        : !pdl.operation, !pdl.attribute, !pdl.attribute) | 
|  | 18 | + | 
|  | 19 | +  // Skip if the operation already has ranges. | 
|  | 20 | +  %attr_name = pdl.attribute = "iree_codegen.specialization_ranges" | 
|  | 21 | +  pdl.apply_native_constraint "hasAttr"( | 
|  | 22 | +        %matmul, %attr_name | 
|  | 23 | +        : !pdl.operation, !pdl.attribute) {isNegated = true} | 
|  | 24 | + | 
|  | 25 | +  pdl.rewrite %matmul { | 
|  | 26 | +    %ranges = pdl.attribute = #util<int.assumption.multi_array[ | 
|  | 27 | +        [<umin = 2048, udiv = 256>, <umin = 2048, udiv = 256>, <udiv = 64>], // Large pingpong | 
|  | 28 | +        [<umin = 1024, udiv = 128>, <umin = 1024, udiv = 128>, <udiv = 64>]  // Medium pingpong | 
|  | 29 | +      ]> | 
|  | 30 | +    pdl.apply_native_rewrite "annotateOperation"( | 
|  | 31 | +        %matmul, %attr_name, %ranges | 
|  | 32 | +        : !pdl.operation, !pdl.attribute, !pdl.attribute) | 
|  | 33 | +  } | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +pdl.pattern @bf16_pingpong : benefit(1) { | 
|  | 37 | +  %imaps = pdl.attribute = [ | 
|  | 38 | +    affine_map<(d0, d1, d2) -> (d0, d2)>, | 
|  | 39 | +    affine_map<(d0, d1, d2) -> (d1, d2)>, | 
|  | 40 | +    affine_map<(d0, d1, d2) -> (d0, d1)> | 
|  | 41 | +  ] | 
|  | 42 | +  %elemtypes = pdl.attribute = [bf16, bf16, f32] | 
|  | 43 | +  %operands = pdl.operands | 
|  | 44 | +  %types = pdl.types | 
|  | 45 | +  %matmul = pdl.operation (%operands : !pdl.range<value>) -> (%types : !pdl.range<type>) | 
|  | 46 | +  pdl.apply_native_constraint "matchContraction"( | 
|  | 47 | +        %matmul, %elemtypes, %imaps | 
|  | 48 | +        : !pdl.operation, !pdl.attribute, !pdl.attribute) | 
|  | 49 | + | 
|  | 50 | +  // Skip if the operation already has ranges. | 
|  | 51 | +  %attr_name = pdl.attribute = "iree_codegen.specialization_ranges" | 
|  | 52 | +  pdl.apply_native_constraint "hasAttr"( | 
|  | 53 | +        %matmul, %attr_name | 
|  | 54 | +        : !pdl.operation, !pdl.attribute) {isNegated = true} | 
|  | 55 | + | 
|  | 56 | +  pdl.rewrite %matmul { | 
|  | 57 | +    %ranges = pdl.attribute = #util<int.assumption.multi_array[ | 
|  | 58 | +        [<umin = 2048, udiv = 256>, <umin = 2048, udiv = 256>, <udiv = 64>], // Large pingpong | 
|  | 59 | +        [<umin = 1024, udiv = 128>, <umin = 1024, udiv = 128>, <udiv = 64>]  // Medium pingpong | 
|  | 60 | +      ]> | 
|  | 61 | +    pdl.apply_native_rewrite "annotateOperation"( | 
|  | 62 | +        %matmul, %attr_name, %ranges | 
|  | 63 | +        : !pdl.operation, !pdl.attribute, !pdl.attribute) | 
|  | 64 | +  } | 
|  | 65 | +} | 
|  | 66 | + | 
|  | 67 | +pdl.pattern @f8E4M3_pingpong : benefit(1) { | 
|  | 68 | +  %imaps = pdl.attribute = [ | 
|  | 69 | +    affine_map<(d0, d1, d2) -> (d0, d2)>, | 
|  | 70 | +    affine_map<(d0, d1, d2) -> (d1, d2)>, | 
|  | 71 | +    affine_map<(d0, d1, d2) -> (d0, d1)> | 
|  | 72 | +  ] | 
|  | 73 | +  %elemtypes = pdl.attribute = [f8E4M3FN, f8E4M3FN, f32] | 
|  | 74 | +  %operands = pdl.operands | 
|  | 75 | +  %types = pdl.types | 
|  | 76 | +  %matmul = pdl.operation (%operands : !pdl.range<value>) -> (%types : !pdl.range<type>) | 
|  | 77 | +  pdl.apply_native_constraint "matchContraction"( | 
|  | 78 | +        %matmul, %elemtypes, %imaps | 
|  | 79 | +        : !pdl.operation, !pdl.attribute, !pdl.attribute) | 
|  | 80 | + | 
|  | 81 | +  // Skip if the operation already has ranges. | 
|  | 82 | +  %attr_name = pdl.attribute = "iree_codegen.specialization_ranges" | 
|  | 83 | +  pdl.apply_native_constraint "hasAttr"( | 
|  | 84 | +        %matmul, %attr_name | 
|  | 85 | +        : !pdl.operation, !pdl.attribute) {isNegated = true} | 
|  | 86 | + | 
|  | 87 | +  pdl.rewrite %matmul { | 
|  | 88 | +    %ranges = pdl.attribute = #util<int.assumption.multi_array[ | 
|  | 89 | +        [<umin = 2048, udiv = 256>, <umin = 2048, udiv = 256>, <udiv = 128>], // Large pingpong | 
|  | 90 | +        [<umin = 1024, udiv = 128>, <umin = 1024, udiv = 128>, <udiv = 128>]  // Medium pingpong | 
|  | 91 | +      ]> | 
|  | 92 | +    pdl.apply_native_rewrite "annotateOperation"( | 
|  | 93 | +        %matmul, %attr_name, %ranges | 
|  | 94 | +        : !pdl.operation, !pdl.attribute, !pdl.attribute) | 
|  | 95 | +  } | 
|  | 96 | +} | 
0 commit comments