changing code on its roots

Started by
5 comments, last by MJP 10 years, 10 months ago

I have this code

float Code(float a,float b, int c)

{

float r=0;

if( c==0)

{

r=a*b;

}

else if( c==1)

{

r=a/b;

}

else if( c==2)

{

r=2*a;

}

return r;

}

Lets say i don't have 3 different "if" but a thousand different "if", and "c" as it is in the code just selects one "if"

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code

Or is there anything related to this that solves the problem in a different way?

Advertisement

A switch statement? It'll create a jump table to efficiently map code to execute to the value of c.


switch(c)
{
  case 0: r = a * b; break;
  case 1: r = a / b; break;
  case 2: r = 2 * a; break;
  // ...
}

Or you can directly return if you have nothing to after the conditional.

EDIT: damn, missed the HLSL tag, I don't think it has switch case support. In this case, does the value of c stay the same for every shader invocation? If so, you might be able to have the compiler optimize it down. Otherwise, if it changes dynamically, I'm afraid you're going to have to do the comparisons in any case.. are you looking for syntactic sugar or an actual performance improvement? GPU's aren't that good at control flow so any "clever" hacks to minimize the number of comparisons might yield even worse performance. If there's any way you can weave "c" directly into the formula that would be ideal of course, but it doesn't seem possible here.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

HLSL supports switches and in my case the "if"s will translate the same way a switch will do to assembly code

don't derail my real question

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs


Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs

Maybe shader permuation with compile defines?


#ifdef SOMETHING
r = a*b;
#else ifdef SOMETHINGELSE
r = a/b;
#else
r = 2*a;
#endif

Pardon me if I got the actual defines wrong. You can now employ a system, which is called "shader permumations", which will generate for each shader a set of possible compile-define combinations (in that case, a maximum of three). I don't know if this is too complicated/sofisticated for you, you might can up with an easier system based on that, but thats AFAIK the only way to fully eliminate the if-statements.

You can do it with somewhat less code modification like this, but you'll still need to recompile modified source for each shader:


#define c 42
 
float Code(float a,float b)
{
    // original logic stays here
}

this solves what I was asking

Dynamic linking can definitely be used to implement this, although it's a little wacky to use and will often generate sub-optimal code. Personally I would just do this by pre-compiling several permutations of the shader, with the value of c defined in a a preprocessor macro (similar to what Adam_42 suggests). Doing it this way allows the compiler to completely optimize away the if statement, and also any additional operations performed with the value of c. You can specify the macro definition when compiling the shader using the "pDefines" parameter of D3DCompile, and then just compile the shaders in a for loop.

This topic is closed to new replies.

Advertisement