LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 39674 - [C++] ICE on the nested pointer indirection with address space conversion
Summary: [C++] ICE on the nested pointer indirection with address space conversion
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: OpenCL (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks: release-10.0.0
  Show dependency tree
 
Reported: 2018-11-15 02:37 PST by Anastasia Stulova
Modified: 2020-02-12 04:56 PST (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anastasia Stulova 2018-11-15 02:37:59 PST
Multiple pointer indirection casting isn't working correctly. I.e. the following program:

kernel void foo(){
   __private int** loc;
   int** loc_p = loc;
   **loc_p = 1;
}
will fail with ICE in C++ mode, but for C it will generate: 

bitcast i32* addrspace(4)* %0 to i32 addrspace(4)* addrspace(4)*

and then perform store over pointer in AS 4 (generic). We have now lost the information that the original pointer was in private AS and that the adjustment of AS segment has to be performed before accessing memory pointed by the pointer. Based on the current specification of addrspacecast in https://llvm.org/docs/LangRef.html#addrspacecast-to-instruction I am not very clear whether it can be used for this case without any modifications or clarifications and also what would happen if there are multiple AS mismatches.

C++'s rules assume that qualifiers don't introduce real representation differences and that operations on qualified types are compatible with operations on unqualified types. That's not true of qualifiers in general: address space qualifiers can change representations, ARC qualifiers can have incompatible semantics, etc. There is no way to soundly implement a conversion from __private int ** to __generic int **, just there's no way to soundly implement a conversion from Derived ** to Base **.

Following this logic it seems reasonable to just disallow such conversions in order to prevent surprising behavior in the program.

See original discussion in https://reviews.llvm.org/D53764
Comment 1 Bevin Hansson 2019-02-14 03:57:06 PST
The issue in the provided example is that the 'discards qualifiers in nested pointers' diagnostic will override the 'discards qualifiers' diagnostic, even though the latter is more serious. This is seen in checkPointerTypesForAssignment:

      // Treat address-space mismatches as fatal.  TODO: address subspaces
      if (!lhq.isAddressSpaceSupersetOf(rhq))
        ConvTy = Sema::IncompatiblePointerDiscardsQualifiers;

Instead of instantly being an error, this will be overridden by IncompatibleNestedPointerQualifiers if the pointer incompatibility was due to qualifier mismatch. That assignment result doesn't take address spaces into account and will always be a warning rather than an error.

A similar issue is in SemaCast, checkAddressSpaceCast, which only verifies the top level address space.
Comment 2 Anastasia Stulova 2019-02-15 06:46:23 PST
I was wondering if we should just extend the qualification conversion rules to reject code where address spaces are converted in nested pointers? (Just like John suggested in the review I linked). I think this will avoid potential programming errors with address spaces (just like in the example of base and derived that he provided)...
Comment 3 Bevin Hansson 2019-02-15 06:49:46 PST
That sounds reasonable. I can't really think of any case where you'd want to do that.

I've submitted a patch which (I think?) does this here: https://reviews.llvm.org/D58236
Comment 4 Anastasia Stulova 2020-01-24 08:16:22 PST
The fix for C++ mode: https://reviews.llvm.org/D73360
Comment 5 Anastasia Stulova 2020-02-12 04:20:24 PST
@hans, is it still possible to merge https://reviews.llvm.org/rG6064f426a18304e16b51cc79e74c9c2d55ef5a9c in release 10 branch?
Comment 6 Hans Wennborg 2020-02-12 04:50:26 PST
(In reply to Anastasia Stulova from comment #5)
> @hans, is it still possible to merge
> https://reviews.llvm.org/rG6064f426a18304e16b51cc79e74c9c2d55ef5a9c in
> release 10 branch?

Yes. Pushed to 10.x as b33830aea54536f0f03334b2015b03af336ec90c.
Comment 7 Anastasia Stulova 2020-02-12 04:56:43 PST
Thanks!