@@ -945,11 +945,21 @@ void MemorySanitizer::createKernelApi(Module &M, const TargetLibraryInfo &TLI) {
945945}
946946
947947static Constant *getOrInsertGlobal (Module &M, StringRef Name, Type *Ty) {
948- return M.getOrInsertGlobal (Name, Ty, [&] {
949- return new GlobalVariable (M, Ty, false , GlobalVariable::ExternalLinkage,
950- nullptr , Name, nullptr ,
951- GlobalVariable::InitialExecTLSModel);
952- });
948+ // FIXME: spirv target doesn't support TLS, need to handle it later.
949+ if (Triple (M.getTargetTriple ()).isSPIROrSPIRV ()) {
950+ return M.getOrInsertGlobal (Name, Ty, [&] {
951+ return new GlobalVariable (M, Ty, false , GlobalVariable::ExternalLinkage,
952+ Constant::getNullValue (Ty), Name, nullptr ,
953+ GlobalVariable::NotThreadLocal,
954+ kSpirOffloadGlobalAS );
955+ });
956+ } else {
957+ return M.getOrInsertGlobal (Name, Ty, [&] {
958+ return new GlobalVariable (M, Ty, false , GlobalVariable::ExternalLinkage,
959+ nullptr , Name, nullptr ,
960+ GlobalVariable::InitialExecTLSModel);
961+ });
962+ }
953963}
954964
955965// / Insert declarations for userspace-specific functions and globals.
@@ -1275,6 +1285,11 @@ static unsigned TypeSizeToSizeIndex(TypeSize TS) {
12751285}
12761286
12771287static bool isUnsupportedSPIRAccess (const Value *Addr, Instruction *I) {
1288+ if (isa<Instruction>(Addr) &&
1289+ cast<Instruction>(Addr)->getMetadata (LLVMContext::MD_nosanitize)) {
1290+ return true ;
1291+ }
1292+
12781293 // Skip SPIR-V built-in varibles
12791294 auto *OrigValue = Addr->stripInBoundsOffsets ();
12801295 assert (OrigValue != nullptr );
@@ -1285,8 +1300,9 @@ static bool isUnsupportedSPIRAccess(const Value *Addr, Instruction *I) {
12851300 switch (PtrTy->getPointerAddressSpace ()) {
12861301 case kSpirOffloadPrivateAS :
12871302 case kSpirOffloadLocalAS :
1288- case kSpirOffloadGenericAS :
12891303 return true ;
1304+ case kSpirOffloadGenericAS :
1305+ return false ;
12901306 }
12911307
12921308 return false ;
@@ -1302,8 +1318,12 @@ static void setNoSanitizedMetadataSPIR(Instruction &I) {
13021318 Addr = RMW->getPointerOperand ();
13031319 else if (const auto *XCHG = dyn_cast<AtomicCmpXchgInst>(&I))
13041320 Addr = XCHG->getPointerOperand ();
1321+ else if (const auto *ASC = dyn_cast<AddrSpaceCastInst>(&I))
1322+ Addr = ASC->getPointerOperand ();
13051323 else if (isa<MemCpyInst>(&I))
13061324 I.setNoSanitizeMetadata ();
1325+ else if (isa<AllocaInst>(&I))
1326+ I.setNoSanitizeMetadata ();
13071327 else if (const auto *CI = dyn_cast<CallInst>(&I)) {
13081328 auto *Func = CI->getCalledFunction ();
13091329 if (Func) {
@@ -1327,6 +1347,11 @@ static void setNoSanitizedMetadataSPIR(Instruction &I) {
13271347 Addr = CI->getOperand (OpOffset);
13281348 break ;
13291349 }
1350+ case Intrinsic::lifetime_start:
1351+ case Intrinsic::lifetime_end: {
1352+ I.setNoSanitizeMetadata ();
1353+ break ;
1354+ }
13301355 }
13311356 } else {
13321357 auto FuncName = Func->getName ();
@@ -2263,7 +2288,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
22632288 unsigned ArgOffset = 0 ;
22642289 const DataLayout &DL = F->getDataLayout ();
22652290 for (auto &FArg : F->args ()) {
2266- if (!FArg.getType ()->isSized () || FArg.getType ()->isScalableTy ()) {
2291+ // FIXME: Need to find a reasonable way to handle byval arguments for
2292+ // spirv target.
2293+ if (!FArg.getType ()->isSized () || FArg.getType ()->isScalableTy () ||
2294+ (SpirOrSpirv && FArg.hasByValAttr ())) {
22672295 LLVM_DEBUG (dbgs () << (FArg.getType ()->isScalableTy ()
22682296 ? " vscale not fully supported\n "
22692297 : " Arg is not sized\n " ));
@@ -3185,6 +3213,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
31853213
31863214 // Same as memcpy.
31873215 void visitMemSetInst (MemSetInst &I) {
3216+ if (SpirOrSpirv && isa<Instruction>(I.getArgOperand (0 )) &&
3217+ cast<Instruction>(I.getArgOperand (0 ))
3218+ ->getMetadata (LLVMContext::MD_nosanitize))
3219+ return ;
3220+
31883221 IRBuilder<> IRB (&I);
31893222 IRB.CreateCall (
31903223 SpirOrSpirv ? MS.MemsetOffloadFn [cast<PointerType>(
@@ -4874,7 +4907,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
48744907 bool NoUndef = CB.paramHasAttr (i, Attribute::NoUndef);
48754908 bool EagerCheck = MayCheckCall && !ByVal && NoUndef;
48764909
4877- if (EagerCheck) {
4910+ // Always do eager check for spirv target
4911+ if (EagerCheck || SpirOrSpirv) {
48784912 insertShadowCheck (A, &CB);
48794913 Size = DL.getTypeAllocSize (A->getType ());
48804914 } else {
0 commit comments