Date: Mon, 2 Jun 2025 07:37:04 +0000
I hope we have a lightweight way to optimize by caller.
For example,
if we use template to generate sqrt
template<bool p>
double sqrt(double _X)
{
if constexpr(p) { ... }
else { ... }
}
double sqrt(double _X) // normal
{
if (_X >= 0) { ... }
else { ... }
}
then we may get
sqrt_p_true(double _X) { /* p == true */ }
sqrt_p_false(double _X) { /* p == false */ }
sqrt(double _X) { if... else... }
and all codes of sqrt has generated 2 times.
If a function has more branches, generating all branches is impossible.
A function like this:
NOINLINE void func(unsigned int para1, unsigned int para2)
{
switch(para1)
{
case 0:
switch(para2) ...
case 1:
switch(para2) ...
case 2:
switch(para2) ...
case 3: ...
case 114: ...
case ...
default: ...
}
}
when we call func, the worst case is all paraments in default case.
If we know para1 is always greater than 113, we can call func_switch_para1_jmpList_cmpcase114 instead of func.
How to compile?
1.
Find assure in all files.
2.
If possible, generate optimized identifier for functions.
3.
Generate instructions.
________________________________
发件人: Std-Proposals <std-proposals-bounces_at_[hidden]> 代表 Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]>
发送时间: 2025年6月2日 14:42
收件人: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
抄送: Sebastian Wittmeier <wittmeier_at_[hidden]>
主题: Re: [std-proposals] 回复: 回复: A Proposal about A New Keyword assure
The advantage of templates is that different entry points can be stored with the current build infrastructure.
If the execution of templates is similar, they could share code.
Perhaps you can either clearly state, what you suggest,
e.g. during compilation of the function, the compiler sees all calls or sees a list of possible "assures"
(the first would only work with the function in the same translation unit or some kind of link-time-optimization; the second would need different/more syntax)
or state that you want to have a certain outcome and brainstorm with the list how to do it with current and future means in the language.
But then accept that people will move your ideas into existing features or would suggest to redo how it works or how to write it.
-----Ursprüngliche Nachricht-----
Von: SD SH via Std-Proposals <std-proposals_at_[hidden]>
Gesendet: Mo 02.06.2025 06:55
Betreff: [std-proposals] 回复: 回复: A Proposal about A New Keyword assure
An: std-proposals_at_[hidden];
CC: SD SH <Z5515zwy_at_outlook.com>;
Yes. So it can only be used if you know what branch will be (may be) check.
We don't know what paraments will be entered in callee, but we know in caller. So unction programmer can only think about what it need to do in all cases, and caller uses assure to choose entry point.
*
Store entry points?
No. Caller just finds the case it need.
*
Why don't use template?
The size of code using template is too large and it is too slow.
In many cases, different cases can be generated to the same code, it's much smaller and faster.
Template need more effort to program.
*
Other reasons of using assure?
It's clearer.
________________________________
发件人: Std-Proposals <std-proposals-bounces_at_lists.isocpp.org> 代表 Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]>
发送时间: 2025年6月2日 11:03
收件人: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
抄送: Sebastian Wittmeier <wittmeier_at_[hidden]>
主题: Re: [std-proposals] 回复: A Proposal about A New Keyword assure
Do you propose to recommend/specify to the compiler at the caller site, which optimizations can be selected?
If the function is not inlined and you want to use a different function entry point to skip some code and branches, then it would be quite random, whether that is possible.
- If the function is not generated in the same translation unit, the object files or libraries have to somehow store those entry points.
- The compiler would be much more effective to generate that flexibility, if it had a notion or list of possible assurances
- Beside assert/assume/contracts there is also the possibility to provide boolean expressions as function parameters or template parameters or as overloaded parameter types
sqrt(positivedouble d)
sqrt(double d, bool positive = false)
template<bool positive = false> sqrt(double d)
You would have to specify, how your solution is different and what improvements it brings to the table.
-----Ursprüngliche Nachricht-----
Von: SD SH <Z5515zwy_at_[hidden]>
Gesendet: So 01.06.2025 17:32
Betreff: 回复: [std-proposals] A Proposal about A New Keyword assure
An: std-proposals_at_[hidden];
CC: Sebastian Wittmeier <wittmeier_at_[hidden]>;
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 para2
cmp rdx, 0
jne _func_para2_true
cmp rcx, 0
jl _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_geq0
And 2 branches will be skipped.
I think compiler couldn't do above well.
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
For example,
if we use template to generate sqrt
template<bool p>
double sqrt(double _X)
{
if constexpr(p) { ... }
else { ... }
}
double sqrt(double _X) // normal
{
if (_X >= 0) { ... }
else { ... }
}
then we may get
sqrt_p_true(double _X) { /* p == true */ }
sqrt_p_false(double _X) { /* p == false */ }
sqrt(double _X) { if... else... }
and all codes of sqrt has generated 2 times.
If a function has more branches, generating all branches is impossible.
A function like this:
NOINLINE void func(unsigned int para1, unsigned int para2)
{
switch(para1)
{
case 0:
switch(para2) ...
case 1:
switch(para2) ...
case 2:
switch(para2) ...
case 3: ...
case 114: ...
case ...
default: ...
}
}
when we call func, the worst case is all paraments in default case.
If we know para1 is always greater than 113, we can call func_switch_para1_jmpList_cmpcase114 instead of func.
How to compile?
1.
Find assure in all files.
2.
If possible, generate optimized identifier for functions.
3.
Generate instructions.
________________________________
发件人: Std-Proposals <std-proposals-bounces_at_[hidden]> 代表 Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]>
发送时间: 2025年6月2日 14:42
收件人: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
抄送: Sebastian Wittmeier <wittmeier_at_[hidden]>
主题: Re: [std-proposals] 回复: 回复: A Proposal about A New Keyword assure
The advantage of templates is that different entry points can be stored with the current build infrastructure.
If the execution of templates is similar, they could share code.
Perhaps you can either clearly state, what you suggest,
e.g. during compilation of the function, the compiler sees all calls or sees a list of possible "assures"
(the first would only work with the function in the same translation unit or some kind of link-time-optimization; the second would need different/more syntax)
or state that you want to have a certain outcome and brainstorm with the list how to do it with current and future means in the language.
But then accept that people will move your ideas into existing features or would suggest to redo how it works or how to write it.
-----Ursprüngliche Nachricht-----
Von: SD SH via Std-Proposals <std-proposals_at_[hidden]>
Gesendet: Mo 02.06.2025 06:55
Betreff: [std-proposals] 回复: 回复: A Proposal about A New Keyword assure
An: std-proposals_at_[hidden];
CC: SD SH <Z5515zwy_at_outlook.com>;
Yes. So it can only be used if you know what branch will be (may be) check.
We don't know what paraments will be entered in callee, but we know in caller. So unction programmer can only think about what it need to do in all cases, and caller uses assure to choose entry point.
*
Store entry points?
No. Caller just finds the case it need.
*
Why don't use template?
The size of code using template is too large and it is too slow.
In many cases, different cases can be generated to the same code, it's much smaller and faster.
Template need more effort to program.
*
Other reasons of using assure?
It's clearer.
________________________________
发件人: Std-Proposals <std-proposals-bounces_at_lists.isocpp.org> 代表 Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]>
发送时间: 2025年6月2日 11:03
收件人: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
抄送: Sebastian Wittmeier <wittmeier_at_[hidden]>
主题: Re: [std-proposals] 回复: A Proposal about A New Keyword assure
Do you propose to recommend/specify to the compiler at the caller site, which optimizations can be selected?
If the function is not inlined and you want to use a different function entry point to skip some code and branches, then it would be quite random, whether that is possible.
- If the function is not generated in the same translation unit, the object files or libraries have to somehow store those entry points.
- The compiler would be much more effective to generate that flexibility, if it had a notion or list of possible assurances
- Beside assert/assume/contracts there is also the possibility to provide boolean expressions as function parameters or template parameters or as overloaded parameter types
sqrt(positivedouble d)
sqrt(double d, bool positive = false)
template<bool positive = false> sqrt(double d)
You would have to specify, how your solution is different and what improvements it brings to the table.
-----Ursprüngliche Nachricht-----
Von: SD SH <Z5515zwy_at_[hidden]>
Gesendet: So 01.06.2025 17:32
Betreff: 回复: [std-proposals] A Proposal about A New Keyword assure
An: std-proposals_at_[hidden];
CC: Sebastian Wittmeier <wittmeier_at_[hidden]>;
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 para2
cmp rdx, 0
jne _func_para2_true
cmp rcx, 0
jl _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_geq0
And 2 branches will be skipped.
I think compiler couldn't do above well.
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-06-02 07:37:18