This is an archive of the discontinued LLVM Phabricator instance.

[HLSL] Support -E option for HLSL.
ClosedPublic

Authored by python3kgae on May 1 2022, 11:38 PM.

Details

Summary

-E option will set entry function for hlsl.
The format is -E entry_name.

To avoid conflict with existing option with name 'E', add an extra prefix '--'.

A new field HLSLEntry is added to TargetOption.
To share code with HLSLShaderAttr, entry function will be add HLSLShaderAttr attribute too.

Diff Detail

Event Timeline

python3kgae created this revision.May 1 2022, 11:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2022, 11:38 PM
python3kgae requested review of this revision.May 1 2022, 11:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2022, 11:38 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
python3kgae retitled this revision from Support -E option for HLSL. to [HLSL] Support -E option for HLSL..May 2 2022, 12:06 AM
dexonsmith resigned from this revision.May 3 2022, 12:18 PM

Rebase to main for fix merge conflict.

Is there a specification or reference implementation stating that -E is used?

Option<["--", "/", "-"], "E",

Do you need the prefix --? You may define something like CLFlag. I have missed previous patches, but if / options are not necessary, removing them will be the best to avoid collision with filenames starting with /.

Is there a specification or reference implementation stating that -E is used?

Option<["--", "/", "-"], "E",

Do you need the prefix --? You may define something like CLFlag. I have missed previous patches, but if / options are not necessary, removing them will be the best to avoid collision with filenames starting with /.

Unfortunately, '/' is necessary. dxc allow both '-' and '/' like 'CLFlag'.
Remove '/' means existing customers may need to change their command line to compile hlsl.
And add '--' feels not hurt anyone, that's why I choose to add '--' to work-around the conflict.

Is there a specification or reference implementation stating that -E is used?

Option<["--", "/", "-"], "E",

Do you need the prefix --? You may define something like CLFlag. I have missed previous patches, but if / options are not necessary, removing them will be the best to avoid collision with filenames starting with /.

Unfortunately, '/' is necessary. dxc allow both '-' and '/' like 'CLFlag'.
Remove '/' means existing customers may need to change their command line to compile hlsl.
And add '--' feels not hurt anyone, that's why I choose to add '--' to work-around the conflict.

OK. I guess you have a pre-existing implementation using -E and the upstreaming implementation wants to be compatible.
I wonder why the new tool needs to reuse clang/include/clang/Driver/Options.td. ISTM it would be cleaner to use a new .td file.

Is there a specification or reference implementation stating that -E is used?

Option<["--", "/", "-"], "E",

Do you need the prefix --? You may define something like CLFlag. I have missed previous patches, but if / options are not necessary, removing them will be the best to avoid collision with filenames starting with /.

Unfortunately, '/' is necessary. dxc allow both '-' and '/' like 'CLFlag'.
Remove '/' means existing customers may need to change their command line to compile hlsl.
And add '--' feels not hurt anyone, that's why I choose to add '--' to work-around the conflict.

OK. I guess you have a pre-existing implementation using -E and the upstreaming implementation wants to be compatible.
I wonder why the new tool needs to reuse clang/include/clang/Driver/Options.td. ISTM it would be cleaner to use a new .td file.

That is a good idea.
Let me try that.

beanz added inline comments.May 24 2022, 7:03 AM
clang/lib/Sema/SemaDecl.cpp
9887

If the HLSLShaderAttr::ShaderType enum is properly ordered you should be able to convert the integer value:

HLSLShaderAttr::ShaderType StageInteger = (HLSLShaderAttr::ShaderType)TI.getTriple().getEnvironment() -
                            (uint32_t)llvm::Triple::Pixel;

Then this code doesn't need to be updated if a new shader type is added.

clang/test/SemaHLSL/entry.hlsl
16

Please fix.

Is there a specification or reference implementation stating that -E is used?

Option<["--", "/", "-"], "E",

Do you need the prefix --? You may define something like CLFlag. I have missed previous patches, but if / options are not necessary, removing them will be the best to avoid collision with filenames starting with /.

Unfortunately, '/' is necessary. dxc allow both '-' and '/' like 'CLFlag'.
Remove '/' means existing customers may need to change their command line to compile hlsl.
And add '--' feels not hurt anyone, that's why I choose to add '--' to work-around the conflict.

OK. I guess you have a pre-existing implementation using -E and the upstreaming implementation wants to be compatible.
I wonder why the new tool needs to reuse clang/include/clang/Driver/Options.td. ISTM it would be cleaner to use a new .td file.

I tried to create a DxcOptions.td which only have dxc options and core options. Then create OptTable from DxcOptions.td and use the Dxc OptTable when ParseArgs in Driver::ParseArgStrings. It works fine.

But after ParseArgs, there're lots of code in Driver::BuildCompilation use options::OPT_* enum which depends on Options.td. Cannot find a clean solution to switch to DxcOptions while share code about core options in Driver::BuildCompilation. Do you have any suggestions about how to update options::OPT_* enum after switch to new .td file?

Add library to HLSLShaderAttr to help convert ShaderType.

bruno added a subscriber: bruno.May 26 2022, 5:52 PM

Hi, nice to see this getting in. Comments inline!

clang/lib/Sema/SemaDecl.cpp
11747

You can use getEnvironmentTypeName to get the name here.

11748

Since this is both checking and invalidating a decl, perhaps it should return a bool (maybe some asserts in the default case?) and be renamed CheckAndInvalidateHLSLEntryPoint?

clang/test/SemaHLSL/entry.hlsl
3

Can you also add a check that rejects -Efoo with an error message when the language is not hlsl?

MaskRay added inline comments.May 26 2022, 6:15 PM
clang/test/SemaHLSL/entry.hlsl
3

For most triples, the cc1 -E means stopping after preprocessing. If dxil -E is different, better to use a different option to avoid confusion.

Add hlsl-entry for cc1 option.

python3kgae marked 3 inline comments as done.May 27 2022, 8:00 AM
python3kgae added inline comments.
clang/lib/Sema/SemaDecl.cpp
11748

The name is following CheckMain and CheckMSVCRTEntryPoint which also checking and invalidating.

Fix test fail after rebased on llvm/main.

python3kgae marked 3 inline comments as done.Jun 6 2022, 2:31 PM

Gentle Ping.

All the previous issues are fixed.

Looks to me that all the comments were responded to. I see nothing else worth commenting on.

beanz accepted this revision.Aug 4 2022, 9:18 AM

One very nitpick-y comment, otherwise LGTM.

clang/lib/Sema/SemaDecl.cpp
9873

nit: Comments in the middle of the if make it a bit hard to read, and is not a common style in LLVM code. Please put the comments above the if describing the condition.

This revision is now accepted and ready to land.Aug 4 2022, 9:18 AM

Cleanup comments and fix test after rebase.

This revision was automatically updated to reflect the committed changes.
MaskRay added inline comments.Sep 29 2022, 12:24 PM
clang/test/Driver/hlsl-entry.cpp
1 ↗(On Diff #450185)

Note, test/Driver deliberately discourages %clang_cc1 and %clang -cc1 tests. The tests test other components of Clang (not Driver) and should be moved to appropriate components.

python3kgae marked an inline comment as done.Sep 29 2022, 2:21 PM
python3kgae added inline comments.
clang/test/Driver/hlsl-entry.cpp
1 ↗(On Diff #450185)

Moved to Frontend.