Skip to content

Commit 52ec50f

Browse files
committed
[CIR] Add support for CK_AtomicToNonAtomic casts
Handle implicit casts from _Atomic T to T by treating them the same as NonAtomicToAtomic casts - just visit the subexpression. This matches the traditional CodeGen approach and enables the cast infrastructure for C11/C++11 _Atomic types. Note: Full atomic load/store support remains NYI and will be addressed in follow-up work. This commit only implements the cast operation itself. Test: clang/test/CIR/CodeGen/atomic-type-casts.cpp ghstack-source-id: 74b4bbe Pull-Request: #1989
1 parent a5e6a00 commit 52ec50f

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,6 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
17101710
CGF, Visit(E), SrcAS, DestAS, convertType(DestTy));
17111711
}
17121712
case CK_AtomicToNonAtomic:
1713-
llvm_unreachable("NYI");
17141713
case CK_NonAtomicToAtomic:
17151714
case CK_UserDefinedConversion:
17161715
return Visit(const_cast<Expr *>(E));
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.og.ll
6+
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.og.ll %s
7+
8+
// Test CK_AtomicToNonAtomic and CK_NonAtomicToAtomic casts
9+
// Note: Full atomic load/store support is NYI - this tests just the casts
10+
11+
// Test NonAtomicToAtomic cast (assigning non-atomic to atomic)
12+
void test_non_atomic_to_atomic() {
13+
int x = 50;
14+
_Atomic int y = x; // Implicit NonAtomicToAtomic cast
15+
// CIR: cir.func{{.*}}test_non_atomic_to_atomicv
16+
// CIR: cir.alloca !s32i, !cir.ptr<!s32i>, ["x"
17+
// CIR: cir.alloca !s32i, !cir.ptr<!s32i>, ["y"
18+
// CIR: cir.load
19+
// CIR: cir.store
20+
// LLVM-LABEL: @_Z25test_non_atomic_to_atomicv
21+
// LLVM: alloca i32
22+
// LLVM: alloca i32
23+
// LLVM: store i32 50
24+
// LLVM: load i32
25+
// LLVM: store i32
26+
// OGCG-LABEL: @_Z25test_non_atomic_to_atomicv
27+
// OGCG: %x = alloca i32
28+
// OGCG: %y = alloca i32
29+
// OGCG: store i32 50
30+
}
31+
32+
// Test that atomic type casts don't crash the compiler
33+
void test_atomic_cast_exists() {
34+
int regular = 42;
35+
_Atomic int atomic_val = regular;
36+
// Just verify this compiles - the cast infrastructure exists
37+
// CIR: cir.func{{.*}}test_atomic_cast_existsv
38+
// CIR: cir.alloca !s32i, !cir.ptr<!s32i>, ["regular"
39+
// CIR: cir.alloca !s32i, !cir.ptr<!s32i>, ["atomic_val"
40+
// LLVM-LABEL: @_Z23test_atomic_cast_existsv
41+
// LLVM: alloca i32
42+
// LLVM: alloca i32
43+
// LLVM: store i32 42
44+
// OGCG-LABEL: @_Z23test_atomic_cast_existsv
45+
// OGCG: %regular = alloca i32
46+
// OGCG: %atomic_val = alloca i32
47+
// OGCG: store i32 42
48+
}
49+
50+
// Test with different types
51+
void test_atomic_float_cast() {
52+
float f = 3.14f;
53+
_Atomic float g = f;
54+
// CIR: cir.func{{.*}}test_atomic_float_castv
55+
// CIR: cir.alloca !cir.float
56+
// CIR: cir.alloca !cir.float
57+
// LLVM-LABEL: @_Z22test_atomic_float_castv
58+
// LLVM: alloca float
59+
// LLVM: alloca float
60+
// LLVM: store float
61+
// OGCG-LABEL: @_Z22test_atomic_float_castv
62+
// OGCG: %f = alloca float
63+
// OGCG: %g = alloca float
64+
// OGCG: store float
65+
}
66+
67+
// Test that cast infrastructure is in place for pointers
68+
void test_atomic_pointer_cast() {
69+
int val = 42;
70+
int* ptr = &val;
71+
_Atomic(int*) atomic_ptr = ptr;
72+
// CIR: cir.func{{.*}}test_atomic_pointer_castv
73+
// CIR: cir.alloca !cir.ptr<!s32i>
74+
// CIR: cir.alloca !cir.ptr<!s32i>
75+
// LLVM-LABEL: @_Z24test_atomic_pointer_castv
76+
// LLVM: alloca i32
77+
// LLVM: alloca ptr
78+
// LLVM: alloca ptr
79+
// LLVM: store i32 42
80+
// OGCG-LABEL: @_Z24test_atomic_pointer_castv
81+
// OGCG: %val = alloca i32
82+
// OGCG: %ptr = alloca ptr
83+
// OGCG: %atomic_ptr = alloca ptr
84+
// OGCG: store i32 42
85+
}

0 commit comments

Comments
 (0)