This is an archive of the discontinued LLVM Phabricator instance.

Fix incorrect cast in VisitSYCLUniqueStableNameExpr
ClosedPublic

Authored by arichardson on Nov 18 2022, 3:46 AM.

Details

Summary

Fix incorrect cast in VisitSYCLUniqueStableNameExpr

Clang language-level address spaces and LLVM pointer address spaces are
not the same thing (even though they will both have a numeric value of
zero in many cases). LangAS is a enum class to avoid implicit conversions,
but eba69b59d1a30dead07da2c279c8ecfd2b62ba9f avoided the compiler error by
adding a static_cast<>. While touching this code, simplify it by using
CreatePointerBitCastOrAddrSpaceCast() which is already a no-op if the types
match.

This changes the code generation for spir64 to place the globals in
the sycl_global addreds space, which maps to addrspace(1).

Diff Detail

Event Timeline

arichardson created this revision.Nov 18 2022, 3:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 18 2022, 3:46 AM
arichardson requested review of this revision.Nov 18 2022, 3:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 18 2022, 3:46 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added a subscriber: bader.

Adding @bader as SYCL code owner. The changes look reasonable to me, but codegen is not my area of expertise.

Adding @bader as SYCL code owner. The changes look reasonable to me, but codegen is not my area of expertise.

Yeah, same to me. I wrote this originally, but I think the address-space stuff was added in the downstream by someone else (though I can't find proof of it now!).

bader added inline comments.Nov 18 2022, 6:58 AM
clang/lib/CodeGen/CGExprScalar.cpp
1635

This changes the code generation for spir64 to place the globals in addrspace(4). I believe is correct, but it would be good for someone who is familiar with the target to confirm.

Globals must reside in sycl_global namespace, which is addrspace(1) for spir* targets.
addrspace(4) represents "generic" address space, which is a placeholder for a specific address space. If we leave it addrspace(4) for global definition, the compiler won't be able to infer genuine address space.

Adding @bader as SYCL code owner. The changes look reasonable to me, but codegen is not my area of expertise.

Yeah, same to me. I wrote this originally, but I think the address-space stuff was added in the downstream by someone else (though I can't find proof of it now!).

(Unfortunately) that was me. Not the best design, but it catches lots of subtle run-time errors at compile time. Has been extremely useful for the downstream CHERI fork.

clang/lib/CodeGen/CGExprScalar.cpp
1635

Okay that's interesting - I guess it means we should not be using getConstantAddressSpace() here? Or getConstantAddressSpace() should actually return a value that maps to addrspace(1)?

arichardson added inline comments.Nov 18 2022, 7:27 AM
clang/lib/CodeGen/CGExprScalar.cpp
1635

Ah it looks like we should be using CodeGenModule::GetGlobalConstantAddressSpace() instead of getTarget().getConstantAddressSpace(). Is that correct?

LangAS CodeGenModule::GetGlobalConstantAddressSpace() const {
  // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
  if (LangOpts.OpenCL)
    return LangAS::opencl_constant;
  if (LangOpts.SYCLIsDevice)
    return LangAS::sycl_global;
  if (LangOpts.HIP && LangOpts.CUDAIsDevice && getTriple().isSPIRV())
    // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
    // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
    // with OpVariable instructions with Generic storage class which is not
    // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
    // UniformConstant storage class is not viable as pointers to it may not be
    // casted to Generic pointers which are used to model HIP's "flat" pointers.
    return LangAS::cuda_device;
  if (auto AS = getTarget().getConstantAddressSpace())
    return *AS;
  return LangAS::Default;
}

Another problem appears to be that the default implementation of getConstantAddressSpace() returns LangAS::Default instead of None, so the .value_or() will never be used.

Fix to use addrspace(1)

arichardson edited the summary of this revision. (Show Details)Nov 18 2022, 7:34 AM
bader added inline comments.Nov 18 2022, 7:37 AM
clang/lib/CodeGen/CGExprScalar.cpp
1635

Ah it looks like we should be using CodeGenModule::GetGlobalConstantAddressSpace() instead of getTarget().getConstantAddressSpace(). Is that correct?

Yes.

bader accepted this revision.Nov 18 2022, 7:58 AM

Thanks for the fix!

This revision is now accepted and ready to land.Nov 18 2022, 7:58 AM
This revision was automatically updated to reflect the committed changes.