This is an archive of the discontinued LLVM Phabricator instance.

[SYCL] Always set NoUnwind attribute for SYCL.
ClosedPublic

Authored by hvdijk on Mar 28 2023, 5:39 PM.

Details

Summary

Like CUDA and OpenCL, the SYCL specification says that throwing and
catching exceptions in device functions is not supported, so this change
extends the logic for adding the NoUnwind attribute to SYCL.

The existing convergent.cpp test, which tests that the convergent
attribute is added to functions by default, is renamed and reused to
test that the nounwind attribute is added by default. This test now has
-fexceptions added to it, which the driver adds by default as well.

The obvious question here is why not simply change the driver to remove
-fexceptions. This change follows the direction given by the TODO
comment because removing -fexceptions would also disable the
__EXCEPTIONS macro, which should reflect whether exceptions are enabled
on the host, rather than on the device, to avoid conflicts in types
shared between host and device.

Diff Detail

Event Timeline

hvdijk created this revision.Mar 28 2023, 5:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2023, 5:39 PM
hvdijk requested review of this revision.Mar 28 2023, 5:39 PM

Is the rationale I gave in the description correct, or would it be better for SYCL device code to unconditionally build without -fexceptions and get the nounwind attribute added that way?

bader added a comment.Mar 28 2023, 5:57 PM

@hvdijk, thanks a lot for fixing this.

Is the rationale I gave in the description correct, or would it be better for SYCL device code to unconditionally build without -fexceptions and get the nounwind attribute added that way?

That's a good question. I haven't looked into this issue deep enough, but I think using -fexceptions requires using delayed diagnostics to avoid false diagnostics during host code analysis.
Anyway, all GPU offloading single-source modes have the same restriction and design and we better have unified solution whether it's using -fexceptions or adding nounwind attribute in CodeGen.

That's a good question. I haven't looked into this issue deep enough, but I think using -fexceptions requires using delayed diagnostics to avoid false diagnostics during host code analysis.

I am assuming you mean -fno-exceptions (or, in clang -cc1, the absence of -fexceptions)? This is a good point. Delayed diagnostics would probably be good in general: we currently already emit warnings for host code when compiling for device, but as long as the generated warnings are identical for the host code as when compiling for host, it is easy enough to ignore; if the device compilation were to result in additional warnings for host code, and those warnings are incorrect, that would be a rather poor user experience. That sounds like a good additional reason to do it the way I had done here, the way the existing code comment had indicated.

hvdijk updated this revision to Diff 509335.EditedMar 29 2023, 6:29 AM

Apparently the test was already passing without my change, so I updated the test to make it a function that did not previously get the nounwind attribute. This also revealed that the test was more fragile than I realised: it depends on the precise order in which functions are emitted. We always match "Function Attrs:" to the first emitted function, and then CHECK-NEXT can fail. We'd ideally first CHECK for void @_Z3foov() (now void @_Z3barv()) and then CHECK-PREV for the attributes, but FileCheck does not support that. What we can do instead though is find the matching attributes #n = { ... } line and verify that everything is in there; updated accordingly.

hvdijk updated this revision to Diff 509355.Mar 29 2023, 6:57 AM

Forgot to update the call to foo() to call bar() instead.

bader accepted this revision.Mar 29 2023, 11:26 AM

LGTM. Thanks!

This revision is now accepted and ready to land.Mar 29 2023, 11:26 AM
This revision was landed with ongoing or failed builds.Mar 29 2023, 6:19 PM
This revision was automatically updated to reflect the committed changes.
clang/lib/CodeGen/CGCall.cpp