Is there a way to control codegen function prototypes
9 ビュー (過去 30 日間)
古いコメントを表示
Is there any way to see the internals of why codegen decided to define a prototype the way it did? Or to force a particular function prototype for non-entry point functions?
I have a fairly complex function, let's call it functionA, which calls several internal functions.
outStruct = functionA( struct1, double1, double2, struct2)
When I use codegen to generate functionA as an entry point, the prototype looks the way I'd expect it to:
extern void functionA( const struct1_T *struct1, double double1, double double2, const struct2_T *struct2, struct0_T *outStruct)
Now, I want to add a second function, test_functionA, which coder.load's some test structures from a file, then calls functionA on the data.
function test_functionA()
coder.inline('never');
inputs = coder.load('data');
struct1 = coder.ignoreConst(inputs.struct1);
double1 = coder.ignoreConst(inputs.double1);
double2 = coder.ignoreConst(inputs.double2);
struct2 = coder.ignoreConst(inputs.struct2);
o = functionA(struct1,double1,double2,struct2);
% then I print elements of o, so o won't get optimized out
What I was hoping -- and what happens in my simple test cases trying to reproduce this -- was that test_funcitonA C++ code does in fact call the functionA using the already generated library code. But what actually happens, is that test_functionA calls a copy of the function, with a totally different prototype
void b_functionA(const double struct1_field1[8], double double2, double struct2_field1, struct0_T *outStruct)
Coder has obviously folded some constants and folded constants within the structures into separate arguments, even though I told it to ignoreConst.
Is there some way to force a particular function prototype in the generated code? I'd have thought that since functionA is an entry point with specified args, that test_functionA would use that same definition, rather than some strange optimized specialization.
Possibly related ... functionA lives in a package. When I codegen test_functionA, the copy of functionA that it creates lives in its own namespace. I can't seem to reproduce that with simple files.
Is there a way to sort of debug what codegen thinks it is dealing with ? Like some sort of AST output of the code before it starts to optimize?
4 件のコメント
Denis Gurchenkov
2023 年 5 月 5 日
Hi Jon,
Three thoughts here:
- One possible thing to try is to disable the compiler optimization that splits structs into individual fields:
cfg = coder.config('lib'); % or whatever config you use
cfg.EnableStructExplosion = false;
codegen ...... -config cfg .....
I don't expect this to fully resolve the issue but it should made the prototype of functionA to be more similar to what you expect.
2. Can you speak more about your end goal, why do you need the same functionA to be an entry point and also invoked from test_functionA? If you explain the intent, maybe someone can suggest a different solution to get there.
3. To fully resolve the issue, we need figure out why codegen thinks that the version of functionA that you already have (the entry point) is not good enough to be called from test_functionA. Something makes it unable to reuse the function in that 2nd call site, and makes it to create a new one. This is hard to diagnose without a specific example. If you can attach reproduction steps (.m files and doit.m that calls codegen) we'll take a look. If you prefer to not to post your code publicly, please contact the technical support, the support request will get routed to the coder team and we can debug further (we still need repros though).
Thanks,
Denis
MATLAB Coder dev team
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!