================
@@ -411,7 +412,48 @@ static void emitAtomicCmpXchg(CodeGenFunction &CGF, 
AtomicExpr *E, bool IsWeak,
 
   CGF.Builder.SetInsertPoint(StoreExpectedBB);
   // Update the memory at Expected with Old's value.
-  CGF.Builder.CreateStore(Old, Val1);
+llvm::Type *ExpectedType = ExpectedResult.getElementType();
+const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
+uint64_t ExpectedSizeInBytes = DL.getTypeStoreSize(ExpectedType);
+
+if (ExpectedSizeInBytes == Size) {
+  // Sizes match: store directly
+  CGF.Builder.CreateStore(Old, ExpectedResult);
+
+} else {
+  // store only the first ExpectedSizeInBytes bytes of Old
+  llvm::Type *OldType = Old->getType();
+
+  llvm::Align SrcAlignLLVM = DL.getABITypeAlign(OldType);
+  llvm::Align DstAlignLLVM = DL.getABITypeAlign(ExpectedType);
+
+  clang::CharUnits SrcAlign = 
clang::CharUnits::fromQuantity(SrcAlignLLVM.value());
+  clang::CharUnits DstAlign = 
clang::CharUnits::fromQuantity(DstAlignLLVM.value());
----------------
huixie90 wrote:

thanks. I replaced the DstAlignLLVM thing with `ExpectedResult.getAlignment()`. 
However, for `Old`, I don't have an `Address`, could you please advice the 
right way to get the alignment from an `llvm::Value`?

https://github.com/llvm/llvm-project/pull/78707
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to