#Self modifyable executeable and intrinsics?

1 messages · Page 1 of 1 (latest)

west drift
#

so recently my friend introduced me to intrinsics (AVX2 etc), and honestly, I've been busting my head nonstop how I could automate it at runtime. That is, to check if CPU has an instruction and if it does, it uses it, but if it doesn't it uses regular methods.

Ofc, since the point of it is optimization, I want to avoid ifs as much as possible.

My first idea was to write DLLs with and without intrinsics, and then in an init function I load appropriate DLL, which my friend said is a bad idea as it would be function pointers which would also have performance overhead.

Today I was talking to my friend and I asked him if it would be possible to at runtime replace a function with another function without the use of function pointers, and he mentioned self-modifyable code, but said he knows nothing about it.

So I was wondering, how would I do it?

eg. I have operator+ overload for __m256, and it calls _m256_add_ps, but in init function I want to replace it with a regular function that adds them sequentialy in case cpu doesn't support AVX2

ofc none of those info is know at compile time, which is the source of my problems

tepid grottoBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

daring reef
west drift
daring reef
#

Yes, self modifying code is definitely only suitable for debug/testing because it is a big security risk

#

You would probably just have to branch as early as possible into code that contains whatever vectorization is necessary - i believe this is how most applications do this but I'm not sure since i've not done vectorization before

dusk remnant
#

But that isn’t runtime though you can set these during compilation and then in your actual code check if it is defined

#

And here there are switches what to use

#

Though it is much more than avx

#

Though modern compilers have auto vectorization (if you pass the correct compilation flag)

#

So my guess this doing all this manually adds a 5% speedup

#

Maybe more if certain hot code paths don’t get auto vectorized

latent topaz
#

especially if you consider that the if would be 100% predictable, so it's gonna be essentially free on a modern CPU

#

another option would be to just provide multiple .exe files and run the appropriate one based on what machine you're on

#

self-modifying code would also be possible ofc, but that's gonna be insanely complex to pull off and probably not gonna scale well to larger projects.

west drift
west drift
dusk remnant
#

Unless it has the same instruction set

#

Otherwise I think you can see what march flags native activates and then use this to define stuff

#

And then in your code use different instruction similar to what stockfish does

west drift
#

seems like the discussion is pretty much at a standstill so I'll close it now, thank you all for your answers, I'll try to think of more ways, but these suggestion are also great

#

!solved

tepid grottoBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] Self modifyable executeable and intrinsics?

tepid grottoBOT
#

@west drift

This question thread is being automatically marked as solved.