Function constant pinning
function_constant_pinning
PIN_CONSTANT_VALS = 'pin_to_constants'
module-attribute
FunctionConstantPinning
Bases: RewritePattern
Source code in xdsl/transforms/experimental/function_constant_pinning.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
match_and_rewrite(func_op: func.FuncOp, rewriter: PatternRewriter)
Source code in xdsl/transforms/experimental/function_constant_pinning.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
FunctionConstantPinningPass
dataclass
Bases: ModulePass
This pass consumes IR annotated with special hints to generate new functions that have certain SSA values pinned to a constant, usually to enable further optimization options on this pinned function.
The original function is changed to dynamically dispatch to this pinned function when the ssa value matches the given constant.
Any single-result operation annotated with a "pin_to_constants" attribute containing an array of values, that is located within a function body triggers this optimization. These annotations are usually inserted by previous passes that know that they would want to generate a more optimized version of their function for specific values of a run-time determined variable.
An example might be a function that branches repeatedly on a specific variable:
function test() {
x = calc_condition()
if (x) {
specific_thing()
}
some_thing() // A
if (x) {
another_thing()
}
some_thing() // B
}
if we can pin x to true, we are suddenly able to generate two much simple function bodies (after constant folding)
function test() {
x = calc_condition()
if (x) {
test_pinned()
return
}
some_thing() // A
some_thing() // B
}
function test_pinned() {
specific_thing()
some_thing() // A
another_thing()
some_thing() // B
}
Note that the function test_pinned might be much easier to optimize for a compiler if there are state
dependencies between specific_thing, another_thing and some_thing.
Source code in xdsl/transforms/experimental/function_constant_pinning.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
name = 'function-constant-pinning'
class-attribute
instance-attribute
apply(ctx: Context, op: builtin.ModuleOp) -> None
Source code in xdsl/transforms/experimental/function_constant_pinning.py
299 300 | |
generate_func_with_pinned_val(func_op: func.FuncOp, pin: IntegerAttr[IntegerType | IndexType], rewriter: PatternRewriter)
Specializes a function to pin a value to a compile time constant. Assumes the function is top-level inside the module.
This will do the following things: - clone the function - rename it to be uniquely named inside the module - erase all operations up until the operation producing the pinned value - replace the operation with a constant instantiation
Source code in xdsl/transforms/experimental/function_constant_pinning.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
func_contains_pinning_annotation(funcop: func.FuncOp) -> Operation | None
Return the first operation inside the function that has a "pin_to_constants" attribute.
Only works on top-level operations, we can't handle nested things right now.
Source code in xdsl/transforms/experimental/function_constant_pinning.py
167 168 169 170 171 172 173 174 175 176 177 | |
get_pinned_vals_for_op(op: Operation) -> list[IntegerAttr[IntegerType | IndexType]] | None
Reads the "pin_to_constants" attribute of an operation, checks for valid formatting, and return the list of attribute values that should be pinned.
Source code in xdsl/transforms/experimental/function_constant_pinning.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
ops_between_op_and_func_start(func_op: func.FuncOp, op: Operation) -> Iterable[Operation]
Get a list of all operations localed between op and the start of body. Returns them in reverse order of occurrence.
op must be a direct child of func_op!
func.func @test() { // <- func_op test.test() // A test.test() // B test.test() // <- op test.test() // C
should return only B, A not C
Source code in xdsl/transforms/experimental/function_constant_pinning.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
unique_pinned_name(module: builtin.ModuleOp, name: str, hint: str) -> str
Generate a new name that is unique to the module
Source code in xdsl/transforms/experimental/function_constant_pinning.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |