This is an archive of the discontinued LLVM Phabricator instance.

[openmp] Add addrspacecast to getOrCreateIdent
ClosedPublic

Authored by JonChesterfield on Sep 27 2021, 8:14 AM.

Details

Summary

Fixes 51982. Adds a missing CreatePointerCast and allocates a global in
the correct address space.

Test case derived from https://github.com/ROCm-Developer-Tools/aomp/\
blob/aomp-dev/test/smoke/nest_call_par2/nest_call_par2.c by deleting
parts while checking the assertion failure still occurred.

Diff Detail

Event Timeline

JonChesterfield requested review of this revision.Sep 27 2021, 8:14 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptSep 27 2021, 8:14 AM

@pdhaliwal could you run this locally and confirm it lets you revert the patch to amd-stg-open?

What AS are the "ident globals" generated in if we target amdgcn? If it is not 0 we should generate them in the right AS rather than introducing a cast. If it is 0, this patch looks reasonable.

What AS are the "ident globals" generated in if we target amdgcn? If it is not 0 we should generate them in the right AS rather than introducing a cast. If it is 0, this patch looks reasonable.

They're in AS1 which seems right. Standard GPU memory. The choice is either we qualify all the deviceRTL functions with an addrspace(1) qualifier, or we create the variable in the GPU memory address space and addrspacecast the pointer to unspecified address space. It works out the same after inlining. This approach follows the pattern we use for stack variables, where they're alloca'ed in AS5 and then immediately addrspacecast to unspecified.

We could remove the AS1 qualification by putting the variable in unspecified address space, but that should lead to either infer address spaces putting it back to AS1 or a minor degradation in codegen (using flat instructions instead of the more specific global one).

We should put it in the right AS by construction. That matches the stack case and is the right thing to do. Casting things after the fact is a necessary evil but not a solution.
I don't know how clang decides they go into AS(1) but we should do the same. Some datalayout or target hook should exist to tell us that. Instead of OpenMPIRBuilder::Ident we
would cast that type first into the AS we need and then create the global. The rest should fall in place.

JonChesterfield added a comment.EditedSep 27 2021, 8:41 AM

The variable is in the right address space when it is returned by new GlobalVariable. This patch doesn't change that. I assume this is derived from the datalayout string but haven't checked that's the mechanism.

The only reason this showed up is the variable in AS1 being passed to a function that didn't specify any address space, which hits the assert in Instruction.cpp.

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
282

Replacing this constructor call with one that specifies the address space does not solve this bug. It probably makes it more widespread as now all Ident variables would be in AS1 as opposed to only some of them.

Likewise, only casting the newly created GlobalVariable to addrspace(0) does not help, as the one that was returned by return Ident = &GV;

The patch here that ensures the result of getOrCreateIdent is in same address space as IdentPtr (i.e. none) looks necessary regardless of whether the global variable is created in AS1 or ASnone.

I tested this by hardcoding 1

Thanks for the push to look at this more closely. The change from CreatePointerCast to CreatePointerBitCastOrAddrSpaceCast did nothing. CreatePointerCast ends up calling into the latter. Likewise explicitly putting the variable in the default globals address space doesn't help this bug, but seems a good thing to do anyway.

The actual bug here is at the return Ident = &GV line, which was missed when adding the CreatePointerCast in D84345. The first diff here works because it sends that value through CreatePointerBitCastOrAddrSpaceCast, but an equally valid fix would be to change it to return Ident = Builder.CreatePointerCast(&GV, IdentPtr).

  • Set addrspace on the nearby variable, revert to CreatePointerCast
This revision is now accepted and ready to land.Sep 27 2021, 11:12 AM
This revision was landed with ongoing or failed builds.Sep 27 2021, 11:23 AM
This revision was automatically updated to reflect the committed changes.

Failed the amdgpu buildbot, in what looks like an independent failure mode. Will try to debug and fix that, then look at re-landing this. Don't really want to land the code change without the test.

JonChesterfield reopened this revision.EditedSep 27 2021, 2:47 PM

Had to fix two other things first. This test case fails because it is compiled as generic spmd mode, and the amdgpu plugin doesn't know what that is so refuses to load it. Patch at D110845

This revision is now accepted and ready to land.Sep 27 2021, 2:47 PM
JonChesterfield added a comment.EditedSep 30 2021, 1:34 PM

Enum patch landed and passed the staging CI, this change works for me locally, going to re-commit. Passed staging CI.

JonChesterfield edited the summary of this revision. (Show Details)Sep 30 2021, 1:36 PM
This revision was automatically updated to reflect the committed changes.