Skip to content

Commit

Permalink
[CIR][CIRGen] Fix follow up stores for atomic loads
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardosolopes committed Apr 25, 2024
1 parent 5dc3353 commit af189a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenAtomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir::cir::PointerType>();
if (Dest.getElementType() != ptrTy.getPointee()) {
Dest = Dest.withPointer(
builder.createPtrBitcast(Dest.getPointer(), Dest.getElementType()));
}
builder.createStore(loc, load->getResult(0), Dest);
return;
}
Expand Down
16 changes: 15 additions & 1 deletion clang/test/CIR/CodeGen/atomic-xchg-field.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ void structAtomicExchange(unsigned referenceCount, wPtr item) {

// LLVM: [[STORE_OLD]]:
// LLVM: store i32 %[[OLD]], ptr
// LLVM: br label %[[CONTINUE]]
// 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<!void>, cir.ptr <!cir.ptr<!void>>, ["atomic-temp"]
// CHECK: %[[ATOMIC_LOAD:.*]] = cir.load atomic(seq_cst) %6 : cir.ptr <!u64i>, !u64i
// CHECK: %[[RES:.*]] = cir.cast(bitcast, %[[ATOMIC_TEMP]] : !cir.ptr<!cir.ptr<!void>>), !cir.ptr<!u64i>
// CHECK: cir.store %[[ATOMIC_LOAD]], %[[RES]] : !u64i, cir.ptr <!u64i>

// No LLVM tests needed for this one, already covered elsewhere.

0 comments on commit af189a9

Please sign in to comment.