@@ -1310,71 +1310,117 @@ function _generated_stub(gen::Symbol, args::Vector{Any}, params::Vector{Any}, li
1310
1310
return Expr (:meta , :generated , stub)
1311
1311
end
1312
1312
1313
- f24852_kernel (x, y) = x * y
1314
-
1315
- function f24852_kernel_cinfo (x, y)
1316
- sig, spvals, method = Base. _methods_by_ftype (Tuple{typeof (f24852_kernel),x,y}, - 1 , typemax (UInt))[1 ]
1313
+ f24852_kernel1 (x, y:: Tuple ) = x * y[1 ][1 ][1 ]
1314
+ f24852_kernel2 (x, y:: Tuple ) = f24852_kernel1 (x, (y,))
1315
+ f24852_kernel3 (x, y:: Tuple ) = f24852_kernel2 (x, (y,))
1316
+ f24852_kernel (x, y:: Number ) = f24852_kernel3 (x, (y,))
1317
+
1318
+ function f24852_kernel_cinfo (fsig:: Type )
1319
+ world = typemax (UInt) # FIXME
1320
+ sig, spvals, method = Base. _methods_by_ftype (fsig, - 1 , world)[1 ]
1321
+ isdefined (method, :source ) || return (nothing , :(f (x, y)))
1317
1322
code_info = Base. uncompressed_ast (method)
1318
1323
body = Expr (:block , code_info. code... )
1319
- Base. Core. Compiler. substitute! (body, 0 , Any[], sig, Any[spvals... ], 0 , :propagate )
1324
+ Base. Core. Compiler. substitute! (body, 0 , Any[], sig, Any[spvals... ], 1 , :propagate )
1325
+ if startswith (String (method. name), " f24852" )
1326
+ for a in body. args
1327
+ if a isa Expr && a. head == :(= )
1328
+ a = a. args[2 ]
1329
+ end
1330
+ if a isa Expr && length (a. args) === 3 && a. head === :call
1331
+ pushfirst! (a. args, Core. SlotNumber (1 ))
1332
+ end
1333
+ end
1334
+ end
1335
+ pushfirst! (code_info. slotnames, Symbol (" #self#" ))
1336
+ pushfirst! (code_info. slotflags, 0x00 )
1320
1337
return method, code_info
1321
1338
end
1322
1339
1323
- function f24852_gen_cinfo_uninflated (X, Y, f, x, y)
1324
- _, code_info = f24852_kernel_cinfo (x, y)
1340
+ function f24852_gen_cinfo_uninflated (X, Y, _, f, x, y)
1341
+ _, code_info = f24852_kernel_cinfo (Tuple{f, x, y} )
1325
1342
return code_info
1326
1343
end
1327
1344
1328
- function f24852_gen_cinfo_inflated (X, Y, f, x, y)
1329
- method, code_info = f24852_kernel_cinfo (x, y)
1330
- code_info. signature_for_inference_heuristics = Core . Compiler . svec (f, (x, y), typemax (UInt))
1345
+ function f24852_gen_cinfo_inflated (X, Y, _, f, x, y)
1346
+ method, code_info = f24852_kernel_cinfo (Tuple{f, x, y} )
1347
+ code_info. signature_for_inference_heuristics = method
1331
1348
return code_info
1332
1349
end
1333
1350
1334
- function f24852_gen_expr (X, Y, f, x, y)
1335
- return :(f24852_kernel (x:: $X , y:: $Y ))
1351
+ function f24852_gen_expr (X, Y, _, f, x, y) # deparse f(x::X, y::Y) where {X, Y}
1352
+ if f === typeof (f24852_kernel)
1353
+ f2 = :f24852_kernel3
1354
+ elseif f === typeof (f24852_kernel3)
1355
+ f2 = :f24852_kernel2
1356
+ elseif f === typeof (f24852_kernel2)
1357
+ f2 = :f24852_kernel1
1358
+ elseif f === typeof (f24852_kernel1)
1359
+ return :((x:: $X ) * (y:: $Y )[1 ][1 ][1 ])
1360
+ else
1361
+ return :(error (repr (f)))
1362
+ end
1363
+ return :(f24852_late_expr ($ f2, x:: $X , (y:: $Y ,)))
1336
1364
end
1337
1365
1338
1366
@eval begin
1339
- function f24852_late_expr (x:: X , y:: Y ) where {X, Y}
1340
- $ (_generated_stub (:f24852_gen_expr , Any[:f24852_late_expr , :x , :y ],
1367
+ function f24852_late_expr (f, x:: X , y:: Y ) where {X, Y}
1368
+ $ (_generated_stub (:f24852_gen_expr , Any[:self , :f , :x , :y ],
1341
1369
Any[:X , :Y ], @__LINE__ , QuoteNode (Symbol (@__FILE__ )), false ))
1370
+ $ (Expr (:meta , :generated_only ))
1371
+ #= no body =#
1342
1372
end
1343
- function f24852_late_inflated (x:: X , y:: Y ) where {X, Y}
1344
- $ (_generated_stub (:f24852_gen_cinfo_inflated , Any[:f24852_late_inflated , :x , :y ],
1373
+ function f24852_late_inflated (f, x:: X , y:: Y ) where {X, Y}
1374
+ $ (_generated_stub (:f24852_gen_cinfo_inflated , Any[:self , :f , :x , :y ],
1345
1375
Any[:X , :Y ], @__LINE__ , QuoteNode (Symbol (@__FILE__ )), false ))
1376
+ $ (Expr (:meta , :generated_only ))
1377
+ #= no body =#
1346
1378
end
1347
- function f24852_late_uninflated (x:: X , y:: Y ) where {X, Y}
1348
- $ (_generated_stub (:f24852_gen_cinfo_uninflated , Any[:f24852_late_uninflated , :x , :y ],
1379
+ function f24852_late_uninflated (f, x:: X , y:: Y ) where {X, Y}
1380
+ $ (_generated_stub (:f24852_gen_cinfo_uninflated , Any[:self , :f , :x , :y ],
1349
1381
Any[:X , :Y ], @__LINE__ , QuoteNode (Symbol (@__FILE__ )), false ))
1382
+ $ (Expr (:meta , :generated_only ))
1383
+ #= no body =#
1350
1384
end
1351
1385
end
1352
1386
1353
1387
@eval begin
1354
- function f24852_early_expr (x:: X , y:: Y ) where {X, Y}
1355
- $ (_generated_stub (:f24852_gen_expr , Any[:f24852_early_expr , :x , :y ],
1388
+ function f24852_early_expr (f, x:: X , y:: Y ) where {X, Y}
1389
+ $ (_generated_stub (:f24852_gen_expr , Any[:self , :f , :x , :y ],
1356
1390
Any[:X , :Y ], @__LINE__ , QuoteNode (Symbol (@__FILE__ )), true ))
1391
+ $ (Expr (:meta , :generated_only ))
1392
+ #= no body =#
1357
1393
end
1358
- function f24852_early_inflated (x:: X , y:: Y ) where {X, Y}
1359
- $ (_generated_stub (:f24852_gen_cinfo_inflated , Any[:f24852_early_inflated , :x , :y ],
1394
+ function f24852_early_inflated (f, x:: X , y:: Y ) where {X, Y}
1395
+ $ (_generated_stub (:f24852_gen_cinfo_inflated , Any[:self , :f , :x , :y ],
1360
1396
Any[:X , :Y ], @__LINE__ , QuoteNode (Symbol (@__FILE__ )), true ))
1397
+ $ (Expr (:meta , :generated_only ))
1398
+ #= no body =#
1361
1399
end
1362
- function f24852_early_uninflated (x:: X , y:: Y ) where {X, Y}
1363
- $ (_generated_stub (:f24852_gen_cinfo_uninflated , Any[:f24852_early_uninflated , :x , :y ],
1400
+ function f24852_early_uninflated (f, x:: X , y:: Y ) where {X, Y}
1401
+ $ (_generated_stub (:f24852_gen_cinfo_uninflated , Any[:self , :f , :x , :y ],
1364
1402
Any[:X , :Y ], @__LINE__ , QuoteNode (Symbol (@__FILE__ )), true ))
1403
+ $ (Expr (:meta , :generated_only ))
1404
+ #= no body =#
1365
1405
end
1366
1406
end
1367
1407
1368
1408
x, y = rand (), rand ()
1369
1409
result = f24852_kernel (x, y)
1370
1410
1371
- @test result === f24852_late_expr (x, y)
1372
- @test result === f24852_late_uninflated (x, y)
1373
- @test result === f24852_late_inflated (x, y)
1374
-
1375
- @test result === f24852_early_expr (x, y)
1376
- @test result === f24852_early_uninflated (x, y)
1377
- @test result === f24852_early_inflated (x, y)
1411
+ @test result === f24852_late_expr (f24852_kernel, x, y)
1412
+ @test Base. return_types (f24852_late_expr, typeof ((f24852_kernel, x, y))) == Any[Any]
1413
+ @test result === f24852_late_uninflated (f24852_kernel, x, y)
1414
+ @test Base. return_types (f24852_late_uninflated, typeof ((f24852_kernel, x, y))) == Any[Any]
1415
+ @test result === f24852_late_uninflated (f24852_kernel, x, y)
1416
+ @test Base. return_types (f24852_late_uninflated, typeof ((f24852_kernel, x, y))) == Any[Any]
1417
+
1418
+ @test result === f24852_early_expr (f24852_kernel, x, y)
1419
+ @test Base. return_types (f24852_early_expr, typeof ((f24852_kernel, x, y))) == Any[Any]
1420
+ @test result === f24852_early_uninflated (f24852_kernel, x, y)
1421
+ @test Base. return_types (f24852_early_uninflated, typeof ((f24852_kernel, x, y))) == Any[Any]
1422
+ @test result === @inferred f24852_early_inflated (f24852_kernel, x, y)
1423
+ @test Base. return_types (f24852_early_inflated, typeof ((f24852_kernel, x, y))) == Any[Float64]
1378
1424
1379
1425
# TODO : test that `expand_early = true` + inflated `signature_for_inference_heuristics`
1380
1426
# can be used to tighten up some inference result.
0 commit comments