llvm-project
2380272f - [CodeGen] Fix emission of function pointer casts with non-zero program AS (#186210)

Commit
1 day ago
[CodeGen] Fix emission of function pointer casts with non-zero program AS (#186210) Imagine we have the following code: ```c++ void foo() {} void bar() { void *ptr = reinterpret_cast<void*>(foo); } ``` Usually clang would treat this as a simple `bitcast`, but in the case that the target has a non-default program address space, this needs to be an `addrspacecast`. Today, if we try to codegen this, we get an assert due to the two types not being valid for a `bitcast`. ``` clang-23: /tmp/llvm/clang/lib/CodeGen/CGExprScalar.cpp:2661: llvm::Value* {anonymous}::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*): Assertion `(!SrcTy->isPtrOrPtrVectorTy() || !DstTy->isPtrOrPtrVectorTy() || SrcTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace()) && "Address-space cast must be used to convert address spaces"' failed. ``` The complicating issue is that Sema has no idea about the target's default program AS, that's part of the LLVM target data layout which is only known to CodeGen, so there I don't see a way we could represent this as a `AddressSpaceConversion` -type `CastExpr` in Sema, the only place we have the required info is during codegen for the `CastExpr`. Modify the codegen of `BitCast`-type `CastExpr` to generate an LLVM `addrspacecast` if necessary. --------- Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
Author
Parents
Loading