Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions clang/lib/CodeGen/CGAtomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,29 @@ static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
Load->setAtomic(Order, Scope);
Load->setVolatile(E->isVolatile());

if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
CGF.Builder.CreateStore(Load, Dest);
return;
}

QualType Ty = E->getValueType();
if (!Ty->isBooleanType()) {
CGF.Builder.CreateStore(Load, Dest);
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an early return like this is weird; just stick a normal if statement around the code that creates the metadata.

}

llvm::MDBuilder MDHelper(CGF.getLLVMContext());
llvm::APInt BooleanMin = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0);
llvm::APInt BooleanEnd = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2);

if (llvm::MDNode *RangeInfo =
MDHelper.createRange(BooleanMin, BooleanEnd)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refactor the code from CodeGenFunction::EmitLoadOfScalar, instead of reinventing the code to compute the range metadata.

Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
Load->setMetadata(llvm::LLVMContext::MD_noundef,
llvm::MDNode::get(CGF.getLLVMContext(), {}));
}

CGF.Builder.CreateStore(Load, Dest);
return;
}
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CodeGen/atomic-ops-load.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -triple riscv64 -O1 -emit-llvm %s -o - | FileCheck %s
#include <stdbool.h>

extern bool t1;
bool test1(void) {
// CHECK-LABEL: define{{.*}} i1 @test1
// CHECK: load atomic i8, ptr @t1 monotonic, align 1, !range ![[$WS_RANGE:[0-9]*]], !noundef !{{[0-9]+}}
// CHECK-NEXT: trunc nuw i8 %{{.*}} to i1
// CHECK-NEXT: ret i1 %{{.*}}
return __atomic_load_n(&t1, __ATOMIC_RELAXED);
}