6767#include " llvm/IR/PassManager.h"
6868#include " llvm/IR/Type.h"
6969#include " llvm/IR/Value.h"
70+ #include " llvm/Support/AMDGPUAddrSpace.h"
7071#include " llvm/Support/Casting.h"
7172#include " llvm/Support/KnownBits.h"
7273#include " llvm/Support/raw_ostream.h"
@@ -122,8 +123,10 @@ class Lint : public InstVisitor<Lint> {
122123 Value *findValueImpl (Value *V, bool OffsetOk,
123124 SmallPtrSetImpl<Value *> &Visited) const ;
124125
126+ bool isConstantAddressSpace (unsigned AS) const ;
125127public:
126128 Module *Mod;
129+ Triple TT;
127130 const DataLayout *DL;
128131 AliasAnalysis *AA;
129132 AssumptionCache *AC;
@@ -135,7 +138,8 @@ class Lint : public InstVisitor<Lint> {
135138
136139 Lint (Module *Mod, const DataLayout *DL, AliasAnalysis *AA,
137140 AssumptionCache *AC, DominatorTree *DT, TargetLibraryInfo *TLI)
138- : Mod(Mod), DL(DL), AA(AA), AC(AC), DT(DT), TLI(TLI),
141+ : Mod(Mod), TT(Triple::normalize(Mod->getTargetTriple ())),
142+ DL(DL), AA(AA), AC(AC), DT(DT), TLI(TLI),
139143 MessagesStr(Messages) {}
140144
141145 void WriteValues (ArrayRef<const Value *> Vs) {
@@ -378,6 +382,36 @@ void Lint::visitReturnInst(ReturnInst &I) {
378382 }
379383}
380384
385+ bool Lint::isConstantAddressSpace (unsigned AS) const {
386+ if (TT.isAMDGPU ()) {
387+ switch (AS) {
388+ using namespace AMDGPUAS ;
389+ case CONSTANT_ADDRESS:
390+ case CONSTANT_ADDRESS_32BIT:
391+ case CONSTANT_BUFFER_0:
392+ case CONSTANT_BUFFER_1:
393+ case CONSTANT_BUFFER_2:
394+ case CONSTANT_BUFFER_3:
395+ case CONSTANT_BUFFER_4:
396+ case CONSTANT_BUFFER_5:
397+ case CONSTANT_BUFFER_6:
398+ case CONSTANT_BUFFER_7:
399+ case CONSTANT_BUFFER_8:
400+ case CONSTANT_BUFFER_9:
401+ case CONSTANT_BUFFER_10:
402+ case CONSTANT_BUFFER_11:
403+ case CONSTANT_BUFFER_12:
404+ case CONSTANT_BUFFER_13:
405+ case CONSTANT_BUFFER_14:
406+ case CONSTANT_BUFFER_15:
407+ return true ;
408+ default :
409+ return false ;
410+ }
411+ }
412+ return false ;
413+ }
414+
381415// TODO: Check that the reference is in bounds.
382416// TODO: Check readnone/readonly function attributes.
383417void Lint::visitMemoryReference (Instruction &I, const MemoryLocation &Loc,
@@ -401,6 +435,9 @@ void Lint::visitMemoryReference(Instruction &I, const MemoryLocation &Loc,
401435 " Unusual: Address one pointer dereference" , &I);
402436
403437 if (Flags & MemRef::Write) {
438+ Check (!isConstantAddressSpace (UnderlyingObject->getType ()->getPointerAddressSpace ()),
439+ " Undefined behavior: Write to const memory" , &I);
440+
404441 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
405442 Check (!GV->isConstant (), " Undefined behavior: Write to read-only memory" ,
406443 &I);
0 commit comments