--True, but not only inlined functions.It can make compiler jump to the excepted branch without redundant check-ups. We can even use 1 function and many tag.Like this:
_func:; fastcall; rcx = int para1; rdx = bool para2cmp rdx, 0jne _func_para2_truecmp rcx, 0jl _func_para2_true_para1_l0_func_para2_false_para1_geq0; ...ret_func_para2_true:; ...ret_func_para2_true_para1_l0:; ...ret
In fact, even if compiler not inlined the function, it still works.For example, _func(i assure(i >= 0, b assure(!b)) will be:call _func_para2_false_para1_geq0And 2 branches will be skipped.
I think compiler couldn't do above well.
发件人: Std-Proposals <std-proposals-bounces@lists.isocpp.org> 代表 Sebastian Wittmeier via Std-Proposals <std-proposals@lists.isocpp.org>
发送时间: 2025年6月1日 21:25
收件人: std-proposals@lists.isocpp.org <std-proposals@lists.isocpp.org>
抄送: Sebastian Wittmeier <wittmeier@projectalpha.org>
主题: Re: [std-proposals] A Proposal about A New Keyword assureDo I understand that correctly that you are talking about getting gains with inlined functions by giving additional static (constexpr expression) constraints at the call-site?
Why is it better (except syntax) compared to existing facilities?
-----Ursprüngliche Nachricht-----
Von: SD SH via Std-Proposals <std-proposals@lists.isocpp.org>
Gesendet: So 01.06.2025 15:19
Betreff: [std-proposals] A Proposal about A New Keyword assure
An: std-proposals@lists.isocpp.org;
CC: SD SH <Z5515zwy@outlook.com>;
This proposal introduces a new contextual-keyword assure that allows callers to declare parament Constraints at function call sites. can utilizing these Constraints to optimize function calling. (e.g. removing branches) Violations result in undefined behavior. Analogous to [[assume]].MotivationCheck-ups of function definition sites is unnessary at some call sites, but compilers may preserve check-ups for them. Sometimes we need more performance optimizations but we can't do many optimizations such as built-in functions and third-party libraries.e.g. Lighting computing will call sqrt many times.If sqrt(double _X) like this:double __fastcall sqrt(double _X){if(_X < 0.0){return std::numeric_limits<double>::infinity();}else{// ...}}// asm:/*_sqrt:pxor xmm1, xmm1ucomisd xmm0, xmm1jl _sqrt__Xl0; ..._sqrt__Xl0:movsd xmm0, 0x7FF8000000000000ret*/In lighting computing, _X is always equals zero or greater than zero, so branch of if(_X < 0.0) is never performed there, but compiler may not remove it.
assure will slove this issue.sqrt(d assure(d >= 0)) may generates_sqrt:pxor xmm1, xmm1ucomisd xmm0, xmm1jl _sqrt__Xl0_sqrt_sometime:; ...ret_sqrt__Xl0:movsd xmm0, 0x7FF8000000000000ret;...movsd xmm0, [somewhere]call _sqrt_simetimeor inline call or a new function of sqrt, we needn't program another sqrt for this call site and compiler can optimize program better.Design DecisionsSyntaxfunc(para1 assure(const-bool-expr(INPUT: { para1, const paras... })), paras...)Rejected Alternatives:func(paras...) assure(const-bool-expr(INPUT: const paras...))makes compiler implementation difficult.Semantics:Constraints are purely caller-to-compiler optimization hints.Expressions must be side-effect-free and dependent only on caller-visible state.Expressions should be true, or function calling has undefined behaviorAlternative Considered:Runtime assertions (for security) rejected due to performance impact.Ecosystem ImpactUser: Performance gains with constraintsImplementers: Medium or low implementation effortSTL: Greater room for optimizationView the full text (R0) on GitHub: CPP-Proposals/assure.txtThank you!E. S. Himers <Z5515zwy@outlook.com>-- Std-Proposals mailing list Std-Proposals@lists.isocpp.org https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals