Skip to content

Commit ae211fa

Browse files
committed
Added cmpxchg, atomicrmw, call to Lint
1 parent 393311c commit ae211fa

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

llvm/include/llvm/Support/AMDGPUAddrSpace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ inline bool isExtendedGlobalAddrSpace(unsigned AS) {
9595
}
9696

9797
inline bool isConstantAddressSpace(unsigned AS) {
98-
switch(AS) {
98+
switch (AS) {
9999
using namespace AMDGPUAS;
100100
case CONSTANT_ADDRESS:
101101
case CONSTANT_ADDRESS_32BIT:

llvm/lib/Analysis/Lint.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class Lint : public InstVisitor<Lint> {
103103
void visitReturnInst(ReturnInst &I);
104104
void visitLoadInst(LoadInst &I);
105105
void visitStoreInst(StoreInst &I);
106+
void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I);
107+
void visitAtomicRMWInst(AtomicRMWInst &I);
106108
void visitXor(BinaryOperator &I);
107109
void visitSub(BinaryOperator &I);
108110
void visitLShr(BinaryOperator &I);
@@ -122,6 +124,7 @@ class Lint : public InstVisitor<Lint> {
122124
Value *findValue(Value *V, bool OffsetOk) const;
123125
Value *findValueImpl(Value *V, bool OffsetOk,
124126
SmallPtrSetImpl<Value *> &Visited) const;
127+
125128
public:
126129
Module *Mod;
127130
Triple TT;
@@ -136,9 +139,8 @@ class Lint : public InstVisitor<Lint> {
136139

137140
Lint(Module *Mod, const DataLayout *DL, AliasAnalysis *AA,
138141
AssumptionCache *AC, DominatorTree *DT, TargetLibraryInfo *TLI)
139-
: Mod(Mod), TT(Triple::normalize(Mod->getTargetTriple())),
140-
DL(DL), AA(AA), AC(AC), DT(DT), TLI(TLI),
141-
MessagesStr(Messages) {}
142+
: Mod(Mod), TT(Triple::normalize(Mod->getTargetTriple())), DL(DL), AA(AA),
143+
AC(AC), DT(DT), TLI(TLI), MessagesStr(Messages) {}
142144

143145
void WriteValues(ArrayRef<const Value *> Vs) {
144146
for (const Value *V : Vs) {
@@ -404,8 +406,9 @@ void Lint::visitMemoryReference(Instruction &I, const MemoryLocation &Loc,
404406

405407
if (Flags & MemRef::Write) {
406408
if (TT.isAMDGPU())
407-
Check(!AMDGPU::isConstantAddressSpace(UnderlyingObject->getType()->getPointerAddressSpace()),
408-
"Undefined behavior: Write to const memory", &I);
409+
Check(!AMDGPU::isConstantAddressSpace(
410+
UnderlyingObject->getType()->getPointerAddressSpace()),
411+
"Undefined behavior: Write to memory in const addrspace", &I);
409412

410413
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
411414
Check(!GV->isConstant(), "Undefined behavior: Write to read-only memory",
@@ -486,6 +489,16 @@ void Lint::visitStoreInst(StoreInst &I) {
486489
I.getOperand(0)->getType(), MemRef::Write);
487490
}
488491

492+
void Lint::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
493+
visitMemoryReference(I, MemoryLocation::get(&I), I.getAlign(),
494+
I.getOperand(0)->getType(), MemRef::Write);
495+
}
496+
497+
void Lint::visitAtomicRMWInst(AtomicRMWInst &I) {
498+
visitMemoryReference(I, MemoryLocation::get(&I), I.getAlign(),
499+
I.getOperand(0)->getType(), MemRef::Write);
500+
}
501+
489502
void Lint::visitXor(BinaryOperator &I) {
490503
Check(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)),
491504
"Undefined result: xor(undef, undef)", &I);
Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
1-
; RUN: not opt --mtriple=amdgcn --passes=lint --lint-abort-on-error %s -o /dev/null |& FileCheck %s
2-
; RUN: opt --mtriple=amdgcn --mcpu=gfx1030 --passes=lint %s -o /dev/null |& FileCheck %s --check-prefixes=CHECK,CHECK0
3-
; RUN: opt --mtriple=x86_64 --passes=lint --lint-abort-on-error %s -o /dev/null |& FileCheck %s --allow-empty --check-prefix=NOERR
1+
; RUN: not opt --mtriple=amdgcn --passes=lint --lint-abort-on-error %s -disable-output 2>&1 | FileCheck %s
2+
; RUN: opt --mtriple=amdgcn --mcpu=gfx1030 --passes=lint %s -disable-output 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK0
3+
; RUN: opt --mtriple=x86_64 --passes=lint --lint-abort-on-error %s -disable-output 2>&1 | FileCheck %s --allow-empty --check-prefix=NOERR
44
; NOERR: {{^$}}
55

66
define amdgpu_kernel void @store_const(ptr addrspace(4) %out, i32 %a, i32 %b) {
7-
; CHECK: Undefined behavior: Write to const memory
7+
; CHECK: Undefined behavior: Write to memory in const addrspace
88
; CHECK-NEXT: store i32 %r, ptr addrspace(4) %out
99
%r = add i32 %a, %b
1010
store i32 %r, ptr addrspace(4) %out
1111
ret void
1212
}
1313

14+
declare void @llvm.memset.p4.i64(ptr addrspace(4) noalias nocapture writeonly, i8, i64, i1)
15+
define amdgpu_kernel void @memset_const(ptr addrspace(4) %dst) {
16+
; CHECK0: Undefined behavior: Write to memory in const addrspace
17+
; CHECK0-NEXT: call void @llvm.memset.p4.i64(ptr addrspace(4) %dst, i8 0, i64 256, i1 false)
18+
call void @llvm.memset.p4.i64(ptr addrspace(4) %dst, i8 0, i64 256, i1 false)
19+
ret void
20+
}
21+
22+
declare void @llvm.memcpy.p6.p0.i32(ptr addrspace(6) noalias nocapture writeonly, ptr noalias nocapture readonly, i32, i1)
1423
define amdgpu_kernel void @memcpy_to_const(ptr addrspace(6) %dst, ptr %src) {
15-
; CHECK0: Undefined behavior: Write to const memory
24+
; CHECK0: Undefined behavior: Write to memory in const addrspace
1625
; CHECK0-NEXT: call void @llvm.memcpy.p6.p0.i32(ptr addrspace(6) %dst, ptr %src, i32 256, i1 false)
17-
call void @llvm.memcpy.px.py.i32(ptr addrspace(6) %dst, ptr %src, i32 256, i1 false)
26+
call void @llvm.memcpy.p6.p0.i32(ptr addrspace(6) %dst, ptr %src, i32 256, i1 false)
27+
ret void
28+
}
29+
30+
define amdgpu_kernel void @cmpxchg_to_const(ptr addrspace(4) %dst, i32 %src) {
31+
; CHECK0: Undefined behavior: Write to memory in const addrspace
32+
; CHECK0-NEXT: %void = cmpxchg ptr addrspace(4) %dst, i32 0, i32 %src seq_cst monotonic
33+
%void = cmpxchg ptr addrspace(4) %dst, i32 0, i32 %src seq_cst monotonic
1834
ret void
1935
}
2036

21-
declare void @llvm.memcpy.px.py.i32(ptr addrspace(6) noalias nocapture writeonly, ptr noalias nocapture readonly, i32, i1 immarg)
37+
define amdgpu_kernel void @atomicrmw_to_const(ptr addrspace(4) %dst, i32 %src) {
38+
; CHECK0: Undefined behavior: Write to memory in const addrspace
39+
; CHECK0-NEXT: %void = atomicrmw add ptr addrspace(4) %dst, i32 %src acquire
40+
%void = atomicrmw add ptr addrspace(4) %dst, i32 %src acquire
41+
ret void
42+
}
43+
44+
declare void @const_param(ptr addrspace(6))
45+
define amdgpu_kernel void @call_with_const(ptr addrspace(6) %dst) {
46+
; CHECK0-NOT: call void @const_param(ptr addrspace(6) %dst)
47+
call void @const_param(ptr addrspace(6) %dst)
48+
ret void
49+
}

0 commit comments

Comments
 (0)