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

Add --no-link option to only compile (not link) individual shaders and export all functions #3319

Merged
merged 1 commit into from
Sep 18, 2023

Conversation

ncesario-lunarg
Copy link
Collaborator

@ncesario-lunarg ncesario-lunarg commented Aug 24, 2023

After internal discussion, we will I'm changing this slightly such that --no-link will export all functions, and the ability to export/import individual functions/variables with an attribute will be delayed until such an attribute is standardized.

[[export]]/[[import]] attribute proposal (on hold) This change experiments with adding a "compile-only" option to glslang as well as an `[[export]]` function attribute allowing functions to be exported and then later used by spirv-link.

Proposed usage:

glslang -V --no-link myfuncs.comp -o myfuncs.spv

Where myfuncs.comp looks something like

#version 450

[[export]]
int foo(...) { ... }

[[export]]
float bar(...) { ... }

And where "foo" and "bar" will get the "Export" linkage decoration in the resulting spir-v.

To be more "complete" this change should also:

  • Add an [[import]] attribute for importing functions from another spir-v module
  • Allow variables to be exported and imported in addition to functions (ideally using the same [[export]]/[[import]] attributes)
  • Specify additional spir-v binaries at link time

However, in its current (functional) form, this change could benefit the ongoing improvements to the GPU-AV workflow in Vulkan-ValidationLayers. Before spending anymore time on this, I'm hoping to get some feedback on the overall approach here. I know [[export]]/[[import]] are not official attributes and that may not be the best mechanism for achieving the end goal here (e.g., just exporting all functions when in compile-only mode would work for the purposes of GPU-AV).

@ncesario-lunarg ncesario-lunarg changed the title RFC: Add --no-link option and [[export]] attribute Add --no-link option to only compile (not link) individual shaders and export all functions Sep 6, 2023
@ncesario-lunarg ncesario-lunarg marked this pull request as ready for review September 6, 2023 15:17
@ncesario-lunarg
Copy link
Collaborator Author

FYI @jeremyg-lunarg.

@jeremyg-lunarg
Copy link

I'll update my code this morning and make sure it works. I don't anticipate this change being a problem. Thanks!

@jeremyg-lunarg
Copy link

Works for me locally! I pushed my updated VVL branch to depend on the newest commit.

Copy link
Contributor

@arcady-lunarg arcady-lunarg left a comment

Choose a reason for hiding this comment

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

Just a few minor questions and comments, but on the whole this looks good to me.

@@ -142,6 +142,8 @@ namespace glslang {
return EatUnroll;
else if (name == "loop")
return EatLoop;
else if (name == "export")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be spv::export or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It can. Though, wouldn't it make sense for all of the other strings in here to be constants as well then?

Copy link
Contributor

Choose a reason for hiding this comment

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

What I meant is that you should put it under the if (nameSpace == "spv") block so that in the HLSL, the attribute is spelled [spv::export].

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, yes, I should have put it there.

@@ -4329,6 +4340,15 @@ spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& ty
return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly);
}

spv::LinkageType TGlslangToSpvTraverser::convertGlslangToSpv(glslang::TLinkType linkType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you give this function a better name? Given we have GlslangToSpv, convertGlslangToSpv sounds very confusing. Maybe something like glslangToSpvLinkageType?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, my original intent was to merge this with one of the existing convertGlslangToSpvType, but hadn't found a good way to do that and forgot to circle back. I'll rename it for now.

Copy link
Collaborator Author

@ncesario-lunarg ncesario-lunarg Sep 13, 2023

Choose a reason for hiding this comment

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

Does convertGlslangLinkageToSpv sound ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that seems like a cromulent name.

@@ -2223,6 +2245,12 @@ void Builder::enterFunction(Function const* function)
defInst->addIdOperand(funcId);
buildPoint->addInstruction(std::unique_ptr<Instruction>(defInst));
}

if (auto linkType = function->getLinkType(); linkType != LinkageTypeMax) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know that we have a rule against multiple statements in an if condition, but this is the only place in the codebase such a construct appears. I would prefer you hoist the linkType declaration out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can do that if that is the preferred style. In this case, linkType is only intended to be used within the scope of the if block here, so using the c++17 "init-statement" went beyond just "style" here IMHO (I do realize we don't use this ubiquitously, presumably since most of the codebase is written pre-c++17).

Name 5 "a"
Name 6 "b"
Name 11 "foo("
Decorate 7(add(f1;f1;) Linkage Attributes 6579297 0
Copy link
Contributor

Choose a reason for hiding this comment

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

That random constant in there is apparently the name of the exported symbol, unfortunate that it gets printed in such an unreadable way but I don't think it's worth fixing in this change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, this is really unreadable. The spirv-dis output makes a lot more sense:

...
OpName %7 "add(f1;f1;"
...
OpDecorate %7 LinkageAttributes "add" Export
...
%7 = OpFunction %2 None %4
...

I'm not sure how easy this output is to fix for the tests (or the ROI for such a change).

Adds the --no-link option which outputs the compiled shader binaries
without linking them. This is a first step towards allowing users to
create SPIR-v binary, non-executable libraries.

When using the --no-link option, all functions are decorated with the
Export linkage attribute.
Copy link
Contributor

@arcady-lunarg arcady-lunarg left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

@arcady-lunarg arcady-lunarg merged commit 4c57db1 into KhronosGroup:main Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants