[clang] [clang][CodeGen] add addr space cast if needed when storing ptrs (PR #154380)

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 20 03:04:48 PDT 2025


================
@@ -2209,6 +2209,18 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr,
     }
   }
 
+  // When storing a pointer, perform address space cast if needed.
----------------
macurtis-amd wrote:

@efriedma-quic Thanks for looking at this. Did not realize that the types are expected to match at this point. 

Would just before to [the call to `EmitStoreThroughLValue`](https://github.com/llvm/llvm-project/blob/460e9a883786c23197d064e71c810cb2a95f3564/clang/lib/CodeGen/CGDecl.cpp#L800) in [`CodeGenFunction::EmitScalarInit`](https://github.com/llvm/llvm-project/blob/460e9a883786c23197d064e71c810cb2a95f3564/clang/lib/CodeGen/CGDecl.cpp#L786) be an appropriate place to do the cast if needed? 
```
void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
                                     LValue lvalue, bool capturedByInit) {
  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
  if (!lifetime) {
    llvm::Value *Value;
    if (PointerAuthQualifier PtrAuth = lvalue.getQuals().getPointerAuth()) {
      Value = EmitPointerAuthQualify(PtrAuth, init, lvalue.getAddress());
      lvalue.getQuals().removePointerAuth();
    } else {
      Value = EmitScalarExpr(init);
    }
    if (capturedByInit)
      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
    EmitNullabilityCheck(lvalue, Value, init->getExprLoc());
    EmitStoreThroughLValue(RValue::get(Value), lvalue, true);
    return;
  }
```

https://github.com/llvm/llvm-project/pull/154380


More information about the cfe-commits mailing list