In general, investigating this features is mostly trial and error, because Compile
is a pure kernel function that resists inspection by PrintDefinitions
or Trace
. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.
What can be inlined by "InlineExternalDefinitions" -> True
?
In principle, basic own values (see OwnValues
) created using Set
can potentially be inlined by "InlineExternalDefinitions" -> True
.
ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;
cf = Compile[{{x, _Real, 0}},
ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
, CompilationOptions -> {"InlineExternalDefinitions" -> True}
]
Needs["CompiledFunctionTools`"];
CompilePrint@cf
Also the DownValues
of indexed objects (the DownValues
with a single match) can become inlined.
extDV1[1] := 1;
extDV2[1] = 1;
extDV3[y] = x;
cf2 = Compile[{{x, _Real, 0}}, extDV1[1] + extDV2[1] + extDV3[y],
CompilationOptions -> {"InlineExternalDefinitions" -> True}]
CompilePrint@cf2
What can't be inlined by "InlineExternalDefinitions" -> True
?
Everything else, especially OwnValues
created by SetDelayed
and DownValues
that rely on pattern matching, can't be inlined by "InlineExternalDefinitions" -> True
.
ext7 := 1;
cf2cf3 = Compile[{{x, _Real, 0}},
ext7*x
, CompilationOptions -> {"InlineExternalDefinitions" -> True}
]
CompilePrint@cf2CompilePrint@cf3
ext8[x_] := x;
cf3cf4 = Compile[{{x, _Real, 0}},
ext8[x]
, CompilationOptions -> {"InlineExternalDefinitions" -> True}
]
CompilePrint@cf3CompilePrint@cf4
What will be inlined?
Which DownValues
will be inlined by "InlineExternalDefinitions" -> True
is decided by the algorithm of Compile
and is therefore hard to predict. It has even changed between version 9 and 10.
Wolfram Technical Support: "According to the developers, nPara is not inlined because Mathematica is making the conservative assumption that nPara might change in the future."