Mesh: Make edge_other_vert function branchless #114682

Merged
Hans Goudey merged 8 commits from mod_moder/blender:tmp_cleanup_edge_other_vert into main 2023-11-13 18:46:15 +01:00

This function shouldn't return invalid index. Instead assertion in used.

This function shouldn't return invalid index. Instead assertion in used.
Iliya Katushenock added 1 commit 2023-11-09 19:38:11 +01:00
Iliya Katushenock requested review from Hans Goudey 2023-11-09 19:38:18 +01:00
Member

I think this has limitations for large vertex indices. If it's not actually faster, might be better to stick with the existing code. In other words, the PR needs to be a bit more convincing I guess.

I think this has limitations for large vertex indices. If it's not actually faster, might be better to stick with the existing code. In other words, the PR needs to be a bit more convincing I guess.
Author
Member

I did short research.
There is kind similar thing called swapping by using sum (Stack Overflow with explanation why that is not good approach: Stack Overflow: Answer to the question).
Lookup for another vertex of edge have the same bad side.
But that still works for me on godbolt.
I pretty sure that is not safe approath and this shouldn't used at all (even in already exist code).

But, current case have some differences, that can protect us:

  1. Edge vertex indices will never be the same.
  2. Edge vertex indices will never be negative.

This means that the order of operations matters.

  1. tmp = (edge[0] - vert) will never be undefined in case of limitation from above.
  2. tmp + edge[1] the same case, if expected index is not an overflow then result will be correct too.

Fixed version


About the code with regular branches:

Assembly code of functions without optimizations from godbolt (GCC x86-64) on godbolt

edge_other_vert(std::pair<int, int>, int): // branchless
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     eax, DWORD PTR [rbp-8]
        sub     eax, DWORD PTR [rbp-12]
        mov     edx, eax
        mov     eax, DWORD PTR [rbp-4]
        add     eax, edx
        pop     rbp
        ret
edge_other_vert_2(std::pair<int, int>, int): // branchfull
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     eax, DWORD PTR [rbp-8]
        cmp     DWORD PTR [rbp-12], eax
        jne     .L4
        mov     eax, DWORD PTR [rbp-4]
        jmp     .L5
.L4:
        mov     eax, DWORD PTR [rbp-4]
        cmp     DWORD PTR [rbp-12], eax
        jne     .L6
        mov     eax, DWORD PTR [rbp-8]
        jmp     .L5
.L6:

Version with branches is larger and contains at least one comparison.
Potential using of this function in hot loops and simd code could be less productive than it can be.

That means branchless function make sense in future respective at least.

That goal of that pull request is not an optimization. But just to measure impact of that change i did benchmark, but there is no any real change. I hope this will be occur in simd tests...

I did short research. There is kind similar thing called swapping by using sum (Stack Overflow with explanation why that is not good approach: [Stack Overflow: Answer to the question](https://stackoverflow.com/questions/30419394/why-does-this-swap-using-simple-addition-and-subtraction-wont-overflow/30419507#30419507)). Lookup for another vertex of edge have the same bad side. But that still works for me on [godbolt](https://godbolt.org/z/zcnqjx5GM). I pretty sure that is not safe approath and this shouldn't used at all (even in already exist code). But, current case have some differences, that can protect us: 1. Edge vertex indices will never be the same. 2. Edge vertex indices will never be negative. This means that the order of operations matters. 1. `tmp = (edge[0] - vert)` will never be undefined in case of limitation from above. 2. `tmp + edge[1]` the same case, if expected index is not an overflow then result will be correct too. [Fixed version](https://godbolt.org/z/Wcn8zod4W) --- About the code with regular branches: Assembly code of functions without optimizations from godbolt (GCC x86-64) [on godbolt](https://godbolt.org/z/q57bTrvo7) ```Asm edge_other_vert(std::pair<int, int>, int): // branchless push rbp mov rbp, rsp mov QWORD PTR [rbp-8], rdi mov DWORD PTR [rbp-12], esi mov eax, DWORD PTR [rbp-8] sub eax, DWORD PTR [rbp-12] mov edx, eax mov eax, DWORD PTR [rbp-4] add eax, edx pop rbp ret edge_other_vert_2(std::pair<int, int>, int): // branchfull push rbp mov rbp, rsp mov QWORD PTR [rbp-8], rdi mov DWORD PTR [rbp-12], esi mov eax, DWORD PTR [rbp-8] cmp DWORD PTR [rbp-12], eax jne .L4 mov eax, DWORD PTR [rbp-4] jmp .L5 .L4: mov eax, DWORD PTR [rbp-4] cmp DWORD PTR [rbp-12], eax jne .L6 mov eax, DWORD PTR [rbp-8] jmp .L5 .L6: ``` Version with branches is larger and contains at least one comparison. Potential using of this function in *hot loops* and simd code could be less productive than it can be. That means branchless function make sense in future respective at least. That goal of that pull request is not an optimization. But just to measure impact of that change i did benchmark, but there is no any real change. I hope this will be occur in simd tests...
Hans Goudey reviewed 2023-11-12 22:07:58 +01:00
@ -280,2 +274,2 @@
}
return -1;
BLI_assert(ELEM(vert, edge[0], edge[1]));
BLI_assert(vert >= 0);
Member

This assert seems to be redundant with the others.

This assert seems to be redundant with the others.
mod_moder marked this conversation as resolved
@ -282,0 +275,4 @@
BLI_assert(vert >= 0);
BLI_assert(edge[0] >= 0);
BLI_assert(edge[1] >= 0);
return edge[0] + edge[1] - vert;
Member

Seems best to prevent overflow explicitly here with the parentheses like you mentioned.

Seems best to prevent overflow explicitly here with the parentheses like you mentioned.
mod_moder marked this conversation as resolved
Iliya Katushenock added 6 commits 2023-11-13 15:44:48 +01:00
Hans Goudey added 1 commit 2023-11-13 15:50:23 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
07473045e0
Expand comment slightly
Hans Goudey approved these changes 2023-11-13 15:50:34 +01:00
Member

@blender-bot build

@blender-bot build
Hans Goudey changed title from Cleanup: Mesh: Branchless function edge_other_vert to Mesh: Make edge_other_vert function branchless 2023-11-13 18:45:16 +01:00
Hans Goudey merged commit a1a31659ea into main 2023-11-13 18:46:15 +01:00
Iliya Katushenock deleted branch tmp_cleanup_edge_other_vert 2023-11-13 18:51:46 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#114682
No description provided.