43
43
#include " llvm/Support/Debug.h"
44
44
#include " llvm/Support/Path.h"
45
45
#include " llvm/Support/raw_ostream.h"
46
+ #include " llvm/Transforms/Instrumentation/SPIRVSanitizerCommonUtils.h"
46
47
#include " llvm/Transforms/Utils/EscapeEnumerator.h"
47
48
#include " llvm/Transforms/Utils/Instrumentation.h"
48
49
#include " llvm/Transforms/Utils/Local.h"
@@ -52,13 +53,6 @@ using namespace llvm;
52
53
53
54
#define DEBUG_TYPE " tsan"
54
55
55
- // Spir memory address space
56
- static constexpr unsigned kSpirOffloadPrivateAS = 0 ;
57
- static constexpr unsigned kSpirOffloadGlobalAS = 1 ;
58
- static constexpr unsigned kSpirOffloadConstantAS = 2 ;
59
- static constexpr unsigned kSpirOffloadLocalAS = 3 ;
60
- static constexpr unsigned kSpirOffloadGenericAS = 4 ;
61
-
62
56
static cl::opt<bool > ClInstrumentMemoryAccesses (
63
57
" tsan-instrument-memory-accesses" , cl::init(true ),
64
58
cl::desc(" Instrument memory accesses" ), cl::Hidden);
@@ -127,6 +121,8 @@ struct ThreadSanitizerOnSpirv {
127
121
128
122
void appendDebugInfoToArgs (Instruction *I, SmallVectorImpl<Value *> &Args);
129
123
124
+ bool isUnsupportedSPIRAccess (Value *Addr, Instruction *Inst);
125
+
130
126
private:
131
127
void instrumentGlobalVariables ();
132
128
@@ -383,6 +379,38 @@ void ThreadSanitizerOnSpirv::appendDebugInfoToArgs(
383
379
Args.push_back (ConstantExpr::getPointerCast (FuncNameGV, ConstASPtrTy));
384
380
}
385
381
382
+ bool ThreadSanitizerOnSpirv::isUnsupportedSPIRAccess (Value *Addr,
383
+ Instruction *Inst) {
384
+ auto *OrigValue = getUnderlyingObject (Addr);
385
+ if (OrigValue->getName ().starts_with (" __spirv_BuiltIn" ))
386
+ return true ;
387
+
388
+ // Ignore load/store for target ext type since we can't know exactly what size
389
+ // it is.
390
+ if (auto *SI = dyn_cast<StoreInst>(Inst))
391
+ if (getTargetExtType (SI->getValueOperand ()->getType ()) ||
392
+ isJointMatrixAccess (SI->getPointerOperand ()))
393
+ return true ;
394
+
395
+ if (auto *LI = dyn_cast<LoadInst>(Inst))
396
+ if (getTargetExtType (Inst->getType ()) ||
397
+ isJointMatrixAccess (LI->getPointerOperand ()))
398
+ return true ;
399
+
400
+ auto AddrAS = cast<PointerType>(Addr->getType ()->getScalarType ())
401
+ ->getPointerAddressSpace ();
402
+ switch (AddrAS) {
403
+ case kSpirOffloadPrivateAS :
404
+ case kSpirOffloadLocalAS :
405
+ case kSpirOffloadConstantAS :
406
+ return true ;
407
+ case kSpirOffloadGlobalAS :
408
+ case kSpirOffloadGenericAS :
409
+ return false ;
410
+ }
411
+ return false ;
412
+ }
413
+
386
414
bool ThreadSanitizerOnSpirv::isSupportedSPIRKernel (Function &F) {
387
415
388
416
if (!F.hasFnAttribute (Attribute::SanitizeThread) ||
@@ -709,30 +737,12 @@ static bool shouldInstrumentReadWriteFromAddress(const Module *M, Value *Addr) {
709
737
}
710
738
}
711
739
712
- if (Triple (M->getTargetTriple ()).isSPIROrSPIRV ()) {
713
- auto *OrigValue = getUnderlyingObject (Addr);
714
- if (OrigValue->getName ().starts_with (" __spirv_BuiltIn" ))
740
+ // Do not instrument accesses from different address spaces; we cannot deal
741
+ // with them.
742
+ if (Addr) {
743
+ Type *PtrTy = cast<PointerType>(Addr->getType ()->getScalarType ());
744
+ if (PtrTy->getPointerAddressSpace () != 0 )
715
745
return false ;
716
-
717
- auto AddrAS = cast<PointerType>(Addr->getType ()->getScalarType ())
718
- ->getPointerAddressSpace ();
719
- switch (AddrAS) {
720
- case kSpirOffloadPrivateAS :
721
- case kSpirOffloadLocalAS :
722
- case kSpirOffloadConstantAS :
723
- return false ;
724
- case kSpirOffloadGlobalAS :
725
- case kSpirOffloadGenericAS :
726
- return true ;
727
- }
728
- } else {
729
- // Do not instrument accesses from different address spaces; we cannot deal
730
- // with them.
731
- if (Addr) {
732
- Type *PtrTy = cast<PointerType>(Addr->getType ()->getScalarType ());
733
- if (PtrTy->getPointerAddressSpace () != 0 )
734
- return false ;
735
- }
736
746
}
737
747
738
748
return true ;
@@ -781,7 +791,10 @@ void ThreadSanitizer::chooseInstructionsToInstrument(
781
791
Value *Addr = IsWrite ? cast<StoreInst>(I)->getPointerOperand ()
782
792
: cast<LoadInst>(I)->getPointerOperand ();
783
793
784
- if (!shouldInstrumentReadWriteFromAddress (I->getModule (), Addr))
794
+ if (Spirv) {
795
+ if (Spirv->isUnsupportedSPIRAccess (Addr, I))
796
+ continue ;
797
+ } else if (!shouldInstrumentReadWriteFromAddress (I->getModule (), Addr))
785
798
continue ;
786
799
787
800
if (!IsWrite) {
@@ -890,7 +903,8 @@ bool ThreadSanitizer::sanitizeFunction(Function &F,
890
903
else if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
891
904
LocalLoadsAndStores.push_back (&Inst);
892
905
else if (Spirv && isa<AllocaInst>(Inst) &&
893
- cast<AllocaInst>(Inst).getAllocatedType ()->isSized ())
906
+ cast<AllocaInst>(Inst).getAllocatedType ()->isSized () &&
907
+ !getTargetExtType (cast<AllocaInst>(Inst).getAllocatedType ()))
894
908
Allocas.push_back (&Inst);
895
909
else if ((isa<CallInst>(Inst) && !isa<DbgInfoIntrinsic>(Inst)) ||
896
910
isa<InvokeInst>(Inst)) {
0 commit comments