diff --git a/clang/lib/CIR/CodeGen/CIRGenAtomic.cpp b/clang/lib/CIR/CodeGen/CIRGenAtomic.cpp index 85dfff104cc7..2e736206c775 100644 --- a/clang/lib/CIR/CodeGen/CIRGenAtomic.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenAtomic.cpp @@ -449,6 +449,14 @@ static void buildAtomicOp(CIRGenFunction &CGF, AtomicExpr *E, Address Dest, load->setAttr("mem_order", orderAttr); if (E->isVolatile()) load->setAttr("is_volatile", mlir::UnitAttr::get(builder.getContext())); + + // TODO(cir): this logic should be part of createStore, but doing so + // currently breaks CodeGen/union.cpp and CodeGen/union.cpp. + auto ptrTy = Dest.getPointer().getType().cast(); + if (Dest.getElementType() != ptrTy.getPointee()) { + Dest = Dest.withPointer( + builder.createPtrBitcast(Dest.getPointer(), Dest.getElementType())); + } builder.createStore(loc, load->getResult(0), Dest); return; } diff --git a/clang/test/CIR/CodeGen/atomic-xchg-field.c b/clang/test/CIR/CodeGen/atomic-xchg-field.c index 2265d3a94335..71e970ba8f41 100644 --- a/clang/test/CIR/CodeGen/atomic-xchg-field.c +++ b/clang/test/CIR/CodeGen/atomic-xchg-field.c @@ -65,4 +65,18 @@ void structAtomicExchange(unsigned referenceCount, wPtr item) { // LLVM: [[STORE_OLD]]: // LLVM: store i32 %[[OLD]], ptr -// LLVM: br label %[[CONTINUE]] \ No newline at end of file +// LLVM: br label %[[CONTINUE]] + +void f2(const void *cf); + +void structLoad(unsigned referenceCount, wPtr item) { + f2(__atomic_load_n(&item->ref, 5)); +} + +// CHECK-LABEL: @structLoad +// CHECK: %[[ATOMIC_TEMP:.*]] = cir.alloca !cir.ptr, cir.ptr >, ["atomic-temp"] +// CHECK: %[[ATOMIC_LOAD:.*]] = cir.load atomic(seq_cst) %6 : cir.ptr , !u64i +// CHECK: %[[RES:.*]] = cir.cast(bitcast, %[[ATOMIC_TEMP]] : !cir.ptr>), !cir.ptr +// CHECK: cir.store %[[ATOMIC_LOAD]], %[[RES]] : !u64i, cir.ptr + +// No LLVM tests needed for this one, already covered elsewhere. \ No newline at end of file