[clang] [clang] Recover necessary AddrSpaceCast (PR #119246)

Youngsuk Kim via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 12 07:57:49 PST 2024


JOE1994 wrote:

> May it be possible that the wrapper function's return type is invalid and the cast isn't needed?

LLVM-IR (from llvm without assertions, before this revision)
shows that the wrapper function returns `ptr` while `MyGlobVar` has `addrspace(1)`.

```llvm
$thread-local wrapper routine for MyGlobVar = comdat any

@MyGlobVar = external thread_local addrspace(1) global i32, align 4
@__oclc_ABI_version = weak_odr hidden local_unnamed_addr addrspace(4) constant i32 500

define weak_odr hidden noundef ptr @thread-local wrapper routine for MyGlobVar() local_unnamed_addr #0 comdat {
  %1 = tail call align 4 ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) align 4 @MyGlobVar)
  ret ptr addrspace(1) %1
}

declare nonnull ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) nonnull) #1

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="gfx906" "target-features"="+16-bit-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx9-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" "uniform-work-group-size"="false" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
```

Updating the wrapper function's return type as below also fixes the error:

```diff
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -3078,6 +3078,10 @@ ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
       getContext().getPointerType(RetQT), FunctionArgList());

   llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FI);
+  // Adjust wrapper function's ret type to have matching llvm addrspace with Val.
+  if (llvm::Type *WrapperRetTy = FnTy->getReturnType();
+      WrapperRetTy->isPointerTy() && Val->getType()->isPointerTy() && WrapperRetTy != Val->getType())
+    FnTy = llvm::FunctionType::get(Val->getType(), FnTy->params(), FnTy->isVarArg());
   llvm::Function *Wrapper =
       llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
                              WrapperName.str(), &CGM.getModule());
```



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


More information about the cfe-commits mailing list