Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLSL: Diverging preprocessor behavior #1424

Closed
TiemoJung opened this issue Jun 29, 2018 · 9 comments
Closed

HLSL: Diverging preprocessor behavior #1424

TiemoJung opened this issue Jun 29, 2018 · 9 comments

Comments

@TiemoJung
Copy link
Contributor

TiemoJung commented Jun 29, 2018

Hi,

the preprocessor step seems to not handle constructs like this:

#define EXPAND(a)
struct A
{
float4 v EXPAND({1,2,3,4});
};

We use this technique to hide initializer lists that are used in shared code between HLSL and C++.

@johnkslang
Copy link
Member

What did you want this to expand to?

The PP expands it to:

struct A
{
float4 v { 1, 2, 3, 4 };
};

Which looks illegal to me, so you should get some error out for that.

@TiemoJung
Copy link
Contributor Author

TiemoJung commented Jun 29, 2018

It is expected to just do nothing (as EXPAND does nothing with the parameter), the problem is the parsing fails at the comma after the 1.

@johnkslang
Copy link
Member

I see. I will look at fixing it.

@johnkslang
Copy link
Member

The normative C++ specification for the preprocessor for GLSL says that commas separate arguments, unless they are nested in a pair of parentheses. Generally, the curly brace does not have meaning to the preprocessor.

Microsoft's DXC also sees the commas as argument separators, giving: error: too many arguments provided to function-like macro invocation

That is, the first argument, by any spec I can find, is {1. It seems pretty bizarre to have a preprocessor find and deal with match { ... } given how much the preprocessor is used to manipulate (turn on/off) such things. So, this is apparently a very specialized and ill-defined feature in FXC.

Since MS apparently agrees, given the behavior of DXC, perhaps you want a different approach?

Or, would you still want glslang to match FXC behavior for HLSL instead of matching DXC?

Or, do you think this is a bug in DXC?

@antiagainst might be interested in this as well.

@TiemoJung
Copy link
Contributor Author

We would like to have this to have parity with FXC.
An alternative would be to have variadic macros.

@johnkslang
Copy link
Member

I'll look into it.

Out of curiosity, what C++ compiler is accepting this? It would also seem to be out of spec.

@TiemoJung
Copy link
Contributor Author

Basically the same macro is for HLSL a single argument macro (as fxc does not support var arg macros, but it accepts this malformed form somehow) and for normal c/c++ its a var arg macro.

@antiagainst
Copy link
Contributor

antiagainst commented Jul 3, 2018

Interesting! Thanks @johnkslang!

@TiemoJung: You said that the above macro is used for sharing code between HLSL and C++. But I'm wondering whether GCC/Clang accepts the above syntax at all: https://stackoverflow.com/questions/30372148/passing-an-initialization-list-to-a-macro

Yes, FXC supports it, but agreed to @johnkslang: this seems a pretty ill-defined feature. From my understanding, the long-term goal of HLSL is to be more C++-like, with one of its goal to foster code sharing between HLSL and C++. Thus DXC is based on LLVM/Clang. And we already have divergent behavior between DXC and FXC for macro to improve the sharability between HLSL and C++: microsoft/DirectXShaderCompiler#1005.

@johnkslang
Copy link
Member

If curly braces are not recognized to protect comma as argument separators, then the comma becomes a separator. That means, EXPAND({1, 2, 3, 4}) has 4 arguments instead of 1: {1, 2, 3, 4}, hence the need for either variadic arguments or recognizing {...} as a comma container. If DXC has one or the other, it is okay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants