Skip to content

[SYCL] Enhance the sycl device libraries filename check when constructing ll… #2944

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

Merged
merged 6 commits into from
Jan 18, 2021

Conversation

jinge90
Copy link
Contributor

@jinge90 jinge90 commented Dec 23, 2020

…vm-link commands

Signed-off-by: gejin [email protected]

We check input files' name when constructing "llvm-link -only-needed" for linking SYCL device libraries. Currently, the criteria is:

  1. has the prefix 'libsycl-'
  2. has the postfix '.o'
    Such check is too fragile, this PR aims to enhance the check, input SYCL device libraries' filename pattern is "libsycl-${pure-name}-$random.o" where ${pure-name} is in list {'crt', 'cmath', 'cmath-fp64', 'complex', 'complex-fp64', 'fallback-cassert', 'fallback-cmath', 'fallback-cmath-fp64', 'fallback-complex', 'fallback-complex-fp64'} and ${random} is a random string,
    so the updated criteria is:
  3. has the prefix 'libsycl-'
  4. has the postfix '.o'
  5. includes at least 2 '-' character in the filename
  6. the 'pure name' extracted from the input filename should be in sycl device library 'pure name' list.
  7. Input file list includes 2 files at least(when using llvm-link -only-needed, we will put user's device code and several sycl device libraries into input list which means there are 2 input files at least)

This enhancement can only mitigate the fragility since the check is still based on filename. If we switch to approach of "-mlink-builtin-bitcode" for SYCL device library link in the future, this problem will be gone automatically.

@jinge90
Copy link
Contributor Author

jinge90 commented Dec 23, 2020

/summary:run

return false;
});
bool LinkSYCLDeviceLibs = (SYCLDeviceLibIter != InputFiles.end());
bool LinkSYCLDeviceLibs =
(SYCLDeviceLibIter != InputFiles.end()) && (InputFiles.size() > 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to verify that all inputs except the first are sycl libraries. Otherwise I guess it will not work as you expect it to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense, I have updated the patch.
Thanks very much.

@jinge90
Copy link
Contributor Author

jinge90 commented Dec 23, 2020

/summary:run

@jinge90
Copy link
Contributor Author

jinge90 commented Jan 7, 2021

/summary:run

mdtoguchi
mdtoguchi previously approved these changes Jan 8, 2021
Copy link
Contributor

@mdtoguchi mdtoguchi left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM

@jinge90
Copy link
Contributor Author

jinge90 commented Jan 11, 2021

@AGindinson
Could you help me review this PR?
Thanks very much.

Copy link
Contributor

@AGindinson AGindinson left a comment

Choose a reason for hiding this comment

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

Could you please add a test of some sort, at least a negative one?

Comment on lines +99 to +108
"crt",
"cmath",
"cmath-fp64",
"complex",
"complex-fp64",
"fallback-cassert",
"fallback-cmath",
"fallback-cmath-fp64",
"fallback-complex",
"fallback-complex-fp64"};
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean that if we were to add a new device library, we'd have to put its base name here as well? Or is it that no new library is likely to be added?

Copy link
Contributor Author

@jinge90 jinge90 Jan 11, 2021

Choose a reason for hiding this comment

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

Currently, those device libraries can meet our requirements but we may add new ones in the future and we need to put the basename here as well then.
Thanks very much.

Copy link
Contributor

Choose a reason for hiding this comment

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

The explanatory comment you've added should help, I think. I wonder if, in the future, we could make some sort of a configuration file with the device library base names, parsing that both in the relevant CMakeLists and in the driver code (via #include for compile-time?). I understand this is somewhat a vague approach - for example, where should we put the configuration file so that it could be conveniently accessed from all relevant LLVM subprojects? But just as something to consider for future improvements.

@jinge90
Copy link
Contributor Author

jinge90 commented Jan 12, 2021

Could you please add a test of some sort, at least a negative one?

Hi, @AGindinson
It seems very difficult to craft a test case to meet our requirement. The reason is the SYCL device library file name check is done by clang driver which users can't intercept. But we can analyze based on current compiler driver's behavior. The latest compiler will do 2 step link:

  1. use normal llvm-link to link all user's input modules into one.
  2. use llvm-link to link the "complete" user's device image generated from step 1 with all SYCL device library modules with "OnlyNeeded" flag.

We need to recognize whether current llvm-link phase is used to do "step-2" link, in order to achieve this, we check llvm-link's input list. This PR aims to enhance check for SYCL device libraries, the logic is:

  1. the first input module is not a SYCL device library module
  2. All other input modules in input list are SYCL device library module.

The SYCL device library modules are pre-built binary whose file name matches the pattern "libsycl-${pure file name}.o/obj" and after clang-offload-bundler extracting the internal LLVM IR as the input of llvm-link, the extracted LLVM IR file name matches pattern "libsycl-${pure file name}-${random temporary string}.o/obj", so the enhanced check logic is:

  1. SYCL device library module should have the prefix "libsycl-" and postfix ".o" or ".obj"(Windows + CL mode)
  2. Pure file name of SYCL device module should be in list {crt, cmath, cmath-fp64, complex.complex-fp64,fallback-cassert, fallback-cmath, fallback-cmath-fp64, fallback-complex, fallback-complex-fp64}.

User's input device module is generated by llvm-link and will always have the postfix ".bc" which won't be recognized as SYCL device library module. The SYCL device library module's file name are defined in libdevice's CMakeFile.

Thanks very much.

Signed-off-by: gejin <[email protected]>
@AGindinson
Copy link
Contributor

@jinge90, thanks a lot, your recent comment is really helpful!

Based on your explanation, a following simple test case could be envisioned: clang driver is fed with a "user" object that is named "almost like" the actual device libraries (e.g. "libsycl-test-user-library.o"). The checks would have to ensure that the "user" object is processed in the regular llvm-link call, and that a separate --only-needed call is generated for the actual device libraries. Basically, this would be just like the existing test case, but with the user object renamed to something more resembling the libraries, maybe even replacing the existing test case if you find this more preferable. Doing this would, to some extent, test the new filtering mechanism that you've implemented.

Does this make sense, or am I missing some more nuances (or, for that matter, am I giving this too much of a priority)?

@jinge90
Copy link
Contributor Author

jinge90 commented Jan 12, 2021

@jinge90, thanks a lot, your recent comment is really helpful!

Based on your explanation, a following simple test case could be envisioned: clang driver is fed with a "user" object that is named "almost like" the actual device libraries (e.g. "libsycl-test-user-library.o"). The checks would have to ensure that the "user" object is processed in the regular llvm-link call, and that a separate --only-needed call is generated for the actual device libraries. Basically, this would be just like the existing test case, but with the user object renamed to something more resembling the libraries, maybe even replacing the existing test case if you find this more preferable. Doing this would, to some extent, test the new filtering mechanism that you've implemented.

Does this make sense, or am I missing some more nuances (or, for that matter, am I giving this too much of a priority)?

So, do you mean we use a input file whose name is same as some SYCL device libraries to see if the 2-step llvm-link still works as expected?

@AGindinson
Copy link
Contributor

So, do you mean we use a input file whose name is same as some SYCL device libraries to see if the 2-step llvm-link still works as expected?

Yep, a name that would be almost similar to the real device library names.

Signed-off-by: gejin <[email protected]>
@jinge90 jinge90 changed the title [SYCL]Enhance the sycl device libraries filename check when constructing ll… [SYCL] Enhance the sycl device libraries filename check when constructing ll… Jan 13, 2021
@jinge90
Copy link
Contributor Author

jinge90 commented Jan 13, 2021

/summary:run

@jinge90
Copy link
Contributor Author

jinge90 commented Jan 13, 2021

Hi, @mdtoguchi
I updated the patch to add a test, could you help me review and +1 again?
Thanks very much.

@jinge90
Copy link
Contributor Author

jinge90 commented Jan 15, 2021

Hi, @mdtoguchi
I updated the patch to add a test, could you help me review and +1 again?
Thanks very much.

Hi, @mdtoguchi
Could you help me approve again? The updated patch only adds some test.
Thanks very much.

@romanovvlad romanovvlad merged commit f930a01 into intel:sycl Jan 18, 2021
alexbatashev pushed a commit to alexbatashev/llvm that referenced this pull request Jan 19, 2021
* sycl:
  [SYCL][NFC] Fix warnings reported by Clang (intel#3050)
  [SYCL] Enhance device libraries filename check when constructing llvm-link commands (intel#2944)
  [SYCL][NFC] Add vtable ABI checks (intel#3032)
  [SYCL] Cleanup the deprecated Intel attribute logic (intel#3036)
@jinge90 jinge90 deleted the enhance_sycl_devicelib_check branch January 26, 2021 01:20
jsji pushed a commit that referenced this pull request Jan 10, 2025
Motivations is similar as f729c49. This PR addresses SYCL device global which may have attributes "sycl-device-image-scope", "sycl-host-access" and "sycl-unique-id". Failure to preserve "sycl-unique-id" after llvm-spirv translation triggers assert at
https://github.com/intel/llvm/blob/2824f61dd36790448a224cd596985bd01cbcd0f3/llvm/lib/SYCLLowerIR/DeviceGlobals.cpp#L85

Also preserve GlobalVariable metadata as an improvement, though there is no test to show this is really needed.

Original commit:
KhronosGroup/SPIRV-LLVM-Translator@f2d913cb1a22cb3
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.

5 participants