Skip to content

Commit 5254e71

Browse files
committed
[CHERI] Fix Clang code generation for _Atomic(uintptr_t)
1 parent fe4af84 commit 5254e71

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,9 +3355,10 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
33553355
!(type->isUnsignedIntegerType() &&
33563356
CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
33573357
CGF.getLangOpts().getSignedOverflowBehavior() !=
3358-
LangOptions::SOB_Trapping) {
3359-
llvm::AtomicRMWInst::BinOp aop = isInc ? llvm::AtomicRMWInst::Add :
3360-
llvm::AtomicRMWInst::Sub;
3358+
LangOptions::SOB_Trapping &&
3359+
!type->isCHERICapabilityType(CGF.getContext())) {
3360+
llvm::AtomicRMWInst::BinOp aop =
3361+
isInc ? llvm::AtomicRMWInst::Add : llvm::AtomicRMWInst::Sub;
33613362
llvm::Instruction::BinaryOps op = isInc ? llvm::Instruction::Add :
33623363
llvm::Instruction::Sub;
33633364
llvm::Value *amt = CGF.EmitToMemory(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 %s -o - "-triple" "riscv32cheriot-unknown-unknown" "-emit-llvm" "-mframe-pointer=none" "-mcmodel=small" "-target-abi" "cheriot" "-Oz" "-Werror" -std=c2x | FileCheck %s
3+
4+
// Check that we can generate code for C11 atomic increment of uintptr
5+
6+
// CHECK-LABEL: define dso_local void @test1(
7+
// CHECK-SAME: ) local_unnamed_addr addrspace(200) #[[ATTR0:[0-9]+]] {
8+
// CHECK-NEXT: [[ENTRY:.*]]:
9+
// CHECK-NEXT: [[P:%.*]] = alloca ptr addrspace(200), align 8, addrspace(200)
10+
// CHECK-NEXT: [[ATOMIC_TEMP:%.*]] = alloca ptr addrspace(200), align 8, addrspace(200)
11+
// CHECK-NEXT: [[ATOMIC_TEMP1:%.*]] = alloca ptr addrspace(200), align 8, addrspace(200)
12+
// CHECK-NEXT: [[ATOMIC_TEMP2:%.*]] = alloca ptr addrspace(200), align 8, addrspace(200)
13+
// CHECK-NEXT: call void @llvm.lifetime.start.p200(i64 8, ptr addrspace(200) nonnull [[P]]) #[[ATTR4:[0-9]+]]
14+
// CHECK-NEXT: call cherilibcallcc void @__atomic_load(i32 noundef 8, ptr addrspace(200) noundef nonnull [[P]], ptr addrspace(200) noundef nonnull [[ATOMIC_TEMP]], i32 noundef 5) #[[ATTR5:[0-9]+]]
15+
// CHECK-NEXT: br label %[[ATOMIC_OP:.*]]
16+
// CHECK: [[ATOMIC_OP]]:
17+
// CHECK-NEXT: [[DOTIN:%.*]] = phi ptr addrspace(200) [ [[ATOMIC_TEMP]], %[[ENTRY]] ], [ [[ATOMIC_TEMP1]], %[[ATOMIC_OP]] ]
18+
// CHECK-NEXT: [[TMP0:%.*]] = load ptr addrspace(200), ptr addrspace(200) [[DOTIN]], align 8, !tbaa [[TBAA7:![0-9]+]]
19+
// CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr addrspace(200) [[TMP0]], i32 1
20+
// CHECK-NEXT: store ptr addrspace(200) [[TMP0]], ptr addrspace(200) [[ATOMIC_TEMP1]], align 8, !tbaa [[TBAA11:![0-9]+]]
21+
// CHECK-NEXT: store ptr addrspace(200) [[TMP1]], ptr addrspace(200) [[ATOMIC_TEMP2]], align 8, !tbaa [[TBAA11]]
22+
// CHECK-NEXT: [[CALL:%.*]] = call cherilibcallcc zeroext i1 @__atomic_compare_exchange(i32 noundef 8, ptr addrspace(200) noundef nonnull [[P]], ptr addrspace(200) noundef nonnull [[ATOMIC_TEMP1]], ptr addrspace(200) noundef nonnull [[ATOMIC_TEMP2]], i32 noundef 5, i32 noundef 5) #[[ATTR5]]
23+
// CHECK-NEXT: br i1 [[CALL]], label %[[ATOMIC_CONT:.*]], label %[[ATOMIC_OP]]
24+
// CHECK: [[ATOMIC_CONT]]:
25+
// CHECK-NEXT: call void @llvm.lifetime.end.p200(i64 8, ptr addrspace(200) nonnull [[P]]) #[[ATTR4]]
26+
// CHECK-NEXT: ret void
27+
//
28+
void test1() {
29+
_Atomic(__UINTPTR_TYPE__) p;
30+
++p;
31+
}
32+
//.
33+
// CHECK: [[TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
34+
// CHECK: [[META8]] = !{!"unsigned __intcap", [[META9:![0-9]+]], i64 0}
35+
// CHECK: [[META9]] = !{!"omnipotent char", [[META10:![0-9]+]], i64 0}
36+
// CHECK: [[META10]] = !{!"Simple C/C++ TBAA"}
37+
// CHECK: [[TBAA11]] = !{[[META9]], [[META9]], i64 0}
38+
//.

0 commit comments

Comments
 (0)