Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ static Value *MakeBinaryAtomicValue(

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

Expand Down
15 changes: 15 additions & 0 deletions clang/test/CodeGen/MSKernel/interlocked.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Check that we don't fold no-op andl to memory barrier
// REQUIRES: x86-registered-target,aarch64-registered-target
// 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
// RUN: %clang_cc1 -fms-kernel -fms-extensions -Wno-implicit-function-declaration -triple aarch64-pc-win32 -O2 -S -o - %s | FileCheck %s --check-prefix=ARM64

// X86: lock andl $-1, (%rcx)
// X86-NEXT: retq

// ARM64: ldaxr
// ARM64-NEXT: stlxr
// ARM64-NEXT: cbnz

void access_via_interlocked(long volatile* addr) {
_InterlockedAnd(addr, (long)-1);
}
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/AtomicExpandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,8 @@ bool AtomicExpandImpl::isIdempotentRMW(AtomicRMWInst *RMWI) {
auto C = dyn_cast<ConstantInt>(RMWI->getValOperand());
if (!C)
return false;

if (RMWI->isVolatile())
return false;
AtomicRMWInst::BinOp Op = RMWI->getOperation();
switch (Op) {
case AtomicRMWInst::Add:
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/CodeGen/X86/volatile-atomicrmw.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; RUN: opt -S -passes='require<libcall-lowering-info>,expand-ir-insts,atomic-expand' %s -o - | FileCheck %s

; volatile atomicrmw shouldn't be converted to a fence
; CHECK: %0 = atomicrmw volatile and ptr %addr, i32 -1 seq_cst

target triple = "x86_64-pc-windows-msvc"

define dso_local void @access_via_interlocked(ptr noundef %addr) {
entry:
%0 = atomicrmw volatile and ptr %addr, i32 -1 seq_cst, align 4
ret void
}