Cyles: TextureInterpolator Undefined linker behavior - performance regression. #55054

Closed
opened 2018-05-13 21:56:12 +02:00 by Ray molenkamp · 8 comments
Member

Allright, while implementing clang support in D3304 I ran into an issue where
I'd hit an andn BMI instruction (haswell and up, my ivy lake didn't take that too well)

And it took me a few days to figure out, and it's gonna take quite a bit of explaining
to get to the cause of the bug.

lets fist take some detours to get some basic concepts ironed out.

Detour number 1: Undefined behavior - Libraries and duplicate functions

/* ===== a.cpp ===== */ 

#include <stdio.h>

void MyFunc()
{
	printf("Hello from A\n");
}

/* ===== b.cpp ===== */ 

#include <stdio.h>

void MyFunc()
{
	printf("Hello from B\n");
}

/* ===== main.cpp ===== */ 

#include <stdio.h>

extern void MyFunc();
int main(int argc, char**argv)
{
	MyFunc();
}

/* ===== CMakelists.txt ===== */ 

cmake_minimum_required(VERSION 2.8)
project(libtest)
add_library(mytest a.cpp b.cpp)
add_executable(maintest main.cpp)
target_link_libraries(maintest mytest)

Seems simple enough. Question time, what does this do?

a) Error out at link time, duplicate functions aren't allowed!
b) print "Hello From A"
c) print "Hello From B"
d) print "Hello From A" on windows, "Hello From B" on linux

turns out the answer is d. If there's multiple definitions in a library the linker is apparently
allowed to pick 'whatever it feels like' and not tell anyone about it, on linux it seems to
depend on the link order, microsoft's linker ... errr..yeah....I have honestly no idea what the logic is.

Detour number 2, behavior of the static keyword in C++

when using the static keyword on a c++ class it does NOT limit the visiblity to just
this compile unit like it does in C, it merely marks if you can call this function
without an instance (there's more to it, but given our usage of it, this will suffice)

Bringing it all together:

in kernel_cpu_image.h there's this little snippet

#ifdef __GNUC__
  static ccl_always_inline
#else
  static ccl_never_inline
#endif
  float4 interp_3d_tricubic(const TextureInfo& info, float x, float y, float z)
  {

the never_inline is there, cause the massive amount of inlining added 20 minutes to a cycles release build
with msvc. we put this there before it was a templated class so that worked, however when this got refactored
into a template, wonkyness ensured.

At this point, every kernel compile unit will have a interp_3d_tricubic function, the linker is free
to pick any of them, and when build with msvc will consistently pick the unoptimized one from the 'cpu'
kernel, however when build with clang, it went with the AVX2 implementation causing a segfault with an
invalid instruction.

Current Situation:

  • when build with msvc, interp_3d_tricubic will use the unoptimized cpu implementation
  • when build with clang it'll use the avx2 implementation and segfault on all systems that have no avx2.

Solution:

I have nothing pretty really, but here's some options.

template<typename T> struct KERNEL_FUNCTION_FULL_NAME(TextureInterpolator) {

could work, but it requires hackery in osl_services.cpp since it doesn't know about the KERNEL_FUNCTION_FULL_NAME macro

We could remove the ccl_never_inline but it'll balloon the build time, also i'm not convinced the linker will do
the the right thing in a debbug build with inlining disabled.

anyone else have a bright idea here?

Allright, while implementing clang support in [D3304](https://archive.blender.org/developer/D3304) I ran into an issue where I'd hit an andn BMI instruction (haswell and up, my ivy lake didn't take that too well) And it took me a few days to figure out, and it's gonna take quite a bit of explaining to get to the cause of the bug. lets fist take some detours to get some basic concepts ironed out. **Detour number 1: Undefined behavior - Libraries and duplicate functions** ``` /* ===== a.cpp ===== */ #include <stdio.h> void MyFunc() { printf("Hello from A\n"); } /* ===== b.cpp ===== */ #include <stdio.h> void MyFunc() { printf("Hello from B\n"); } /* ===== main.cpp ===== */ #include <stdio.h> extern void MyFunc(); int main(int argc, char**argv) { MyFunc(); } /* ===== CMakelists.txt ===== */ cmake_minimum_required(VERSION 2.8) project(libtest) add_library(mytest a.cpp b.cpp) add_executable(maintest main.cpp) target_link_libraries(maintest mytest) ``` Seems simple enough. Question time, what does this do? a) Error out at link time, duplicate functions aren't allowed! b) print "Hello From A" c) print "Hello From B" d) print "Hello From A" on windows, "Hello From B" on linux turns out the answer is d. If there's multiple definitions in a library the linker is apparently allowed to pick 'whatever it feels like' and not tell anyone about it, on linux it seems to depend on the link order, microsoft's linker ... errr..yeah....I have honestly no idea what the logic is. **Detour number 2, behavior of the static keyword in C++** when using the static keyword on a c++ class it does NOT limit the visiblity to just this compile unit like it does in C, it merely marks if you can call this function without an instance (there's more to it, but given our usage of it, this will suffice) **Bringing it all together:** in kernel_cpu_image.h there's this little snippet ``` #ifdef __GNUC__ static ccl_always_inline #else static ccl_never_inline #endif float4 interp_3d_tricubic(const TextureInfo& info, float x, float y, float z) { ``` the never_inline is there, cause the massive amount of inlining added 20 minutes to a cycles release build with msvc. we put this there before it was a templated class so that worked, however when this got refactored into a template, wonkyness ensured. At this point, every kernel compile unit will have a interp_3d_tricubic function, the linker is free to pick any of them, and when build with msvc will consistently pick the unoptimized one from the 'cpu' kernel, however when build with clang, it went with the AVX2 implementation causing a segfault with an invalid instruction. **Current Situation:** - when build with msvc, interp_3d_tricubic will use the unoptimized cpu implementation - when build with clang it'll use the avx2 implementation and segfault on all systems that have no avx2. **Solution:** I have nothing pretty really, but here's some options. ``` template<typename T> struct KERNEL_FUNCTION_FULL_NAME(TextureInterpolator) { ``` could work, but it requires hackery in osl_services.cpp since it doesn't know about the KERNEL_FUNCTION_FULL_NAME macro We could remove the ccl_never_inline but it'll balloon the build time, also i'm not convinced the linker will do the the right thing in a debbug build with inlining disabled. anyone else have a bright idea here?
Author
Member

Added subscriber: @LazyDodo

Added subscriber: @LazyDodo
Brecht Van Lommel was assigned by Ray molenkamp 2018-05-13 21:56:39 +02:00
Author
Member

Added subscriber: @Sergey

Added subscriber: @Sergey

Proper way to deal with this would be to move every CPU kernel into a namespace. That would ensure linker never re-uses "wrong" microarchitecture implementation.

Proper way to deal with this would be to move every CPU kernel into a namespace. That would ensure linker never re-uses "wrong" microarchitecture implementation.

Added subscriber: @mano-wii

Added subscriber: @mano-wii
Author
Member

looked some more into this, looks like chrome ran into a similar issue coming from math.h, all i can say is 'yikes'

https://bugs.chromium.org/p/chromium/issues/detail?id=666707

https://randomascii.wordpress.com/2016/12/05/vc-archavx-option-unsafe-at-any-speed/

looked some more into this, looks like chrome ran into a similar issue coming from math.h, all i can say is 'yikes' https://bugs.chromium.org/p/chromium/issues/detail?id=666707 https://randomascii.wordpress.com/2016/12/05/vc-archavx-option-unsafe-at-any-speed/

This issue was referenced by blender/cycles@9933b10a15

This issue was referenced by blender/cycles@9933b10a15c251fe34cbdd6a07743b4830c6ad8e

This issue was referenced by ea8e0df672

This issue was referenced by ea8e0df672713a7b3ff408242c940e1f5e78c9c1

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
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
5 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#55054
No description provided.