Functions: Improve multi-function evaluation to support loop optimizations with msvc. #103737

Closed
opened 2023-01-08 14:52:27 +01:00 by Jacques Lucke · 4 comments
Member
No description provided.
Author
Member

Added subscriber: @JacquesLucke

Added subscriber: @JacquesLucke
Author
Member

This improves field performance on windows quite a bit, it's not on the same level as gcc or clang yet though (D16942).

diff --git a/source/blender/functions/FN_multi_function_builder.hh b/source/blender/functions/FN_multi_function_builder.hh
index 22c6e8a850e..04849bcc221 100644
--- a/source/blender/functions/FN_multi_function_builder.hh
+++ b/source/blender/functions/FN_multi_function_builder.hh
@@ -138,23 +138,18 @@ execute_array(TypeSequence<ParamTags...> /*param_tags*/,
                * other. This is important for some compiler optimizations. */
               Args &&__restrict... args)
 {
-  for (const int64_t i : mask) {
-    element_fn([&]() -> decltype(auto) {
-      using ParamTag = typename TypeSequence<ParamTags...>::template at_index<I>;
-      if constexpr (ParamTag::category == ParamCategory::SingleInput) {
-        /* For inputs, pass the value (or a reference to it) to the function. */
-        return args[i];
-      }
-      else if constexpr (ParamTag::category == ParamCategory::SingleOutput) {
-        /* For outputs, pass a pointer to the function. This is done instead of passing a
-         * reference, because the pointer points to uninitialized memory. */
-        return args + i;
-      }
-      else if constexpr (ParamTag::category == ParamCategory::SingleMutable) {
-        /* For mutables, pass a mutable reference to the function. */
-        return args[i];
-      }
-    }()...);
+  if constexpr (std::is_same_v<std::decay_t<MaskT>, IndexRange>) {
+    /* Having this explicit loop is necessary for msvc to be able to vectorize this. */
+    const int64_t start = mask.start();
+    const int64_t end = mask.one_after_last();
+    for (int64_t i = start; i < end; i++) {
+      element_fn(args[i]...);
+    }
+  }
+  else {
+    for (const int32_t i : mask) {
+      element_fn(args[i]...);
+    }
   }
 }
 
@@ -190,7 +185,7 @@ inline void execute_materialized_impl(TypeSequence<ParamTags...> /*param_tags*/,
         return chunks[in_i];
       }
       else if constexpr (ParamTag::category == ParamCategory::SingleOutput) {
-        return chunks + out_i;
+        return chunks[out_i];
       }
       else if constexpr (ParamTag::category == ParamCategory::SingleMutable) {
         return chunks[out_i];
@@ -472,7 +467,7 @@ inline auto build_multi_function_with_n_inputs_one_output(const char *name,
   constexpr auto param_tags = TypeSequence<MFParamTag<ParamCategory::SingleInput, In>...,
                                            MFParamTag<ParamCategory::SingleOutput, Out>>();
   auto call_fn = build_multi_function_call_from_element_fn(
-      [element_fn](const In &...in, Out *out) { new (out) Out(element_fn(in...)); },
+      [element_fn](const In &...in, Out &out) { new (&out) Out(element_fn(in...)); },
       exec_preset,
       param_tags);
   return CustomMF(name, call_fn, param_tags);
diff --git a/source/blender/modifiers/intern/MOD_nodes.cc b/source/blender/modifiers/intern/MOD_nodes.cc
index 2c5e02fbbe5..ea69e5782a2 100644
--- a/source/blender/modifiers/intern/MOD_nodes.cc
+++ b/source/blender/modifiers/intern/MOD_nodes.cc
@@ -1284,6 +1284,7 @@ static void modifyGeometry(ModifierData *md,
                            const ModifierEvalContext *ctx,
                            GeometrySet &geometry_set)
 {
+  SCOPED_TIMER_AVERAGED(__func__);
   NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
   if (nmd->node_group == nullptr) {
     return;
This improves field performance on windows quite a bit, it's not on the same level as gcc or clang yet though ([D16942](https://archive.blender.org/developer/D16942)). ``` diff --git a/source/blender/functions/FN_multi_function_builder.hh b/source/blender/functions/FN_multi_function_builder.hh index 22c6e8a850e..04849bcc221 100644 --- a/source/blender/functions/FN_multi_function_builder.hh +++ b/source/blender/functions/FN_multi_function_builder.hh @@ -138,23 +138,18 @@ execute_array(TypeSequence<ParamTags...> /*param_tags*/, * other. This is important for some compiler optimizations. */ Args &&__restrict... args) { - for (const int64_t i : mask) { - element_fn([&]() -> decltype(auto) { - using ParamTag = typename TypeSequence<ParamTags...>::template at_index<I>; - if constexpr (ParamTag::category == ParamCategory::SingleInput) { - /* For inputs, pass the value (or a reference to it) to the function. */ - return args[i]; - } - else if constexpr (ParamTag::category == ParamCategory::SingleOutput) { - /* For outputs, pass a pointer to the function. This is done instead of passing a - * reference, because the pointer points to uninitialized memory. */ - return args + i; - } - else if constexpr (ParamTag::category == ParamCategory::SingleMutable) { - /* For mutables, pass a mutable reference to the function. */ - return args[i]; - } - }()...); + if constexpr (std::is_same_v<std::decay_t<MaskT>, IndexRange>) { + /* Having this explicit loop is necessary for msvc to be able to vectorize this. */ + const int64_t start = mask.start(); + const int64_t end = mask.one_after_last(); + for (int64_t i = start; i < end; i++) { + element_fn(args[i]...); + } + } + else { + for (const int32_t i : mask) { + element_fn(args[i]...); + } } } @@ -190,7 +185,7 @@ inline void execute_materialized_impl(TypeSequence<ParamTags...> /*param_tags*/, return chunks[in_i]; } else if constexpr (ParamTag::category == ParamCategory::SingleOutput) { - return chunks + out_i; + return chunks[out_i]; } else if constexpr (ParamTag::category == ParamCategory::SingleMutable) { return chunks[out_i]; @@ -472,7 +467,7 @@ inline auto build_multi_function_with_n_inputs_one_output(const char *name, constexpr auto param_tags = TypeSequence<MFParamTag<ParamCategory::SingleInput, In>..., MFParamTag<ParamCategory::SingleOutput, Out>>(); auto call_fn = build_multi_function_call_from_element_fn( - [element_fn](const In &...in, Out *out) { new (out) Out(element_fn(in...)); }, + [element_fn](const In &...in, Out &out) { new (&out) Out(element_fn(in...)); }, exec_preset, param_tags); return CustomMF(name, call_fn, param_tags); diff --git a/source/blender/modifiers/intern/MOD_nodes.cc b/source/blender/modifiers/intern/MOD_nodes.cc index 2c5e02fbbe5..ea69e5782a2 100644 --- a/source/blender/modifiers/intern/MOD_nodes.cc +++ b/source/blender/modifiers/intern/MOD_nodes.cc @@ -1284,6 +1284,7 @@ static void modifyGeometry(ModifierData *md, const ModifierEvalContext *ctx, GeometrySet &geometry_set) { + SCOPED_TIMER_AVERAGED(__func__); NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md); if (nmd->node_group == nullptr) { return; ```
Author
Member

Changed status from 'Needs Triage' to: 'Resolved'

Changed status from 'Needs Triage' to: 'Resolved'
Jacques Lucke self-assigned this 2023-01-08 15:32:23 +01:00
Author
Member
5f9a48ed59
Sign in to join this conversation.
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
1 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#103737
No description provided.