Skip to content

[spirv] decouple argument scope from call-by-value decision#1991

Merged
jaebaek merged 9 commits intomicrosoft:masterfrom
jaebaek:groupshared_function_param
May 27, 2019
Merged

[spirv] decouple argument scope from call-by-value decision#1991
jaebaek merged 9 commits intomicrosoft:masterfrom
jaebaek:groupshared_function_param

Conversation

@jaebaek
Copy link
Copy Markdown
Collaborator

@jaebaek jaebaek commented Mar 1, 2019

Previously, when passing a variable whose scope is not Function,
DXC creates a temporal variable with Function scope and copies
the value of the variable to the temporal one. It produces legal
SPIR-V code but it causes unnecessary loads / stores if the
function is inlined by spirv-opt.

This commit decouples argument scope from call-by-value
decision to avoid creation/load/store of temporal variables.
This generates illegal SPIR-V code but spirv-opt HLSL
legalization handles it.

Related to #1977

@jaebaek jaebaek requested a review from ehsannas March 1, 2019 20:32
@msftclas
Copy link
Copy Markdown

msftclas commented Mar 1, 2019

CLA assistant check
All CLA requirements met.

@ehsannas ehsannas added the spirv Work related to SPIR-V label Mar 1, 2019
@AppVeyorBot
Copy link
Copy Markdown

@ehsannas ehsannas changed the title Not create temp var for groupshared function param [spirv] Not create temp var for groupshared function param Mar 4, 2019
Copy link
Copy Markdown
Contributor

@ehsannas ehsannas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to add a test for the case where we are calling a structure member function.

For example:

struct testStruct {
	int foo() {
        // ...
	}
};

groupshared testStruct sharedStruct;

void main() {
    int result = sharedStruct.foo();
}

I believe you haven't handled this case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please run clang-format on this file.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@jaebaek jaebaek force-pushed the groupshared_function_param branch from f214c31 to a49bf6e Compare March 5, 2019 17:14
@AppVeyorBot
Copy link
Copy Markdown

@AppVeyorBot
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about variables in the Private storage class? (e.g. static variable)

I believe with the new behavior of SPIRV-Tools, we should never create a temporary variable because of storage class mismatch. (we still do need to create a temporary variable if the param is an rvalue).
If you're not sure, you can check with @s-perron about what the unoptimized output of DXC should look like.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about variables in the Private storage class? (e.g. static variable)

again, I think with the new SPIRV-Tools behavior, we shouldn't create temporary variables unless we have an rvalue? Also think about the cases where the function parameter is out or inout.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add tests for cases where the param is out or inout. e.g.

int bar(out int x) {
...
}
int bar(inout int x) {
  ...
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should also add tests for static cases (to get Private storage class).

@jaebaek jaebaek force-pushed the groupshared_function_param branch from 541c834 to 1ba5a80 Compare April 29, 2019 20:40
@AppVeyorBot
Copy link
Copy Markdown

@AppVeyorBot
Copy link
Copy Markdown

@jaebaek jaebaek force-pushed the groupshared_function_param branch from 626526f to 8aa3f16 Compare May 13, 2019 19:20
@AppVeyorBot
Copy link
Copy Markdown

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removed because it is not generated with the newer SPIRV-Header/Tools

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this is wrong. Let me update it.

@jaebaek jaebaek force-pushed the groupshared_function_param branch from 8aa3f16 to 5dc3b6a Compare May 15, 2019 14:43
@jaebaek jaebaek changed the title [spirv] Not create temp var for groupshared function param [spirv] decouple argument scope from call-by-value decision May 15, 2019
@AppVeyorBot
Copy link
Copy Markdown

@jaebaek jaebaek force-pushed the groupshared_function_param branch from 5dc3b6a to 4abcbcb Compare May 22, 2019 13:59
@AppVeyorBot
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/an/can

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/temporal/temporary

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/must be always Function scope/must always be in Function scope

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better comment:
If it calls a non-static member function, the object itself is argument 0, and therefore all other argument positions are shifted by 1.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops! Thanks for fixing this! :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much nicer now! :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you need to add a beforeHlslLegalization parameter to spirvToolsValidate

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is using setRelaxLogicalPointer instead of setBeforeHLSLLegalization. why?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not contain implicit type conversion between isomorphic types. It only uses a illegal storage class i.e., GroupShared instead of function. In this case, enabling only --relax-logical-pointer is fine.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best to use before-hlsl-legalization.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these ones can maybe still keep using setRelaxLogicalPointer ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of them must be setRelaxLogicalPointer because they have isomorphic function arguments that are actually different from function parameters.

@AppVeyorBot
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@ehsannas ehsannas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, we should remove relaxLogicalPointer from the backend, and only use beforeHlslLegalization. You can do it in a follow-up CL. Thanks

jaebaek added 3 commits May 24, 2019 15:01
This issue is related to HLSL to SPIR-V compile.

Previously, when passing a variable with Workgroup scope, DXC
creates a temporal variable with Function scope and copies
the Workgroup variable to the temporal one. This prevents Workgroup
variables from being a function parameter. However, it causes
unnecessary loads / stores if the function is inlined by spirv-opt.

If a function param is a Workgroup variable, this commit does not
create a temporal one. This is illegal but spirv-opt legalization
will handle it.

Related to microsoft#1977
@jaebaek jaebaek force-pushed the groupshared_function_param branch from ee96997 to 518a346 Compare May 24, 2019 19:08
@AppVeyorBot
Copy link
Copy Markdown

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

Labels

spirv Work related to SPIR-V

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants