Skip to content

Commit 6d4afdf

Browse files
committed
[clang] Make MS atomics volatile in kernel mode
This prevents LLVM backend from folding idempotent atomic instructions to a memory barrier.
1 parent d989c24 commit 6d4afdf

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ static Value *MakeBinaryAtomicValue(
232232

233233
llvm::Value *Result =
234234
CGF.Builder.CreateAtomicRMW(Kind, DestAddr, Val, Ordering);
235+
cast<llvm::AtomicRMWInst>(Result)->setVolatile(CGF.CGM.getLangOpts().Kernel);
235236
return EmitFromInt(CGF, Result, T, ValueType);
236237
}
237238

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Check that we don't fold no-op andl to memory barrier
2+
// REQUIRES: x86-registered-target,aarch64-registered-target
3+
// RUN: %clang_cc1 -fms-kernel -fms-extensions -Wno-implicit-function-declaration -triple x86_64-pc-win32 -O2 -S -o - %s | FileCheck %s --check-prefix=X86
4+
// RUN: %clang_cc1 -fms-kernel -fms-extensions -Wno-implicit-function-declaration -triple aarch64-pc-win32 -O2 -S -o - %s | FileCheck %s --check-prefix=ARM64
5+
6+
// X86: lock andl $-1, (%rcx)
7+
// X86-NEXT: retq
8+
9+
// ARM64: ldaxr
10+
// ARM64-NEXT: stlxr
11+
// ARM64-NEXT: cbnz
12+
13+
void access_via_interlocked(long volatile* addr) {
14+
_InterlockedAnd(addr, (long)-1);
15+
}

llvm/lib/CodeGen/AtomicExpandPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,8 @@ bool AtomicExpandImpl::isIdempotentRMW(AtomicRMWInst *RMWI) {
15631563
auto C = dyn_cast<ConstantInt>(RMWI->getValOperand());
15641564
if (!C)
15651565
return false;
1566-
1566+
if (RMWI->isVolatile())
1567+
return false;
15671568
AtomicRMWInst::BinOp Op = RMWI->getOperation();
15681569
switch (Op) {
15691570
case AtomicRMWInst::Add:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: opt -S -passes='require<libcall-lowering-info>,expand-ir-insts,atomic-expand' %s -o - | FileCheck %s
2+
3+
; volatile atomicrmw shouldn't be converted to a fence
4+
; CHECK: %0 = atomicrmw volatile and ptr %addr, i32 -1 seq_cst
5+
6+
target triple = "x86_64-pc-windows-msvc"
7+
8+
define dso_local void @access_via_interlocked(ptr noundef %addr) {
9+
entry:
10+
%0 = atomicrmw volatile and ptr %addr, i32 -1 seq_cst, align 4
11+
ret void
12+
}
13+

0 commit comments

Comments
 (0)